Skip to content

Commit

Permalink
Allow when an attribute is present in attributes somewhere
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Nov 30, 2023
1 parent f0a6f69 commit 3ff929d
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 2 deletions.
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ parameters:
CallParamConfig: 'array<int|string, int|bool|string|array{position:int, value?:int|bool|string, name?:string}>'
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?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, allowInFunctions?:list<string>, allowInMethods?:list<string>, allowExceptInFunctions?:list<string>, allowExceptInMethods?:list<string>, disallowInFunctions?:list<string>, disallowInMethods?:list<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'
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<string>, allowExceptInClassWithAttributes?:list<string>, allowInCallWithAttributes?:list<string>, allowExceptInCallWithAttributes?:list<string>, allowInAnyMethodWithAttributes?:list<string>, allowExceptInAnyMethodWithAttributes?:list<string>'
AllowDirectives: 'allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, allowInFunctions?:list<string>, allowInMethods?:list<string>, allowExceptInFunctions?:list<string>, allowExceptInMethods?:list<string>, disallowInFunctions?:list<string>, disallowInMethods?:list<string>, %typeAliases.AllowParamDirectives%, %typeAliases.AllowAttributesDirectives%'
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%}'
Expand Down
29 changes: 28 additions & 1 deletion src/Allowed/Allowed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down
90 changes: 90 additions & 0 deletions src/Allowed/AllowedConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ class AllowedConfig
/** @var list<string> */
private $allowExceptInCalls;

/** @var list<string> */
private $allowInClassWithAttributes;

/** @var list<string> */
private $allowExceptInClassWithAttributes;

/** @var list<string> */
private $allowInCallWithAttributes;

/** @var list<string> */
private $allowExceptInCallWithAttributes;

/** @var list<string> */
private $allowInAnyMethodWithAttributes;

/** @var list<string> */
private $allowExceptInAnyMethodWithAttributes;

/** @var array<int|string, Param> */
private $allowParamsInAllowed;

Expand All @@ -38,6 +56,12 @@ class AllowedConfig
* @param list<string> $allowExceptIn
* @param list<string> $allowInCalls
* @param list<string> $allowExceptInCalls
* @param list<string> $allowInClassWithAttributes
* @param list<string> $allowExceptInClassWithAttributes
* @param list<string> $allowInCallWithAttributes
* @param list<string> $allowExceptInCallWithAttributes
* @param list<string> $allowInAnyMethodWithAttributes
* @param list<string> $allowExceptInAnyMethodWithAttributes
* @param array<int|string, Param> $allowParamsInAllowed
* @param array<int|string, Param> $allowParamsAnywhere
* @param array<int|string, Param> $allowExceptParamsInAllowed
Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -100,6 +136,60 @@ public function getAllowExceptInCalls(): array
}


/**
* @return list<string>
*/
public function getAllowInClassWithAttributes(): array
{
return $this->allowInClassWithAttributes;
}


/**
* @return list<string>
*/
public function getAllowExceptInClassWithAttributes(): array
{
return $this->allowExceptInClassWithAttributes;
}


/**
* @return list<string>
*/
public function getAllowInCallWithAttributes(): array
{
return $this->allowInCallWithAttributes;
}


/**
* @return list<string>
*/
public function getAllowExceptInCallWithAttributes(): array
{
return $this->allowExceptInCallWithAttributes;
}


/**
* @return list<string>
*/
public function getAllowInAnyMethodWithAttributes(): array
{
return $this->allowInAnyMethodWithAttributes;
}


/**
* @return list<string>
*/
public function getAllowExceptInAnyMethodWithAttributes(): array
{
return $this->allowExceptInAnyMethodWithAttributes;
}


/**
* @return array<int|string, Param>
*/
Expand Down

0 comments on commit 3ff929d

Please sign in to comment.