From 72c26b6ead03acdb3e7a24e20d9907317a2d3306 Mon Sep 17 00:00:00 2001 From: James Harris Date: Tue, 25 Aug 2020 14:00:38 +1000 Subject: [PATCH] Add parameter and return type hints. --- CHANGELOG.md | 1 + composer.json | 2 +- composer.lock | 40 +++++++++++-------- src/AnyComparable.php | 2 +- src/Comparator/Comparator.php | 4 +- src/Comparator/DeepComparator.php | 16 ++++---- src/Comparator/ObjectIdentityComparator.php | 6 +-- src/Comparator/ParityComparator.php | 10 ++--- src/Comparator/PhpComparator.php | 4 +- src/Comparator/StrictPhpComparator.php | 6 +-- src/ExtendedComparable.php | 12 +++--- src/ExtendedComparableTrait.php | 14 +++---- src/RestrictedComparable.php | 4 +- src/SelfComparable.php | 2 +- src/SubClassComparable.php | 2 +- test/src/Comparator/SelfComparableImpl.php | 2 +- .../src/Comparator/SubClassComparableImpl.php | 2 +- test/src/ExtendedComparableTraitUser.php | 2 +- 18 files changed, 70 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8f3612..2fab340 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/composer.json b/composer.json index 2c7701b..2da6aad 100644 --- a/composer.json +++ b/composer.json @@ -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" } }, diff --git a/composer.lock b/composer.lock index 24adbf5..5d698b2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cbec4f8c243090e02d1334b45d9c69d8", + "content-hash": "24ea395d85647bdfb7b3d8b02083a87b", "packages": [ { "name": "icecave/repr", @@ -690,39 +690,47 @@ }, { "name": "phake/phake", - "version": "v1.0.8", + "version": "v3.1.8", "source": { "type": "git", "url": "https://github.com/mlively/Phake.git", - "reference": "c5a3d7a75fc7431e8a5e21a922f084af95456bc6" + "reference": "9f9dfb12c9ce6e38c73de9631ea2ab0f0ea36a65" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/mlively/Phake/zipball/c5a3d7a75fc7431e8a5e21a922f084af95456bc6", - "reference": "c5a3d7a75fc7431e8a5e21a922f084af95456bc6", + "url": "https://api.github.com/repos/mlively/Phake/zipball/9f9dfb12c9ce6e38c73de9631ea2ab0f0ea36a65", + "reference": "9f9dfb12c9ce6e38c73de9631ea2ab0f0ea36a65", "shasum": "" }, "require": { - "php": ">=5.2.0" + "php": ">=7", + "sebastian/comparator": "^1.1|^2.0|^3.0|^4.0" }, "require-dev": { + "codeclimate/php-test-reporter": "dev-master", "doctrine/common": "2.3.*", "ext-soap": "*", - "hamcrest/hamcrest-php": "1.1.0", - "phpunit/phpunit": "3.7.*" + "hamcrest/hamcrest-php": "1.1.*", + "phpunit/phpunit": "^7.0" + }, + "suggest": { + "doctrine/common": "Allows mock annotations to use import statements for classes.", + "hamcrest/hamcrest-php": "Use Hamcrest matchers." }, "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.0-dev" + } + }, "autoload": { - "classmap": [ - "src" - ] + "psr-0": { + "Phake": "src/" + } }, "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "src" - ], "license": [ - "BSD" + "BSD-3-Clause" ], "authors": [ { @@ -736,7 +744,7 @@ "mock", "testing" ], - "time": "2015-01-27T15:48:39+00:00" + "time": "2020-05-11T18:43:26+00:00" }, { "name": "phar-io/manifest", diff --git a/src/AnyComparable.php b/src/AnyComparable.php index 6913771..0b4275a 100644 --- a/src/AnyComparable.php +++ b/src/AnyComparable.php @@ -23,5 +23,5 @@ interface AnyComparable * * @return int The result of the comparison. */ - public function compare($value); + public function compare($value): int; } diff --git a/src/Comparator/Comparator.php b/src/Comparator/Comparator.php index bcee3af..4217157 100644 --- a/src/Comparator/Comparator.php +++ b/src/Comparator/Comparator.php @@ -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(). @@ -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; } diff --git a/src/Comparator/DeepComparator.php b/src/Comparator/DeepComparator.php index 1de2b22..e8664a1 100644 --- a/src/Comparator/DeepComparator.php +++ b/src/Comparator/DeepComparator.php @@ -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; } @@ -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 = []; @@ -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); } @@ -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); @@ -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); @@ -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; @@ -173,7 +173,7 @@ protected function compareObject($lhs, $rhs, &$visitationContext) * * @return array */ - protected function objectProperties($object, &$visitationContext) + protected function objectProperties($object, &$visitationContext): array { $properties = []; $reflector = new ReflectionObject($object); @@ -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); diff --git a/src/Comparator/ObjectIdentityComparator.php b/src/Comparator/ObjectIdentityComparator.php index aed85db..d3aa083 100644 --- a/src/Comparator/ObjectIdentityComparator.php +++ b/src/Comparator/ObjectIdentityComparator.php @@ -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; } @@ -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); @@ -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); } diff --git a/src/Comparator/ParityComparator.php b/src/Comparator/ParityComparator.php index 74a91c7..081ec29 100644 --- a/src/Comparator/ParityComparator.php +++ b/src/Comparator/ParityComparator.php @@ -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; } @@ -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); @@ -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); } @@ -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; @@ -110,7 +110,7 @@ protected function canCompare($lhs, $rhs) * * @return string */ - protected function compareImplementationClass($value) + protected function compareImplementationClass($value): string { $className = get_class($value); diff --git a/src/Comparator/PhpComparator.php b/src/Comparator/PhpComparator.php index 880d121..e5a3494 100644 --- a/src/Comparator/PhpComparator.php +++ b/src/Comparator/PhpComparator.php @@ -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; @@ -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); } diff --git a/src/Comparator/StrictPhpComparator.php b/src/Comparator/StrictPhpComparator.php index b133d86..7334969 100644 --- a/src/Comparator/StrictPhpComparator.php +++ b/src/Comparator/StrictPhpComparator.php @@ -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); @@ -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); } @@ -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); diff --git a/src/ExtendedComparable.php b/src/ExtendedComparable.php index de1530f..f6163c4 100644 --- a/src/ExtendedComparable.php +++ b/src/ExtendedComparable.php @@ -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; } diff --git a/src/ExtendedComparableTrait.php b/src/ExtendedComparableTrait.php index e2e7a79..1f437b5 100644 --- a/src/ExtendedComparableTrait.php +++ b/src/ExtendedComparableTrait.php @@ -12,7 +12,7 @@ trait ExtendedComparableTrait * * @return bool True if $this == $value. */ - public function isEqualTo($value) + public function isEqualTo($value): bool { return $this->compare($value) === 0; } @@ -22,7 +22,7 @@ public function isEqualTo($value) * * @return bool True if $this != $value. */ - public function isNotEqualTo($value) + public function isNotEqualTo($value): bool { return $this->compare($value) !== 0; } @@ -32,7 +32,7 @@ public function isNotEqualTo($value) * * @return bool True if $this < $value. */ - public function isLessThan($value) + public function isLessThan($value): bool { return $this->compare($value) < 0; } @@ -42,7 +42,7 @@ public function isLessThan($value) * * @return bool True if $this > $value. */ - public function isGreaterThan($value) + public function isGreaterThan($value): bool { return $this->compare($value) > 0; } @@ -52,7 +52,7 @@ public function isGreaterThan($value) * * @return bool True if $this <= $value. */ - public function isLessThanOrEqualTo($value) + public function isLessThanOrEqualTo($value): bool { return $this->compare($value) <= 0; } @@ -62,7 +62,7 @@ public function isLessThanOrEqualTo($value) * * @return bool True if $this >= $value. */ - public function isGreaterThanOrEqualTo($value) + public function isGreaterThanOrEqualTo($value): bool { return $this->compare($value) >= 0; } @@ -72,5 +72,5 @@ public function isGreaterThanOrEqualTo($value) * * @return int */ - abstract protected function compare($value); + abstract protected function compare($value): int; } diff --git a/src/RestrictedComparable.php b/src/RestrictedComparable.php index cd528ed..c643269 100644 --- a/src/RestrictedComparable.php +++ b/src/RestrictedComparable.php @@ -24,7 +24,7 @@ interface RestrictedComparable * @return int The result of the comparison. * @throws Exception\NotComparableException Indicates that the implementation does not know how to compare $this to $value. */ - public function compare($value); + public function compare($value): int; /** * Check if $this is able to be compared to another value. @@ -36,5 +36,5 @@ public function compare($value); * * @return bool True if $this can be compared to $value. */ - public function canCompare($value); + public function canCompare($value): bool; } diff --git a/src/SelfComparable.php b/src/SelfComparable.php index e54b58c..8f19767 100644 --- a/src/SelfComparable.php +++ b/src/SelfComparable.php @@ -24,5 +24,5 @@ interface SelfComparable * @return int The result of the comparison. * @throws Exception\NotComparableException if $value is not the same type as $this. */ - public function compare($value); + public function compare($value): int; } diff --git a/src/SubClassComparable.php b/src/SubClassComparable.php index a8c7103..1610c66 100644 --- a/src/SubClassComparable.php +++ b/src/SubClassComparable.php @@ -25,5 +25,5 @@ interface SubClassComparable * @return int The result of the comparison. * @throws Exception\NotComparableException if $value is not the same type as $this. */ - public function compare($value); + public function compare($value): int; } diff --git a/test/src/Comparator/SelfComparableImpl.php b/test/src/Comparator/SelfComparableImpl.php index ecc06b5..b6d7346 100644 --- a/test/src/Comparator/SelfComparableImpl.php +++ b/test/src/Comparator/SelfComparableImpl.php @@ -6,7 +6,7 @@ class SelfComparableImpl implements SelfComparable { - public function compare($value) + public function compare($value): int { return -10; } diff --git a/test/src/Comparator/SubClassComparableImpl.php b/test/src/Comparator/SubClassComparableImpl.php index 6975548..4ba76ca 100644 --- a/test/src/Comparator/SubClassComparableImpl.php +++ b/test/src/Comparator/SubClassComparableImpl.php @@ -6,7 +6,7 @@ class SubClassComparableImpl implements SubClassComparable { - public function compare($value) + public function compare($value): int { return -10; } diff --git a/test/src/ExtendedComparableTraitUser.php b/test/src/ExtendedComparableTraitUser.php index f07458a..a36ab92 100644 --- a/test/src/ExtendedComparableTraitUser.php +++ b/test/src/ExtendedComparableTraitUser.php @@ -6,7 +6,7 @@ class ExtendedComparableTraitUser implements ExtendedComparable { use ExtendedComparableTrait; - public function compare($rhs) + public function compare($rhs): int { return 0 - $rhs; }