Skip to content

Commit 6f83846

Browse files
Copilotspaze
authored andcommitted
getAllowExceptInCalls loop to check all entries before allowing
1 parent 01eb00e commit 6f83846

3 files changed

Lines changed: 99 additions & 3 deletions

File tree

src/Allowed/Allowed.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ public function isAllowed(?Node $node, Scope $scope, ?array $args, Disallowed $d
6464
return !$hasParams || $this->hasAllowedParamsInAllowed($scope, $args, $disallowed);
6565
}
6666
}
67-
foreach ($disallowed->getAllowExceptInCalls() as $call) {
68-
if (!$this->callMatches($scope, $call)) {
69-
return true;
67+
if ($disallowed->getAllowExceptInCalls()) {
68+
foreach ($disallowed->getAllowExceptInCalls() as $call) {
69+
if ($this->callMatches($scope, $call)) {
70+
return false;
71+
}
7072
}
73+
return true;
7174
}
7275
foreach ($disallowed->getAllowIn() as $allowedPath) {
7376
if ($this->allowedPath->matches($scope, $allowedPath)) {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Spaze\PHPStan\Rules\Disallowed\Calls;
5+
6+
use PHPStan\Rules\Rule;
7+
use PHPStan\ShouldNotHappenException;
8+
use PHPStan\Testing\RuleTestCase;
9+
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
10+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallableParameterRuleErrors;
11+
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedFunctionRuleErrors;
12+
13+
/**
14+
* @extends RuleTestCase<FunctionCalls>
15+
*/
16+
class FunctionCallsAllowExceptInMultipleMethodsTest extends RuleTestCase
17+
{
18+
19+
/**
20+
* @throws ShouldNotHappenException
21+
*/
22+
protected function getRule(): Rule
23+
{
24+
$container = self::getContainer();
25+
return new FunctionCalls(
26+
$container->getByType(DisallowedFunctionRuleErrors::class),
27+
$container->getByType(DisallowedCallableParameterRuleErrors::class),
28+
$container->getByType(DisallowedCallFactory::class),
29+
[
30+
[
31+
'function' => 'crc32()',
32+
'allowExceptInMethods' => [
33+
'\\Fiction\\Pulp\\RoyaleMultiple::methodA()',
34+
'\\Fiction\\Pulp\\RoyaleMultiple::methodB()',
35+
],
36+
],
37+
]
38+
);
39+
}
40+
41+
42+
public function testRule(): void
43+
{
44+
// Based on the configuration above, in this file:
45+
$this->analyse([__DIR__ . '/../src/RoyaleMultiple.php'], [
46+
[
47+
// expect this error message:
48+
'Calling crc32() is forbidden.',
49+
// on this line:
50+
11,
51+
],
52+
[
53+
'Calling crc32() is forbidden.',
54+
17,
55+
],
56+
]);
57+
}
58+
59+
60+
public static function getAdditionalConfigFiles(): array
61+
{
62+
return [
63+
__DIR__ . '/../../extension.neon',
64+
];
65+
}
66+
67+
}

tests/src/RoyaleMultiple.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Fiction\Pulp;
5+
6+
class RoyaleMultiple
7+
{
8+
9+
public function methodA(): void
10+
{
11+
$foo = crc32('a');
12+
}
13+
14+
15+
public function methodB(): void
16+
{
17+
$foo = crc32('b');
18+
}
19+
20+
21+
public function methodC(): void
22+
{
23+
$foo = crc32('c');
24+
}
25+
26+
}

0 commit comments

Comments
 (0)