Skip to content

Commit

Permalink
Full re-allow support for all items (#310)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
spaze authored Feb 22, 2025
2 parents a72f6b2 + 7282763 commit d146959
Show file tree
Hide file tree
Showing 42 changed files with 695 additions and 193 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The extension supports versatile [custom rules](docs/custom-rules.md), too.

### Allow some previously disallowed calls or usages

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:
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:

- [Ignore errors](docs/allow-ignore-errors.md) the PHPStan way
- [Allow in paths](docs/allow-in-paths.md)
Expand Down
7 changes: 0 additions & 7 deletions docs/allow-in-class-with-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@
It is possible to allow a previously disallowed item when done in a class with specified attributes.
You can use the `allowInClassWithAttributes` configuration option.

This is supported for the following items:
- function calls
- method calls
- attribute usages
- namespace usages
- classname usages

For example, if you'd have a configuration like this:

```neon
Expand Down
2 changes: 1 addition & 1 deletion docs/allow-in-class-with-method-attributes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Allow in class with given attributes on any method

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.
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.
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.

```neon
Expand Down
2 changes: 1 addition & 1 deletion docs/allow-in-instance-of.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Allow in classes, child classes, classes implementing an interface

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
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
- a class of given name
- a class that inherits from a class of given name
- a class that implements given interface
Expand Down
2 changes: 1 addition & 1 deletion docs/allow-in-methods-with-attributes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Allow in methods or functions with given attributes

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.
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.

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

Expand Down
2 changes: 1 addition & 1 deletion docs/allow-in-methods.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Allow in methods or functions

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):
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):

