Skip to content

Commit

Permalink
Can specify exclude as string
Browse files Browse the repository at this point in the history
Follow-up to #197 where exclude had to be an array
  • Loading branch information
spaze committed May 28, 2023
1 parent ee07fd9 commit efa81ff
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ parameters:
- 'pcntl_foobar()'
```
This config would disallow all `pcntl` functions except (an imaginary) `pcntl_foobar()`.
Please note `exclude` also accepts [`fnmatch`](https://www.php.net/function.fnmatch) patterns so please be careful to not create a contradicting config.
Please note `exclude` also accepts [`fnmatch`](https://www.php.net/function.fnmatch) patterns so please be careful to not create a contradicting config, and that it can accept both a string and an array of strings.

Another option how to limit the set of functions or methods selected by the `function` or `method` directive is a file path in which these are defined which mostly makes sense when a [`fnmatch`](https://www.php.net/function.fnmatch) pattern is used in those directives.
Imagine a use case in which you want to disallow any function or method defined in any namespace, or none at all, by this legacy package:
Expand Down
4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ parameters:
CallParamAnyValueConfig: 'array<int|string, int|array{position:int, value?:int|bool|string, name?:string}>'
CallParamFlagAnyValueConfig: 'array<int|string, int|array{position:int, value?:int, name?:string}>'
AllowDirectives: 'allowIn?:string[], allowExceptIn?:string[], disallowIn?:string[], allowInFunctions?:string[], allowInMethods?:string[], allowExceptInFunctions?:string[], allowExceptInMethods?:string[], disallowInFunctions?:string[], disallowInMethods?:string[], allowParamsInAllowed?:CallParamConfig, allowParamsInAllowedAnyValue?:CallParamAnyValueConfig, allowParamFlagsInAllowed?:CallParamFlagAnyValueConfig, allowParamsAnywhere?:CallParamConfig, allowParamsAnywhereAnyValue?:CallParamAnyValueConfig, allowParamFlagsAnywhere?:CallParamFlagAnyValueConfig, allowExceptParamsInAllowed?:CallParamConfig, allowExceptParamFlagsInAllowed?:CallParamFlagAnyValueConfig, disallowParamFlagsInAllowed?:CallParamFlagAnyValueConfig, disallowParamsInAllowed?:CallParamConfig, allowExceptParams?:CallParamConfig, disallowParams?:CallParamConfig, allowExceptParamFlags?:CallParamFlagAnyValueConfig, disallowParamFlags?:CallParamFlagAnyValueConfig, allowExceptCaseInsensitiveParams?:CallParamConfig, disallowCaseInsensitiveParams?:CallParamConfig'
ForbiddenCallsConfig: 'array<array{function?:string|list<string>, method?:string|list<string>, exclude?:list<string>, definedIn?:string|list<string>, message?:string, %typeAliases.AllowDirectives%, errorIdentifier?:string, errorTip?:string}>'
DisallowedAttributesConfig: 'array<array{attribute:string|list<string>, exclude?:list<string>, message?:string, %typeAliases.AllowDirectives%, errorIdentifier?:string, errorTip?:string}>'
ForbiddenCallsConfig: 'array<array{function?:string|list<string>, method?:string|list<string>, exclude?:string|list<string>, definedIn?:string|list<string>, message?:string, %typeAliases.AllowDirectives%, errorIdentifier?:string, errorTip?:string}>'
DisallowedAttributesConfig: 'array<array{attribute:string|list<string>, exclude?:string|list<string>, message?:string, %typeAliases.AllowDirectives%, errorIdentifier?:string, errorTip?:string}>'
AllowDirectivesConfig: 'array{%typeAliases.AllowDirectives%}'

includes:
Expand Down
2 changes: 1 addition & 1 deletion src/DisallowedAttributeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function createFromConfig(array $config): array
foreach ($config as $disallowed) {
$attributes = $disallowed['attribute'];
$excludes = [];
foreach ($disallowed['exclude'] ?? [] as $exclude) {
foreach ((array)($disallowed['exclude'] ?? []) as $exclude) {
$excludes[] = $this->normalizer->normalizeNamespace($exclude);
}
foreach ((array)$attributes as $attribute) {
Expand Down
2 changes: 1 addition & 1 deletion src/DisallowedCallFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function createFromConfig(array $config): array
throw new ShouldNotHappenException("Either 'method' or 'function' must be set in configuration items");
}
$excludes = [];
foreach ($disallowed['exclude'] ?? [] as $exclude) {
foreach ((array)($disallowed['exclude'] ?? []) as $exclude) {
$excludes[] = $this->normalizer->normalizeCall($exclude);
}
$calls = (array)$calls;
Expand Down
4 changes: 2 additions & 2 deletions src/DisallowedNamespaceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(Normalizer $normalizer)


/**
* @param array<array{namespace?:string, class?:string, exclude?:list<string>, message?:string, allowIn?:string[], allowExceptIn?:string[], disallowIn?:string[], errorIdentifier?:string, errorTip?:string}> $config
* @param array<array{namespace?:string, class?:string, exclude?:string|list<string>, message?:string, allowIn?:string[], allowExceptIn?:string[], disallowIn?:string[], errorIdentifier?:string, errorTip?:string}> $config
* @return DisallowedNamespace[]
*/
public function createFromConfig(array $config): array
Expand All @@ -33,7 +33,7 @@ public function createFromConfig(array $config): array
throw new ShouldNotHappenException("Either 'namespace' or 'class' must be set in configuration items");
}
$excludes = [];
foreach ($disallowed['exclude'] ?? [] as $exclude) {
foreach ((array)($disallowed['exclude'] ?? []) as $exclude) {
$excludes[] = $this->normalizer->normalizeNamespace($exclude);
}
foreach ((array)$namespaces as $namespace) {
Expand Down
1 change: 1 addition & 0 deletions tests/Calls/MethodCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected function getRule(): Rule
[
'method' => 'Waldo\Quux\Blade::run*()',
'message' => "I've seen tests you people wouldn't believe",
'exclude' => 'Waldo\Quux\Blade::runway()',
'allowIn' => [
'../src/disallowed-allow/*.php',
'../src/*-allow/*.*',
Expand Down
5 changes: 5 additions & 0 deletions tests/libs/Blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function runner(int $everything = 0, bool $yes = false, string $roland =
}


public function runway(): void
{
}


public function server(): void
{
}
Expand Down
3 changes: 3 additions & 0 deletions tests/src/disallowed-allow/methodCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@

// not disallowed by path
$blade->andSorcery();

// allowed by path
$blade->runway();
3 changes: 3 additions & 0 deletions tests/src/disallowed/methodCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@

// disallowed by path
$blade->andSorcery();

// would match run* but is excluded
$blade->runway();

0 comments on commit efa81ff

Please sign in to comment.