Skip to content

Commit 5ae7a3d

Browse files
committed
StrictFunctionCallsRule - support named arguments
1 parent b21c03d commit 5ae7a3d

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
],
88
"require": {
99
"php": "^7.2 || ^8.0",
10-
"phpstan/phpstan": "^1.10"
10+
"phpstan/phpstan": "^1.10.34"
1111
},
1212
"require-dev": {
1313
"nikic/php-parser": "^4.13.0",

src/Rules/StrictCalls/StrictFunctionCallsRule.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Expr\FuncCall;
77
use PhpParser\Node\Name;
8+
use PHPStan\Analyser\ArgumentsNormalizer;
89
use PHPStan\Analyser\Scope;
10+
use PHPStan\Reflection\ParametersAcceptorSelector;
911
use PHPStan\Reflection\ReflectionProvider;
1012
use PHPStan\Rules\Rule;
1113
use PHPStan\Type\Constant\ConstantBooleanType;
@@ -47,11 +49,17 @@ public function processNode(Node $node, Scope $scope): array
4749
return [];
4850
}
4951

50-
$functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
51-
if ($functionName === null) {
52+
if (!$this->reflectionProvider->hasFunction($node->name, $scope)) {
5253
return [];
5354
}
54-
$functionName = strtolower($functionName);
55+
56+
$function = $this->reflectionProvider->getFunction($node->name, $scope);
57+
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $function->getVariants());
58+
$node = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);
59+
if ($node === null) {
60+
return [];
61+
}
62+
$functionName = strtolower($function->getName());
5563
if (!array_key_exists($functionName, $this->functionArguments)) {
5664
return [];
5765
}

tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPStan\Rules\Rule;
66
use PHPStan\Testing\RuleTestCase;
7+
use const PHP_VERSION_ID;
78

89
class StrictFunctionCallsRuleTest extends RuleTestCase
910
{
@@ -71,4 +72,12 @@ public function testRule(): void
7172
]);
7273
}
7374

75+
public function testBug231(): void
76+
{
77+
if (PHP_VERSION_ID < 80100) {
78+
$this->markTestSkipped('Test requires PHP 8.1.');
79+
}
80+
$this->analyse([__DIR__ . '/data/bug-231.php'], []);
81+
}
82+
7483
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug231;
4+
5+
enum MyEnum: string
6+
{
7+
case Foo = 'foo';
8+
case Bar = 'bar';
9+
case Baz = 'baz';
10+
11+
public function isB(): bool
12+
{
13+
return in_array($this, strict: true, haystack: [
14+
self::Bar,
15+
self::Baz,
16+
]);
17+
}
18+
}

0 commit comments

Comments
 (0)