diff --git a/src/DisallowedAttributeFactory.php b/src/DisallowedAttributeFactory.php index c6f922c..b33eb95 100644 --- a/src/DisallowedAttributeFactory.php +++ b/src/DisallowedAttributeFactory.php @@ -37,11 +37,11 @@ public function createFromConfig(array $config): array $attributes = $disallowed['attribute']; $excludes = []; foreach ((array)($disallowed['exclude'] ?? []) as $exclude) { - $excludes[] = $this->normalizer->normalizeNamespace($exclude); + $excludes[] = $this->normalizer->normalizeAttribute($exclude); } foreach ((array)$attributes as $attribute) { $disallowedAttribute = new DisallowedAttribute( - $this->normalizer->normalizeNamespace($attribute), + $this->normalizer->normalizeAttribute($attribute), $excludes, $disallowed['message'] ?? null, $this->allowed->getConfig($disallowed), diff --git a/src/Normalizer/Normalizer.php b/src/Normalizer/Normalizer.php index a8ed67f..0f9ac7b 100644 --- a/src/Normalizer/Normalizer.php +++ b/src/Normalizer/Normalizer.php @@ -8,7 +8,7 @@ class Normalizer public function normalizeCall(string $call): string { - $call = substr($call, -2) === '()' ? substr($call, 0, -2) : $call; + $call = $this->removeParentheses($call); return $this->normalizeNamespace($call); } @@ -18,4 +18,19 @@ public function normalizeNamespace(string $namespace): string return ltrim($namespace, '\\'); } + + public function normalizeAttribute(string $attribute): string + { + $attribute = ltrim($attribute, '#['); + $attribute = rtrim($attribute, ']'); + $attribute = $this->removeParentheses($attribute); + return $this->normalizeNamespace($attribute); + } + + + private function removeParentheses(string $element): string + { + return substr($element, -2) === '()' ? substr($element, 0, -2) : $element; + } + } diff --git a/tests/Normalizer/NormalizerTest.php b/tests/Normalizer/NormalizerTest.php index 74a9346..5c64485 100644 --- a/tests/Normalizer/NormalizerTest.php +++ b/tests/Normalizer/NormalizerTest.php @@ -33,4 +33,15 @@ public function testNormalizeNamespace(): void $this->assertSame('foo\\bar', $this->normalizer->normalizeNamespace('\\foo\\bar')); } + + public function testNormalizeAttribute(): void + { + $this->assertSame('foo', $this->normalizer->normalizeAttribute('foo')); + $this->assertSame('foo', $this->normalizer->normalizeAttribute('foo()')); + $this->assertSame('foo', $this->normalizer->normalizeAttribute('\\foo()')); + $this->assertSame('foo', $this->normalizer->normalizeAttribute('#[\\foo]')); + $this->assertSame('foo', $this->normalizer->normalizeAttribute('#[\\foo()]')); + $this->assertSame('foo\\bar', $this->normalizer->normalizeAttribute('#[\\foo\\bar()]')); + } + } diff --git a/tests/Usages/AttributeUsagesAllowParamsMultipleTest.php b/tests/Usages/AttributeUsagesAllowParamsMultipleTest.php index 535f2a9..ea9658e 100644 --- a/tests/Usages/AttributeUsagesAllowParamsMultipleTest.php +++ b/tests/Usages/AttributeUsagesAllowParamsMultipleTest.php @@ -49,6 +49,12 @@ protected function getRule(): Rule ], ], ], + [ + 'attribute' => '#[\Attributes\AttributeClass()]', + 'allowIn' => [ + '../src/disallowed-allow/ClassWithAttributesAllow.php', + ], + ], ] ); } @@ -64,6 +70,10 @@ public function testRule(): void // on this line: 8, ], + [ + 'Attribute Attributes\AttributeClass is forbidden, because reasons', + 30, + ], ]); $this->analyse([__DIR__ . '/../src/disallowed-allow/ClassWithAttributesAllow.php'], [ [ diff --git a/tests/Usages/AttributeUsagesTest.php b/tests/Usages/AttributeUsagesTest.php index e27d02c..905c396 100644 --- a/tests/Usages/AttributeUsagesTest.php +++ b/tests/Usages/AttributeUsagesTest.php @@ -41,6 +41,12 @@ protected function getRule(): Rule ], ], ], + [ + 'attribute' => '#[\Attributes\AttributeClass()]', + 'allowIn' => [ + '../src/disallowed-allow/ClassWithAttributesAllow.php', + ], + ], ] ); } @@ -56,6 +62,10 @@ public function testRule(): void // on this line: 8, ], + [ + 'Attribute Attributes\AttributeClass is forbidden, because reasons', + 30, + ], ]); $this->analyse([__DIR__ . '/../src/disallowed-allow/ClassWithAttributesAllow.php'], []); } diff --git a/tests/libs/AttributeClass.php b/tests/libs/AttributeClass.php new file mode 100644 index 0000000..ea5df6c --- /dev/null +++ b/tests/libs/AttributeClass.php @@ -0,0 +1,11 @@ +