diff --git a/Makefile b/Makefile index 68bee68..805e4db 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ app-install: ## to install app composer install --prefer-dist app-security-check: ## to check if any security issues in the PHP dependencies - vendor/bin/security-checker security:check --end-point=http://security.sensiolabs.org/check_lock + vendor/bin/security-checker security:check app-static-analysis: ## to run static analysis php -dmemory_limit=-1 vendor/bin/phpstan analyze src tests -l 7 diff --git a/src/Rules/BannedNodesRule.php b/src/Rules/BannedNodesRule.php index a7befd0..276d590 100644 --- a/src/Rules/BannedNodesRule.php +++ b/src/Rules/BannedNodesRule.php @@ -13,6 +13,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; use PHPStan\Analyser\Scope; use PHPStan\Rules\Rule; @@ -55,6 +56,10 @@ public function processNode(Node $node, Scope $scope): array } if ($node instanceof FuncCall) { + if ($node->name instanceof Variable) { + return []; + } + if (!$node->name instanceof Name) { throw new \RuntimeException(sprintf('Expected instance of %s for $node->name, %s given', Name::class, \get_class($node->name))); } diff --git a/tests/Rules/BannedNodesRuleTest.php b/tests/Rules/BannedNodesRuleTest.php index f17f256..238253a 100644 --- a/tests/Rules/BannedNodesRuleTest.php +++ b/tests/Rules/BannedNodesRuleTest.php @@ -18,6 +18,7 @@ use PhpParser\Node\Expr\Exit_; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Include_; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; use PHPStan\Analyser\Scope; use PHPUnit\Framework\MockObject\MockObject; @@ -39,9 +40,9 @@ class BannedNodesRuleTest extends TestCase private $scope; /** - * Initializes the tests. + * {@inheritdoc} */ - protected function setUp() + protected function setUp(): void { $this->rule = new BannedNodesRule([ ['type' => 'Stmt_Echo'], @@ -55,7 +56,7 @@ protected function setUp() /** * Tests getNodeType. */ - public function testGetNodeType() + public function testGetNodeType(): void { $this->assertSame(Node::class, $this->rule->getNodeType()); } @@ -67,7 +68,7 @@ public function testGetNodeType() * * @dataProvider getUnhandledNodes */ - public function testProcessNodeWithUnhandledType(Expr $node) + public function testProcessNodeWithUnhandledType(Expr $node): void { $this->assertCount(0, $this->rule->processNode($node, $this->scope)); } @@ -75,7 +76,7 @@ public function testProcessNodeWithUnhandledType(Expr $node) /** * Tests processNode with banned/allowed functions. */ - public function testProcessNodeWithFunctions() + public function testProcessNodeWithFunctions(): void { foreach (['debug_backtrace', 'dump'] as $bannedFunction) { $node = new FuncCall(new Name($bannedFunction)); @@ -88,6 +89,10 @@ public function testProcessNodeWithFunctions() $this->assertCount(0, $this->rule->processNode($node, $this->scope)); } + + $node = new FuncCall(new Variable('myClosure')); + + $this->assertCount(0, $this->rule->processNode($node, $this->scope)); } /** @@ -97,7 +102,7 @@ public function testProcessNodeWithFunctions() * * @dataProvider getHandledNodes */ - public function testProcessNodeWithHandledTypes(Expr $node) + public function testProcessNodeWithHandledTypes(Expr $node): void { $this->assertCount(1, $this->rule->processNode($node, $this->scope)); } @@ -105,7 +110,7 @@ public function testProcessNodeWithHandledTypes(Expr $node) /** * @return \Generator */ - public function getUnhandledNodes() + public function getUnhandledNodes(): \Generator { yield [new Include_($this->createMock(Expr::class), Include_::TYPE_INCLUDE)]; } @@ -113,7 +118,7 @@ public function getUnhandledNodes() /** * @return \Generator */ - public function getHandledNodes() + public function getHandledNodes(): \Generator { yield [new Eval_($this->createMock(Expr::class))]; yield [new Exit_()]; diff --git a/tests/Rules/BannedUseTestRuleTest.php b/tests/Rules/BannedUseTestRuleTest.php index 16a87c4..f08fca5 100644 --- a/tests/Rules/BannedUseTestRuleTest.php +++ b/tests/Rules/BannedUseTestRuleTest.php @@ -36,9 +36,9 @@ class BannedUseTestRuleTest extends TestCase private $scope; /** - * Initializes the tests. + * {@inheritdoc} */ - protected function setUp() + protected function setUp(): void { $this->rule = new BannedUseTestRule(); $this->scope = $this->createMock(Scope::class); @@ -47,7 +47,7 @@ protected function setUp() /** * Tests getNodeType. */ - public function testGetNodeType() + public function testGetNodeType(): void { $this->assertSame(Use_::class, $this->rule->getNodeType()); } @@ -55,7 +55,7 @@ public function testGetNodeType() /** * Tests processNode if disabled. */ - public function testProcessNodeIfDisabled() + public function testProcessNodeIfDisabled(): void { $this->scope->expects($this->never())->method('getNamespace'); @@ -65,7 +65,7 @@ public function testProcessNodeIfDisabled() /** * Tests processNode with test scope. */ - public function testProcessNodeWithTestScope() + public function testProcessNodeWithTestScope(): void { $this->scope->expects($this->once())->method('getNamespace')->willReturn('Tests\\Foo\\Bar'); @@ -77,7 +77,7 @@ public function testProcessNodeWithTestScope() * * @expectedException \InvalidArgumentException */ - public function testProcessNodeThrowsException() + public function testProcessNodeThrowsException(): void { $this->scope->expects($this->once())->method('getNamespace')->willReturn('Foo\\Bar'); @@ -87,7 +87,7 @@ public function testProcessNodeThrowsException() /** * Tests processNode with errors. */ - public function testProcessNodeWithErrors() + public function testProcessNodeWithErrors(): void { $this->scope->expects($this->once())->method('getNamespace')->willReturn('Foo\\Bar');