From b3ce8b619c26db6abd01b9dcfd6a2c0254060956 Mon Sep 17 00:00:00 2001 From: Giuseppe Mazzapica Date: Sun, 24 Nov 2019 17:03:21 +0100 Subject: [PATCH] Fix login bug in HookStorage::parseArgsToAdd and several doc bloc fixes Thanks @szepeviktor --- src/Container.php | 4 ++-- src/Expectation/EscapeHelper.php | 2 +- src/Expectation/FunctionStub.php | 11 +++++----- src/Hook/HookStorage.php | 6 +++--- src/Name/CallbackStringForm.php | 4 ++-- src/Name/Exception/InvalidCallable.php | 2 +- src/Name/Exception/InvalidClosureParam.php | 12 +++++------ src/Name/Exception/InvalidName.php | 20 +++++++++++-------- src/Name/FunctionName.php | 2 +- tests/cases/unit/Hook/HookStorageTest.php | 7 +++++++ .../unit/Name/ClosureParamStringFormTest.php | 2 +- 11 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/Container.php b/src/Container.php index 246fbe2..c6fc461 100644 --- a/src/Container.php +++ b/src/Container.php @@ -19,7 +19,7 @@ final class Container { /** - * @var Container + * @var Container|null */ private static $instance; @@ -31,7 +31,7 @@ final class Container /** * Static instance lookup. * - * @return static + * @return Container */ public static function instance() { diff --git a/src/Expectation/EscapeHelper.php b/src/Expectation/EscapeHelper.php index 456d3cc..8587f15 100644 --- a/src/Expectation/EscapeHelper.php +++ b/src/Expectation/EscapeHelper.php @@ -21,7 +21,7 @@ class EscapeHelper { /** - * @param $text + * @param string $text * @return string */ public static function esc($text) diff --git a/src/Expectation/FunctionStub.php b/src/Expectation/FunctionStub.php index 6fc8330..f09b130 100644 --- a/src/Expectation/FunctionStub.php +++ b/src/Expectation/FunctionStub.php @@ -136,7 +136,7 @@ public function justEcho($value = null) */ public function returnArg($arg_num = 1) { - $arg_num = $this->assertValidArgNum($arg_num); + $arg_num = $this->assertValidArgNum($arg_num, 'returnArg'); $fqn = $this->function_name->fullyQualifiedName(); @@ -161,7 +161,7 @@ public function returnArg($arg_num = 1) */ public function echoArg($arg_num = 1) { - $arg_num = $this->assertValidArgNum($arg_num); + $arg_num = $this->assertValidArgNum($arg_num, 'echoArg'); $fqn = $this->function_name->fullyQualifiedName(); @@ -185,14 +185,15 @@ public function echoArg($arg_num = 1) } /** - * @param int $arg_num + * @param mixed $arg_num + * @param string $method * @return bool */ - private function assertValidArgNum($arg_num) + private function assertValidArgNum($arg_num, $method) { if ( ! is_int($arg_num) || $arg_num <= 0) { throw new Exception\InvalidArgumentForStub( - sprintf('`%s::returnArg()` first parameter must be a positiver integer.', __CLASS__) + sprintf('`%s::%s()` first parameter must be a positiver integer.', __CLASS__, $method) ); } diff --git a/src/Hook/HookStorage.php b/src/Hook/HookStorage.php index 1015b29..ce94a82 100644 --- a/src/Hook/HookStorage.php +++ b/src/Hook/HookStorage.php @@ -122,8 +122,8 @@ public function isHookAdded($type, $hook, $function = null) } /** - * @param $type - * @param $hook + * @param string $type + * @param string $hook * @return int */ public function isHookDone($type, $hook) @@ -211,7 +211,7 @@ private function parseArgsToAdd(array $args, $key, $type) throw Exception\InvalidHookArgument::forEmptyArguments($key, $type); } - if ( ! count($args) > 3) { + if (count($args) > 3) { throw Exception\InvalidAddedHookArgument::forWrongArgumentsCount($type); } diff --git a/src/Name/CallbackStringForm.php b/src/Name/CallbackStringForm.php index dc1f574..5f8cde4 100644 --- a/src/Name/CallbackStringForm.php +++ b/src/Name/CallbackStringForm.php @@ -57,7 +57,7 @@ public function __toString() } /** - * @param $callback + * @param mixed $callback * @return string */ private function parseCallback($callback) @@ -105,7 +105,7 @@ private function parseCallback($callback) /** * @param string $callback - * @return bool|string + * @return string */ private function parseString($callback) { diff --git a/src/Name/Exception/InvalidCallable.php b/src/Name/Exception/InvalidCallable.php index 84e7899..3155089 100644 --- a/src/Name/Exception/InvalidCallable.php +++ b/src/Name/Exception/InvalidCallable.php @@ -19,7 +19,7 @@ class InvalidCallable extends Exception { /** - * @param $callback + * @param mixed $callback * @return \Brain\Monkey\Name\Exception\InvalidCallable|\Brain\Monkey\Name\Exception\NotInvokableObjectAsCallback */ public static function forCallable($callback) diff --git a/src/Name/Exception/InvalidClosureParam.php b/src/Name/Exception/InvalidClosureParam.php index 75662fd..aeec7d4 100644 --- a/src/Name/Exception/InvalidClosureParam.php +++ b/src/Name/Exception/InvalidClosureParam.php @@ -24,7 +24,7 @@ class InvalidClosureParam extends Exception const CODE_MULTIPLE_VARIADIC = 3; /** - * @param $name + * @param string $name * @return static */ public static function forInvalidName($name) @@ -36,20 +36,20 @@ public static function forInvalidName($name) } /** - * @param $type - * @param $name + * @param string $type + * @param string $name * @return static */ public static function forInvalidType($type, $name) { return new static( sprintf('%s is not a valid function argument type for argument %s.', $type, $name), - self::CODE_INVALID_NAME + self::CODE_INVALID_TYPE ); } /** - * @param $name + * @param string $name * @return static */ public static function forMultipleVariadic($name) @@ -59,7 +59,7 @@ public static function forMultipleVariadic($name) '%s is a variadic argument for a function that already has a variadic argument.', $name ), - self::CODE_INVALID_NAME + self::CODE_MULTIPLE_VARIADIC ); } diff --git a/src/Name/Exception/InvalidName.php b/src/Name/Exception/InvalidName.php index 923681e..5955d38 100644 --- a/src/Name/Exception/InvalidName.php +++ b/src/Name/Exception/InvalidName.php @@ -33,7 +33,7 @@ public static function forFunction($function) } /** - * @param $class + * @param string $class * @return \Brain\Monkey\Name\Exception\InvalidName */ public static function forClass($class) @@ -42,7 +42,7 @@ public static function forClass($class) } /** - * @param $function + * @param string $function * @return \Brain\Monkey\Name\Exception\InvalidName */ public static function forMethod($function) @@ -51,7 +51,7 @@ public static function forMethod($function) } /** - * @param string $thing + * @param mixed $thing * @param int $code * @return static */ @@ -70,11 +70,15 @@ private static function createFor($thing, $code) break; } - $name = "'{$thing}'"; - if ( ! is_string($thing)) { - $name = is_object($thing) - ? 'An instance of '.get_class($thing) - : 'A variable of type '.gettype($thing); + switch (true) { + case is_string($thing): + $name = "'{$thing}'"; + break; + case is_object($thing): + $name = 'An instance of '.get_class($thing); + break; + default: + $name = 'A variable of type '.gettype($thing); } return new static(sprintf('%s is not a valid %s name.', $name, $type), $code); diff --git a/src/Name/FunctionName.php b/src/Name/FunctionName.php index f8dc4dc..0337a9f 100644 --- a/src/Name/FunctionName.php +++ b/src/Name/FunctionName.php @@ -76,7 +76,7 @@ public function equals(FunctionName $name) * Checks the name of a function and throw an exception if is not valid. * When name is valid returns an array of the name itself and its namespace parts. * - * @param string $function_name + * @param mixed $function_name * @return string[] */ private function parseName($function_name) diff --git a/tests/cases/unit/Hook/HookStorageTest.php b/tests/cases/unit/Hook/HookStorageTest.php index fbfc4ef..fc806c4 100644 --- a/tests/cases/unit/Hook/HookStorageTest.php +++ b/tests/cases/unit/Hook/HookStorageTest.php @@ -65,6 +65,13 @@ public function testPushToAddedThrowsIfEmptyArgs() $storage->pushToAdded(HookStorage::FILTERS, 'init', []); } + public function testPushToAddedThrowsIfTooManyArgs() + { + $storage = new HookStorage(); + $this->expectException(InvalidHookArgument::class); + $storage->pushToAdded(HookStorage::FILTERS, 'init', ['x', 1, 10, 11]); + } + public function testPushToAddedAndIsHookAdded() { $storage = new HookStorage(); diff --git a/tests/cases/unit/Name/ClosureParamStringFormTest.php b/tests/cases/unit/Name/ClosureParamStringFormTest.php index f0b8aef..58f5c4f 100644 --- a/tests/cases/unit/Name/ClosureParamStringFormTest.php +++ b/tests/cases/unit/Name/ClosureParamStringFormTest.php @@ -39,7 +39,7 @@ public function testFromStringThrowsForBadName() public function testFromStringThrowsForBadType() { $this->expectException(InvalidClosureParam::class); - $this->expectExceptionCode(InvalidClosureParam::CODE_INVALID_NAME); + $this->expectExceptionCode(InvalidClosureParam::CODE_INVALID_TYPE); ClosureParamStringForm::fromString('F-oo $foo'); }