Skip to content

Commit d146959

Browse files
authored
Full re-allow support for all items (#310)
The following items: - constants - enums - superglobals - control structures can now be re-allowed in given - methods or functions (`allowInMethods`, `allowInFunctions`) - instances (`allowInInstanceOf`) - classes with given attributes (`allowInClassWithAttributes`) - methods and functions with attributes (`allowInMethodsWithAttributes`, `allowInFunctionsWithAttributes`) - classes with any method with given attributes (`allowInClassWithMethodAttributes`) Bringing the items listed above on par with the already fully supported items like namespaces, attributes, method and function calls. Close #307
2 parents a72f6b2 + 7282763 commit d146959

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+695
-193
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The extension supports versatile [custom rules](docs/custom-rules.md), too.
4747

4848
### Allow some previously disallowed calls or usages
4949

50-
Let's say you have disallowed `foo()` with custom rules. But you want to re-allow it when used in your custom wrapper, or when the first parameter equals, or not, a specified value. The extension offers multiple ways of doing that:
50+
Let's say you have disallowed the `foo()` function (or any other supported items like constants or method calls etc.) with custom rules. But you want to re-allow it when used in your custom wrapper, or when the first parameter equals, or not, a specified value. The extension offers multiple ways of doing that:
5151

5252
- [Ignore errors](docs/allow-ignore-errors.md) the PHPStan way
5353
- [Allow in paths](docs/allow-in-paths.md)

docs/allow-in-class-with-attributes.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33
It is possible to allow a previously disallowed item when done in a class with specified attributes.
44
You can use the `allowInClassWithAttributes` configuration option.
55

6-
This is supported for the following items:
7-
- function calls
8-
- method calls
9-
- attribute usages
10-
- namespace usages
11-
- classname usages
12-
136
For example, if you'd have a configuration like this:
147

158
```neon

docs/allow-in-class-with-method-attributes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Allow in class with given attributes on any method
22

3-
You can allow or disallow a function or a method, an attribute, a namespace or a classname in a class where any method, including the method the call is done in, but not the class itself, has the specified attribute.
3+
You can allow or disallow an item in a class where any method, including the method the call is done in, but not the class itself, has the specified attribute.
44
This is done with `allowInClassWithMethodAttributes` and `allowExceptInClassWithMethodAttributes` (or `disallowInClassWithMethodAttributes` which is an alias). The method with the attribute can also be static, final or even abstract, and the method visibility doesn't matter.
55

66
```neon

docs/allow-in-instance-of.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Allow in classes, child classes, classes implementing an interface
22

3-
Use `allowInInstanceOf`, named after the PHP's `instanceof` operator, if you want to allow a function or a method call, an attribute, a classname, or a namespace in
3+
Use `allowInInstanceOf`, named after the PHP's `instanceof` operator, if you want to allow an item like function or a method call, an attribute, a classname, or a namespace etc. in
44
- a class of given name
55
- a class that inherits from a class of given name
66
- a class that implements given interface

docs/allow-in-methods-with-attributes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Allow in methods or functions with given attributes
22

3-
Similar to [allowing items in methods or functions by name](allow-in-methods.md), you can allow or disallow items like functions and method calls, attributes, namespace and classname usage in methods and functions with given attributes.
3+
Similar to [allowing items in methods or functions by name](allow-in-methods.md), you can allow or disallow items like functions and method calls, attributes, namespace and classname usage etc. in methods and functions with given attributes.
44

55
You can use `allowInMethodsWithAttributes` (or the `allowInFunctionsWithAttributes` alias) for that:
66

docs/allow-in-methods.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Allow in methods or functions
22

3-
To allow a previously disallowed method or function only when called from a different method or function in any file, use `allowInFunctions` (or `allowInMethods` alias):
3+
To allow a previously disallowed item like method or function etc. only when called from a different method or function in any file, use `allowInFunctions` (or `allowInMethods` alias):
44

55
```neon
66
parameters:

extension.neon

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,27 @@ parametersSchema:
240240
?allowIn: listOf(string()),
241241
?allowExceptIn: listOf(string()),
242242
?disallowIn: listOf(string()),
243+
?allowInFunctions: listOf(string()),
244+
?allowInMethods: listOf(string()),
245+
?allowExceptInFunctions: listOf(string()),
246+
?allowExceptInMethods: listOf(string()),
247+
?disallowInFunctions: listOf(string()),
248+
?disallowInMethods: listOf(string()),
249+
?allowInInstanceOf: listOf(string()),
250+
?allowExceptInInstanceOf: listOf(string()),
251+
?disallowInInstanceOf: listOf(string()),
252+
?allowInClassWithAttributes: listOf(string()),
253+
?allowExceptInClassWithAttributes: listOf(string()),
254+
?disallowInClassWithAttributes: listOf(string()),
255+
?allowInFunctionsWithAttributes: listOf(string()),
256+
?allowInMethodsWithAttributes: listOf(string()),
257+
?allowExceptInFunctionsWithAttributes: listOf(string()),
258+
?allowExceptInMethodsWithAttributes: listOf(string()),
259+
?disallowInFunctionsWithAttributes: listOf(string()),
260+
?disallowInMethodsWithAttributes: listOf(string()),
261+
?allowInClassWithMethodAttributes: listOf(string()),
262+
?allowExceptInClassWithMethodAttributes: listOf(string()),
263+
?disallowInClassWithMethodAttributes: listOf(string()),
243264
?errorIdentifier: string(),
244265
?errorTip: string(),
245266
])
@@ -251,6 +272,27 @@ parametersSchema:
251272
?allowIn: listOf(string()),
252273
?allowExceptIn: listOf(string()),
253274
?disallowIn: listOf(string()),
275+
?allowInFunctions: listOf(string()),
276+
?allowInMethods: listOf(string()),
277+
?allowExceptInFunctions: listOf(string()),
278+
?allowExceptInMethods: listOf(string()),
279+
?disallowInFunctions: listOf(string()),
280+
?disallowInMethods: listOf(string()),
281+
?allowInInstanceOf: listOf(string()),
282+
?allowExceptInInstanceOf: listOf(string()),
283+
?disallowInInstanceOf: listOf(string()),
284+
?allowInClassWithAttributes: listOf(string()),
285+
?allowExceptInClassWithAttributes: listOf(string()),
286+
?disallowInClassWithAttributes: listOf(string()),
287+
?allowInFunctionsWithAttributes: listOf(string()),
288+
?allowInMethodsWithAttributes: listOf(string()),
289+
?allowExceptInFunctionsWithAttributes: listOf(string()),
290+
?allowExceptInMethodsWithAttributes: listOf(string()),
291+
?disallowInFunctionsWithAttributes: listOf(string()),
292+
?disallowInMethodsWithAttributes: listOf(string()),
293+
?allowInClassWithMethodAttributes: listOf(string()),
294+
?allowExceptInClassWithMethodAttributes: listOf(string()),
295+
?disallowInClassWithMethodAttributes: listOf(string()),
254296
?errorIdentifier: string(),
255297
?errorTip: string(),
256298
])
@@ -262,6 +304,27 @@ parametersSchema:
262304
?allowIn: listOf(string()),
263305
?allowExceptIn: list(string()),
264306
?disallowIn: list(string()),
307+
?allowInFunctions: listOf(string()),
308+
?allowInMethods: listOf(string()),
309+
?allowExceptInFunctions: listOf(string()),
310+
?allowExceptInMethods: listOf(string()),
311+
?disallowInFunctions: listOf(string()),
312+
?disallowInMethods: listOf(string()),
313+
?allowInInstanceOf: listOf(string()),
314+
?allowExceptInInstanceOf: listOf(string()),
315+
?disallowInInstanceOf: listOf(string()),
316+
?allowInClassWithAttributes: listOf(string()),
317+
?allowExceptInClassWithAttributes: listOf(string()),
318+
?disallowInClassWithAttributes: listOf(string()),
319+
?allowInFunctionsWithAttributes: listOf(string()),
320+
?allowInMethodsWithAttributes: listOf(string()),
321+
?allowExceptInFunctionsWithAttributes: listOf(string()),
322+
?allowExceptInMethodsWithAttributes: listOf(string()),
323+
?disallowInFunctionsWithAttributes: listOf(string()),
324+
?disallowInMethodsWithAttributes: listOf(string()),
325+
?allowInClassWithMethodAttributes: listOf(string()),
326+
?allowExceptInClassWithMethodAttributes: listOf(string()),
327+
?disallowInClassWithMethodAttributes: listOf(string()),
265328
?errorIdentifier: string(),
266329
?errorTip: string(),
267330
])
@@ -325,6 +388,27 @@ parametersSchema:
325388
?allowIn: listOf(string()),
326389
?allowExceptIn: list(string()),
327390
?disallowIn: list(string()),
391+
?allowInFunctions: listOf(string()),
392+
?allowInMethods: listOf(string()),
393+
?allowExceptInFunctions: listOf(string()),
394+
?allowExceptInMethods: listOf(string()),
395+
?disallowInFunctions: listOf(string()),
396+
?disallowInMethods: listOf(string()),
397+
?allowInInstanceOf: listOf(string()),
398+
?allowExceptInInstanceOf: listOf(string()),
399+
?disallowInInstanceOf: listOf(string()),
400+
?allowInClassWithAttributes: listOf(string()),
401+
?allowExceptInClassWithAttributes: listOf(string()),
402+
?disallowInClassWithAttributes: listOf(string()),
403+
?allowInFunctionsWithAttributes: listOf(string()),
404+
?allowInMethodsWithAttributes: listOf(string()),
405+
?allowExceptInFunctionsWithAttributes: listOf(string()),
406+
?allowExceptInMethodsWithAttributes: listOf(string()),
407+
?disallowInFunctionsWithAttributes: listOf(string()),
408+
?disallowInMethodsWithAttributes: listOf(string()),
409+
?allowInClassWithMethodAttributes: listOf(string()),
410+
?allowExceptInClassWithMethodAttributes: listOf(string()),
411+
?disallowInClassWithMethodAttributes: listOf(string()),
328412
?errorIdentifier: string(),
329413
?errorTip: string(),
330414
])

src/Allowed/AllowedPath.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace Spaze\PHPStan\Rules\Disallowed\Allowed;
55

66
use PHPStan\Analyser\Scope;
7-
use Spaze\PHPStan\Rules\Disallowed\Disallowed;
87
use Spaze\PHPStan\Rules\Disallowed\File\FilePath;
98

109
class AllowedPath
@@ -25,23 +24,4 @@ public function matches(Scope $scope, string $allowedPath): bool
2524
return $file !== null && $this->filePath->fnMatch($allowedPath, $file);
2625
}
2726

28-
29-
public function isAllowedPath(Scope $scope, Disallowed $disallowed): bool
30-
{
31-
foreach ($disallowed->getAllowIn() as $allowedPath) {
32-
if ($this->matches($scope, $allowedPath)) {
33-
return true;
34-
}
35-
}
36-
if ($disallowed->getAllowExceptIn()) {
37-
foreach ($disallowed->getAllowExceptIn() as $allowedExceptPath) {
38-
if ($this->matches($scope, $allowedExceptPath)) {
39-
return false;
40-
}
41-
}
42-
return true;
43-
}
44-
return false;
45-
}
46-
4727
}

src/ControlStructures/BreakControlStructure.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getNodeType(): string
5353
*/
5454
public function processNode(Node $node, Scope $scope): array
5555
{
56-
return $this->disallowedControlStructureRuleErrors->get($scope, 'break', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_BREAK);
56+
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'break', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_BREAK);
5757
}
5858

5959
}

src/ControlStructures/ContinueControlStructure.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function getNodeType(): string
5353
*/
5454
public function processNode(Node $node, Scope $scope): array
5555
{
56-
return $this->disallowedControlStructureRuleErrors->get($scope, 'continue', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_CONTINUE);
56+
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'continue', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_CONTINUE);
5757
}
5858

5959
}

0 commit comments

Comments
 (0)