From 3ff929dc717a997869f2b4621120607eb6fdfb21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Sat, 29 Apr 2023 20:12:11 +0200 Subject: [PATCH] Allow when an attribute is present in attributes somewhere --- phpstan.neon | 4 +- src/Allowed/Allowed.php | 29 ++++++++++- src/Allowed/AllowedConfig.php | 90 +++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 2 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 93a1589..4a8afb8 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,7 +6,9 @@ parameters: CallParamConfig: 'array' CallParamAnyValueConfig: 'array' CallParamFlagAnyValueConfig: 'array' - AllowDirectives: 'allowIn?:list, allowExceptIn?:list, disallowIn?:list, allowInFunctions?:list, allowInMethods?:list, allowExceptInFunctions?:list, allowExceptInMethods?:list, disallowInFunctions?:list, disallowInMethods?:list, 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' + AllowParamDirectives: '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' + AllowAttributesDirectives: 'allowInClassWithAttributes?:list, allowExceptInClassWithAttributes?:list, allowInCallWithAttributes?:list, allowExceptInCallWithAttributes?:list, allowInAnyMethodWithAttributes?:list, allowExceptInAnyMethodWithAttributes?:list' + AllowDirectives: 'allowIn?:list, allowExceptIn?:list, disallowIn?:list, allowInFunctions?:list, allowInMethods?:list, allowExceptInFunctions?:list, allowExceptInMethods?:list, disallowInFunctions?:list, disallowInMethods?:list, %typeAliases.AllowParamDirectives%, %typeAliases.AllowAttributesDirectives%' ForbiddenCallsConfig: 'array, method?:string|list, exclude?:string|list, definedIn?:string|list, message?:string, %typeAliases.AllowDirectives%, errorIdentifier?:string, errorTip?:string}>' DisallowedAttributesConfig: 'array, exclude?:string|list, message?:string, %typeAliases.AllowDirectives%, errorIdentifier?:string, errorTip?:string}>' AllowDirectivesConfig: 'array{%typeAliases.AllowDirectives%}' diff --git a/src/Allowed/Allowed.php b/src/Allowed/Allowed.php index ca7563e..ed4c4ff 100644 --- a/src/Allowed/Allowed.php +++ b/src/Allowed/Allowed.php @@ -182,13 +182,34 @@ private function getArgType(array $args, Scope $scope, Param $param): ?Type */ public function getConfig(array $allowed): AllowedConfig { - $allowInCalls = $allowExceptInCalls = $allowParamsInAllowed = $allowParamsAnywhere = $allowExceptParamsInAllowed = $allowExceptParams = []; + $allowInCalls = $allowExceptInCalls = $allowInClassWithAttributes = $allowExceptInClassWithAttributes = []; + $allowInCallWithAttributes = $allowExceptInCallWithAttributes = $allowInAnyMethodWithAttributes = $allowExceptInAnyMethodWithAttributes = []; + $allowParamsInAllowed = $allowParamsAnywhere = $allowExceptParamsInAllowed = $allowExceptParams = []; + foreach ($allowed['allowInFunctions'] ?? $allowed['allowInMethods'] ?? [] as $allowedCall) { $allowInCalls[] = $this->normalizer->normalizeCall($allowedCall); } foreach ($allowed['allowExceptInFunctions'] ?? $allowed['allowExceptInMethods'] ?? $allowed['disallowInFunctions'] ?? $allowed['disallowInMethods'] ?? [] as $disallowedCall) { $allowExceptInCalls[] = $this->normalizer->normalizeCall($disallowedCall); } + foreach ($allowed['allowInClassWithAttributes'] ?? [] as $allowInClassAttribute) { + $allowInClassWithAttributes[] = $this->normalizer->normalizeAttribute($allowInClassAttribute); + } + foreach ($allowed['allowExceptInClassWithAttributes'] ?? [] as $allowExceptInClassAttribute) { + $allowExceptInClassWithAttributes[] = $this->normalizer->normalizeAttribute($allowExceptInClassAttribute); + } + foreach ($allowed['allowInCallWithAttributes'] ?? [] as $allowInMethodAttribute) { + $allowInCallWithAttributes[] = $this->normalizer->normalizeAttribute($allowInMethodAttribute); + } + foreach ($allowed['allowExceptInCallWithAttributes'] ?? [] as $allowExceptInMethodAttribute) { + $allowExceptInCallWithAttributes[] = $this->normalizer->normalizeAttribute($allowExceptInMethodAttribute); + } + foreach ($allowed['allowInAnyMethodWithAttributes'] ?? [] as $allowInAnyMethodAttribute) { + $allowInAnyMethodWithAttributes[] = $this->normalizer->normalizeAttribute($allowInAnyMethodAttribute); + } + foreach ($allowed['allowExceptInAnyMethodWithAttributes'] ?? [] as $allowExceptInAnyMethodAttribute) { + $allowExceptInAnyMethodWithAttributes[] = $this->normalizer->normalizeAttribute($allowExceptInAnyMethodAttribute); + } foreach ($allowed['allowParamsInAllowed'] ?? [] as $param => $value) { $allowParamsInAllowed[$param] = $this->paramFactory(ParamValueSpecific::class, $param, $value); } @@ -227,6 +248,12 @@ public function getConfig(array $allowed): AllowedConfig $allowed['allowExceptIn'] ?? $allowed['disallowIn'] ?? [], $allowInCalls, $allowExceptInCalls, + $allowInClassWithAttributes, + $allowExceptInClassWithAttributes, + $allowInCallWithAttributes, + $allowExceptInCallWithAttributes, + $allowInAnyMethodWithAttributes, + $allowExceptInAnyMethodWithAttributes, $allowParamsInAllowed, $allowParamsAnywhere, $allowExceptParamsInAllowed, diff --git a/src/Allowed/AllowedConfig.php b/src/Allowed/AllowedConfig.php index 0d40e47..9ac6736 100644 --- a/src/Allowed/AllowedConfig.php +++ b/src/Allowed/AllowedConfig.php @@ -20,6 +20,24 @@ class AllowedConfig /** @var list */ private $allowExceptInCalls; + /** @var list */ + private $allowInClassWithAttributes; + + /** @var list */ + private $allowExceptInClassWithAttributes; + + /** @var list */ + private $allowInCallWithAttributes; + + /** @var list */ + private $allowExceptInCallWithAttributes; + + /** @var list */ + private $allowInAnyMethodWithAttributes; + + /** @var list */ + private $allowExceptInAnyMethodWithAttributes; + /** @var array */ private $allowParamsInAllowed; @@ -38,6 +56,12 @@ class AllowedConfig * @param list $allowExceptIn * @param list $allowInCalls * @param list $allowExceptInCalls + * @param list $allowInClassWithAttributes + * @param list $allowExceptInClassWithAttributes + * @param list $allowInCallWithAttributes + * @param list $allowExceptInCallWithAttributes + * @param list $allowInAnyMethodWithAttributes + * @param list $allowExceptInAnyMethodWithAttributes * @param array $allowParamsInAllowed * @param array $allowParamsAnywhere * @param array $allowExceptParamsInAllowed @@ -48,6 +72,12 @@ public function __construct( array $allowExceptIn, array $allowInCalls, array $allowExceptInCalls, + array $allowInClassWithAttributes, + array $allowExceptInClassWithAttributes, + array $allowInCallWithAttributes, + array $allowExceptInCallWithAttributes, + array $allowInAnyMethodWithAttributes, + array $allowExceptInAnyMethodWithAttributes, array $allowParamsInAllowed, array $allowParamsAnywhere, array $allowExceptParamsInAllowed, @@ -57,6 +87,12 @@ public function __construct( $this->allowExceptIn = $allowExceptIn; $this->allowInCalls = $allowInCalls; $this->allowExceptInCalls = $allowExceptInCalls; + $this->allowInClassWithAttributes = $allowInClassWithAttributes; + $this->allowExceptInClassWithAttributes = $allowExceptInClassWithAttributes; + $this->allowInCallWithAttributes = $allowInCallWithAttributes; + $this->allowExceptInCallWithAttributes = $allowExceptInCallWithAttributes; + $this->allowInAnyMethodWithAttributes = $allowInAnyMethodWithAttributes; + $this->allowExceptInAnyMethodWithAttributes = $allowExceptInAnyMethodWithAttributes; $this->allowParamsInAllowed = $allowParamsInAllowed; $this->allowParamsAnywhere = $allowParamsAnywhere; $this->allowExceptParamsInAllowed = $allowExceptParamsInAllowed; @@ -100,6 +136,60 @@ public function getAllowExceptInCalls(): array } + /** + * @return list + */ + public function getAllowInClassWithAttributes(): array + { + return $this->allowInClassWithAttributes; + } + + + /** + * @return list + */ + public function getAllowExceptInClassWithAttributes(): array + { + return $this->allowExceptInClassWithAttributes; + } + + + /** + * @return list + */ + public function getAllowInCallWithAttributes(): array + { + return $this->allowInCallWithAttributes; + } + + + /** + * @return list + */ + public function getAllowExceptInCallWithAttributes(): array + { + return $this->allowExceptInCallWithAttributes; + } + + + /** + * @return list + */ + public function getAllowInAnyMethodWithAttributes(): array + { + return $this->allowInAnyMethodWithAttributes; + } + + + /** + * @return list + */ + public function getAllowExceptInAnyMethodWithAttributes(): array + { + return $this->allowExceptInAnyMethodWithAttributes; + } + + /** * @return array */