diff --git a/.github/workflows/tests.yml b/.github/workflows/ci.yml similarity index 55% rename from .github/workflows/tests.yml rename to .github/workflows/ci.yml index 90e806713..0d64c5f58 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/ci.yml @@ -1,12 +1,34 @@ -name: tests +name: ci on: push: pull_request: jobs: - tests: + static_analysis: + name: Static analysis + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.0' + extensions: curl + tools: composer:v2 + coverage: none + + - name: Install PHP dependencies + run: composer update --prefer-dist --no-interaction --no-progress + + - name: Run PHPStan + run: vendor/bin/phpstan analyze + + tests: + name: Tests ${{ matrix.php }} runs-on: ubuntu-latest strategy: matrix: @@ -18,8 +40,6 @@ jobs: - '8.3' - '8.4' - name: PHP ${{ matrix.php }} - steps: - name: Checkout code uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 7611d841f..acf2ac9a8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.lock tests/.phpunit.result.cache +tests/phpunit.xml vendor diff --git a/composer.json b/composer.json index fc65f833d..4a08fda8b 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,9 @@ "require-dev": { "phpunit/php-file-iterator": "^1.4 || ^2.0 || ^3.0", - "phpunit/phpunit": "^4.8.36 || ^5.7 || ^6.5 || ^7.0 || ^8.0 || ^9.0" + "phpunit/phpunit": "^4.8.36 || ^5.7 || ^6.5 || ^7.0 || ^8.0 || ^9.0", + "phpstan/phpstan": "^2.1", + "phpstan/phpstan-phpunit": "^2.0" }, "replace": { @@ -33,7 +35,7 @@ "extra": { "branch-alias": { - "dev-master": "2.1-dev" + "dev-master": "3.0-dev" } } } diff --git a/generator/FactoryFile.php b/generator/FactoryFile.php index dd6109b1f..79d6a54ec 100644 --- a/generator/FactoryFile.php +++ b/generator/FactoryFile.php @@ -65,7 +65,7 @@ public function generateDeclaration($name, FactoryMethod $method) $code = $this->indent . $this->getDeclarationModifiers() . 'function ' . $name . '(' . $this->generateDeclarationArguments($method) - . ')' . "\n" . $this->indent . '{' . "\n"; + . '): ' . $this->generateReturnType($method) . "\n" . $this->indent . '{' . "\n"; return $code; } @@ -83,6 +83,22 @@ public function generateDeclarationArguments(FactoryMethod $method) } } + public function generateReturnType(FactoryMethod $method): string + { + $call = $method->getCalls()[0]; + if (!$call instanceof FactoryCall) { + throw new Exception('The first call in the FactoryMethod cannot be used to determine the return type. Method: '.$method->getName()); + } + + $returnType = $call->getMethod()->getReturnType(); + + if (!$returnType) { + throw new \Exception('The first calls FactoryMethod cannot be used to determine the return type. Method: '.$method->getName()); + } + + return sprintf('\\%s', $returnType); + } + public function generateImport(FactoryMethod $method) { return $this->indent . self::INDENT . "require_once '" . $method->getClass()->getFile() . "';" . "\n"; diff --git a/generator/FactoryMethod.php b/generator/FactoryMethod.php index 8a05371be..d3a04187f 100644 --- a/generator/FactoryMethod.php +++ b/generator/FactoryMethod.php @@ -214,6 +214,21 @@ public function getFullName() return $this->getClassName() . '::' . $this->getName(); } + public function getReturnType(): ?string + { + if (!$this->reflector->hasReturnType()) { + return null; + } + + $returnType = $this->reflector->getReturnType()->getName(); + + if ($returnType === 'self') { + return $this->reflector->getDeclaringClass()->getName(); + } + + return $returnType; + } + public function getCommentText() { return implode("\n", $this->comment); diff --git a/generator/parts/functions_header.txt b/generator/parts/functions_header.txt index 50d23df8f..8cb9b3090 100644 --- a/generator/parts/functions_header.txt +++ b/generator/parts/functions_header.txt @@ -13,7 +13,7 @@ if (!function_exists('assertThat')) { * assertThat("some error", $a > $b); * */ - function assertThat() + function assertThat(): void { $args = func_get_args(); call_user_func_array( diff --git a/hamcrest/Hamcrest.php b/hamcrest/Hamcrest.php index a521de8a4..2fb541a2b 100644 --- a/hamcrest/Hamcrest.php +++ b/hamcrest/Hamcrest.php @@ -20,7 +20,7 @@ * assertThat("some error", $a > $b); * */ - function assertThat() + function assertThat(): void { $args = func_get_args(); call_user_func_array( @@ -34,7 +34,7 @@ function assertThat() /** * Evaluates to true only if each $matcher[$i] is satisfied by $array[$i]. */ - function anArray(/* args... */) + function anArray(/* args... */): \Hamcrest\Arrays\IsArray { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Arrays\IsArray', 'anArray'), $args); @@ -49,7 +49,7 @@ function anArray(/* args... */) * * @return \Hamcrest\Arrays\IsArrayContaining */ - function hasItemInArray($item) + function hasItemInArray($item): \Hamcrest\Arrays\IsArrayContaining { return \Hamcrest\Arrays\IsArrayContaining::hasItemInArray($item); } @@ -63,7 +63,7 @@ function hasItemInArray($item) * * @return \Hamcrest\Arrays\IsArrayContaining */ - function hasValue($item) + function hasValue($item): \Hamcrest\Arrays\IsArrayContaining { return \Hamcrest\Arrays\IsArrayContaining::hasItemInArray($item); } @@ -73,7 +73,7 @@ function hasValue($item) /** * An array with elements that match the given matchers. */ - function arrayContainingInAnyOrder(/* args... */) + function arrayContainingInAnyOrder(/* args... */): \Hamcrest\Arrays\IsArrayContainingInAnyOrder { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Arrays\IsArrayContainingInAnyOrder', 'arrayContainingInAnyOrder'), $args); @@ -84,7 +84,7 @@ function arrayContainingInAnyOrder(/* args... */) /** * An array with elements that match the given matchers. */ - function containsInAnyOrder(/* args... */) + function containsInAnyOrder(/* args... */): \Hamcrest\Arrays\IsArrayContainingInAnyOrder { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Arrays\IsArrayContainingInAnyOrder', 'arrayContainingInAnyOrder'), $args); @@ -95,7 +95,7 @@ function containsInAnyOrder(/* args... */) /** * An array with elements that match the given matchers in the same order. */ - function arrayContaining(/* args... */) + function arrayContaining(/* args... */): \Hamcrest\Arrays\IsArrayContainingInOrder { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Arrays\IsArrayContainingInOrder', 'arrayContaining'), $args); @@ -106,7 +106,7 @@ function arrayContaining(/* args... */) /** * An array with elements that match the given matchers in the same order. */ - function contains(/* args... */) + function contains(/* args... */): \Hamcrest\Arrays\IsArrayContainingInOrder { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Arrays\IsArrayContainingInOrder', 'arrayContaining'), $args); @@ -121,7 +121,7 @@ function contains(/* args... */) * * @return \Hamcrest\Arrays\IsArrayContainingKey */ - function hasKeyInArray($key) + function hasKeyInArray($key): \Hamcrest\Arrays\IsArrayContainingKey { return \Hamcrest\Arrays\IsArrayContainingKey::hasKeyInArray($key); } @@ -135,7 +135,7 @@ function hasKeyInArray($key) * * @return \Hamcrest\Arrays\IsArrayContainingKey */ - function hasKey($key) + function hasKey($key): \Hamcrest\Arrays\IsArrayContainingKey { return \Hamcrest\Arrays\IsArrayContainingKey::hasKeyInArray($key); } @@ -144,8 +144,11 @@ function hasKey($key) if (!function_exists('hasKeyValuePair')) { /** * Test if an array has both an key and value in parity with each other. + * + * @param mixed $key + * @param mixed $value */ - function hasKeyValuePair($key, $value) + function hasKeyValuePair($key, $value): \Hamcrest\Arrays\IsArrayContainingKeyValuePair { return \Hamcrest\Arrays\IsArrayContainingKeyValuePair::hasKeyValuePair($key, $value); } @@ -154,8 +157,11 @@ function hasKeyValuePair($key, $value) if (!function_exists('hasEntry')) { /** * Test if an array has both an key and value in parity with each other. + * + * @param mixed $key + * @param mixed $value */ - function hasEntry($key, $value) + function hasEntry($key, $value): \Hamcrest\Arrays\IsArrayContainingKeyValuePair { return \Hamcrest\Arrays\IsArrayContainingKeyValuePair::hasKeyValuePair($key, $value); } @@ -169,7 +175,7 @@ function hasEntry($key, $value) * * @return \Hamcrest\Arrays\IsArrayWithSize */ - function arrayWithSize($size) + function arrayWithSize($size): \Hamcrest\Arrays\IsArrayWithSize { return \Hamcrest\Arrays\IsArrayWithSize::arrayWithSize($size); } @@ -179,7 +185,7 @@ function arrayWithSize($size) /** * Matches an empty array. */ - function emptyArray() + function emptyArray(): \Hamcrest\Core\DescribedAs { return \Hamcrest\Arrays\IsArrayWithSize::emptyArray(); } @@ -189,7 +195,7 @@ function emptyArray() /** * Matches an empty array. */ - function nonEmptyArray() + function nonEmptyArray(): \Hamcrest\Core\DescribedAs { return \Hamcrest\Arrays\IsArrayWithSize::nonEmptyArray(); } @@ -199,7 +205,7 @@ function nonEmptyArray() /** * Returns true if traversable is empty. */ - function emptyTraversable() + function emptyTraversable(): \Hamcrest\Collection\IsEmptyTraversable { return \Hamcrest\Collection\IsEmptyTraversable::emptyTraversable(); } @@ -209,7 +215,7 @@ function emptyTraversable() /** * Returns true if traversable is not empty. */ - function nonEmptyTraversable() + function nonEmptyTraversable(): \Hamcrest\Collection\IsEmptyTraversable { return \Hamcrest\Collection\IsEmptyTraversable::nonEmptyTraversable(); } @@ -218,8 +224,10 @@ function nonEmptyTraversable() if (!function_exists('traversableWithSize')) { /** * Does traversable size satisfy a given matcher? + * + * @param mixed $size */ - function traversableWithSize($size) + function traversableWithSize($size): \Hamcrest\Collection\IsTraversableWithSize { return \Hamcrest\Collection\IsTraversableWithSize::traversableWithSize($size); } @@ -229,7 +237,7 @@ function traversableWithSize($size) /** * Evaluates to true only if ALL of the passed in matchers evaluate to true. */ - function allOf(/* args... */) + function allOf(/* args... */): \Hamcrest\Core\AllOf { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Core\AllOf', 'allOf'), $args); @@ -240,7 +248,7 @@ function allOf(/* args... */) /** * Evaluates to true if ANY of the passed in matchers evaluate to true. */ - function anyOf(/* args... */) + function anyOf(/* args... */): \Hamcrest\Core\AnyOf { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Core\AnyOf', 'anyOf'), $args); @@ -251,7 +259,7 @@ function anyOf(/* args... */) /** * Evaluates to false if ANY of the passed in matchers evaluate to true. */ - function noneOf(/* args... */) + function noneOf(/* args... */): \Hamcrest\Core\IsNot { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Core\AnyOf', 'noneOf'), $args); @@ -266,7 +274,7 @@ function noneOf(/* args... */) * assertThat($string, both(containsString("a"))->andAlso(containsString("b"))); * */ - function both(\Hamcrest\Matcher $matcher) + function both(\Hamcrest\Matcher $matcher): \Hamcrest\Core\CombinableMatcher { return \Hamcrest\Core\CombinableMatcher::both($matcher); } @@ -280,7 +288,7 @@ function both(\Hamcrest\Matcher $matcher) * assertThat($string, either(containsString("a"))->orElse(containsString("b"))); * */ - function either(\Hamcrest\Matcher $matcher) + function either(\Hamcrest\Matcher $matcher): \Hamcrest\Core\CombinableMatcher { return \Hamcrest\Core\CombinableMatcher::either($matcher); } @@ -290,7 +298,7 @@ function either(\Hamcrest\Matcher $matcher) /** * Wraps an existing matcher and overrides the description when it fails. */ - function describedAs(/* args... */) + function describedAs(/* args... */): \Hamcrest\Core\DescribedAs { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Core\DescribedAs', 'describedAs'), $args); @@ -299,13 +307,13 @@ function describedAs(/* args... */) if (!function_exists('everyItem')) { /** - * @param Matcher $itemMatcher + * @param \Hamcrest\Matcher $itemMatcher * A matcher to apply to every element in an array. * * @return \Hamcrest\Core\Every * Evaluates to TRUE for a collection in which every item matches $itemMatcher */ - function everyItem(\Hamcrest\Matcher $itemMatcher) + function everyItem(\Hamcrest\Matcher $itemMatcher): \Hamcrest\Core\Every { return \Hamcrest\Core\Every::everyItem($itemMatcher); } @@ -315,8 +323,10 @@ function everyItem(\Hamcrest\Matcher $itemMatcher) /** * Creates a matcher that matches any examined object whose toString or * __toString() method returns a value equalTo the specified string. + * + * @param mixed $matcher */ - function hasToString($matcher) + function hasToString($matcher): \Hamcrest\Core\HasToString { return \Hamcrest\Core\HasToString::hasToString($matcher); } @@ -329,8 +339,10 @@ function hasToString($matcher) * * For example: assertThat($cheese, equalTo($smelly)) * vs. assertThat($cheese, is(equalTo($smelly))) + * + * @param mixed $value */ - function is($value) + function is($value): \Hamcrest\Core\Is { return \Hamcrest\Core\Is::is($value); } @@ -341,10 +353,8 @@ function is($value) * This matcher always evaluates to true. * * @param string $description A meaningful string used when describing itself. - * - * @return \Hamcrest\Core\IsAnything */ - function anything($description = 'ANYTHING') + function anything(string $description = 'ANYTHING'): \Hamcrest\Core\IsAnything { return \Hamcrest\Core\IsAnything::anything($description); } @@ -361,7 +371,7 @@ function anything($description = 'ANYTHING') * assertThat(array('a', 'b'), hasItem('b')); * */ - function hasItem(/* args... */) + function hasItem(/* args... */): \Hamcrest\Core\IsCollectionContaining { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Core\IsCollectionContaining', 'hasItem'), $args); @@ -378,7 +388,7 @@ function hasItem(/* args... */) * assertThat(array('a', 'b', 'c'), hasItems(equalTo('a'), equalTo('b'))); * */ - function hasItems(/* args... */) + function hasItems(/* args... */): \Hamcrest\Core\AllOf { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Core\IsCollectionContaining', 'hasItems'), $args); @@ -389,8 +399,10 @@ function hasItems(/* args... */) /** * Is the value equal to another value, as tested by the use of the "==" * comparison operator? + * + * @param mixed $item */ - function equalTo($item) + function equalTo($item): \Hamcrest\Core\IsEqual { return \Hamcrest\Core\IsEqual::equalTo($item); } @@ -399,8 +411,10 @@ function equalTo($item) if (!function_exists('identicalTo')) { /** * Tests of the value is identical to $value as tested by the "===" operator. + * + * @param mixed $value */ - function identicalTo($value) + function identicalTo($value): \Hamcrest\Core\IsIdentical { return \Hamcrest\Core\IsIdentical::identicalTo($value); } @@ -413,7 +427,7 @@ function identicalTo($value) * the signature of the method that sets it up, for example in * assertThat($anObject, anInstanceOf('Thing')); */ - function anInstanceOf($theClass) + function anInstanceOf(string $theClass): \Hamcrest\Core\IsInstanceOf { return \Hamcrest\Core\IsInstanceOf::anInstanceOf($theClass); } @@ -426,7 +440,7 @@ function anInstanceOf($theClass) * the signature of the method that sets it up, for example in * assertThat($anObject, anInstanceOf('Thing')); */ - function any($theClass) + function any(string $theClass): \Hamcrest\Core\IsInstanceOf { return \Hamcrest\Core\IsInstanceOf::anInstanceOf($theClass); } @@ -435,8 +449,10 @@ function any($theClass) if (!function_exists('not')) { /** * Matches if value does not match $value. + * + * @param mixed $value */ - function not($value) + function not($value): \Hamcrest\Core\IsNot { return \Hamcrest\Core\IsNot::not($value); } @@ -446,7 +462,7 @@ function not($value) /** * Matches if value is null. */ - function nullValue() + function nullValue(): \Hamcrest\Core\IsNull { return \Hamcrest\Core\IsNull::nullValue(); } @@ -456,7 +472,7 @@ function nullValue() /** * Matches if value is not null. */ - function notNullValue() + function notNullValue(): \Hamcrest\Core\IsNot { return \Hamcrest\Core\IsNull::notNullValue(); } @@ -472,7 +488,7 @@ function notNullValue() * * @return \Hamcrest\Core\IsSame */ - function sameInstance($object) + function sameInstance($object): \Hamcrest\Core\IsSame { return \Hamcrest\Core\IsSame::sameInstance($object); } @@ -481,8 +497,10 @@ function sameInstance($object) if (!function_exists('typeOf')) { /** * Is the value a particular built-in type? + * + * @param string $theType */ - function typeOf($theType) + function typeOf(string $theType): \Hamcrest\Core\IsTypeOf { return \Hamcrest\Core\IsTypeOf::typeOf($theType); } @@ -491,8 +509,10 @@ function typeOf($theType) if (!function_exists('set')) { /** * Matches if value (class, object, or array) has named $property. + * + * @param mixed $property */ - function set($property) + function set($property): \Hamcrest\Core\Set { return \Hamcrest\Core\Set::set($property); } @@ -501,8 +521,10 @@ function set($property) if (!function_exists('notSet')) { /** * Matches if value (class, object, or array) does not have named $property. + * + * @param mixed $property */ - function notSet($property) + function notSet($property): \Hamcrest\Core\Set { return \Hamcrest\Core\Set::notSet($property); } @@ -512,8 +534,11 @@ function notSet($property) /** * Matches if value is a number equal to $value within some range of * acceptable error $delta. + * + * @param mixed $value + * @param mixed $delta */ - function closeTo($value, $delta) + function closeTo($value, $delta): \Hamcrest\Number\IsCloseTo { return \Hamcrest\Number\IsCloseTo::closeTo($value, $delta); } @@ -522,8 +547,10 @@ function closeTo($value, $delta) if (!function_exists('comparesEqualTo')) { /** * The value is not > $value, nor < $value. + * + * @param mixed $value */ - function comparesEqualTo($value) + function comparesEqualTo($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::comparesEqualTo($value); } @@ -532,8 +559,10 @@ function comparesEqualTo($value) if (!function_exists('greaterThan')) { /** * The value is > $value. + * + * @param mixed $value */ - function greaterThan($value) + function greaterThan($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::greaterThan($value); } @@ -542,8 +571,10 @@ function greaterThan($value) if (!function_exists('greaterThanOrEqualTo')) { /** * The value is >= $value. + * + * @param mixed $value */ - function greaterThanOrEqualTo($value) + function greaterThanOrEqualTo($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::greaterThanOrEqualTo($value); } @@ -552,8 +583,10 @@ function greaterThanOrEqualTo($value) if (!function_exists('atLeast')) { /** * The value is >= $value. + * + * @param mixed $value */ - function atLeast($value) + function atLeast($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::greaterThanOrEqualTo($value); } @@ -562,8 +595,10 @@ function atLeast($value) if (!function_exists('lessThan')) { /** * The value is < $value. + * + * @param mixed $value */ - function lessThan($value) + function lessThan($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::lessThan($value); } @@ -572,8 +607,10 @@ function lessThan($value) if (!function_exists('lessThanOrEqualTo')) { /** * The value is <= $value. + * + * @param mixed $value */ - function lessThanOrEqualTo($value) + function lessThanOrEqualTo($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::lessThanOrEqualTo($value); } @@ -582,8 +619,10 @@ function lessThanOrEqualTo($value) if (!function_exists('atMost')) { /** * The value is <= $value. + * + * @param mixed $value */ - function atMost($value) + function atMost($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::lessThanOrEqualTo($value); } @@ -593,7 +632,7 @@ function atMost($value) /** * Matches if value is a zero-length string. */ - function isEmptyString() + function isEmptyString(): \Hamcrest\Text\IsEmptyString { return \Hamcrest\Text\IsEmptyString::isEmptyString(); } @@ -603,7 +642,7 @@ function isEmptyString() /** * Matches if value is a zero-length string. */ - function emptyString() + function emptyString(): \Hamcrest\Text\IsEmptyString { return \Hamcrest\Text\IsEmptyString::isEmptyString(); } @@ -613,7 +652,7 @@ function emptyString() /** * Matches if value is null or a zero-length string. */ - function isEmptyOrNullString() + function isEmptyOrNullString(): \Hamcrest\Core\AnyOf { return \Hamcrest\Text\IsEmptyString::isEmptyOrNullString(); } @@ -623,7 +662,7 @@ function isEmptyOrNullString() /** * Matches if value is null or a zero-length string. */ - function nullOrEmptyString() + function nullOrEmptyString(): \Hamcrest\Core\AnyOf { return \Hamcrest\Text\IsEmptyString::isEmptyOrNullString(); } @@ -633,7 +672,7 @@ function nullOrEmptyString() /** * Matches if value is a non-zero-length string. */ - function isNonEmptyString() + function isNonEmptyString(): \Hamcrest\Text\IsEmptyString { return \Hamcrest\Text\IsEmptyString::isNonEmptyString(); } @@ -643,7 +682,7 @@ function isNonEmptyString() /** * Matches if value is a non-zero-length string. */ - function nonEmptyString() + function nonEmptyString(): \Hamcrest\Text\IsEmptyString { return \Hamcrest\Text\IsEmptyString::isNonEmptyString(); } @@ -652,8 +691,10 @@ function nonEmptyString() if (!function_exists('equalToIgnoringCase')) { /** * Matches if value is a string equal to $string, regardless of the case. + * + * @param mixed $string */ - function equalToIgnoringCase($string) + function equalToIgnoringCase($string): \Hamcrest\Text\IsEqualIgnoringCase { return \Hamcrest\Text\IsEqualIgnoringCase::equalToIgnoringCase($string); } @@ -662,8 +703,10 @@ function equalToIgnoringCase($string) if (!function_exists('equalToIgnoringWhiteSpace')) { /** * Matches if value is a string equal to $string, regardless of whitespace. + * + * @param mixed $string */ - function equalToIgnoringWhiteSpace($string) + function equalToIgnoringWhiteSpace($string): \Hamcrest\Text\IsEqualIgnoringWhiteSpace { return \Hamcrest\Text\IsEqualIgnoringWhiteSpace::equalToIgnoringWhiteSpace($string); } @@ -672,8 +715,10 @@ function equalToIgnoringWhiteSpace($string) if (!function_exists('matchesPattern')) { /** * Matches if value is a string that matches regular expression $pattern. + * + * @param mixed $pattern */ - function matchesPattern($pattern) + function matchesPattern($pattern): \Hamcrest\Text\MatchesPattern { return \Hamcrest\Text\MatchesPattern::matchesPattern($pattern); } @@ -682,8 +727,10 @@ function matchesPattern($pattern) if (!function_exists('containsString')) { /** * Matches if value is a string that contains $substring. + * + * @param mixed $substring */ - function containsString($substring) + function containsString($substring): \Hamcrest\Text\StringContains { return \Hamcrest\Text\StringContains::containsString($substring); } @@ -692,8 +739,10 @@ function containsString($substring) if (!function_exists('containsStringIgnoringCase')) { /** * Matches if value is a string that contains $substring regardless of the case. + * + * @param mixed $substring */ - function containsStringIgnoringCase($substring) + function containsStringIgnoringCase($substring): \Hamcrest\Text\StringContainsIgnoringCase { return \Hamcrest\Text\StringContainsIgnoringCase::containsStringIgnoringCase($substring); } @@ -703,7 +752,7 @@ function containsStringIgnoringCase($substring) /** * Matches if value contains $substrings in a constrained order. */ - function stringContainsInOrder(/* args... */) + function stringContainsInOrder(/* args... */): \Hamcrest\Text\StringContainsInOrder { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Text\StringContainsInOrder', 'stringContainsInOrder'), $args); @@ -713,8 +762,10 @@ function stringContainsInOrder(/* args... */) if (!function_exists('endsWith')) { /** * Matches if value is a string that ends with $substring. + * + * @param mixed $substring */ - function endsWith($substring) + function endsWith($substring): \Hamcrest\Text\StringEndsWith { return \Hamcrest\Text\StringEndsWith::endsWith($substring); } @@ -723,8 +774,10 @@ function endsWith($substring) if (!function_exists('startsWith')) { /** * Matches if value is a string that starts with $substring. + * + * @param mixed $substring */ - function startsWith($substring) + function startsWith($substring): \Hamcrest\Text\StringStartsWith { return \Hamcrest\Text\StringStartsWith::startsWith($substring); } @@ -734,7 +787,7 @@ function startsWith($substring) /** * Is the value an array? */ - function arrayValue() + function arrayValue(): \Hamcrest\Type\IsArray { return \Hamcrest\Type\IsArray::arrayValue(); } @@ -744,7 +797,7 @@ function arrayValue() /** * Is the value a boolean? */ - function booleanValue() + function booleanValue(): \Hamcrest\Type\IsBoolean { return \Hamcrest\Type\IsBoolean::booleanValue(); } @@ -754,7 +807,7 @@ function booleanValue() /** * Is the value a boolean? */ - function boolValue() + function boolValue(): \Hamcrest\Type\IsBoolean { return \Hamcrest\Type\IsBoolean::booleanValue(); } @@ -764,7 +817,7 @@ function boolValue() /** * Is the value callable? */ - function callableValue() + function callableValue(): \Hamcrest\Type\IsCallable { return \Hamcrest\Type\IsCallable::callableValue(); } @@ -774,7 +827,7 @@ function callableValue() /** * Is the value a float/double? */ - function doubleValue() + function doubleValue(): \Hamcrest\Type\IsDouble { return \Hamcrest\Type\IsDouble::doubleValue(); } @@ -784,7 +837,7 @@ function doubleValue() /** * Is the value a float/double? */ - function floatValue() + function floatValue(): \Hamcrest\Type\IsDouble { return \Hamcrest\Type\IsDouble::doubleValue(); } @@ -794,7 +847,7 @@ function floatValue() /** * Is the value an integer? */ - function integerValue() + function integerValue(): \Hamcrest\Type\IsInteger { return \Hamcrest\Type\IsInteger::integerValue(); } @@ -804,7 +857,7 @@ function integerValue() /** * Is the value an integer? */ - function intValue() + function intValue(): \Hamcrest\Type\IsInteger { return \Hamcrest\Type\IsInteger::integerValue(); } @@ -814,7 +867,7 @@ function intValue() /** * Is the value a numeric? */ - function numericValue() + function numericValue(): \Hamcrest\Type\IsNumeric { return \Hamcrest\Type\IsNumeric::numericValue(); } @@ -824,7 +877,7 @@ function numericValue() /** * Is the value an object? */ - function objectValue() + function objectValue(): \Hamcrest\Type\IsObject { return \Hamcrest\Type\IsObject::objectValue(); } @@ -834,7 +887,7 @@ function objectValue() /** * Is the value an object? */ - function anObject() + function anObject(): \Hamcrest\Type\IsObject { return \Hamcrest\Type\IsObject::objectValue(); } @@ -844,7 +897,7 @@ function anObject() /** * Is the value a resource? */ - function resourceValue() + function resourceValue(): \Hamcrest\Type\IsResource { return \Hamcrest\Type\IsResource::resourceValue(); } @@ -854,7 +907,7 @@ function resourceValue() /** * Is the value a scalar (boolean, integer, double, or string)? */ - function scalarValue() + function scalarValue(): \Hamcrest\Type\IsScalar { return \Hamcrest\Type\IsScalar::scalarValue(); } @@ -864,7 +917,7 @@ function scalarValue() /** * Is the value a string? */ - function stringValue() + function stringValue(): \Hamcrest\Type\IsString { return \Hamcrest\Type\IsString::stringValue(); } @@ -875,8 +928,11 @@ function stringValue() * Wraps $matcher with {@link Hamcrest\Core\IsEqual) * if it's not a matcher and the XPath in count() * if it's an integer. + * + * @param string $xpath + * @param null|Matcher|int|mixed $matcher */ - function hasXPath($xpath, $matcher = null) + function hasXPath(string $xpath, $matcher = null): \Hamcrest\Xml\HasXPath { return \Hamcrest\Xml\HasXPath::hasXPath($xpath, $matcher); } diff --git a/hamcrest/Hamcrest/Arrays/IsArray.php b/hamcrest/Hamcrest/Arrays/IsArray.php index 9ea569703..0d90d3860 100644 --- a/hamcrest/Hamcrest/Arrays/IsArray.php +++ b/hamcrest/Hamcrest/Arrays/IsArray.php @@ -10,6 +10,7 @@ // TODO: Allow this to take matchers or values within the array use Hamcrest\Description; +use Hamcrest\Matcher; use Hamcrest\TypeSafeMatcher; use Hamcrest\Util; @@ -20,8 +21,14 @@ class IsArray extends TypeSafeMatcher { - private $_elementMatchers; + /** + * @var array + */ + private array $_elementMatchers; + /** + * @param array $elementMatchers + */ public function __construct(array $elementMatchers) { parent::__construct(self::TYPE_ARRAY); @@ -31,13 +38,12 @@ public function __construct(array $elementMatchers) $this->_elementMatchers = $elementMatchers; } - protected function matchesSafely($array) + protected function matchesSafely($array): bool { if (array_keys($array) != array_keys($this->_elementMatchers)) { return false; } - /** @var $matcher \Hamcrest\Matcher */ foreach ($this->_elementMatchers as $k => $matcher) { if (!$matcher->matches($array[$k])) { return false; @@ -47,7 +53,7 @@ protected function matchesSafely($array) return true; } - protected function describeMismatchSafely($actual, Description $mismatchDescription) + protected function describeMismatchSafely($actual, Description $mismatchDescription): void { if (count($actual) != count($this->_elementMatchers)) { $mismatchDescription->appendText('array length was ' . count($actual)); @@ -66,7 +72,6 @@ protected function describeMismatchSafely($actual, Description $mismatchDescript return; } - /** @var $matcher \Hamcrest\Matcher */ foreach ($this->_elementMatchers as $k => $matcher) { if (!$matcher->matches($actual[$k])) { $mismatchDescription->appendText('element ')->appendValue($k) @@ -77,7 +82,7 @@ protected function describeMismatchSafely($actual, Description $mismatchDescript } } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendList( $this->descriptionStart(), @@ -92,7 +97,7 @@ public function describeTo(Description $description) * * @factory ... */ - public static function anArray(/* args... */) + public static function anArray(/* args... */): self { $args = func_get_args(); @@ -101,17 +106,17 @@ public static function anArray(/* args... */) // -- Protected Methods - protected function descriptionStart() + protected function descriptionStart(): string { return '['; } - protected function descriptionSeparator() + protected function descriptionSeparator(): string { return ', '; } - protected function descriptionEnd() + protected function descriptionEnd(): string { return ']'; } diff --git a/hamcrest/Hamcrest/Arrays/IsArrayContaining.php b/hamcrest/Hamcrest/Arrays/IsArrayContaining.php index 0e4a1eda9..b267e3fb7 100644 --- a/hamcrest/Hamcrest/Arrays/IsArrayContaining.php +++ b/hamcrest/Hamcrest/Arrays/IsArrayContaining.php @@ -15,7 +15,7 @@ class IsArrayContaining extends TypeSafeMatcher { - private $_elementMatcher; + private Matcher $_elementMatcher; public function __construct(Matcher $elementMatcher) { @@ -24,7 +24,7 @@ public function __construct(Matcher $elementMatcher) $this->_elementMatcher = $elementMatcher; } - protected function matchesSafely($array) + protected function matchesSafely($array): bool { foreach ($array as $element) { if ($this->_elementMatcher->matches($element)) { @@ -35,12 +35,12 @@ protected function matchesSafely($array) return false; } - protected function describeMismatchSafely($array, Description $mismatchDescription) + protected function describeMismatchSafely($array, Description $mismatchDescription): void { $mismatchDescription->appendText('was ')->appendValue($array); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description ->appendText('an array containing ') @@ -56,7 +56,7 @@ public function describeTo(Description $description) * @return \Hamcrest\Arrays\IsArrayContaining * @factory hasValue */ - public static function hasItemInArray($item) + public static function hasItemInArray($item): self { return new self(Util::wrapValueWithIsEqual($item)); } diff --git a/hamcrest/Hamcrest/Arrays/IsArrayContainingInAnyOrder.php b/hamcrest/Hamcrest/Arrays/IsArrayContainingInAnyOrder.php index 9009026b8..185737e75 100644 --- a/hamcrest/Hamcrest/Arrays/IsArrayContainingInAnyOrder.php +++ b/hamcrest/Hamcrest/Arrays/IsArrayContainingInAnyOrder.php @@ -5,6 +5,7 @@ Copyright (c) 2009 hamcrest.org */ use Hamcrest\Description; +use Hamcrest\Matcher; use Hamcrest\TypeSafeDiagnosingMatcher; use Hamcrest\Util; @@ -14,8 +15,14 @@ class IsArrayContainingInAnyOrder extends TypeSafeDiagnosingMatcher { - private $_elementMatchers; + /** + * @var array + */ + private array $_elementMatchers; + /** + * @param array $elementMatchers + */ public function __construct(array $elementMatchers) { parent::__construct(self::TYPE_ARRAY); @@ -25,7 +32,7 @@ public function __construct(array $elementMatchers) $this->_elementMatchers = $elementMatchers; } - protected function matchesSafelyWithDiagnosticDescription($array, Description $mismatchDescription) + protected function matchesSafelyWithDiagnosticDescription($array, Description $mismatchDescription): bool { $matching = new MatchingOnce($this->_elementMatchers, $mismatchDescription); @@ -38,7 +45,7 @@ protected function matchesSafelyWithDiagnosticDescription($array, Description $m return $matching->isFinished($array); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendList('[', ', ', ']', $this->_elementMatchers) ->appendText(' in any order') @@ -50,7 +57,7 @@ public function describeTo(Description $description) * * @factory containsInAnyOrder ... */ - public static function arrayContainingInAnyOrder(/* args... */) + public static function arrayContainingInAnyOrder(/* args... */): self { $args = func_get_args(); diff --git a/hamcrest/Hamcrest/Arrays/IsArrayContainingInOrder.php b/hamcrest/Hamcrest/Arrays/IsArrayContainingInOrder.php index 611574045..b28cd442a 100644 --- a/hamcrest/Hamcrest/Arrays/IsArrayContainingInOrder.php +++ b/hamcrest/Hamcrest/Arrays/IsArrayContainingInOrder.php @@ -5,6 +5,7 @@ Copyright (c) 2009 hamcrest.org */ use Hamcrest\Description; +use Hamcrest\Matcher; use Hamcrest\TypeSafeDiagnosingMatcher; use Hamcrest\Util; @@ -14,8 +15,14 @@ class IsArrayContainingInOrder extends TypeSafeDiagnosingMatcher { - private $_elementMatchers; + /** + * @var array + */ + private array $_elementMatchers; + /** + * @param array $elementMatchers + */ public function __construct(array $elementMatchers) { parent::__construct(self::TYPE_ARRAY); @@ -25,7 +32,7 @@ public function __construct(array $elementMatchers) $this->_elementMatchers = $elementMatchers; } - protected function matchesSafelyWithDiagnosticDescription($array, Description $mismatchDescription) + protected function matchesSafelyWithDiagnosticDescription($array, Description $mismatchDescription): bool { $series = new SeriesMatchingOnce($this->_elementMatchers, $mismatchDescription); @@ -38,7 +45,7 @@ protected function matchesSafelyWithDiagnosticDescription($array, Description $m return $series->isFinished(); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendList('[', ', ', ']', $this->_elementMatchers); } @@ -48,7 +55,7 @@ public function describeTo(Description $description) * * @factory contains ... */ - public static function arrayContaining(/* args... */) + public static function arrayContaining(/* args... */): self { $args = func_get_args(); diff --git a/hamcrest/Hamcrest/Arrays/IsArrayContainingKey.php b/hamcrest/Hamcrest/Arrays/IsArrayContainingKey.php index 523477e7b..be556bde6 100644 --- a/hamcrest/Hamcrest/Arrays/IsArrayContainingKey.php +++ b/hamcrest/Hamcrest/Arrays/IsArrayContainingKey.php @@ -15,7 +15,7 @@ class IsArrayContainingKey extends TypeSafeMatcher { - private $_keyMatcher; + private Matcher $_keyMatcher; public function __construct(Matcher $keyMatcher) { @@ -24,7 +24,7 @@ public function __construct(Matcher $keyMatcher) $this->_keyMatcher = $keyMatcher; } - protected function matchesSafely($array) + protected function matchesSafely($array): bool { foreach ($array as $key => $element) { if ($this->_keyMatcher->matches($key)) { @@ -35,7 +35,7 @@ protected function matchesSafely($array) return false; } - protected function describeMismatchSafely($array, Description $mismatchDescription) + protected function describeMismatchSafely($array, Description $mismatchDescription): void { //Not using appendValueList() so that keys can be shown $mismatchDescription->appendText('array was ') @@ -52,7 +52,7 @@ protected function describeMismatchSafely($array, Description $mismatchDescripti $mismatchDescription->appendText(']'); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description ->appendText('array with key ') @@ -68,7 +68,7 @@ public function describeTo(Description $description) * @return \Hamcrest\Arrays\IsArrayContainingKey * @factory hasKey */ - public static function hasKeyInArray($key) + public static function hasKeyInArray($key): self { return new self(Util::wrapValueWithIsEqual($key)); } diff --git a/hamcrest/Hamcrest/Arrays/IsArrayContainingKeyValuePair.php b/hamcrest/Hamcrest/Arrays/IsArrayContainingKeyValuePair.php index 9ac3eba80..5aa736805 100644 --- a/hamcrest/Hamcrest/Arrays/IsArrayContainingKeyValuePair.php +++ b/hamcrest/Hamcrest/Arrays/IsArrayContainingKeyValuePair.php @@ -15,9 +15,8 @@ class IsArrayContainingKeyValuePair extends TypeSafeMatcher { - - private $_keyMatcher; - private $_valueMatcher; + private Matcher $_keyMatcher; + private Matcher $_valueMatcher; public function __construct(Matcher $keyMatcher, Matcher $valueMatcher) { @@ -27,7 +26,7 @@ public function __construct(Matcher $keyMatcher, Matcher $valueMatcher) $this->_valueMatcher = $valueMatcher; } - protected function matchesSafely($array) + protected function matchesSafely($array): bool { foreach ($array as $key => $value) { if ($this->_keyMatcher->matches($key) && $this->_valueMatcher->matches($value)) { @@ -38,7 +37,7 @@ protected function matchesSafely($array) return false; } - protected function describeMismatchSafely($array, Description $mismatchDescription) + protected function describeMismatchSafely($array, Description $mismatchDescription): void { //Not using appendValueList() so that keys can be shown $mismatchDescription->appendText('array was ') @@ -55,7 +54,7 @@ protected function describeMismatchSafely($array, Description $mismatchDescripti $mismatchDescription->appendText(']'); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('array containing [') ->appendDescriptionOf($this->_keyMatcher) @@ -69,8 +68,10 @@ public function describeTo(Description $description) * Test if an array has both an key and value in parity with each other. * * @factory hasEntry + * @param mixed $key + * @param mixed $value */ - public static function hasKeyValuePair($key, $value) + public static function hasKeyValuePair($key, $value): self { return new self( Util::wrapValueWithIsEqual($key), diff --git a/hamcrest/Hamcrest/Arrays/IsArrayWithSize.php b/hamcrest/Hamcrest/Arrays/IsArrayWithSize.php index 074375ce1..4be0ef31e 100644 --- a/hamcrest/Hamcrest/Arrays/IsArrayWithSize.php +++ b/hamcrest/Hamcrest/Arrays/IsArrayWithSize.php @@ -40,7 +40,7 @@ protected function featureValueOf($array) * @return \Hamcrest\Arrays\IsArrayWithSize * @factory */ - public static function arrayWithSize($size) + public static function arrayWithSize($size): self { return new self(Util::wrapValueWithIsEqual($size)); } @@ -50,7 +50,7 @@ public static function arrayWithSize($size) * * @factory */ - public static function emptyArray() + public static function emptyArray(): DescribedAs { return DescribedAs::describedAs( 'an empty array', @@ -63,7 +63,7 @@ public static function emptyArray() * * @factory */ - public static function nonEmptyArray() + public static function nonEmptyArray(): DescribedAs { return DescribedAs::describedAs( 'a non-empty array', diff --git a/hamcrest/Hamcrest/Arrays/MatchingOnce.php b/hamcrest/Hamcrest/Arrays/MatchingOnce.php index 324c7e089..f8051afde 100644 --- a/hamcrest/Hamcrest/Arrays/MatchingOnce.php +++ b/hamcrest/Hamcrest/Arrays/MatchingOnce.php @@ -6,25 +6,38 @@ */ use Hamcrest\Description; +use Hamcrest\Matcher; class MatchingOnce { - private $_elementMatchers; - private $_mismatchDescription; + /** + * @var array + */ + private array $_elementMatchers; + private Description $_mismatchDescription; + /** + * @param array $elementMatchers + */ public function __construct(array $elementMatchers, Description $mismatchDescription) { $this->_elementMatchers = $elementMatchers; $this->_mismatchDescription = $mismatchDescription; } - public function matches($item) + /** + * @param mixed $item + */ + public function matches($item): bool { return $this->_isNotSurplus($item) && $this->_isMatched($item); } - public function isFinished($items) + /** + * @param mixed $items + */ + public function isFinished($items): bool { if (empty($this->_elementMatchers)) { return true; @@ -40,7 +53,10 @@ public function isFinished($items) // -- Private Methods - private function _isNotSurplus($item) + /** + * @param mixed $item + */ + private function _isNotSurplus($item): bool { if (empty($this->_elementMatchers)) { $this->_mismatchDescription->appendText('Not matched: ')->appendValue($item); @@ -51,9 +67,11 @@ private function _isNotSurplus($item) return true; } - private function _isMatched($item) + /** + * @param mixed $item + */ + private function _isMatched($item): bool { - /** @var $matcher \Hamcrest\Matcher */ foreach ($this->_elementMatchers as $i => $matcher) { if ($matcher->matches($item)) { unset($this->_elementMatchers[$i]); diff --git a/hamcrest/Hamcrest/Arrays/SeriesMatchingOnce.php b/hamcrest/Hamcrest/Arrays/SeriesMatchingOnce.php index 12a912d86..3df544a03 100644 --- a/hamcrest/Hamcrest/Arrays/SeriesMatchingOnce.php +++ b/hamcrest/Hamcrest/Arrays/SeriesMatchingOnce.php @@ -11,11 +11,23 @@ class SeriesMatchingOnce { - private $_elementMatchers; - private $_keys; - private $_mismatchDescription; + /** + * @var array + */ + private array $_elementMatchers; + /** + * @var list + */ + private array $_keys; + private Description $_mismatchDescription; + /** + * @var int|string|null + */ private $_nextMatchKey; + /** + * @param array $elementMatchers + */ public function __construct(array $elementMatchers, Description $mismatchDescription) { $this->_elementMatchers = $elementMatchers; @@ -23,12 +35,15 @@ public function __construct(array $elementMatchers, Description $mismatchDescrip $this->_mismatchDescription = $mismatchDescription; } - public function matches($item) + /** + * @param mixed $item + */ + public function matches($item): bool { return $this->_isNotSurplus($item) && $this->_isMatched($item); } - public function isFinished() + public function isFinished(): bool { if (!empty($this->_elementMatchers)) { $nextMatcher = current($this->_elementMatchers); @@ -42,7 +57,10 @@ public function isFinished() // -- Private Methods - private function _isNotSurplus($item) + /** + * @param mixed $item + */ + private function _isNotSurplus($item): bool { if (empty($this->_elementMatchers)) { $this->_mismatchDescription->appendText('Not matched: ')->appendValue($item); @@ -53,7 +71,10 @@ private function _isNotSurplus($item) return true; } - private function _isMatched($item) + /** + * @param mixed $item + */ + private function _isMatched($item): bool { $this->_nextMatchKey = array_shift($this->_keys); $nextMatcher = array_shift($this->_elementMatchers); @@ -67,7 +88,10 @@ private function _isMatched($item) return true; } - private function _describeMismatch(Matcher $matcher, $item) + /** + * @param mixed $item + */ + private function _describeMismatch(Matcher $matcher, $item): void { $this->_mismatchDescription->appendText('item with key ' . $this->_nextMatchKey . ': '); $matcher->describeMismatch($item, $this->_mismatchDescription); diff --git a/hamcrest/Hamcrest/BaseDescription.php b/hamcrest/Hamcrest/BaseDescription.php index bcd4fef8c..4805443e6 100644 --- a/hamcrest/Hamcrest/BaseDescription.php +++ b/hamcrest/Hamcrest/BaseDescription.php @@ -12,7 +12,7 @@ abstract class BaseDescription implements Description { - public function appendText($text) + public function appendText(string $text): self { $this->append($text); @@ -26,7 +26,7 @@ public function appendDescriptionOf(SelfDescribing $value) return $this; } - public function appendValue($value) + public function appendValue($value): self { if (is_null($value)) { $this->append('null'); @@ -55,7 +55,7 @@ public function appendValue($value) return $this; } - public function appendValueList($start, $separator, $end, $values) + public function appendValueList(string $start, string $separator, string $end, iterable $values): self { $list = array(); foreach ($values as $v) { @@ -67,7 +67,7 @@ public function appendValueList($start, $separator, $end, $values) return $this; } - public function appendList($start, $separator, $end, $values) + public function appendList(string $start, string $separator, string $end, iterable $values): self { $this->append($start); @@ -96,12 +96,13 @@ public function appendList($start, $separator, $end, $values) /** * Append the String $str to the description. + * @param mixed $str */ - abstract protected function append($str); + abstract protected function append($str): void; // -- Private Methods - private function _toPhpSyntax($value) + private function _toPhpSyntax(string $value): void { $str = '"'; for ($i = 0, $len = strlen($value); $i < $len; ++$i) { diff --git a/hamcrest/Hamcrest/BaseMatcher.php b/hamcrest/Hamcrest/BaseMatcher.php index 06055698c..679f38112 100644 --- a/hamcrest/Hamcrest/BaseMatcher.php +++ b/hamcrest/Hamcrest/BaseMatcher.php @@ -12,18 +12,20 @@ */ abstract class BaseMatcher implements Matcher { - - public function describeMismatch($item, Description $description) + /** + * @param mixed $item + */ + public function describeMismatch($item, Description $description): void { $description->appendText('was ')->appendValue($item); } - public function __toString() + public function __toString(): string { return StringDescription::toString($this); } - public function __invoke() + public function __invoke(): bool { return call_user_func_array(array($this, 'matches'), func_get_args()); } diff --git a/hamcrest/Hamcrest/Collection/IsEmptyTraversable.php b/hamcrest/Hamcrest/Collection/IsEmptyTraversable.php index 8ab58ea5a..855d4e4ac 100644 --- a/hamcrest/Hamcrest/Collection/IsEmptyTraversable.php +++ b/hamcrest/Hamcrest/Collection/IsEmptyTraversable.php @@ -13,17 +13,17 @@ class IsEmptyTraversable extends BaseMatcher { - private static $_INSTANCE; - private static $_NOT_INSTANCE; + private static ?self $_INSTANCE = null; + private static ?self $_NOT_INSTANCE = null; - private $_empty; + private bool $_empty; - public function __construct($empty = true) + public function __construct(bool $empty = true) { $this->_empty = $empty; } - public function matches($item) + public function matches($item): bool { if (!$item instanceof \Traversable) { return false; @@ -36,7 +36,7 @@ public function matches($item) return $this->_empty; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText($this->_empty ? 'an empty traversable' : 'a non-empty traversable'); } @@ -46,7 +46,7 @@ public function describeTo(Description $description) * * @factory */ - public static function emptyTraversable() + public static function emptyTraversable(): self { if (!self::$_INSTANCE) { self::$_INSTANCE = new self; @@ -60,7 +60,7 @@ public static function emptyTraversable() * * @factory */ - public static function nonEmptyTraversable() + public static function nonEmptyTraversable(): self { if (!self::$_NOT_INSTANCE) { self::$_NOT_INSTANCE = new self(false); diff --git a/hamcrest/Hamcrest/Collection/IsTraversableWithSize.php b/hamcrest/Hamcrest/Collection/IsTraversableWithSize.php index c95edc5c3..a0f89c801 100644 --- a/hamcrest/Hamcrest/Collection/IsTraversableWithSize.php +++ b/hamcrest/Hamcrest/Collection/IsTraversableWithSize.php @@ -39,8 +39,9 @@ protected function featureValueOf($actual) * Does traversable size satisfy a given matcher? * * @factory + * @param mixed $size */ - public static function traversableWithSize($size) + public static function traversableWithSize($size): self { return new self(Util::wrapValueWithIsEqual($size)); } diff --git a/hamcrest/Hamcrest/Core/AllOf.php b/hamcrest/Hamcrest/Core/AllOf.php index 7aefd5386..bdf3faf84 100644 --- a/hamcrest/Hamcrest/Core/AllOf.php +++ b/hamcrest/Hamcrest/Core/AllOf.php @@ -6,6 +6,7 @@ */ use Hamcrest\Description; use Hamcrest\DiagnosingMatcher; +use Hamcrest\Matcher; use Hamcrest\Util; /** @@ -16,8 +17,14 @@ class AllOf extends DiagnosingMatcher { - private $_matchers; + /** + * @var array + */ + private array $_matchers; + /** + * @param array $matchers + */ public function __construct(array $matchers) { Util::checkAllAreMatchers($matchers); @@ -25,9 +32,8 @@ public function __construct(array $matchers) $this->_matchers = $matchers; } - public function matchesWithDiagnosticDescription($item, Description $mismatchDescription) + public function matchesWithDiagnosticDescription($item, Description $mismatchDescription): bool { - /** @var $matcher \Hamcrest\Matcher */ foreach ($this->_matchers as $matcher) { if (!$matcher->matches($item)) { $mismatchDescription->appendDescriptionOf($matcher)->appendText(' '); @@ -40,7 +46,7 @@ public function matchesWithDiagnosticDescription($item, Description $mismatchDes return true; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendList('(', ' and ', ')', $this->_matchers); } @@ -50,7 +56,7 @@ public function describeTo(Description $description) * * @factory ... */ - public static function allOf(/* args... */) + public static function allOf(/* args... */): self { $args = func_get_args(); diff --git a/hamcrest/Hamcrest/Core/AnyOf.php b/hamcrest/Hamcrest/Core/AnyOf.php index 4504279f3..4f04d8c00 100644 --- a/hamcrest/Hamcrest/Core/AnyOf.php +++ b/hamcrest/Hamcrest/Core/AnyOf.php @@ -5,6 +5,7 @@ Copyright (c) 2009 hamcrest.org */ use Hamcrest\Description; +use Hamcrest\Matcher; use Hamcrest\Util; /** @@ -15,17 +16,20 @@ class AnyOf extends ShortcutCombination { + /** + * @param array $matchers + */ public function __construct(array $matchers) { parent::__construct($matchers); } - public function matches($item) + public function matches($item): bool { return $this->matchesWithShortcut($item, true); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $this->describeToWithOperator($description, 'or'); } @@ -35,7 +39,7 @@ public function describeTo(Description $description) * * @factory ... */ - public static function anyOf(/* args... */) + public static function anyOf(/* args... */): self { $args = func_get_args(); @@ -47,7 +51,7 @@ public static function anyOf(/* args... */) * * @factory ... */ - public static function noneOf(/* args... */) + public static function noneOf(/* args... */): IsNot { $args = func_get_args(); diff --git a/hamcrest/Hamcrest/Core/CombinableMatcher.php b/hamcrest/Hamcrest/Core/CombinableMatcher.php index e3b4aa782..7a4574832 100644 --- a/hamcrest/Hamcrest/Core/CombinableMatcher.php +++ b/hamcrest/Hamcrest/Core/CombinableMatcher.php @@ -12,31 +12,31 @@ class CombinableMatcher extends BaseMatcher { - private $_matcher; + private Matcher $_matcher; public function __construct(Matcher $matcher) { $this->_matcher = $matcher; } - public function matches($item) + public function matches($item): bool { return $this->_matcher->matches($item); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendDescriptionOf($this->_matcher); } /** Diversion from Hamcrest-Java... Logical "and" not permitted */ - public function andAlso(Matcher $other) + public function andAlso(Matcher $other): self { return new self(new AllOf($this->_templatedListWith($other))); } /** Diversion from Hamcrest-Java... Logical "or" not permitted */ - public function orElse(Matcher $other) + public function orElse(Matcher $other): self { return new self(new AnyOf($this->_templatedListWith($other))); } @@ -50,7 +50,7 @@ public function orElse(Matcher $other) * * @factory */ - public static function both(Matcher $matcher) + public static function both(Matcher $matcher): self { return new self($matcher); } @@ -64,14 +64,17 @@ public static function both(Matcher $matcher) * * @factory */ - public static function either(Matcher $matcher) + public static function either(Matcher $matcher): self { return new self($matcher); } // -- Private Methods - private function _templatedListWith(Matcher $other) + /** + * @return list + */ + private function _templatedListWith(Matcher $other): array { return array($this->_matcher, $other); } diff --git a/hamcrest/Hamcrest/Core/DescribedAs.php b/hamcrest/Hamcrest/Core/DescribedAs.php index 5b2583fa7..28fc867f4 100644 --- a/hamcrest/Hamcrest/Core/DescribedAs.php +++ b/hamcrest/Hamcrest/Core/DescribedAs.php @@ -14,25 +14,31 @@ class DescribedAs extends BaseMatcher { - private $_descriptionTemplate; - private $_matcher; - private $_values; + private string $_descriptionTemplate; + private Matcher $_matcher; + /** + * @var array + */ + private array $_values; - const ARG_PATTERN = '/%([0-9]+)/'; + private const ARG_PATTERN = '/%([0-9]+)/'; - public function __construct($descriptionTemplate, Matcher $matcher, array $values) + /** + * @param array $values + */ + public function __construct(string $descriptionTemplate, Matcher $matcher, array $values) { $this->_descriptionTemplate = $descriptionTemplate; $this->_matcher = $matcher; $this->_values = $values; } - public function matches($item) + public function matches($item): bool { return $this->_matcher->matches($item); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $textStart = 0; while (preg_match(self::ARG_PATTERN, $this->_descriptionTemplate, $matches, PREG_OFFSET_CAPTURE, $textStart)) { @@ -56,7 +62,7 @@ public function describeTo(Description $description) * * @factory ... */ - public static function describedAs(/* $description, Hamcrest\Matcher $matcher, $values... */) + public static function describedAs(/* $description, Hamcrest\Matcher $matcher, $values... */): self { $args = func_get_args(); $description = array_shift($args); diff --git a/hamcrest/Hamcrest/Core/Every.php b/hamcrest/Hamcrest/Core/Every.php index d686f8dac..76a447b49 100644 --- a/hamcrest/Hamcrest/Core/Every.php +++ b/hamcrest/Hamcrest/Core/Every.php @@ -12,7 +12,7 @@ class Every extends TypeSafeDiagnosingMatcher { - private $_matcher; + private Matcher $_matcher; public function __construct(Matcher $matcher) { @@ -21,7 +21,7 @@ public function __construct(Matcher $matcher) $this->_matcher = $matcher; } - protected function matchesSafelyWithDiagnosticDescription($items, Description $mismatchDescription) + protected function matchesSafelyWithDiagnosticDescription($items, Description $mismatchDescription): bool { foreach ($items as $item) { if (!$this->_matcher->matches($item)) { @@ -35,13 +35,13 @@ protected function matchesSafelyWithDiagnosticDescription($items, Description $m return true; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('every item is ')->appendDescriptionOf($this->_matcher); } /** - * @param Matcher $itemMatcher + * @param \Hamcrest\Matcher $itemMatcher * A matcher to apply to every element in an array. * * @return \Hamcrest\Core\Every @@ -49,7 +49,7 @@ public function describeTo(Description $description) * * @factory */ - public static function everyItem(Matcher $itemMatcher) + public static function everyItem(Matcher $itemMatcher): self { return new self($itemMatcher); } diff --git a/hamcrest/Hamcrest/Core/HasToString.php b/hamcrest/Hamcrest/Core/HasToString.php index c2e4801ba..90ab0f827 100644 --- a/hamcrest/Hamcrest/Core/HasToString.php +++ b/hamcrest/Hamcrest/Core/HasToString.php @@ -26,7 +26,7 @@ public function __construct(Matcher $toStringMatcher) ); } - public function matchesSafelyWithDiagnosticDescription($actual, Description $mismatchDescription) + public function matchesSafelyWithDiagnosticDescription($actual, Description $mismatchDescription): bool { if (method_exists($actual, 'toString') || method_exists($actual, '__toString')) { return parent::matchesSafelyWithDiagnosticDescription($actual, $mismatchDescription); @@ -49,8 +49,9 @@ protected function featureValueOf($actual) * __toString() method returns a value equalTo the specified string. * * @factory + * @param mixed $matcher */ - public static function hasToString($matcher) + public static function hasToString($matcher): self { return new self(Util::wrapValueWithIsEqual($matcher)); } diff --git a/hamcrest/Hamcrest/Core/Is.php b/hamcrest/Hamcrest/Core/Is.php index 41266dc1f..fccfe1404 100644 --- a/hamcrest/Hamcrest/Core/Is.php +++ b/hamcrest/Hamcrest/Core/Is.php @@ -19,24 +19,24 @@ class Is extends BaseMatcher { - private $_matcher; + private Matcher $_matcher; public function __construct(Matcher $matcher) { $this->_matcher = $matcher; } - public function matches($arg) + public function matches($arg): bool { return $this->_matcher->matches($arg); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('is ')->appendDescriptionOf($this->_matcher); } - public function describeMismatch($item, Description $mismatchDescription) + public function describeMismatch($item, Description $mismatchDescription): void { $this->_matcher->describeMismatch($item, $mismatchDescription); } @@ -49,8 +49,9 @@ public function describeMismatch($item, Description $mismatchDescription) * vs. assertThat($cheese, is(equalTo($smelly))) * * @factory + * @param mixed $value */ - public static function is($value) + public static function is($value): self { return new self(Util::wrapValueWithIsEqual($value)); } diff --git a/hamcrest/Hamcrest/Core/IsAnything.php b/hamcrest/Hamcrest/Core/IsAnything.php index f20e6c0dc..0ec9d9f04 100644 --- a/hamcrest/Hamcrest/Core/IsAnything.php +++ b/hamcrest/Hamcrest/Core/IsAnything.php @@ -13,19 +13,19 @@ class IsAnything extends BaseMatcher { - private $_message; + private string $_message; - public function __construct($message = 'ANYTHING') + public function __construct(string $message = 'ANYTHING') { $this->_message = $message; } - public function matches($item) + public function matches($item): bool { return true; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText($this->_message); } @@ -35,10 +35,9 @@ public function describeTo(Description $description) * * @param string $description A meaningful string used when describing itself. * - * @return \Hamcrest\Core\IsAnything * @factory */ - public static function anything($description = 'ANYTHING') + public static function anything(string $description = 'ANYTHING'): self { return new self($description); } diff --git a/hamcrest/Hamcrest/Core/IsCollectionContaining.php b/hamcrest/Hamcrest/Core/IsCollectionContaining.php index 5e60426d1..a623a0397 100644 --- a/hamcrest/Hamcrest/Core/IsCollectionContaining.php +++ b/hamcrest/Hamcrest/Core/IsCollectionContaining.php @@ -15,7 +15,7 @@ class IsCollectionContaining extends TypeSafeMatcher { - private $_elementMatcher; + private Matcher $_elementMatcher; public function __construct(Matcher $elementMatcher) { @@ -24,7 +24,7 @@ public function __construct(Matcher $elementMatcher) $this->_elementMatcher = $elementMatcher; } - protected function matchesSafely($items) + protected function matchesSafely($items): bool { foreach ($items as $item) { if ($this->_elementMatcher->matches($item)) { @@ -35,12 +35,12 @@ protected function matchesSafely($items) return false; } - protected function describeMismatchSafely($items, Description $mismatchDescription) + protected function describeMismatchSafely($items, Description $mismatchDescription): void { $mismatchDescription->appendText('was ')->appendValue($items); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description ->appendText('a collection containing ') @@ -60,7 +60,7 @@ public function describeTo(Description $description) * * @factory ... */ - public static function hasItem() + public static function hasItem(): self { $args = func_get_args(); $firstArg = array_shift($args); @@ -79,7 +79,7 @@ public static function hasItem() * * @factory ... */ - public static function hasItems(/* args... */) + public static function hasItems(/* args... */): AllOf { $args = func_get_args(); $matchers = array(); diff --git a/hamcrest/Hamcrest/Core/IsEqual.php b/hamcrest/Hamcrest/Core/IsEqual.php index 523fba0b1..e37411b7a 100644 --- a/hamcrest/Hamcrest/Core/IsEqual.php +++ b/hamcrest/Hamcrest/Core/IsEqual.php @@ -13,20 +13,25 @@ */ class IsEqual extends BaseMatcher { - + /** + * @var mixed + */ private $_item; + /** + * @param mixed $item + */ public function __construct($item) { $this->_item = $item; } - public function matches($arg) + public function matches($arg): bool { return (($arg == $this->_item) && ($this->_item == $arg)); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendValue($this->_item); } @@ -36,8 +41,9 @@ public function describeTo(Description $description) * comparison operator? * * @factory + * @param mixed $item */ - public static function equalTo($item) + public static function equalTo($item): self { return new self($item); } diff --git a/hamcrest/Hamcrest/Core/IsIdentical.php b/hamcrest/Hamcrest/Core/IsIdentical.php index 28f7b36ea..6fad940a7 100644 --- a/hamcrest/Hamcrest/Core/IsIdentical.php +++ b/hamcrest/Hamcrest/Core/IsIdentical.php @@ -13,15 +13,21 @@ class IsIdentical extends IsSame { + /** + * @var mixed $_value + */ private $_value; + /** + * @param mixed $value + */ public function __construct($value) { parent::__construct($value); $this->_value = $value; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendValue($this->_value); } @@ -30,8 +36,9 @@ public function describeTo(Description $description) * Tests of the value is identical to $value as tested by the "===" operator. * * @factory + * @param mixed $value */ - public static function identicalTo($value) + public static function identicalTo($value): self { return new self($value); } diff --git a/hamcrest/Hamcrest/Core/IsInstanceOf.php b/hamcrest/Hamcrest/Core/IsInstanceOf.php index 7a5c92a6b..a3828372e 100644 --- a/hamcrest/Hamcrest/Core/IsInstanceOf.php +++ b/hamcrest/Hamcrest/Core/IsInstanceOf.php @@ -13,7 +13,7 @@ class IsInstanceOf extends DiagnosingMatcher { - private $_theClass; + private string $_theClass; /** * Creates a new instance of IsInstanceOf @@ -22,12 +22,12 @@ class IsInstanceOf extends DiagnosingMatcher * The predicate evaluates to true for instances of this class * or one of its subclasses. */ - public function __construct($theClass) + public function __construct(string $theClass) { $this->_theClass = $theClass; } - protected function matchesWithDiagnosticDescription($item, Description $mismatchDescription) + protected function matchesWithDiagnosticDescription($item, Description $mismatchDescription): bool { if (!is_object($item)) { $mismatchDescription->appendText('was ')->appendValue($item); @@ -45,7 +45,7 @@ protected function matchesWithDiagnosticDescription($item, Description $mismatch return true; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('an instance of ') ->appendText($this->_theClass) @@ -60,7 +60,7 @@ public function describeTo(Description $description) * * @factory any */ - public static function anInstanceOf($theClass) + public static function anInstanceOf(string $theClass): self { return new self($theClass); } diff --git a/hamcrest/Hamcrest/Core/IsNot.php b/hamcrest/Hamcrest/Core/IsNot.php index 167f0d063..1fb750f51 100644 --- a/hamcrest/Hamcrest/Core/IsNot.php +++ b/hamcrest/Hamcrest/Core/IsNot.php @@ -15,19 +15,19 @@ class IsNot extends BaseMatcher { - private $_matcher; + private Matcher $_matcher; public function __construct(Matcher $matcher) { $this->_matcher = $matcher; } - public function matches($arg) + public function matches($arg): bool { return !$this->_matcher->matches($arg); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('not ')->appendDescriptionOf($this->_matcher); } @@ -36,8 +36,9 @@ public function describeTo(Description $description) * Matches if value does not match $value. * * @factory + * @param mixed $value */ - public static function not($value) + public static function not($value): self { return new self(Util::wrapValueWithIsEqual($value)); } diff --git a/hamcrest/Hamcrest/Core/IsNull.php b/hamcrest/Hamcrest/Core/IsNull.php index 91a454c17..26f79025c 100644 --- a/hamcrest/Hamcrest/Core/IsNull.php +++ b/hamcrest/Hamcrest/Core/IsNull.php @@ -13,15 +13,15 @@ class IsNull extends BaseMatcher { - private static $_INSTANCE; - private static $_NOT_INSTANCE; + private static ?self $_INSTANCE = null; + private static ?IsNot $_NOT_INSTANCE = null; - public function matches($item) + public function matches($item): bool { return is_null($item); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('null'); } @@ -31,7 +31,7 @@ public function describeTo(Description $description) * * @factory */ - public static function nullValue() + public static function nullValue(): self { if (!self::$_INSTANCE) { self::$_INSTANCE = new self(); @@ -45,7 +45,7 @@ public static function nullValue() * * @factory */ - public static function notNullValue() + public static function notNullValue(): IsNot { if (!self::$_NOT_INSTANCE) { self::$_NOT_INSTANCE = IsNot::not(self::nullValue()); diff --git a/hamcrest/Hamcrest/Core/IsSame.php b/hamcrest/Hamcrest/Core/IsSame.php index 810787050..f7e4c32a3 100644 --- a/hamcrest/Hamcrest/Core/IsSame.php +++ b/hamcrest/Hamcrest/Core/IsSame.php @@ -14,19 +14,25 @@ class IsSame extends BaseMatcher { + /** + * @var mixed object + */ private $_object; + /** + * @param mixed $object + */ public function __construct($object) { $this->_object = $object; } - public function matches($object) + public function matches($object): bool { return ($object === $this->_object) && ($this->_object === $object); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('sameInstance(') ->appendValue($this->_object) @@ -44,7 +50,7 @@ public function describeTo(Description $description) * @return \Hamcrest\Core\IsSame * @factory */ - public static function sameInstance($object) + public static function sameInstance($object): self { return new self($object); } diff --git a/hamcrest/Hamcrest/Core/IsTypeOf.php b/hamcrest/Hamcrest/Core/IsTypeOf.php index d24f0f94c..9fa874ff0 100644 --- a/hamcrest/Hamcrest/Core/IsTypeOf.php +++ b/hamcrest/Hamcrest/Core/IsTypeOf.php @@ -13,7 +13,7 @@ class IsTypeOf extends BaseMatcher { - private $_theType; + private string $_theType; /** * Creates a new instance of IsTypeOf @@ -21,22 +21,22 @@ class IsTypeOf extends BaseMatcher * @param string $theType * The predicate evaluates to true for values with this built-in type. */ - public function __construct($theType) + public function __construct(string $theType) { $this->_theType = strtolower($theType); } - public function matches($item) + public function matches($item): bool { return strtolower(gettype($item)) == $this->_theType; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText(self::getTypeDescription($this->_theType)); } - public function describeMismatch($item, Description $description) + public function describeMismatch($item, Description $description): void { if ($item === null) { $description->appendText('was null'); @@ -49,7 +49,7 @@ public function describeMismatch($item, Description $description) } } - public static function getTypeDescription($type) + public static function getTypeDescription(string $type): string { if ($type == 'null') { return 'null'; @@ -63,8 +63,9 @@ public static function getTypeDescription($type) * Is the value a particular built-in type? * * @factory + * @param string $theType */ - public static function typeOf($theType) + public static function typeOf(string $theType): self { return new self($theType); } diff --git a/hamcrest/Hamcrest/Core/Set.php b/hamcrest/Hamcrest/Core/Set.php index cdc45d538..8da692b3f 100644 --- a/hamcrest/Hamcrest/Core/Set.php +++ b/hamcrest/Hamcrest/Core/Set.php @@ -22,16 +22,22 @@ class Set extends BaseMatcher { + /** + * @var mixed $_property + */ private $_property; - private $_not; + private bool $_not; - public function __construct($property, $not = false) + /** + * @param mixed $property + */ + public function __construct($property, bool $not = false) { $this->_property = $property; $this->_not = $not; } - public function matches($item) + public function matches($item): bool { if ($item === null) { return false; @@ -50,12 +56,12 @@ public function matches($item) return $this->_not ? !$result : $result; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText($this->_not ? 'unset property ' : 'set property ')->appendText($this->_property); } - public function describeMismatch($item, Description $description) + public function describeMismatch($item, Description $description): void { $value = ''; if (!$this->_not) { @@ -77,8 +83,9 @@ public function describeMismatch($item, Description $description) * Matches if value (class, object, or array) has named $property. * * @factory + * @param mixed $property */ - public static function set($property) + public static function set($property): self { return new self($property); } @@ -87,8 +94,9 @@ public static function set($property) * Matches if value (class, object, or array) does not have named $property. * * @factory + * @param mixed $property */ - public static function notSet($property) + public static function notSet($property): self { return new self($property, true); } diff --git a/hamcrest/Hamcrest/Core/ShortcutCombination.php b/hamcrest/Hamcrest/Core/ShortcutCombination.php index d93db74ff..076ffd4a4 100644 --- a/hamcrest/Hamcrest/Core/ShortcutCombination.php +++ b/hamcrest/Hamcrest/Core/ShortcutCombination.php @@ -4,19 +4,22 @@ /* Copyright (c) 2009 hamcrest.org */ - use Hamcrest\BaseMatcher; use Hamcrest\Description; +use Hamcrest\Matcher; use Hamcrest\Util; abstract class ShortcutCombination extends BaseMatcher { /** - * @var array<\Hamcrest\Matcher> + * @var array */ private $_matchers; + /** + * @param array $matchers + */ public function __construct(array $matchers) { Util::checkAllAreMatchers($matchers); @@ -24,9 +27,11 @@ public function __construct(array $matchers) $this->_matchers = $matchers; } - protected function matchesWithShortcut($item, $shortcut) + /** + * @param mixed $item + */ + protected function matchesWithShortcut($item, bool $shortcut): bool { - /** @var $matcher \Hamcrest\Matcher */ foreach ($this->_matchers as $matcher) { if ($matcher->matches($item) == $shortcut) { return $shortcut; @@ -36,7 +41,7 @@ protected function matchesWithShortcut($item, $shortcut) return !$shortcut; } - public function describeToWithOperator(Description $description, $operator) + public function describeToWithOperator(Description $description, string $operator): void { $description->appendList('(', ' ' . $operator . ' ', ')', $this->_matchers); } diff --git a/hamcrest/Hamcrest/Description.php b/hamcrest/Hamcrest/Description.php index b09554b28..780a4e78f 100644 --- a/hamcrest/Hamcrest/Description.php +++ b/hamcrest/Hamcrest/Description.php @@ -21,7 +21,7 @@ interface Description * * @return static */ - public function appendText($text); + public function appendText(string $text): self; /** * Appends the description of a {@link Hamcrest\SelfDescribing} value to @@ -40,7 +40,7 @@ public function appendDescriptionOf(SelfDescribing $value); * * @return static */ - public function appendValue($value); + public function appendValue($value): self; /** * Appends a list of values to the description. @@ -48,11 +48,11 @@ public function appendValue($value); * @param string $start * @param string $separator * @param string $end - * @param array|\IteratorAggregate|\Iterator $values + * @param iterable $values * * @return static */ - public function appendValueList($start, $separator, $end, $values); + public function appendValueList(string $start, string $separator, string $end, iterable $values): self; /** * Appends a list of {@link Hamcrest\SelfDescribing} objects to the @@ -61,10 +61,9 @@ public function appendValueList($start, $separator, $end, $values); * @param string $start * @param string $separator * @param string $end - * @param array|\\IteratorAggregate|\\Iterator $values - * must be instances of {@link Hamcrest\SelfDescribing} + * @param iterable $values * * @return static */ - public function appendList($start, $separator, $end, $values); + public function appendList(string $start, string $separator, string $end, iterable $values): self; } diff --git a/hamcrest/Hamcrest/DiagnosingMatcher.php b/hamcrest/Hamcrest/DiagnosingMatcher.php index 3e45395c3..4239fffad 100644 --- a/hamcrest/Hamcrest/DiagnosingMatcher.php +++ b/hamcrest/Hamcrest/DiagnosingMatcher.php @@ -11,15 +11,18 @@ abstract class DiagnosingMatcher extends BaseMatcher { - final public function matches($item) + final public function matches($item): bool { return $this->matchesWithDiagnosticDescription($item, new NullDescription()); } - public function describeMismatch($item, Description $mismatchDescription) + public function describeMismatch($item, Description $mismatchDescription): void { $this->matchesWithDiagnosticDescription($item, $mismatchDescription); } - abstract protected function matchesWithDiagnosticDescription($item, Description $mismatchDescription); + /** + * @param mixed $item + */ + abstract protected function matchesWithDiagnosticDescription($item, Description $mismatchDescription): bool; } diff --git a/hamcrest/Hamcrest/FeatureMatcher.php b/hamcrest/Hamcrest/FeatureMatcher.php index 59f6cc734..094ef38f9 100644 --- a/hamcrest/Hamcrest/FeatureMatcher.php +++ b/hamcrest/Hamcrest/FeatureMatcher.php @@ -13,20 +13,20 @@ abstract class FeatureMatcher extends TypeSafeDiagnosingMatcher { - private $_subMatcher; - private $_featureDescription; - private $_featureName; + private Matcher $_subMatcher; + private string $_featureDescription; + private string $_featureName; /** * Constructor. * - * @param string $type - * @param string $subtype + * @param self::TYPE_* $type + * @param ?string $subtype * @param \Hamcrest\Matcher $subMatcher The matcher to apply to the feature * @param string $featureDescription Descriptive text to use in describeTo * @param string $featureName Identifying text for mismatch message */ - public function __construct($type, $subtype, Matcher $subMatcher, $featureDescription, $featureName) + public function __construct(int $type, ?string $subtype, Matcher $subMatcher, string $featureDescription, string $featureName) { parent::__construct($type, $subtype); @@ -44,7 +44,7 @@ public function __construct($type, $subtype, Matcher $subMatcher, $featureDescri */ abstract protected function featureValueOf($actual); - public function matchesSafelyWithDiagnosticDescription($actual, Description $mismatchDescription) + public function matchesSafelyWithDiagnosticDescription($actual, Description $mismatchDescription): bool { $featureValue = $this->featureValueOf($actual); @@ -58,7 +58,7 @@ public function matchesSafelyWithDiagnosticDescription($actual, Description $mis return true; } - final public function describeTo(Description $description) + final public function describeTo(Description $description): void { $description->appendText($this->_featureDescription)->appendText(' ') ->appendDescriptionOf($this->_subMatcher) diff --git a/hamcrest/Hamcrest/Internal/SelfDescribingValue.php b/hamcrest/Hamcrest/Internal/SelfDescribingValue.php index 995da71de..cc362cdbe 100644 --- a/hamcrest/Hamcrest/Internal/SelfDescribingValue.php +++ b/hamcrest/Hamcrest/Internal/SelfDescribingValue.php @@ -12,15 +12,20 @@ */ class SelfDescribingValue implements SelfDescribing { - + /** + * @var mixed + */ private $_value; + /** + * @param mixed $value + */ public function __construct($value) { $this->_value = $value; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendValue($this->_value); } diff --git a/hamcrest/Hamcrest/Matcher.php b/hamcrest/Hamcrest/Matcher.php index e5dcf0939..2880a5bbe 100644 --- a/hamcrest/Hamcrest/Matcher.php +++ b/hamcrest/Hamcrest/Matcher.php @@ -33,7 +33,7 @@ interface Matcher extends SelfDescribing * * @see Hamcrest\BaseMatcher */ - public function matches($item); + public function matches($item): bool; /** * Generate a description of why the matcher has not accepted the item. @@ -44,7 +44,7 @@ public function matches($item); * * @param mixed $item The item that the Matcher has rejected. * @param Description $description - * @return + * @return void */ - public function describeMismatch($item, Description $description); + public function describeMismatch($item, Description $description): void; } diff --git a/hamcrest/Hamcrest/MatcherAssert.php b/hamcrest/Hamcrest/MatcherAssert.php index d546dbee6..c450ef1aa 100644 --- a/hamcrest/Hamcrest/MatcherAssert.php +++ b/hamcrest/Hamcrest/MatcherAssert.php @@ -13,7 +13,7 @@ class MatcherAssert * * @var int */ - private static $_count = 0; + private static int $_count = 0; /** * Make an assertion and throw {@link Hamcrest\AssertionError} if it fails. @@ -35,7 +35,7 @@ class MatcherAssert * assertThat($a > $b); * */ - public static function assertThat(/* $args ... */) + public static function assertThat(/* $args ... */): void { $args = func_get_args(); switch (count($args)) { @@ -74,7 +74,7 @@ public static function assertThat(/* $args ... */) * * @return int */ - public static function getCount() + public static function getCount(): int { return self::$_count; } @@ -82,7 +82,7 @@ public static function getCount() /** * Resets the number of assertions performed to zero. */ - public static function resetCount() + public static function resetCount(): void { self::$_count = 0; } @@ -99,7 +99,7 @@ public static function resetCount() * @param \Hamcrest\Matcher $matcher applied to $actual * @throws AssertionError */ - private static function doAssert($identifier, $actual, Matcher $matcher) + private static function doAssert($identifier, $actual, Matcher $matcher): void { if (!$matcher->matches($actual)) { $description = new StringDescription(); diff --git a/hamcrest/Hamcrest/Matchers.php b/hamcrest/Hamcrest/Matchers.php index 719d2f9e5..b7e62a1cb 100644 --- a/hamcrest/Hamcrest/Matchers.php +++ b/hamcrest/Hamcrest/Matchers.php @@ -17,7 +17,7 @@ class Matchers /** * Evaluates to true only if each $matcher[$i] is satisfied by $array[$i]. */ - public static function anArray(/* args... */) + public static function anArray(/* args... */): \Hamcrest\Arrays\IsArray { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Arrays\IsArray', 'anArray'), $args); @@ -30,7 +30,7 @@ public static function anArray(/* args... */) * * @return \Hamcrest\Arrays\IsArrayContaining */ - public static function hasItemInArray($item) + public static function hasItemInArray($item): \Hamcrest\Arrays\IsArrayContaining { return \Hamcrest\Arrays\IsArrayContaining::hasItemInArray($item); } @@ -42,7 +42,7 @@ public static function hasItemInArray($item) * * @return \Hamcrest\Arrays\IsArrayContaining */ - public static function hasValue($item) + public static function hasValue($item): \Hamcrest\Arrays\IsArrayContaining { return \Hamcrest\Arrays\IsArrayContaining::hasItemInArray($item); } @@ -50,7 +50,7 @@ public static function hasValue($item) /** * An array with elements that match the given matchers. */ - public static function arrayContainingInAnyOrder(/* args... */) + public static function arrayContainingInAnyOrder(/* args... */): \Hamcrest\Arrays\IsArrayContainingInAnyOrder { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Arrays\IsArrayContainingInAnyOrder', 'arrayContainingInAnyOrder'), $args); @@ -59,7 +59,7 @@ public static function arrayContainingInAnyOrder(/* args... */) /** * An array with elements that match the given matchers. */ - public static function containsInAnyOrder(/* args... */) + public static function containsInAnyOrder(/* args... */): \Hamcrest\Arrays\IsArrayContainingInAnyOrder { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Arrays\IsArrayContainingInAnyOrder', 'arrayContainingInAnyOrder'), $args); @@ -68,7 +68,7 @@ public static function containsInAnyOrder(/* args... */) /** * An array with elements that match the given matchers in the same order. */ - public static function arrayContaining(/* args... */) + public static function arrayContaining(/* args... */): \Hamcrest\Arrays\IsArrayContainingInOrder { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Arrays\IsArrayContainingInOrder', 'arrayContaining'), $args); @@ -77,7 +77,7 @@ public static function arrayContaining(/* args... */) /** * An array with elements that match the given matchers in the same order. */ - public static function contains(/* args... */) + public static function contains(/* args... */): \Hamcrest\Arrays\IsArrayContainingInOrder { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Arrays\IsArrayContainingInOrder', 'arrayContaining'), $args); @@ -90,7 +90,7 @@ public static function contains(/* args... */) * * @return \Hamcrest\Arrays\IsArrayContainingKey */ - public static function hasKeyInArray($key) + public static function hasKeyInArray($key): \Hamcrest\Arrays\IsArrayContainingKey { return \Hamcrest\Arrays\IsArrayContainingKey::hasKeyInArray($key); } @@ -102,23 +102,29 @@ public static function hasKeyInArray($key) * * @return \Hamcrest\Arrays\IsArrayContainingKey */ - public static function hasKey($key) + public static function hasKey($key): \Hamcrest\Arrays\IsArrayContainingKey { return \Hamcrest\Arrays\IsArrayContainingKey::hasKeyInArray($key); } /** * Test if an array has both an key and value in parity with each other. + * + * @param mixed $key + * @param mixed $value */ - public static function hasKeyValuePair($key, $value) + public static function hasKeyValuePair($key, $value): \Hamcrest\Arrays\IsArrayContainingKeyValuePair { return \Hamcrest\Arrays\IsArrayContainingKeyValuePair::hasKeyValuePair($key, $value); } /** * Test if an array has both an key and value in parity with each other. + * + * @param mixed $key + * @param mixed $value */ - public static function hasEntry($key, $value) + public static function hasEntry($key, $value): \Hamcrest\Arrays\IsArrayContainingKeyValuePair { return \Hamcrest\Arrays\IsArrayContainingKeyValuePair::hasKeyValuePair($key, $value); } @@ -130,7 +136,7 @@ public static function hasEntry($key, $value) * * @return \Hamcrest\Arrays\IsArrayWithSize */ - public static function arrayWithSize($size) + public static function arrayWithSize($size): \Hamcrest\Arrays\IsArrayWithSize { return \Hamcrest\Arrays\IsArrayWithSize::arrayWithSize($size); } @@ -138,7 +144,7 @@ public static function arrayWithSize($size) /** * Matches an empty array. */ - public static function emptyArray() + public static function emptyArray(): \Hamcrest\Core\DescribedAs { return \Hamcrest\Arrays\IsArrayWithSize::emptyArray(); } @@ -146,7 +152,7 @@ public static function emptyArray() /** * Matches an empty array. */ - public static function nonEmptyArray() + public static function nonEmptyArray(): \Hamcrest\Core\DescribedAs { return \Hamcrest\Arrays\IsArrayWithSize::nonEmptyArray(); } @@ -154,7 +160,7 @@ public static function nonEmptyArray() /** * Returns true if traversable is empty. */ - public static function emptyTraversable() + public static function emptyTraversable(): \Hamcrest\Collection\IsEmptyTraversable { return \Hamcrest\Collection\IsEmptyTraversable::emptyTraversable(); } @@ -162,15 +168,17 @@ public static function emptyTraversable() /** * Returns true if traversable is not empty. */ - public static function nonEmptyTraversable() + public static function nonEmptyTraversable(): \Hamcrest\Collection\IsEmptyTraversable { return \Hamcrest\Collection\IsEmptyTraversable::nonEmptyTraversable(); } /** * Does traversable size satisfy a given matcher? + * + * @param mixed $size */ - public static function traversableWithSize($size) + public static function traversableWithSize($size): \Hamcrest\Collection\IsTraversableWithSize { return \Hamcrest\Collection\IsTraversableWithSize::traversableWithSize($size); } @@ -178,7 +186,7 @@ public static function traversableWithSize($size) /** * Evaluates to true only if ALL of the passed in matchers evaluate to true. */ - public static function allOf(/* args... */) + public static function allOf(/* args... */): \Hamcrest\Core\AllOf { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Core\AllOf', 'allOf'), $args); @@ -187,7 +195,7 @@ public static function allOf(/* args... */) /** * Evaluates to true if ANY of the passed in matchers evaluate to true. */ - public static function anyOf(/* args... */) + public static function anyOf(/* args... */): \Hamcrest\Core\AnyOf { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Core\AnyOf', 'anyOf'), $args); @@ -196,7 +204,7 @@ public static function anyOf(/* args... */) /** * Evaluates to false if ANY of the passed in matchers evaluate to true. */ - public static function noneOf(/* args... */) + public static function noneOf(/* args... */): \Hamcrest\Core\IsNot { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Core\AnyOf', 'noneOf'), $args); @@ -209,7 +217,7 @@ public static function noneOf(/* args... */) * assertThat($string, both(containsString("a"))->andAlso(containsString("b"))); * */ - public static function both(\Hamcrest\Matcher $matcher) + public static function both(\Hamcrest\Matcher $matcher): \Hamcrest\Core\CombinableMatcher { return \Hamcrest\Core\CombinableMatcher::both($matcher); } @@ -221,7 +229,7 @@ public static function both(\Hamcrest\Matcher $matcher) * assertThat($string, either(containsString("a"))->orElse(containsString("b"))); * */ - public static function either(\Hamcrest\Matcher $matcher) + public static function either(\Hamcrest\Matcher $matcher): \Hamcrest\Core\CombinableMatcher { return \Hamcrest\Core\CombinableMatcher::either($matcher); } @@ -229,20 +237,20 @@ public static function either(\Hamcrest\Matcher $matcher) /** * Wraps an existing matcher and overrides the description when it fails. */ - public static function describedAs(/* args... */) + public static function describedAs(/* args... */): \Hamcrest\Core\DescribedAs { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Core\DescribedAs', 'describedAs'), $args); } /** - * @param Matcher $itemMatcher + * @param \Hamcrest\Matcher $itemMatcher * A matcher to apply to every element in an array. * * @return \Hamcrest\Core\Every * Evaluates to TRUE for a collection in which every item matches $itemMatcher */ - public static function everyItem(\Hamcrest\Matcher $itemMatcher) + public static function everyItem(\Hamcrest\Matcher $itemMatcher): \Hamcrest\Core\Every { return \Hamcrest\Core\Every::everyItem($itemMatcher); } @@ -250,8 +258,10 @@ public static function everyItem(\Hamcrest\Matcher $itemMatcher) /** * Creates a matcher that matches any examined object whose toString or * __toString() method returns a value equalTo the specified string. + * + * @param mixed $matcher */ - public static function hasToString($matcher) + public static function hasToString($matcher): \Hamcrest\Core\HasToString { return \Hamcrest\Core\HasToString::hasToString($matcher); } @@ -262,8 +272,10 @@ public static function hasToString($matcher) * * For example: assertThat($cheese, equalTo($smelly)) * vs. assertThat($cheese, is(equalTo($smelly))) + * + * @param mixed $value */ - public static function is($value) + public static function is($value): \Hamcrest\Core\Is { return \Hamcrest\Core\Is::is($value); } @@ -272,10 +284,8 @@ public static function is($value) * This matcher always evaluates to true. * * @param string $description A meaningful string used when describing itself. - * - * @return \Hamcrest\Core\IsAnything */ - public static function anything($description = 'ANYTHING') + public static function anything(string $description = 'ANYTHING'): \Hamcrest\Core\IsAnything { return \Hamcrest\Core\IsAnything::anything($description); } @@ -290,7 +300,7 @@ public static function anything($description = 'ANYTHING') * assertThat(array('a', 'b'), hasItem('b')); * */ - public static function hasItem(/* args... */) + public static function hasItem(/* args... */): \Hamcrest\Core\IsCollectionContaining { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Core\IsCollectionContaining', 'hasItem'), $args); @@ -305,7 +315,7 @@ public static function hasItem(/* args... */) * assertThat(array('a', 'b', 'c'), hasItems(equalTo('a'), equalTo('b'))); * */ - public static function hasItems(/* args... */) + public static function hasItems(/* args... */): \Hamcrest\Core\AllOf { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Core\IsCollectionContaining', 'hasItems'), $args); @@ -314,16 +324,20 @@ public static function hasItems(/* args... */) /** * Is the value equal to another value, as tested by the use of the "==" * comparison operator? + * + * @param mixed $item */ - public static function equalTo($item) + public static function equalTo($item): \Hamcrest\Core\IsEqual { return \Hamcrest\Core\IsEqual::equalTo($item); } /** * Tests of the value is identical to $value as tested by the "===" operator. + * + * @param mixed $value */ - public static function identicalTo($value) + public static function identicalTo($value): \Hamcrest\Core\IsIdentical { return \Hamcrest\Core\IsIdentical::identicalTo($value); } @@ -334,7 +348,7 @@ public static function identicalTo($value) * the signature of the method that sets it up, for example in * assertThat($anObject, anInstanceOf('Thing')); */ - public static function anInstanceOf($theClass) + public static function anInstanceOf(string $theClass): \Hamcrest\Core\IsInstanceOf { return \Hamcrest\Core\IsInstanceOf::anInstanceOf($theClass); } @@ -345,15 +359,17 @@ public static function anInstanceOf($theClass) * the signature of the method that sets it up, for example in * assertThat($anObject, anInstanceOf('Thing')); */ - public static function any($theClass) + public static function any(string $theClass): \Hamcrest\Core\IsInstanceOf { return \Hamcrest\Core\IsInstanceOf::anInstanceOf($theClass); } /** * Matches if value does not match $value. + * + * @param mixed $value */ - public static function not($value) + public static function not($value): \Hamcrest\Core\IsNot { return \Hamcrest\Core\IsNot::not($value); } @@ -361,7 +377,7 @@ public static function not($value) /** * Matches if value is null. */ - public static function nullValue() + public static function nullValue(): \Hamcrest\Core\IsNull { return \Hamcrest\Core\IsNull::nullValue(); } @@ -369,7 +385,7 @@ public static function nullValue() /** * Matches if value is not null. */ - public static function notNullValue() + public static function notNullValue(): \Hamcrest\Core\IsNot { return \Hamcrest\Core\IsNull::notNullValue(); } @@ -383,31 +399,37 @@ public static function notNullValue() * * @return \Hamcrest\Core\IsSame */ - public static function sameInstance($object) + public static function sameInstance($object): \Hamcrest\Core\IsSame { return \Hamcrest\Core\IsSame::sameInstance($object); } /** * Is the value a particular built-in type? + * + * @param string $theType */ - public static function typeOf($theType) + public static function typeOf(string $theType): \Hamcrest\Core\IsTypeOf { return \Hamcrest\Core\IsTypeOf::typeOf($theType); } /** * Matches if value (class, object, or array) has named $property. + * + * @param mixed $property */ - public static function set($property) + public static function set($property): \Hamcrest\Core\Set { return \Hamcrest\Core\Set::set($property); } /** * Matches if value (class, object, or array) does not have named $property. + * + * @param mixed $property */ - public static function notSet($property) + public static function notSet($property): \Hamcrest\Core\Set { return \Hamcrest\Core\Set::notSet($property); } @@ -415,64 +437,81 @@ public static function notSet($property) /** * Matches if value is a number equal to $value within some range of * acceptable error $delta. + * + * @param mixed $value + * @param mixed $delta */ - public static function closeTo($value, $delta) + public static function closeTo($value, $delta): \Hamcrest\Number\IsCloseTo { return \Hamcrest\Number\IsCloseTo::closeTo($value, $delta); } /** * The value is not > $value, nor < $value. + * + * @param mixed $value */ - public static function comparesEqualTo($value) + public static function comparesEqualTo($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::comparesEqualTo($value); } /** * The value is > $value. + * + * @param mixed $value */ - public static function greaterThan($value) + public static function greaterThan($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::greaterThan($value); } /** * The value is >= $value. + * + * @param mixed $value */ - public static function greaterThanOrEqualTo($value) + public static function greaterThanOrEqualTo($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::greaterThanOrEqualTo($value); } /** * The value is >= $value. + * + * @param mixed $value */ - public static function atLeast($value) + public static function atLeast($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::greaterThanOrEqualTo($value); } /** * The value is < $value. + * + * @param mixed $value */ - public static function lessThan($value) + public static function lessThan($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::lessThan($value); } /** * The value is <= $value. + * + * @param mixed $value */ - public static function lessThanOrEqualTo($value) + public static function lessThanOrEqualTo($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::lessThanOrEqualTo($value); } /** * The value is <= $value. + * + * @param mixed $value */ - public static function atMost($value) + public static function atMost($value): \Hamcrest\Number\OrderingComparison { return \Hamcrest\Number\OrderingComparison::lessThanOrEqualTo($value); } @@ -480,7 +519,7 @@ public static function atMost($value) /** * Matches if value is a zero-length string. */ - public static function isEmptyString() + public static function isEmptyString(): \Hamcrest\Text\IsEmptyString { return \Hamcrest\Text\IsEmptyString::isEmptyString(); } @@ -488,7 +527,7 @@ public static function isEmptyString() /** * Matches if value is a zero-length string. */ - public static function emptyString() + public static function emptyString(): \Hamcrest\Text\IsEmptyString { return \Hamcrest\Text\IsEmptyString::isEmptyString(); } @@ -496,7 +535,7 @@ public static function emptyString() /** * Matches if value is null or a zero-length string. */ - public static function isEmptyOrNullString() + public static function isEmptyOrNullString(): \Hamcrest\Core\AnyOf { return \Hamcrest\Text\IsEmptyString::isEmptyOrNullString(); } @@ -504,7 +543,7 @@ public static function isEmptyOrNullString() /** * Matches if value is null or a zero-length string. */ - public static function nullOrEmptyString() + public static function nullOrEmptyString(): \Hamcrest\Core\AnyOf { return \Hamcrest\Text\IsEmptyString::isEmptyOrNullString(); } @@ -512,7 +551,7 @@ public static function nullOrEmptyString() /** * Matches if value is a non-zero-length string. */ - public static function isNonEmptyString() + public static function isNonEmptyString(): \Hamcrest\Text\IsEmptyString { return \Hamcrest\Text\IsEmptyString::isNonEmptyString(); } @@ -520,47 +559,57 @@ public static function isNonEmptyString() /** * Matches if value is a non-zero-length string. */ - public static function nonEmptyString() + public static function nonEmptyString(): \Hamcrest\Text\IsEmptyString { return \Hamcrest\Text\IsEmptyString::isNonEmptyString(); } /** * Matches if value is a string equal to $string, regardless of the case. + * + * @param mixed $string */ - public static function equalToIgnoringCase($string) + public static function equalToIgnoringCase($string): \Hamcrest\Text\IsEqualIgnoringCase { return \Hamcrest\Text\IsEqualIgnoringCase::equalToIgnoringCase($string); } /** * Matches if value is a string equal to $string, regardless of whitespace. + * + * @param mixed $string */ - public static function equalToIgnoringWhiteSpace($string) + public static function equalToIgnoringWhiteSpace($string): \Hamcrest\Text\IsEqualIgnoringWhiteSpace { return \Hamcrest\Text\IsEqualIgnoringWhiteSpace::equalToIgnoringWhiteSpace($string); } /** * Matches if value is a string that matches regular expression $pattern. + * + * @param mixed $pattern */ - public static function matchesPattern($pattern) + public static function matchesPattern($pattern): \Hamcrest\Text\MatchesPattern { return \Hamcrest\Text\MatchesPattern::matchesPattern($pattern); } /** * Matches if value is a string that contains $substring. + * + * @param mixed $substring */ - public static function containsString($substring) + public static function containsString($substring): \Hamcrest\Text\StringContains { return \Hamcrest\Text\StringContains::containsString($substring); } /** * Matches if value is a string that contains $substring regardless of the case. + * + * @param mixed $substring */ - public static function containsStringIgnoringCase($substring) + public static function containsStringIgnoringCase($substring): \Hamcrest\Text\StringContainsIgnoringCase { return \Hamcrest\Text\StringContainsIgnoringCase::containsStringIgnoringCase($substring); } @@ -568,7 +617,7 @@ public static function containsStringIgnoringCase($substring) /** * Matches if value contains $substrings in a constrained order. */ - public static function stringContainsInOrder(/* args... */) + public static function stringContainsInOrder(/* args... */): \Hamcrest\Text\StringContainsInOrder { $args = func_get_args(); return call_user_func_array(array('\Hamcrest\Text\StringContainsInOrder', 'stringContainsInOrder'), $args); @@ -576,16 +625,20 @@ public static function stringContainsInOrder(/* args... */) /** * Matches if value is a string that ends with $substring. + * + * @param mixed $substring */ - public static function endsWith($substring) + public static function endsWith($substring): \Hamcrest\Text\StringEndsWith { return \Hamcrest\Text\StringEndsWith::endsWith($substring); } /** * Matches if value is a string that starts with $substring. + * + * @param mixed $substring */ - public static function startsWith($substring) + public static function startsWith($substring): \Hamcrest\Text\StringStartsWith { return \Hamcrest\Text\StringStartsWith::startsWith($substring); } @@ -593,7 +646,7 @@ public static function startsWith($substring) /** * Is the value an array? */ - public static function arrayValue() + public static function arrayValue(): \Hamcrest\Type\IsArray { return \Hamcrest\Type\IsArray::arrayValue(); } @@ -601,7 +654,7 @@ public static function arrayValue() /** * Is the value a boolean? */ - public static function booleanValue() + public static function booleanValue(): \Hamcrest\Type\IsBoolean { return \Hamcrest\Type\IsBoolean::booleanValue(); } @@ -609,7 +662,7 @@ public static function booleanValue() /** * Is the value a boolean? */ - public static function boolValue() + public static function boolValue(): \Hamcrest\Type\IsBoolean { return \Hamcrest\Type\IsBoolean::booleanValue(); } @@ -617,7 +670,7 @@ public static function boolValue() /** * Is the value callable? */ - public static function callableValue() + public static function callableValue(): \Hamcrest\Type\IsCallable { return \Hamcrest\Type\IsCallable::callableValue(); } @@ -625,7 +678,7 @@ public static function callableValue() /** * Is the value a float/double? */ - public static function doubleValue() + public static function doubleValue(): \Hamcrest\Type\IsDouble { return \Hamcrest\Type\IsDouble::doubleValue(); } @@ -633,7 +686,7 @@ public static function doubleValue() /** * Is the value a float/double? */ - public static function floatValue() + public static function floatValue(): \Hamcrest\Type\IsDouble { return \Hamcrest\Type\IsDouble::doubleValue(); } @@ -641,7 +694,7 @@ public static function floatValue() /** * Is the value an integer? */ - public static function integerValue() + public static function integerValue(): \Hamcrest\Type\IsInteger { return \Hamcrest\Type\IsInteger::integerValue(); } @@ -649,7 +702,7 @@ public static function integerValue() /** * Is the value an integer? */ - public static function intValue() + public static function intValue(): \Hamcrest\Type\IsInteger { return \Hamcrest\Type\IsInteger::integerValue(); } @@ -657,7 +710,7 @@ public static function intValue() /** * Is the value a numeric? */ - public static function numericValue() + public static function numericValue(): \Hamcrest\Type\IsNumeric { return \Hamcrest\Type\IsNumeric::numericValue(); } @@ -665,7 +718,7 @@ public static function numericValue() /** * Is the value an object? */ - public static function objectValue() + public static function objectValue(): \Hamcrest\Type\IsObject { return \Hamcrest\Type\IsObject::objectValue(); } @@ -673,7 +726,7 @@ public static function objectValue() /** * Is the value an object? */ - public static function anObject() + public static function anObject(): \Hamcrest\Type\IsObject { return \Hamcrest\Type\IsObject::objectValue(); } @@ -681,7 +734,7 @@ public static function anObject() /** * Is the value a resource? */ - public static function resourceValue() + public static function resourceValue(): \Hamcrest\Type\IsResource { return \Hamcrest\Type\IsResource::resourceValue(); } @@ -689,7 +742,7 @@ public static function resourceValue() /** * Is the value a scalar (boolean, integer, double, or string)? */ - public static function scalarValue() + public static function scalarValue(): \Hamcrest\Type\IsScalar { return \Hamcrest\Type\IsScalar::scalarValue(); } @@ -697,7 +750,7 @@ public static function scalarValue() /** * Is the value a string? */ - public static function stringValue() + public static function stringValue(): \Hamcrest\Type\IsString { return \Hamcrest\Type\IsString::stringValue(); } @@ -706,8 +759,11 @@ public static function stringValue() * Wraps $matcher with {@link Hamcrest\Core\IsEqual) * if it's not a matcher and the XPath in count() * if it's an integer. + * + * @param string $xpath + * @param null|Matcher|int|mixed $matcher */ - public static function hasXPath($xpath, $matcher = null) + public static function hasXPath(string $xpath, $matcher = null): \Hamcrest\Xml\HasXPath { return \Hamcrest\Xml\HasXPath::hasXPath($xpath, $matcher); } diff --git a/hamcrest/Hamcrest/NullDescription.php b/hamcrest/Hamcrest/NullDescription.php index aae8e4616..1d86da479 100644 --- a/hamcrest/Hamcrest/NullDescription.php +++ b/hamcrest/Hamcrest/NullDescription.php @@ -11,27 +11,27 @@ class NullDescription implements Description { - public function appendText($text) + public function appendText(string $text): self { return $this; } - public function appendDescriptionOf(SelfDescribing $value) + public function appendDescriptionOf(SelfDescribing $value): self { return $this; } - public function appendValue($value) + public function appendValue($value): self { return $this; } - public function appendValueList($start, $separator, $end, $values) + public function appendValueList(string $start, string $separator, string $end, $values): self { return $this; } - public function appendList($start, $separator, $end, $values) + public function appendList(string $start, string $separator, string $end, $values): self { return $this; } diff --git a/hamcrest/Hamcrest/Number/IsCloseTo.php b/hamcrest/Hamcrest/Number/IsCloseTo.php index 15453e526..748419df3 100644 --- a/hamcrest/Hamcrest/Number/IsCloseTo.php +++ b/hamcrest/Hamcrest/Number/IsCloseTo.php @@ -14,9 +14,19 @@ class IsCloseTo extends TypeSafeMatcher { + /** + * @var mixed + */ private $_value; + /** + * @var mixed + */ private $_delta; + /** + * @param mixed $value + * @param mixed $delta + */ public function __construct($value, $delta) { parent::__construct(self::TYPE_NUMERIC); @@ -25,12 +35,12 @@ public function __construct($value, $delta) $this->_delta = $delta; } - protected function matchesSafely($item) + protected function matchesSafely($item): bool { return $this->_actualDelta($item) <= 0.0; } - protected function describeMismatchSafely($item, Description $mismatchDescription) + protected function describeMismatchSafely($item, Description $mismatchDescription): void { $mismatchDescription->appendValue($item) ->appendText(' differed by ') @@ -38,7 +48,7 @@ protected function describeMismatchSafely($item, Description $mismatchDescriptio ; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('a numeric value within ') ->appendValue($this->_delta) @@ -52,14 +62,20 @@ public function describeTo(Description $description) * acceptable error $delta. * * @factory + * @param mixed $value + * @param mixed $delta */ - public static function closeTo($value, $delta) + public static function closeTo($value, $delta): self { return new self($value, $delta); } // -- Private Methods + /** + * @param mixed $item + * @return int|float + */ private function _actualDelta($item) { return (abs(($item - $this->_value)) - $this->_delta); diff --git a/hamcrest/Hamcrest/Number/OrderingComparison.php b/hamcrest/Hamcrest/Number/OrderingComparison.php index 369d0cfa5..1eb1713d2 100644 --- a/hamcrest/Hamcrest/Number/OrderingComparison.php +++ b/hamcrest/Hamcrest/Number/OrderingComparison.php @@ -11,10 +11,24 @@ class OrderingComparison extends TypeSafeMatcher { + /** + * @var mixed + */ private $_value; + /** + * @var mixed + */ private $_minCompare; + /** + * @var mixed + */ private $_maxCompare; + /** + * @param mixed $value + * @param mixed $maxCompare + * @param mixed $minCompare + */ public function __construct($value, $minCompare, $maxCompare) { parent::__construct(self::TYPE_NUMERIC); @@ -24,14 +38,14 @@ public function __construct($value, $minCompare, $maxCompare) $this->_maxCompare = $maxCompare; } - protected function matchesSafely($other) + protected function matchesSafely($other): bool { $compare = $this->_compare($this->_value, $other); return ($this->_minCompare <= $compare) && ($compare <= $this->_maxCompare); } - protected function describeMismatchSafely($item, Description $mismatchDescription) + protected function describeMismatchSafely($item, Description $mismatchDescription): void { $mismatchDescription ->appendValue($item)->appendText(' was ') @@ -40,7 +54,7 @@ protected function describeMismatchSafely($item, Description $mismatchDescriptio ; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('a value ') ->appendText($this->_comparison($this->_minCompare)) @@ -57,8 +71,9 @@ public function describeTo(Description $description) * The value is not > $value, nor < $value. * * @factory + * @param mixed $value */ - public static function comparesEqualTo($value) + public static function comparesEqualTo($value): self { return new self($value, 0, 0); } @@ -67,8 +82,9 @@ public static function comparesEqualTo($value) * The value is > $value. * * @factory + * @param mixed $value */ - public static function greaterThan($value) + public static function greaterThan($value): self { return new self($value, -1, -1); } @@ -77,8 +93,9 @@ public static function greaterThan($value) * The value is >= $value. * * @factory atLeast + * @param mixed $value */ - public static function greaterThanOrEqualTo($value) + public static function greaterThanOrEqualTo($value): self { return new self($value, -1, 0); } @@ -87,8 +104,9 @@ public static function greaterThanOrEqualTo($value) * The value is < $value. * * @factory + * @param mixed $value */ - public static function lessThan($value) + public static function lessThan($value): self { return new self($value, 1, 1); } @@ -97,15 +115,20 @@ public static function lessThan($value) * The value is <= $value. * * @factory atMost + * @param mixed $value */ - public static function lessThanOrEqualTo($value) + public static function lessThanOrEqualTo($value): self { return new self($value, 0, 1); } // -- Private Methods - private function _compare($left, $right) + /** + * @param mixed $left + * @param mixed $right + */ + private function _compare($left, $right): int { $a = $left; $b = $right; @@ -119,7 +142,10 @@ private function _compare($left, $right) } } - private function _comparison($compare) + /** + * @param mixed $compare + */ + private function _comparison($compare): string { if ($compare > 0) { return 'less than'; diff --git a/hamcrest/Hamcrest/SelfDescribing.php b/hamcrest/Hamcrest/SelfDescribing.php index 872fdf9c5..6712a02ef 100644 --- a/hamcrest/Hamcrest/SelfDescribing.php +++ b/hamcrest/Hamcrest/SelfDescribing.php @@ -19,5 +19,5 @@ interface SelfDescribing * @param \Hamcrest\Description $description * The description to be built or appended to. */ - public function describeTo(Description $description); + public function describeTo(Description $description): void; } diff --git a/hamcrest/Hamcrest/StringDescription.php b/hamcrest/Hamcrest/StringDescription.php index 4b36fa2d1..766501252 100644 --- a/hamcrest/Hamcrest/StringDescription.php +++ b/hamcrest/Hamcrest/StringDescription.php @@ -11,8 +11,11 @@ class StringDescription extends BaseDescription { - private $_out; + private string $_out; + /** + * @param mixed $out + */ public function __construct($out = '') { $this->_out = (string) $out; @@ -33,7 +36,7 @@ public function __toString() * @return string * The description of the object. */ - public static function toString(SelfDescribing $selfDescribing) + public static function toString(SelfDescribing $selfDescribing): string { $self = new self(); @@ -43,14 +46,14 @@ public static function toString(SelfDescribing $selfDescribing) /** * Alias for {@link toString()}. */ - public static function asString(SelfDescribing $selfDescribing) + public static function asString(SelfDescribing $selfDescribing): string { return self::toString($selfDescribing); } // -- Protected Methods - protected function append($str) + protected function append($str): void { $this->_out .= $str; } diff --git a/hamcrest/Hamcrest/Text/IsEmptyString.php b/hamcrest/Hamcrest/Text/IsEmptyString.php index 2ae61b96c..947a2c6de 100644 --- a/hamcrest/Hamcrest/Text/IsEmptyString.php +++ b/hamcrest/Hamcrest/Text/IsEmptyString.php @@ -15,25 +15,25 @@ class IsEmptyString extends BaseMatcher { - private static $_INSTANCE; - private static $_NULL_OR_EMPTY_INSTANCE; - private static $_NOT_INSTANCE; + private static ?self $_INSTANCE = null; + private static ?AnyOf $_NULL_OR_EMPTY_INSTANCE = null; + private static ?self $_NOT_INSTANCE = null; - private $_empty; + private bool $_empty; - public function __construct($empty = true) + public function __construct(bool $empty = true) { $this->_empty = $empty; } - public function matches($item) + public function matches($item): bool { return $this->_empty ? ($item === '') : is_string($item) && $item !== ''; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText($this->_empty ? 'an empty string' : 'a non-empty string'); } @@ -43,7 +43,7 @@ public function describeTo(Description $description) * * @factory emptyString */ - public static function isEmptyString() + public static function isEmptyString(): self { if (!self::$_INSTANCE) { self::$_INSTANCE = new self(true); @@ -57,7 +57,7 @@ public static function isEmptyString() * * @factory nullOrEmptyString */ - public static function isEmptyOrNullString() + public static function isEmptyOrNullString(): AnyOf { if (!self::$_NULL_OR_EMPTY_INSTANCE) { self::$_NULL_OR_EMPTY_INSTANCE = AnyOf::anyOf( @@ -74,7 +74,7 @@ public static function isEmptyOrNullString() * * @factory nonEmptyString */ - public static function isNonEmptyString() + public static function isNonEmptyString(): self { if (!self::$_NOT_INSTANCE) { self::$_NOT_INSTANCE = new self(false); diff --git a/hamcrest/Hamcrest/Text/IsEqualIgnoringCase.php b/hamcrest/Hamcrest/Text/IsEqualIgnoringCase.php index 3836a8c37..027923f8d 100644 --- a/hamcrest/Hamcrest/Text/IsEqualIgnoringCase.php +++ b/hamcrest/Hamcrest/Text/IsEqualIgnoringCase.php @@ -12,9 +12,14 @@ */ class IsEqualIgnoringCase extends TypeSafeMatcher { - + /** + * @var mixed + */ private $_string; + /** + * @param mixed $string + */ public function __construct($string) { parent::__construct(self::TYPE_STRING); @@ -22,17 +27,17 @@ public function __construct($string) $this->_string = $string; } - protected function matchesSafely($item) + protected function matchesSafely($item): bool { return strtolower($this->_string) === strtolower($item); } - protected function describeMismatchSafely($item, Description $mismatchDescription) + protected function describeMismatchSafely($item, Description $mismatchDescription): void { $mismatchDescription->appendText('was ')->appendText($item); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('equalToIgnoringCase(') ->appendValue($this->_string) @@ -44,8 +49,9 @@ public function describeTo(Description $description) * Matches if value is a string equal to $string, regardless of the case. * * @factory + * @param mixed $string */ - public static function equalToIgnoringCase($string) + public static function equalToIgnoringCase($string): self { return new self($string); } diff --git a/hamcrest/Hamcrest/Text/IsEqualIgnoringWhiteSpace.php b/hamcrest/Hamcrest/Text/IsEqualIgnoringWhiteSpace.php index 853692b03..3127552e4 100644 --- a/hamcrest/Hamcrest/Text/IsEqualIgnoringWhiteSpace.php +++ b/hamcrest/Hamcrest/Text/IsEqualIgnoringWhiteSpace.php @@ -13,9 +13,14 @@ */ class IsEqualIgnoringWhiteSpace extends TypeSafeMatcher { - + /** + * @var mixed + */ private $_string; + /** + * @param mixed $string + */ public function __construct($string) { parent::__construct(self::TYPE_STRING); @@ -23,18 +28,18 @@ public function __construct($string) $this->_string = $string; } - protected function matchesSafely($item) + protected function matchesSafely($item): bool { return (strtolower($this->_stripSpace($item)) === strtolower($this->_stripSpace($this->_string))); } - protected function describeMismatchSafely($item, Description $mismatchDescription) + protected function describeMismatchSafely($item, Description $mismatchDescription): void { $mismatchDescription->appendText('was ')->appendText($item); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('equalToIgnoringWhiteSpace(') ->appendValue($this->_string) @@ -46,15 +51,16 @@ public function describeTo(Description $description) * Matches if value is a string equal to $string, regardless of whitespace. * * @factory + * @param mixed $string */ - public static function equalToIgnoringWhiteSpace($string) + public static function equalToIgnoringWhiteSpace($string): self { return new self($string); } // -- Private Methods - private function _stripSpace($string) + private function _stripSpace(string $string): string { $parts = preg_split("/[\r\n\t ]+/", $string); foreach ($parts as $i => $part) { diff --git a/hamcrest/Hamcrest/Text/MatchesPattern.php b/hamcrest/Hamcrest/Text/MatchesPattern.php index fa0d68eea..639759408 100644 --- a/hamcrest/Hamcrest/Text/MatchesPattern.php +++ b/hamcrest/Hamcrest/Text/MatchesPattern.php @@ -10,7 +10,9 @@ */ class MatchesPattern extends SubstringMatcher { - + /** + * @param mixed $pattern + */ public function __construct($pattern) { parent::__construct($pattern); @@ -20,20 +22,21 @@ public function __construct($pattern) * Matches if value is a string that matches regular expression $pattern. * * @factory + * @param mixed $pattern */ - public static function matchesPattern($pattern) + public static function matchesPattern($pattern): self { return new self($pattern); } // -- Protected Methods - protected function evalSubstringOf($item) + protected function evalSubstringOf(string $item): bool { return preg_match($this->_substring, (string) $item) >= 1; } - protected function relationship() + protected function relationship(): string { return 'matching'; } diff --git a/hamcrest/Hamcrest/Text/StringContains.php b/hamcrest/Hamcrest/Text/StringContains.php index b92786b60..65284dbe0 100644 --- a/hamcrest/Hamcrest/Text/StringContains.php +++ b/hamcrest/Hamcrest/Text/StringContains.php @@ -10,13 +10,15 @@ */ class StringContains extends SubstringMatcher { - + /** + * @param mixed $substring + */ public function __construct($substring) { parent::__construct($substring); } - public function ignoringCase() + public function ignoringCase(): StringContainsIgnoringCase { return new StringContainsIgnoringCase($this->_substring); } @@ -25,20 +27,21 @@ public function ignoringCase() * Matches if value is a string that contains $substring. * * @factory + * @param mixed $substring */ - public static function containsString($substring) + public static function containsString($substring): self { return new self($substring); } // -- Protected Methods - protected function evalSubstringOf($item) + protected function evalSubstringOf(string $item): bool { return (false !== strpos((string) $item, $this->_substring)); } - protected function relationship() + protected function relationship(): string { return 'containing'; } diff --git a/hamcrest/Hamcrest/Text/StringContainsIgnoringCase.php b/hamcrest/Hamcrest/Text/StringContainsIgnoringCase.php index 69f37c258..53d55eecb 100644 --- a/hamcrest/Hamcrest/Text/StringContainsIgnoringCase.php +++ b/hamcrest/Hamcrest/Text/StringContainsIgnoringCase.php @@ -11,6 +11,9 @@ class StringContainsIgnoringCase extends SubstringMatcher { + /** + * @param mixed $substring + */ public function __construct($substring) { parent::__construct($substring); @@ -20,20 +23,21 @@ public function __construct($substring) * Matches if value is a string that contains $substring regardless of the case. * * @factory + * @param mixed $substring */ - public static function containsStringIgnoringCase($substring) + public static function containsStringIgnoringCase($substring): self { return new self($substring); } // -- Protected Methods - protected function evalSubstringOf($item) + protected function evalSubstringOf(string $item): bool { return (false !== stripos((string) $item, $this->_substring)); } - protected function relationship() + protected function relationship(): string { return 'containing in any case'; } diff --git a/hamcrest/Hamcrest/Text/StringContainsInOrder.php b/hamcrest/Hamcrest/Text/StringContainsInOrder.php index e75de65d2..33b3ab638 100644 --- a/hamcrest/Hamcrest/Text/StringContainsInOrder.php +++ b/hamcrest/Hamcrest/Text/StringContainsInOrder.php @@ -12,9 +12,14 @@ */ class StringContainsInOrder extends TypeSafeMatcher { + /** + * @var array + */ + private array $_substrings; - private $_substrings; - + /** + * @param array $substrings + */ public function __construct(array $substrings) { parent::__construct(self::TYPE_STRING); @@ -22,7 +27,7 @@ public function __construct(array $substrings) $this->_substrings = $substrings; } - protected function matchesSafely($item) + protected function matchesSafely($item): bool { $fromIndex = 0; @@ -35,12 +40,12 @@ protected function matchesSafely($item) return true; } - protected function describeMismatchSafely($item, Description $mismatchDescription) + protected function describeMismatchSafely($item, Description $mismatchDescription): void { $mismatchDescription->appendText('was ')->appendText($item); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('a string containing ') ->appendValueList('', ', ', '', $this->_substrings) @@ -53,7 +58,7 @@ public function describeTo(Description $description) * * @factory ... */ - public static function stringContainsInOrder(/* args... */) + public static function stringContainsInOrder(/* args... */): self { $args = func_get_args(); diff --git a/hamcrest/Hamcrest/Text/StringEndsWith.php b/hamcrest/Hamcrest/Text/StringEndsWith.php index f802ee4d1..d3468f3cf 100644 --- a/hamcrest/Hamcrest/Text/StringEndsWith.php +++ b/hamcrest/Hamcrest/Text/StringEndsWith.php @@ -11,6 +11,9 @@ class StringEndsWith extends SubstringMatcher { + /** + * @param mixed $substring + */ public function __construct($substring) { parent::__construct($substring); @@ -20,20 +23,21 @@ public function __construct($substring) * Matches if value is a string that ends with $substring. * * @factory + * @param mixed $substring */ - public static function endsWith($substring) + public static function endsWith($substring): self { return new self($substring); } // -- Protected Methods - protected function evalSubstringOf($string) + protected function evalSubstringOf(string $string): bool { return (substr($string, (-1 * strlen($this->_substring))) === $this->_substring); } - protected function relationship() + protected function relationship(): string { return 'ending with'; } diff --git a/hamcrest/Hamcrest/Text/StringStartsWith.php b/hamcrest/Hamcrest/Text/StringStartsWith.php index 79c95656a..e1c67c0ff 100644 --- a/hamcrest/Hamcrest/Text/StringStartsWith.php +++ b/hamcrest/Hamcrest/Text/StringStartsWith.php @@ -11,6 +11,9 @@ class StringStartsWith extends SubstringMatcher { + /** + * @param mixed $substring + */ public function __construct($substring) { parent::__construct($substring); @@ -20,20 +23,21 @@ public function __construct($substring) * Matches if value is a string that starts with $substring. * * @factory + * @param mixed $substring */ - public static function startsWith($substring) + public static function startsWith($substring): self { return new self($substring); } // -- Protected Methods - protected function evalSubstringOf($string) + protected function evalSubstringOf(string $string): bool { return (substr($string, 0, strlen($this->_substring)) === $this->_substring); } - protected function relationship() + protected function relationship(): string { return 'starting with'; } diff --git a/hamcrest/Hamcrest/Text/SubstringMatcher.php b/hamcrest/Hamcrest/Text/SubstringMatcher.php index e560ad627..02747736b 100644 --- a/hamcrest/Hamcrest/Text/SubstringMatcher.php +++ b/hamcrest/Hamcrest/Text/SubstringMatcher.php @@ -11,8 +11,14 @@ abstract class SubstringMatcher extends TypeSafeMatcher { + /** + * @var mixed + */ protected $_substring; + /** + * @param mixed $substring + */ public function __construct($substring) { parent::__construct(self::TYPE_STRING); @@ -20,17 +26,17 @@ public function __construct($substring) $this->_substring = $substring; } - protected function matchesSafely($item) + protected function matchesSafely($item): bool { return $this->evalSubstringOf($item); } - protected function describeMismatchSafely($item, Description $mismatchDescription) + protected function describeMismatchSafely($item, Description $mismatchDescription): void { $mismatchDescription->appendText('was "')->appendText($item)->appendText('"'); } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('a string ') ->appendText($this->relationship()) @@ -39,7 +45,7 @@ public function describeTo(Description $description) ; } - abstract protected function evalSubstringOf($string); + abstract protected function evalSubstringOf(string $string): bool; - abstract protected function relationship(); + abstract protected function relationship(): string; } diff --git a/hamcrest/Hamcrest/Type/IsArray.php b/hamcrest/Hamcrest/Type/IsArray.php index 9179102ff..19c6e2ea5 100644 --- a/hamcrest/Hamcrest/Type/IsArray.php +++ b/hamcrest/Hamcrest/Type/IsArray.php @@ -25,7 +25,7 @@ public function __construct() * * @factory */ - public static function arrayValue() + public static function arrayValue(): self { return new self; } diff --git a/hamcrest/Hamcrest/Type/IsBoolean.php b/hamcrest/Hamcrest/Type/IsBoolean.php index 35b617cf4..d9fbddd91 100644 --- a/hamcrest/Hamcrest/Type/IsBoolean.php +++ b/hamcrest/Hamcrest/Type/IsBoolean.php @@ -25,7 +25,7 @@ public function __construct() * * @factory boolValue */ - public static function booleanValue() + public static function booleanValue(): self { return new self; } diff --git a/hamcrest/Hamcrest/Type/IsCallable.php b/hamcrest/Hamcrest/Type/IsCallable.php index f2bcd35ba..ef930d7cd 100644 --- a/hamcrest/Hamcrest/Type/IsCallable.php +++ b/hamcrest/Hamcrest/Type/IsCallable.php @@ -20,7 +20,7 @@ public function __construct() parent::__construct('callable'); } - public function matches($item) + public function matches($item): bool { return is_callable($item); } @@ -30,7 +30,7 @@ public function matches($item) * * @factory */ - public static function callableValue() + public static function callableValue(): self { return new self; } diff --git a/hamcrest/Hamcrest/Type/IsDouble.php b/hamcrest/Hamcrest/Type/IsDouble.php index 3ddd8e852..0dffc8443 100644 --- a/hamcrest/Hamcrest/Type/IsDouble.php +++ b/hamcrest/Hamcrest/Type/IsDouble.php @@ -27,7 +27,7 @@ public function __construct() * * @factory floatValue */ - public static function doubleValue() + public static function doubleValue(): self { return new self; } diff --git a/hamcrest/Hamcrest/Type/IsInteger.php b/hamcrest/Hamcrest/Type/IsInteger.php index 47c86bd68..f26997ec6 100644 --- a/hamcrest/Hamcrest/Type/IsInteger.php +++ b/hamcrest/Hamcrest/Type/IsInteger.php @@ -25,7 +25,7 @@ public function __construct() * * @factory intValue */ - public static function integerValue() + public static function integerValue(): self { return new self; } diff --git a/hamcrest/Hamcrest/Type/IsNumeric.php b/hamcrest/Hamcrest/Type/IsNumeric.php index bc7440547..bf60cec24 100644 --- a/hamcrest/Hamcrest/Type/IsNumeric.php +++ b/hamcrest/Hamcrest/Type/IsNumeric.php @@ -17,7 +17,7 @@ public function __construct() parent::__construct('number'); } - public function matches($item) + public function matches($item): bool { if ($this->isHexadecimal($item)) { return true; @@ -47,7 +47,7 @@ private function isHexadecimal($item) * * @factory */ - public static function numericValue() + public static function numericValue(): self { return new self; } diff --git a/hamcrest/Hamcrest/Type/IsObject.php b/hamcrest/Hamcrest/Type/IsObject.php index 65918fcf3..4edab887d 100644 --- a/hamcrest/Hamcrest/Type/IsObject.php +++ b/hamcrest/Hamcrest/Type/IsObject.php @@ -25,7 +25,7 @@ public function __construct() * * @factory anObject */ - public static function objectValue() + public static function objectValue(): self { return new self; } diff --git a/hamcrest/Hamcrest/Type/IsResource.php b/hamcrest/Hamcrest/Type/IsResource.php index 426cf77c9..fd3250efe 100644 --- a/hamcrest/Hamcrest/Type/IsResource.php +++ b/hamcrest/Hamcrest/Type/IsResource.php @@ -25,7 +25,7 @@ public function __construct() * * @factory */ - public static function resourceValue() + public static function resourceValue(): self { return new self; } diff --git a/hamcrest/Hamcrest/Type/IsScalar.php b/hamcrest/Hamcrest/Type/IsScalar.php index 3f3b427fa..5670c5f5d 100644 --- a/hamcrest/Hamcrest/Type/IsScalar.php +++ b/hamcrest/Hamcrest/Type/IsScalar.php @@ -17,7 +17,7 @@ public function __construct() parent::__construct('scalar'); } - public function matches($item) + public function matches($item): bool { return is_scalar($item); } @@ -27,7 +27,7 @@ public function matches($item) * * @factory */ - public static function scalarValue() + public static function scalarValue(): self { return new self; } diff --git a/hamcrest/Hamcrest/Type/IsString.php b/hamcrest/Hamcrest/Type/IsString.php index d96d7db38..c29a7084b 100644 --- a/hamcrest/Hamcrest/Type/IsString.php +++ b/hamcrest/Hamcrest/Type/IsString.php @@ -25,7 +25,7 @@ public function __construct() * * @factory */ - public static function stringValue() + public static function stringValue(): self { return new self; } diff --git a/hamcrest/Hamcrest/TypeSafeDiagnosingMatcher.php b/hamcrest/Hamcrest/TypeSafeDiagnosingMatcher.php index af934e8ac..1fd8dd8b7 100644 --- a/hamcrest/Hamcrest/TypeSafeDiagnosingMatcher.php +++ b/hamcrest/Hamcrest/TypeSafeDiagnosingMatcher.php @@ -9,14 +9,14 @@ abstract class TypeSafeDiagnosingMatcher extends TypeSafeMatcher { - final public function matchesSafely($item) + final public function matchesSafely($actual): bool { - return $this->matchesSafelyWithDiagnosticDescription($item, new NullDescription()); + return $this->matchesSafelyWithDiagnosticDescription($actual, new NullDescription()); } - final public function describeMismatchSafely($item, Description $mismatchDescription) + final public function describeMismatchSafely($actual, Description $mismatchDescription): void { - $this->matchesSafelyWithDiagnosticDescription($item, $mismatchDescription); + $this->matchesSafelyWithDiagnosticDescription($actual, $mismatchDescription); } // -- Protected Methods @@ -24,6 +24,7 @@ final public function describeMismatchSafely($item, Description $mismatchDescrip /** * Subclasses should implement these. The item will already have been checked for * the specific type. + * @param mixed $actual */ - abstract protected function matchesSafelyWithDiagnosticDescription($item, Description $mismatchDescription); + abstract protected function matchesSafelyWithDiagnosticDescription($actual, Description $mismatchDescription): bool; } diff --git a/hamcrest/Hamcrest/TypeSafeMatcher.php b/hamcrest/Hamcrest/TypeSafeMatcher.php index 56e299a9a..adb5d1215 100644 --- a/hamcrest/Hamcrest/TypeSafeMatcher.php +++ b/hamcrest/Hamcrest/TypeSafeMatcher.php @@ -14,40 +14,44 @@ abstract class TypeSafeMatcher extends BaseMatcher { /* Types that PHP can compare against */ - const TYPE_ANY = 0; - const TYPE_STRING = 1; - const TYPE_NUMERIC = 2; - const TYPE_ARRAY = 3; - const TYPE_OBJECT = 4; - const TYPE_RESOURCE = 5; - const TYPE_BOOLEAN = 6; + protected const TYPE_ANY = 0; + protected const TYPE_STRING = 1; + protected const TYPE_NUMERIC = 2; + protected const TYPE_ARRAY = 3; + protected const TYPE_OBJECT = 4; + protected const TYPE_RESOURCE = 5; + protected const TYPE_BOOLEAN = 6; /** * The type that is required for a safe comparison * * @var int */ - private $_expectedType; + private int $_expectedType; /** * The subtype (e.g. class for objects) that is required * * @var string */ - private $_expectedSubtype; + private ?string $_expectedSubtype; - public function __construct($expectedType, $expectedSubtype = null) + /** + * @param self::TYPE_* $expectedType + * @param string|null $expectedSubtype + */ + public function __construct(int $expectedType, ?string $expectedSubtype = null) { $this->_expectedType = $expectedType; $this->_expectedSubtype = $expectedSubtype; } - final public function matches($item) + final public function matches($item): bool { return $this->_isSafeType($item) && $this->matchesSafely($item); } - final public function describeMismatch($item, Description $mismatchDescription) + final public function describeMismatch($item, Description $mismatchDescription): void { if (!$this->_isSafeType($item)) { parent::describeMismatch($item, $mismatchDescription); @@ -60,17 +64,22 @@ final public function describeMismatch($item, Description $mismatchDescription) /** * The item will already have been checked for the specific type and subtype. + * @param mixed $item */ - abstract protected function matchesSafely($item); + abstract protected function matchesSafely($item): bool; /** * The item will already have been checked for the specific type and subtype. + * @param mixed $item */ - abstract protected function describeMismatchSafely($item, Description $mismatchDescription); + abstract protected function describeMismatchSafely($item, Description $mismatchDescription): void; // -- Private Methods - private function _isSafeType($value) + /** + * @param mixed $value + */ + private function _isSafeType($value): bool { switch ($this->_expectedType) { diff --git a/hamcrest/Hamcrest/Util.php b/hamcrest/Hamcrest/Util.php index 169b03663..aedf1e32a 100644 --- a/hamcrest/Hamcrest/Util.php +++ b/hamcrest/Hamcrest/Util.php @@ -12,7 +12,7 @@ */ class Util { - public static function registerGlobalFunctions() + public static function registerGlobalFunctions(): void { require_once __DIR__.'/../Hamcrest.php'; } @@ -34,10 +34,10 @@ public static function wrapValueWithIsEqual($item) /** * Throws an exception if any item in $matchers is not a Hamcrest\Matcher. * - * @param array $matchers expected to contain only matchers + * @param array $matchers expected to contain only matchers * @throws \InvalidArgumentException if any item is not a matcher */ - public static function checkAllAreMatchers(array $matchers) + public static function checkAllAreMatchers(array $matchers): void { foreach ($matchers as $m) { if (!($m instanceof Matcher)) { @@ -54,8 +54,8 @@ public static function checkAllAreMatchers(array $matchers) * is an array, it is used as the $items array to support the old style * of passing an array as the sole argument to a matcher. * - * @param array $items contains items and matchers - * @return array all items are + * @param array $items contains items and matchers + * @return array all items are */ public static function createMatcherArray(array $items) { diff --git a/hamcrest/Hamcrest/Xml/HasXPath.php b/hamcrest/Hamcrest/Xml/HasXPath.php index bedf9694a..2529f7270 100644 --- a/hamcrest/Hamcrest/Xml/HasXPath.php +++ b/hamcrest/Hamcrest/Xml/HasXPath.php @@ -22,17 +22,20 @@ class HasXPath extends DiagnosingMatcher * * @var string */ - private $_xpath; + private string $_xpath; /** * Optional matcher to apply to the XPath expression result * or the content of the returned nodes. * - * @var Matcher + * @var ?Matcher */ - private $_matcher; + private ?Matcher $_matcher; - public function __construct($xpath, ?Matcher $matcher = null) + /** + * @param string $xpath + */ + public function __construct(string $xpath, ?Matcher $matcher = null) { $this->_xpath = $xpath; $this->_matcher = $matcher; @@ -45,11 +48,11 @@ public function __construct($xpath, ?Matcher $matcher = null) * @param Description $mismatchDescription * @return bool */ - protected function matchesWithDiagnosticDescription($actual, Description $mismatchDescription) + protected function matchesWithDiagnosticDescription($actual, Description $mismatchDescription): bool { if (is_string($actual)) { $actual = $this->createDocument($actual); - } elseif (!$actual instanceof \DOMNode) { + } elseif (!$actual instanceof \DOMNode) { // @phpstan-ignore instanceof.alwaysTrue (unless actual is a native union type, we want to check this) $mismatchDescription->appendText('was ')->appendValue($actual); return false; @@ -114,7 +117,7 @@ protected function evaluate(\DOMNode $node) * @param Description $mismatchDescription * @return bool */ - protected function matchesContent(\DOMNodeList $nodes, Description $mismatchDescription) + protected function matchesContent(\DOMNodeList $nodes, Description $mismatchDescription): bool { if ($nodes->length == 0) { $mismatchDescription->appendText('XPath returned no results'); @@ -145,7 +148,7 @@ protected function matchesContent(\DOMNodeList $nodes, Description $mismatchDesc * @param Description $mismatchDescription * @return bool */ - protected function matchesExpression($result, Description $mismatchDescription) + protected function matchesExpression($result, Description $mismatchDescription): bool { if ($this->_matcher === null) { if ($result) { @@ -164,7 +167,7 @@ protected function matchesExpression($result, Description $mismatchDescription) return false; } - public function describeTo(Description $description) + public function describeTo(Description $description): void { $description->appendText('XML or HTML document with XPath "') ->appendText($this->_xpath) @@ -181,8 +184,10 @@ public function describeTo(Description $description) * if it's an integer. * * @factory + * @param string $xpath + * @param null|Matcher|int|mixed $matcher */ - public static function hasXPath($xpath, $matcher = null) + public static function hasXPath(string $xpath, $matcher = null): self { if ($matcher === null || $matcher instanceof Matcher) { return new self($xpath, $matcher); diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 000000000..27b204db8 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,19 @@ +includes: + - vendor/phpstan/phpstan-phpunit/extension.neon + - tests/phpstan-baseline.neon + +parameters: + level: 8 + paths: + - hamcrest + - tests + + excludePaths: + analyse: + - tests/ + + + editorUrl: 'phpstorm://open?file=%%file%%&line=%%line%%&project=hamcrest-php' + + reportUnmatchedIgnoredErrors: false + diff --git a/tests/Hamcrest/BaseMatcherTest.php b/tests/Hamcrest/BaseMatcherTest.php index 833e2c3ec..f915f8641 100644 --- a/tests/Hamcrest/BaseMatcherTest.php +++ b/tests/Hamcrest/BaseMatcherTest.php @@ -5,12 +5,12 @@ class BaseMatcherTest extends \Hamcrest\BaseMatcher { - public function matches($item) + public function matches($item): bool { throw new \RuntimeException(); } - public function describeTo(\Hamcrest\Description $description) + public function describeTo(\Hamcrest\Description $description): void { $description->appendText('SOME DESCRIPTION'); } diff --git a/tests/Hamcrest/Core/IsInstanceOfTest.php b/tests/Hamcrest/Core/IsInstanceOfTest.php index 795b43012..5a8fb5e84 100644 --- a/tests/Hamcrest/Core/IsInstanceOfTest.php +++ b/tests/Hamcrest/Core/IsInstanceOfTest.php @@ -18,7 +18,7 @@ protected function setUpTest() $this->_subClassInstance = new \Hamcrest\Core\SampleSubClass('good'); } - protected function createMatcher() + protected function createMatcher(): IsInstanceOf { return \Hamcrest\Core\IsInstanceOf::anInstanceOf('stdClass'); } diff --git a/tests/Hamcrest/InvokedMatcherTest.php b/tests/Hamcrest/InvokedMatcherTest.php index dfa770061..6731acdf9 100644 --- a/tests/Hamcrest/InvokedMatcherTest.php +++ b/tests/Hamcrest/InvokedMatcherTest.php @@ -12,7 +12,7 @@ public function __construct($matchAgainst) $this->matchAgainst = $matchAgainst; } - public function matches($item) + public function matches($item): bool { return $item == $this->matchAgainst; } diff --git a/tests/Hamcrest/StringDescriptionTest.php b/tests/Hamcrest/StringDescriptionTest.php index 4a403e994..537ae7d61 100644 --- a/tests/Hamcrest/StringDescriptionTest.php +++ b/tests/Hamcrest/StringDescriptionTest.php @@ -12,7 +12,7 @@ public function __construct($text) $this->_text = $text; } - public function describeTo(\Hamcrest\Description $description) + public function describeTo(\Hamcrest\Description $description): void { $description->appendText($this->_text); } diff --git a/tests/phpstan-baseline.neon b/tests/phpstan-baseline.neon new file mode 100644 index 000000000..86f54f27a --- /dev/null +++ b/tests/phpstan-baseline.neon @@ -0,0 +1,43 @@ +parameters: + ignoreErrors: + - + message: '#^Cannot call method matches\(\) on Hamcrest\\Matcher\|null\.$#' + identifier: method.nonObject + count: 1 + path: ../hamcrest/Hamcrest/Arrays/SeriesMatchingOnce.php + + - + message: '#^Parameter \#1 \$matcher of method Hamcrest\\Arrays\\SeriesMatchingOnce\:\:_describeMismatch\(\) expects Hamcrest\\Matcher, Hamcrest\\Matcher\|null given\.$#' + identifier: argument.type + count: 1 + path: ../hamcrest/Hamcrest/Arrays/SeriesMatchingOnce.php + + - + message: '#^Cannot call method toString\(\) on class\-string\|object\.$#' + identifier: method.nonObject + count: 1 + path: ../hamcrest/Hamcrest/Core/HasToString.php + + - + message: '#^Argument of an invalid type list\\|false supplied for foreach, only iterables are supported\.$#' + identifier: foreach.nonIterable + count: 1 + path: ../hamcrest/Hamcrest/Text/IsEqualIgnoringWhiteSpace.php + + - + message: '#^Cannot access offset int\<0, max\> on non\-empty\-array\, string\>\|false\.$#' + identifier: offsetAccess.nonOffsetAccessible + count: 1 + path: ../hamcrest/Hamcrest/Text/IsEqualIgnoringWhiteSpace.php + + - + message: '#^Method Hamcrest\\Xml\\HasXPath\:\:matchesContent\(\) has parameter \$nodes with generic class DOMNodeList but does not specify its types\: TNode$#' + identifier: missingType.generics + count: 1 + path: ../hamcrest/Hamcrest/Xml/HasXPath.php + + - + message: '#^Parameter \#1 \$document of class DOMXPath constructor expects DOMDocument, DOMDocument\|null given\.$#' + identifier: argument.type + count: 1 + path: ../hamcrest/Hamcrest/Xml/HasXPath.php