Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can use disallow* aliases to allowExcept* #104

Merged
merged 1 commit into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ Such configuration only makes sense when both the parameters of `log()` are opti
## Allow calls except when a param has a specified value

Sometimes, it's handy to disallow a function or a method call only when a parameter matches but allow it otherwise. For example the `hash()` function, it's fine using it with algorithm families like SHA-2 & SHA-3 (not for passwords though) but you'd like PHPStan to report when it's used with MD5 like `hash('md5', ...)`.
You can use `allowExceptParams`, `allowExceptCaseInsensitiveParams`, `allowExceptParamsInAllowed` config options to disallow only some calls:
You can use `allowExceptParams` (or `disallowParams`), `allowExceptCaseInsensitiveParams` (or `disallowCaseInsensitiveParams`), `allowExceptParamsInAllowed` (or `disallowParamsInAllowed`) config options to disallow only some calls:

```neon
parameters:
Expand Down
2 changes: 1 addition & 1 deletion extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ parameters:
parametersSchema:
allowInRootDir: schema(string(), nullable())
# These should be defined using `structure` with listed keys but it seems to me that PHPStan requires
# all keys to be present in a structure but `message` & `allow*` are optional.
# all keys to be present in a structure but `message` & `allow*`/`disallow*` are optional.
disallowedNamespaces: listOf(
arrayOf(
anyOf(
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ parameters:
- src
level: max
typeAliases:
ForbiddenCallsConfig: 'array<array{function?:string, method?:string, message?:string, allowIn?:string[], allowInFunctions?:string[], allowInMethods?:string[], allowParamsInAllowed?:array<integer, integer|boolean|string>, allowParamsInAllowedAnyValue?:array<integer, integer>, allowParamsAnywhere?:array<integer, integer|boolean|string>, allowParamsAnywhereAnyValue?:array<integer, integer>, allowExceptParamsInAllowed?:array<integer, integer|boolean|string>, allowExceptParams?:array<integer, integer|boolean|string>, allowExceptCaseInsensitiveParams?:array<integer, integer|boolean|string>, errorIdentifier?:string}>'
ForbiddenCallsConfig: 'array<array{function?:string, method?:string, message?:string, allowIn?:string[], allowInFunctions?:string[], allowInMethods?:string[], allowParamsInAllowed?:array<integer, integer|boolean|string>, allowParamsInAllowedAnyValue?:array<integer, integer>, allowParamsAnywhere?:array<integer, integer|boolean|string>, allowParamsAnywhereAnyValue?:array<integer, integer>, allowExceptParamsInAllowed?:array<integer, integer|boolean|string>, disallowParamsInAllowed?:array<integer, integer|boolean|string>, allowExceptParams?:array<integer, integer|boolean|string>, disallowParams?:array<integer, integer|boolean|string>, allowExceptCaseInsensitiveParams?:array<integer, integer|boolean|string>, disallowCaseInsensitiveParams?:array<integer, integer|boolean|string>, errorIdentifier?:string}>'

includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
Expand Down
6 changes: 3 additions & 3 deletions src/DisallowedCallFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public function createFromConfig(array $config): array
foreach ($disallowedCall['allowParamsAnywhereAnyValue'] ?? [] as $param) {
$allowParamsAnywhere[$param] = new DisallowedCallParamWithAnyValue();
}
foreach ($disallowedCall['allowExceptParamsInAllowed'] ?? [] as $param => $value) {
foreach ($disallowedCall['allowExceptParamsInAllowed'] ?? $disallowedCall['disallowParamsInAllowed'] ?? [] as $param => $value) {
$allowExceptParamsInAllowed[$param] = new DisallowedCallParamExceptValue($value);
}
foreach ($disallowedCall['allowExceptParams'] ?? [] as $param => $value) {
foreach ($disallowedCall['allowExceptParams'] ?? $disallowedCall['disallowParams'] ?? [] as $param => $value) {
$allowExceptParams[$param] = new DisallowedCallParamExceptValue($value);
}
foreach ($disallowedCall['allowExceptCaseInsensitiveParams'] ?? [] as $param => $value) {
foreach ($disallowedCall['allowExceptCaseInsensitiveParams'] ?? $disallowedCall['disallowCaseInsensitiveParams'] ?? [] as $param => $value) {
$allowExceptParams[$param] = new DisallowedCallParamExceptCaseInsensitiveValue($value);
}
$disallowedCall = new DisallowedCall(
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/FunctionCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected function getRule(): Rule
'../src/disallowed-allowed/*.php',
'../src/*-allow/*.*',
],
'allowExceptParams' => [
'disallowParams' => [
1 => 'sha1',
],
],
Expand Down