Skip to content

Commit

Permalink
StrictFunctionCallsRule - support named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 30, 2023
1 parent b21c03d commit 5ae7a3d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^1.10"
"phpstan/phpstan": "^1.10.34"
},
"require-dev": {
"nikic/php-parser": "^4.13.0",
Expand Down
14 changes: 11 additions & 3 deletions src/Rules/StrictCalls/StrictFunctionCallsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Type\Constant\ConstantBooleanType;
Expand Down Expand Up @@ -47,11 +49,17 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
if ($functionName === null) {
if (!$this->reflectionProvider->hasFunction($node->name, $scope)) {
return [];
}
$functionName = strtolower($functionName);

$function = $this->reflectionProvider->getFunction($node->name, $scope);
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $function->getVariants());
$node = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);
if ($node === null) {
return [];
}
$functionName = strtolower($function->getName());
if (!array_key_exists($functionName, $this->functionArguments)) {
return [];
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use const PHP_VERSION_ID;

class StrictFunctionCallsRuleTest extends RuleTestCase
{
Expand Down Expand Up @@ -71,4 +72,12 @@ public function testRule(): void
]);
}

public function testBug231(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');

Check failure on line 78 in tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, lowest)

Dynamic call to static method PHPUnit\Framework\Assert::markTestSkipped().

Check failure on line 78 in tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, highest)

Dynamic call to static method PHPUnit\Framework\Assert::markTestSkipped().

Check failure on line 78 in tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, lowest)

Dynamic call to static method PHPUnit\Framework\Assert::markTestSkipped().

Check failure on line 78 in tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, highest)

Dynamic call to static method PHPUnit\Framework\Assert::markTestSkipped().

Check failure on line 78 in tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, lowest)

Dynamic call to static method PHPUnit\Framework\Assert::markTestSkipped().

Check failure on line 78 in tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, highest)

Dynamic call to static method PHPUnit\Framework\Assert::markTestSkipped().

Check failure on line 78 in tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, lowest)

Dynamic call to static method PHPUnit\Framework\Assert::markTestSkipped().

Check failure on line 78 in tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, highest)

Dynamic call to static method PHPUnit\Framework\Assert::markTestSkipped().

Check failure on line 78 in tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, lowest)

Dynamic call to static method PHPUnit\Framework\Assert::markTestSkipped().

Check failure on line 78 in tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, highest)

Dynamic call to static method PHPUnit\Framework\Assert::markTestSkipped().

Check failure on line 78 in tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Dynamic call to static method PHPUnit\Framework\Assert::markTestSkipped().

Check failure on line 78 in tests/Rules/StrictCalls/StrictFunctionCallsRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, highest)

Dynamic call to static method PHPUnit\Framework\Assert::markTestSkipped().
}
$this->analyse([__DIR__ . '/data/bug-231.php'], []);
}

}
18 changes: 18 additions & 0 deletions tests/Rules/StrictCalls/data/bug-231.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php // lint >= 8.1

namespace Bug231;

enum MyEnum: string
{
case Foo = 'foo';
case Bar = 'bar';
case Baz = 'baz';

public function isB(): bool
{
return in_array($this, strict: true, haystack: [
self::Bar,
self::Baz,
]);
}
}

0 comments on commit 5ae7a3d

Please sign in to comment.