Skip to content

Commit

Permalink
Fix login bug in HookStorage::parseArgsToAdd
Browse files Browse the repository at this point in the history
and several doc bloc fixes

Thanks @szepeviktor
  • Loading branch information
gmazzap committed Nov 24, 2019
1 parent c916d1e commit b3ce8b6
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class Container
{

/**
* @var Container
* @var Container|null
*/
private static $instance;

Expand All @@ -31,7 +31,7 @@ final class Container
/**
* Static instance lookup.
*
* @return static
* @return Container
*/
public static function instance()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Expectation/EscapeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EscapeHelper
{

/**
* @param $text
* @param string $text
* @return string
*/
public static function esc($text)
Expand Down
11 changes: 6 additions & 5 deletions src/Expectation/FunctionStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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)
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Hook/HookStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Name/CallbackStringForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __toString()
}

/**
* @param $callback
* @param mixed $callback
* @return string
*/
private function parseCallback($callback)
Expand Down Expand Up @@ -105,7 +105,7 @@ private function parseCallback($callback)

/**
* @param string $callback
* @return bool|string
* @return string
*/
private function parseString($callback)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Name/Exception/InvalidCallable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions src/Name/Exception/InvalidClosureParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
);
}

Expand Down
20 changes: 12 additions & 8 deletions src/Name/Exception/InvalidName.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -51,7 +51,7 @@ public static function forMethod($function)
}

/**
* @param string $thing
* @param mixed $thing
* @param int $code
* @return static
*/
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Name/FunctionName.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions tests/cases/unit/Hook/HookStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/unit/Name/ClosureParamStringFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down

1 comment on commit b3ce8b6

@szepeviktor
Copy link

@szepeviktor szepeviktor commented on b3ce8b6 Nov 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're welcome.

Powered by @phpstan (not me)

Please sign in to comment.