Skip to content

Commit

Permalink
Remove PHP 5 support
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkla committed Mar 1, 2018
1 parent 6942c61 commit a17d348
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 46 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: php

php:
- 5.6
- 7.0
- 7.1
- 7.2

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
}
],
"require": {
"php": "^5.6.4 || ^7.0"
"php": "^7.1.3"
},
"require-dev": {
"phpunit/phpunit": "^5.7 || ^6.3"
"phpunit/phpunit": "^7.0"
},
"autoload": {
"psr-4": {
Expand Down
24 changes: 13 additions & 11 deletions src/Hashids.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Hashids;

use Hashids\Math\Bc;
Expand Down Expand Up @@ -159,7 +161,7 @@ public function __construct($salt = '', $minHashLength = 0, $alphabet = 'abcdefg
*
* @return string
*/
public function encode(...$numbers)
public function encode(...$numbers): string
{
$ret = '';

Expand All @@ -184,7 +186,7 @@ public function encode(...$numbers)
$numbersHashInt = 0;

foreach ($numbers as $i => $number) {
$numbersHashInt += $this->math->intval($this->math->mod($number, ($i + 100)));
$numbersHashInt += $this->math->intval($this->math->mod((string) $number, (string) ($i + 100)));
}

$lottery = $ret = $alphabet[$numbersHashInt % strlen($alphabet)];
Expand Down Expand Up @@ -234,7 +236,7 @@ public function encode(...$numbers)
*
* @return array
*/
public function decode($hash)
public function decode(string $hash): array
{
$ret = [];

Expand Down Expand Up @@ -263,7 +265,7 @@ public function decode($hash)
foreach ($hashArray as $subHash) {
$alphabet = $this->shuffle($alphabet, substr($lottery.$this->salt.$alphabet, 0, strlen($alphabet)));
$result = $this->unhash($subHash, $alphabet);
if ($this->math->greaterThan($result, PHP_INT_MAX)) {
if ($this->math->greaterThan($result, (string) PHP_INT_MAX)) {
$ret[] = $this->math->strval($result);
} else {
$ret[] = $this->math->intval($result);
Expand All @@ -285,7 +287,7 @@ public function decode($hash)
*
* @return string
*/
public function encodeHex($str)
public function encodeHex(string $str): string
{
if (!ctype_xdigit((string) $str)) {
return '';
Expand All @@ -308,7 +310,7 @@ public function encodeHex($str)
*
* @return string
*/
public function decodeHex($hash)
public function decodeHex(string $hash): string
{
$ret = '';
$numbers = $this->decode($hash);
Expand All @@ -328,7 +330,7 @@ public function decodeHex($hash)
*
* @return string
*/
protected function shuffle($alphabet, $salt)
protected function shuffle(string $alphabet, string $salt): string
{
$key = $alphabet.' '.$salt;

Expand Down Expand Up @@ -365,7 +367,7 @@ protected function shuffle($alphabet, $salt)
*
* @return string
*/
protected function hash($input, $alphabet)
protected function hash(string $input, string $alphabet): string
{
$hash = '';
$alphabetLength = strlen($alphabet);
Expand All @@ -385,11 +387,11 @@ protected function hash($input, $alphabet)
* @param string $input
* @param string $alphabet
*
* @return int
* @return string
*/
protected function unhash($input, $alphabet)
protected function unhash(string $input, string $alphabet): string
{
$number = 0;
$number = '0';
$inputLength = strlen($input);

if ($inputLength && $alphabet) {
Expand Down
2 changes: 2 additions & 0 deletions src/HashidsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Hashids;

use InvalidArgumentException;
Expand Down
10 changes: 6 additions & 4 deletions src/HashidsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Hashids;

/**
Expand All @@ -26,7 +28,7 @@ interface HashidsInterface
*
* @return string
*/
public function encode(...$numbers);
public function encode(...$numbers): string;

/**
* Decode a hash to the original parameter values.
Expand All @@ -35,7 +37,7 @@ public function encode(...$numbers);
*
* @return array
*/
public function decode($hash);
public function decode(string $hash): array;

/**
* Encode hexadecimal values and generate a hash string.
Expand All @@ -44,7 +46,7 @@ public function decode($hash);
*
* @return string
*/
public function encodeHex($str);
public function encodeHex(string $str): string;

/**
* Decode a hexadecimal hash.
Expand All @@ -53,5 +55,5 @@ public function encodeHex($str);
*
* @return string
*/
public function decodeHex($hash);
public function decodeHex(string $hash): string;
}
20 changes: 11 additions & 9 deletions src/Math/Bc.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Hashids\Math;

/**
Expand All @@ -28,7 +30,7 @@ class Bc implements MathInterface
*
* @return string
*/
public function add($a, $b)
public function add(string $a, string $b): string
{
return bcadd($a, $b, 0);
}
Expand All @@ -41,7 +43,7 @@ public function add($a, $b)
*
* @return string
*/
public function multiply($a, $b)
public function multiply(string $a, string $b): string
{
return bcmul($a, $b, 0);
}
Expand All @@ -54,7 +56,7 @@ public function multiply($a, $b)
*
* @return string
*/
public function divide($a, $b)
public function divide(string $a, string $b): string
{
return bcdiv($a, $b, 0);
}
Expand All @@ -67,7 +69,7 @@ public function divide($a, $b)
*
* @return string
*/
public function mod($n, $d)
public function mod(string $n, string $d): string
{
return bcmod($n, $d);
}
Expand All @@ -80,7 +82,7 @@ public function mod($n, $d)
*
* @return bool
*/
public function greaterThan($a, $b)
public function greaterThan(string $a, string $b): bool
{
return bccomp($a, $b, 0) > 0;
}
Expand All @@ -92,7 +94,7 @@ public function greaterThan($a, $b)
*
* @return int
*/
public function intval($a)
public function intval(string $a): int
{
return intval($a);
}
Expand All @@ -104,7 +106,7 @@ public function intval($a)
*
* @return string
*/
public function strval($a)
public function strval(string $a): string
{
return $a;
}
Expand All @@ -114,9 +116,9 @@ public function strval($a)
*
* @param int $a
*
* @return string
* @return int
*/
public function get($a)
public function get(int $a): int
{
return $a;
}
Expand Down
20 changes: 11 additions & 9 deletions src/Math/Gmp.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Hashids\Math;

/**
Expand All @@ -28,7 +30,7 @@ class Gmp implements MathInterface
*
* @return string
*/
public function add($a, $b)
public function add(string $a, string $b): string
{
return gmp_add($a, $b);
}
Expand All @@ -41,7 +43,7 @@ public function add($a, $b)
*
* @return string
*/
public function multiply($a, $b)
public function multiply(string $a, string $b): string
{
return gmp_mul($a, $b);
}
Expand All @@ -54,7 +56,7 @@ public function multiply($a, $b)
*
* @return string
*/
public function divide($a, $b)
public function divide(string $a, string $b): string
{
return gmp_div_q($a, $b);
}
Expand All @@ -67,7 +69,7 @@ public function divide($a, $b)
*
* @return string
*/
public function mod($n, $d)
public function mod(string $n, string $d): string
{
return gmp_mod($n, $d);
}
Expand All @@ -80,7 +82,7 @@ public function mod($n, $d)
*
* @return bool
*/
public function greaterThan($a, $b)
public function greaterThan(string $a, string $b): bool
{
return gmp_cmp($a, $b) > 0;
}
Expand All @@ -92,7 +94,7 @@ public function greaterThan($a, $b)
*
* @return int
*/
public function intval($a)
public function intval(string $a): int
{
return gmp_intval($a);
}
Expand All @@ -104,7 +106,7 @@ public function intval($a)
*
* @return string
*/
public function strval($a)
public function strval(string $a): string
{
return gmp_strval($a);
}
Expand All @@ -114,9 +116,9 @@ public function strval($a)
*
* @param int $a
*
* @return string
* @return int
*/
public function get($a)
public function get(int $a): int
{
return gmp_init($a);
}
Expand Down
Loading

0 comments on commit a17d348

Please sign in to comment.