Skip to content

Commit

Permalink
Add parameter and return type hints.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Aug 25, 2020
1 parent c62c585 commit 72c26b6
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 3.0.0 (2020-08-25)

- **[BC]** Drop support for PHP 7.1
- **[Bc]** Add parameter and return type hints
- **[BC]** Remove `PackageInfo` class
- **[BC]** Rename `ComparatorInterface` to `Comparator`
- **[BC]** Rename `AnyComparableInterface` to `AnyComparable`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"require-dev": {
"eloquent/liberator": "^2",
"friendsofphp/php-cs-fixer": "^2",
"phake/phake": "^1",
"phake/phake": "^3",
"phpunit/phpunit": "^8"
},
"autoload": { "psr-4": { "Icecave\\Parity\\": "src" } },
Expand Down
40 changes: 24 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/AnyComparable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ interface AnyComparable
*
* @return int The result of the comparison.
*/
public function compare($value);
public function compare($value): int;
}
4 changes: 2 additions & 2 deletions src/Comparator/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface Comparator
*
* @return int The result of the comparison.
*/
public function compare($lhs, $rhs);
public function compare($lhs, $rhs): int;

/**
* An alias for compare().
Expand All @@ -33,5 +33,5 @@ public function compare($lhs, $rhs);
*
* @return int The result of the comparison.
*/
public function __invoke($lhs, $rhs);
public function __invoke($lhs, $rhs): int;
}
16 changes: 8 additions & 8 deletions src/Comparator/DeepComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
*
* @return Comparator The comparator to use when the operands are not arrays or objects.
*/
public function fallbackComparator()
public function fallbackComparator(): Comparator
{
return $this->fallbackComparator;
}
Expand All @@ -55,7 +55,7 @@ public function fallbackComparator()
*
* @return int The result of the comparison.
*/
public function compare($lhs, $rhs)
public function compare($lhs, $rhs): int
{
$visitationContext = [];

Expand All @@ -70,7 +70,7 @@ public function compare($lhs, $rhs)
*
* @return int The result of the comparison.
*/
public function __invoke($lhs, $rhs)
public function __invoke($lhs, $rhs): int
{
return $this->compare($lhs, $rhs);
}
Expand All @@ -82,7 +82,7 @@ public function __invoke($lhs, $rhs)
*
* @return int The result of the comparison.
*/
protected function compareValue($lhs, $rhs, &$visitationContext)
protected function compareValue($lhs, $rhs, &$visitationContext): int
{
if (is_array($lhs) && is_array($rhs)) {
return $this->compareArray($lhs, $rhs, $visitationContext);
Expand All @@ -100,7 +100,7 @@ protected function compareValue($lhs, $rhs, &$visitationContext)
*
* @return int The result of the comparison.
*/
protected function compareArray(array $lhs, array $rhs, &$visitationContext)
protected function compareArray(array $lhs, array $rhs, &$visitationContext): int
{
reset($lhs);
reset($rhs);
Expand Down Expand Up @@ -144,7 +144,7 @@ protected function compareArray(array $lhs, array $rhs, &$visitationContext)
*
* @return int The result of the comparison.
*/
protected function compareObject($lhs, $rhs, &$visitationContext)
protected function compareObject($lhs, $rhs, &$visitationContext): int
{
if ($lhs === $rhs) {
return 0;
Expand Down Expand Up @@ -173,7 +173,7 @@ protected function compareObject($lhs, $rhs, &$visitationContext)
*
* @return array<string,mixed>
*/
protected function objectProperties($object, &$visitationContext)
protected function objectProperties($object, &$visitationContext): array
{
$properties = [];
$reflector = new ReflectionObject($object);
Expand Down Expand Up @@ -207,7 +207,7 @@ protected function objectProperties($object, &$visitationContext)
*
* @return bool
*/
protected function isNestedComparison($lhs, $rhs, &$visitationContext)
protected function isNestedComparison($lhs, $rhs, &$visitationContext): bool
{
$key = spl_object_hash($lhs) . ':' . spl_object_hash($rhs);

Expand Down
6 changes: 3 additions & 3 deletions src/Comparator/ObjectIdentityComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(Comparator $fallbackComparator)
*
* @return Comparator The comparator to use for non-objects.
*/
public function fallbackComparator()
public function fallbackComparator(): Comparator
{
return $this->fallbackComparator;
}
Expand All @@ -44,7 +44,7 @@ public function fallbackComparator()
*
* @return int The result of the comparison.
*/
public function compare($lhs, $rhs)
public function compare($lhs, $rhs): int
{
if (!is_object($lhs) || !is_object($rhs)) {
return $this->fallbackComparator()->compare($lhs, $rhs);
Expand All @@ -65,7 +65,7 @@ public function compare($lhs, $rhs)
*
* @return int The result of the comparison.
*/
public function __invoke($lhs, $rhs)
public function __invoke($lhs, $rhs): int
{
return $this->compare($lhs, $rhs);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Comparator/ParityComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(Comparator $fallbackComparator)
*
* @return Comparator The comparator to use when the operands do not provide their own comparison algorithm.
*/
public function fallbackComparator()
public function fallbackComparator(): Comparator
{
return $this->fallbackComparator;
}
Expand All @@ -55,7 +55,7 @@ public function fallbackComparator()
*
* @return int The result of the comparison.
*/
public function compare($lhs, $rhs)
public function compare($lhs, $rhs): int
{
if ($this->canCompare($lhs, $rhs)) {
return $lhs->compare($rhs);
Expand All @@ -74,7 +74,7 @@ public function compare($lhs, $rhs)
*
* @return int The result of the comparison.
*/
public function __invoke($lhs, $rhs)
public function __invoke($lhs, $rhs): int
{
return $this->compare($lhs, $rhs);
}
Expand All @@ -87,7 +87,7 @@ public function __invoke($lhs, $rhs)
*
* @return bool
*/
protected function canCompare($lhs, $rhs)
protected function canCompare($lhs, $rhs): bool
{
if ($lhs instanceof AnyComparable) {
return true;
Expand All @@ -110,7 +110,7 @@ protected function canCompare($lhs, $rhs)
*
* @return string
*/
protected function compareImplementationClass($value)
protected function compareImplementationClass($value): string
{
$className = get_class($value);

Expand Down
4 changes: 2 additions & 2 deletions src/Comparator/PhpComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PhpComparator implements Comparator
*
* @return int The result of the comparison.
*/
public function compare($lhs, $rhs)
public function compare($lhs, $rhs): int
{
if ($lhs < $rhs) {
return -1;
Expand All @@ -42,7 +42,7 @@ public function compare($lhs, $rhs)
*
* @return int The result of the comparison.
*/
public function __invoke($lhs, $rhs)
public function __invoke($lhs, $rhs): int
{
return $this->compare($lhs, $rhs);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Comparator/StrictPhpComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct($relaxNumericComparisons = true)
*
* @return int The result of the comparison.
*/
public function compare($lhs, $rhs)
public function compare($lhs, $rhs): int
{
$lhsType = $this->transformTypeName($lhs);
$rhsType = $this->transformTypeName($rhs);
Expand All @@ -61,7 +61,7 @@ public function compare($lhs, $rhs)
*
* @return int The result of the comparison.
*/
public function __invoke($lhs, $rhs)
public function __invoke($lhs, $rhs): int
{
return $this->compare($lhs, $rhs);
}
Expand All @@ -71,7 +71,7 @@ public function __invoke($lhs, $rhs)
*
* @return string The effective type name to use when comparing types.
*/
private function transformTypeName($value)
private function transformTypeName($value): string
{
if (is_object($value)) {
return 'object:' . get_class($value);
Expand Down
12 changes: 6 additions & 6 deletions src/ExtendedComparable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,40 @@ interface ExtendedComparable
*
* @return bool True if $this == $value.
*/
public function isEqualTo($value);
public function isEqualTo($value): bool;

/**
* @param mixed $value The value to compare.
*
* @return bool True if $this != $value.
*/
public function isNotEqualTo($value);
public function isNotEqualTo($value): bool;

/**
* @param mixed $value The value to compare.
*
* @return bool True if $this < $value.
*/
public function isLessThan($value);
public function isLessThan($value): bool;

/**
* @param mixed $value The value to compare.
*
* @return bool True if $this > $value.
*/
public function isGreaterThan($value);
public function isGreaterThan($value): bool;

/**
* @param mixed $value The value to compare.
*
* @return bool True if $this <= $value.
*/
public function isLessThanOrEqualTo($value);
public function isLessThanOrEqualTo($value): bool;

/**
* @param mixed $value The value to compare.
*
* @return bool True if $this >= $value.
*/
public function isGreaterThanOrEqualTo($value);
public function isGreaterThanOrEqualTo($value): bool;
}
Loading

0 comments on commit 72c26b6

Please sign in to comment.