Skip to content

Commit 74b1cf6

Browse files
authored
Merge pull request #11 from mremi/handle-closure
fix(exception): do nothing if node name is instance of Variable (clos…
2 parents 72357b4 + 233055a commit 74b1cf6

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ app-install: ## to install app
1919
composer install --prefer-dist
2020

2121
app-security-check: ## to check if any security issues in the PHP dependencies
22-
vendor/bin/security-checker security:check --end-point=http://security.sensiolabs.org/check_lock
22+
vendor/bin/security-checker security:check
2323

2424
app-static-analysis: ## to run static analysis
2525
php -dmemory_limit=-1 vendor/bin/phpstan analyze src tests -l 7

src/Rules/BannedNodesRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PhpParser\Node;
1515
use PhpParser\Node\Expr\FuncCall;
16+
use PhpParser\Node\Expr\Variable;
1617
use PhpParser\Node\Name;
1718
use PHPStan\Analyser\Scope;
1819
use PHPStan\Rules\Rule;
@@ -55,6 +56,10 @@ public function processNode(Node $node, Scope $scope): array
5556
}
5657

5758
if ($node instanceof FuncCall) {
59+
if ($node->name instanceof Variable) {
60+
return [];
61+
}
62+
5863
if (!$node->name instanceof Name) {
5964
throw new \RuntimeException(sprintf('Expected instance of %s for $node->name, %s given', Name::class, \get_class($node->name)));
6065
}

tests/Rules/BannedNodesRuleTest.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PhpParser\Node\Expr\Exit_;
1919
use PhpParser\Node\Expr\FuncCall;
2020
use PhpParser\Node\Expr\Include_;
21+
use PhpParser\Node\Expr\Variable;
2122
use PhpParser\Node\Name;
2223
use PHPStan\Analyser\Scope;
2324
use PHPUnit\Framework\MockObject\MockObject;
@@ -39,9 +40,9 @@ class BannedNodesRuleTest extends TestCase
3940
private $scope;
4041

4142
/**
42-
* Initializes the tests.
43+
* {@inheritdoc}
4344
*/
44-
protected function setUp()
45+
protected function setUp(): void
4546
{
4647
$this->rule = new BannedNodesRule([
4748
['type' => 'Stmt_Echo'],
@@ -55,7 +56,7 @@ protected function setUp()
5556
/**
5657
* Tests getNodeType.
5758
*/
58-
public function testGetNodeType()
59+
public function testGetNodeType(): void
5960
{
6061
$this->assertSame(Node::class, $this->rule->getNodeType());
6162
}
@@ -67,15 +68,15 @@ public function testGetNodeType()
6768
*
6869
* @dataProvider getUnhandledNodes
6970
*/
70-
public function testProcessNodeWithUnhandledType(Expr $node)
71+
public function testProcessNodeWithUnhandledType(Expr $node): void
7172
{
7273
$this->assertCount(0, $this->rule->processNode($node, $this->scope));
7374
}
7475

7576
/**
7677
* Tests processNode with banned/allowed functions.
7778
*/
78-
public function testProcessNodeWithFunctions()
79+
public function testProcessNodeWithFunctions(): void
7980
{
8081
foreach (['debug_backtrace', 'dump'] as $bannedFunction) {
8182
$node = new FuncCall(new Name($bannedFunction));
@@ -88,6 +89,10 @@ public function testProcessNodeWithFunctions()
8889

8990
$this->assertCount(0, $this->rule->processNode($node, $this->scope));
9091
}
92+
93+
$node = new FuncCall(new Variable('myClosure'));
94+
95+
$this->assertCount(0, $this->rule->processNode($node, $this->scope));
9196
}
9297

9398
/**
@@ -97,23 +102,23 @@ public function testProcessNodeWithFunctions()
97102
*
98103
* @dataProvider getHandledNodes
99104
*/
100-
public function testProcessNodeWithHandledTypes(Expr $node)
105+
public function testProcessNodeWithHandledTypes(Expr $node): void
101106
{
102107
$this->assertCount(1, $this->rule->processNode($node, $this->scope));
103108
}
104109

105110
/**
106111
* @return \Generator
107112
*/
108-
public function getUnhandledNodes()
113+
public function getUnhandledNodes(): \Generator
109114
{
110115
yield [new Include_($this->createMock(Expr::class), Include_::TYPE_INCLUDE)];
111116
}
112117

113118
/**
114119
* @return \Generator
115120
*/
116-
public function getHandledNodes()
121+
public function getHandledNodes(): \Generator
117122
{
118123
yield [new Eval_($this->createMock(Expr::class))];
119124
yield [new Exit_()];

tests/Rules/BannedUseTestRuleTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class BannedUseTestRuleTest extends TestCase
3636
private $scope;
3737

3838
/**
39-
* Initializes the tests.
39+
* {@inheritdoc}
4040
*/
41-
protected function setUp()
41+
protected function setUp(): void
4242
{
4343
$this->rule = new BannedUseTestRule();
4444
$this->scope = $this->createMock(Scope::class);
@@ -47,15 +47,15 @@ protected function setUp()
4747
/**
4848
* Tests getNodeType.
4949
*/
50-
public function testGetNodeType()
50+
public function testGetNodeType(): void
5151
{
5252
$this->assertSame(Use_::class, $this->rule->getNodeType());
5353
}
5454

5555
/**
5656
* Tests processNode if disabled.
5757
*/
58-
public function testProcessNodeIfDisabled()
58+
public function testProcessNodeIfDisabled(): void
5959
{
6060
$this->scope->expects($this->never())->method('getNamespace');
6161

@@ -65,7 +65,7 @@ public function testProcessNodeIfDisabled()
6565
/**
6666
* Tests processNode with test scope.
6767
*/
68-
public function testProcessNodeWithTestScope()
68+
public function testProcessNodeWithTestScope(): void
6969
{
7070
$this->scope->expects($this->once())->method('getNamespace')->willReturn('Tests\\Foo\\Bar');
7171

@@ -77,7 +77,7 @@ public function testProcessNodeWithTestScope()
7777
*
7878
* @expectedException \InvalidArgumentException
7979
*/
80-
public function testProcessNodeThrowsException()
80+
public function testProcessNodeThrowsException(): void
8181
{
8282
$this->scope->expects($this->once())->method('getNamespace')->willReturn('Foo\\Bar');
8383

@@ -87,7 +87,7 @@ public function testProcessNodeThrowsException()
8787
/**
8888
* Tests processNode with errors.
8989
*/
90-
public function testProcessNodeWithErrors()
90+
public function testProcessNodeWithErrors(): void
9191
{
9292
$this->scope->expects($this->once())->method('getNamespace')->willReturn('Foo\\Bar');
9393

0 commit comments

Comments
 (0)