Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5ae7a3d

Browse files
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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 11 additions & 3 deletions
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‎

Lines changed: 9 additions & 0 deletions
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
}
Lines changed: 18 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /