Skip to content

Commit

Permalink
Fixed issue where calling a function with an array was always conside…
Browse files Browse the repository at this point in the history
…red disallowed when using disallowParams.
  • Loading branch information
mad-briller committed Jan 20, 2023
1 parent d8b634b commit f2f6d99
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Params/DisallowedCallParamValueExcept.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Spaze\PHPStan\Rules\Disallowed\Params;

use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Type;

Expand All @@ -14,9 +15,14 @@ class DisallowedCallParamValueExcept extends DisallowedCallParamValue

public function matches(Type $type): bool
{
if ($type instanceof ConstantArrayType) {
return true;
}

if (!$type instanceof ConstantScalarType) {
return false;
}

return $this->getValue() !== $type->getValue();
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Calls/FunctionCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ protected function getRule(): Rule
1 => Blade::class,
],
],
[
'function' => '\Foo\Bar\Waldo\config()',
'disallowParams' => [
1 => 'string-key',
],
],
]
);
}
Expand Down Expand Up @@ -283,6 +289,10 @@ public function testRule(): void
'Calling mocky() is forbidden, mocking Blade is not allowed.',
83,
],
[
'Calling Foo\Bar\Waldo\config() is forbidden, because reasons',
86,
],
]);
// Based on the configuration above, no errors in this file:
$this->analyse([__DIR__ . '/../src/disallowed-allow/functionCalls.php'], [
Expand Down
9 changes: 9 additions & 0 deletions tests/libs/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,12 @@ function waldo(string $name = '', string $value = '', int $expires = 0, string $
function mocky(string $className): void
{
}

/**
* @param array|string|null $key
* @param mixed $default
* @return mixed
*/
function config($key = null, $default = null)
{
}
3 changes: 3 additions & 0 deletions tests/src/disallowed/functionCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@
// allowed when not FQCN to Blade class
mocky(\Fiction\Pulp\Royale::class);
mocky(\Waldo\Quux\Blade::class);

\Foo\Bar\Waldo\config(['key' => 'value']);
\Foo\Bar\Waldo\config('string-key');

0 comments on commit f2f6d99

Please sign in to comment.