```neon
parameters:
Expand Down
84 changes: 84 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,27 @@ parametersSchema:
?allowIn: listOf(string()),
?allowExceptIn: listOf(string()),
?disallowIn: listOf(string()),
?allowInFunctions: listOf(string()),
?allowInMethods: listOf(string()),
?allowExceptInFunctions: listOf(string()),
?allowExceptInMethods: listOf(string()),
?disallowInFunctions: listOf(string()),
?disallowInMethods: listOf(string()),
?allowInInstanceOf: listOf(string()),
?allowExceptInInstanceOf: listOf(string()),
?disallowInInstanceOf: listOf(string()),
?allowInClassWithAttributes: listOf(string()),
?allowExceptInClassWithAttributes: listOf(string()),
?disallowInClassWithAttributes: listOf(string()),
?allowInFunctionsWithAttributes: listOf(string()),
?allowInMethodsWithAttributes: listOf(string()),
?allowExceptInFunctionsWithAttributes: listOf(string()),
?allowExceptInMethodsWithAttributes: listOf(string()),
?disallowInFunctionsWithAttributes: listOf(string()),
?disallowInMethodsWithAttributes: listOf(string()),
?allowInClassWithMethodAttributes: listOf(string()),
?allowExceptInClassWithMethodAttributes: listOf(string()),
?disallowInClassWithMethodAttributes: listOf(string()),
?errorIdentifier: string(),
?errorTip: string(),
])
Expand All @@ -251,6 +272,27 @@ parametersSchema:
?allowIn: listOf(string()),
?allowExceptIn: listOf(string()),
?disallowIn: listOf(string()),
?allowInFunctions: listOf(string()),
?allowInMethods: listOf(string()),
?allowExceptInFunctions: listOf(string()),
?allowExceptInMethods: listOf(string()),
?disallowInFunctions: listOf(string()),
?disallowInMethods: listOf(string()),
?allowInInstanceOf: listOf(string()),
?allowExceptInInstanceOf: listOf(string()),
?disallowInInstanceOf: listOf(string()),
?allowInClassWithAttributes: listOf(string()),
?allowExceptInClassWithAttributes: listOf(string()),
?disallowInClassWithAttributes: listOf(string()),
?allowInFunctionsWithAttributes: listOf(string()),
?allowInMethodsWithAttributes: listOf(string()),
?allowExceptInFunctionsWithAttributes: listOf(string()),
?allowExceptInMethodsWithAttributes: listOf(string()),
?disallowInFunctionsWithAttributes: listOf(string()),
?disallowInMethodsWithAttributes: listOf(string()),
?allowInClassWithMethodAttributes: listOf(string()),
?allowExceptInClassWithMethodAttributes: listOf(string()),
?disallowInClassWithMethodAttributes: listOf(string()),
?errorIdentifier: string(),
?errorTip: string(),
])
Expand All @@ -262,6 +304,27 @@ parametersSchema:
?allowIn: listOf(string()),
?allowExceptIn: list(string()),
?disallowIn: list(string()),
?allowInFunctions: listOf(string()),
?allowInMethods: listOf(string()),
?allowExceptInFunctions: listOf(string()),
?allowExceptInMethods: listOf(string()),
?disallowInFunctions: listOf(string()),
?disallowInMethods: listOf(string()),
?allowInInstanceOf: listOf(string()),
?allowExceptInInstanceOf: listOf(string()),
?disallowInInstanceOf: listOf(string()),
?allowInClassWithAttributes: listOf(string()),
?allowExceptInClassWithAttributes: listOf(string()),
?disallowInClassWithAttributes: listOf(string()),
?allowInFunctionsWithAttributes: listOf(string()),
?allowInMethodsWithAttributes: listOf(string()),
?allowExceptInFunctionsWithAttributes: listOf(string()),
?allowExceptInMethodsWithAttributes: listOf(string()),
?disallowInFunctionsWithAttributes: listOf(string()),
?disallowInMethodsWithAttributes: listOf(string()),
?allowInClassWithMethodAttributes: listOf(string()),
?allowExceptInClassWithMethodAttributes: listOf(string()),
?disallowInClassWithMethodAttributes: listOf(string()),
?errorIdentifier: string(),
?errorTip: string(),
])
Expand Down Expand Up @@ -325,6 +388,27 @@ parametersSchema:
?allowIn: listOf(string()),
?allowExceptIn: list(string()),
?disallowIn: list(string()),
?allowInFunctions: listOf(string()),
?allowInMethods: listOf(string()),
?allowExceptInFunctions: listOf(string()),
?allowExceptInMethods: listOf(string()),
?disallowInFunctions: listOf(string()),
?disallowInMethods: listOf(string()),
?allowInInstanceOf: listOf(string()),
?allowExceptInInstanceOf: listOf(string()),
?disallowInInstanceOf: listOf(string()),
?allowInClassWithAttributes: listOf(string()),
?allowExceptInClassWithAttributes: listOf(string()),
?disallowInClassWithAttributes: listOf(string()),
?allowInFunctionsWithAttributes: listOf(string()),
?allowInMethodsWithAttributes: listOf(string()),
?allowExceptInFunctionsWithAttributes: listOf(string()),
?allowExceptInMethodsWithAttributes: listOf(string()),
?disallowInFunctionsWithAttributes: listOf(string()),
?disallowInMethodsWithAttributes: listOf(string()),
?allowInClassWithMethodAttributes: listOf(string()),
?allowExceptInClassWithMethodAttributes: listOf(string()),
?disallowInClassWithMethodAttributes: listOf(string()),
?errorIdentifier: string(),
?errorTip: string(),
])
Expand Down
20 changes: 0 additions & 20 deletions src/Allowed/AllowedPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace Spaze\PHPStan\Rules\Disallowed\Allowed;

use PHPStan\Analyser\Scope;
use Spaze\PHPStan\Rules\Disallowed\Disallowed;
use Spaze\PHPStan\Rules\Disallowed\File\FilePath;

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


public function isAllowedPath(Scope $scope, Disallowed $disallowed): bool
{
foreach ($disallowed->getAllowIn() as $allowedPath) {
if ($this->matches($scope, $allowedPath)) {
return true;
}
}
if ($disallowed->getAllowExceptIn()) {
foreach ($disallowed->getAllowExceptIn() as $allowedExceptPath) {
if ($this->matches($scope, $allowedExceptPath)) {
return false;
}
}
return true;
}
return false;
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/BreakControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'break', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_BREAK);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'break', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_BREAK);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/ContinueControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'continue', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_CONTINUE);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'continue', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_CONTINUE);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/DeclareControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'declare', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_DECLARE);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'declare', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_DECLARE);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/DoWhileControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'do-while', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_DO_WHILE);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'do-while', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_DO_WHILE);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/ElseControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'else', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_ELSE);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'else', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_ELSE);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/ElseIfControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'elseif', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_ELSE_IF);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'elseif', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_ELSE_IF);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/ForControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'for', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_FOR);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'for', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_FOR);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/ForeachControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'foreach', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_FOREACH);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'foreach', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_FOREACH);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/GotoControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'goto', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_GOTO);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'goto', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_GOTO);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/IfControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'if', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_IF);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'if', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_IF);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/MatchControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'match', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_MATCH);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'match', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_MATCH);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/RequireIncludeControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function processNode(Node $node, Scope $scope): array
if ($type === null) {
return [];
}
return $this->disallowedControlStructureRuleErrors->get($scope, $type, $this->disallowedControlStructures, $identifier);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, $type, $this->disallowedControlStructures, $identifier);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/ReturnControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'return', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_RETURN);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'return', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_RETURN);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/SwitchControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'switch', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_SWITCH);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'switch', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_SWITCH);
}

}
2 changes: 1 addition & 1 deletion src/ControlStructures/WhileControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getNodeType(): string
*/
public function processNode(Node $node, Scope $scope): array
{
return $this->disallowedControlStructureRuleErrors->get($scope, 'while', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_WHILE);
return $this->disallowedControlStructureRuleErrors->get($node, $scope, 'while', $this->disallowedControlStructures, ErrorIdentifiers::DISALLOWED_WHILE);
}

}
Loading

0 comments on commit d146959

Please sign in to comment.