Skip to content

Commit

Permalink
Attributes can be specified like #[\Foo()], not just like Foo
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Jul 2, 2023
1 parent f80ca03 commit ce2fe73
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/DisallowedAttributeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
17 changes: 16 additions & 1 deletion src/Normalizer/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
}

}
11 changes: 11 additions & 0 deletions tests/Normalizer/NormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]'));
}

}
10 changes: 10 additions & 0 deletions tests/Usages/AttributeUsagesAllowParamsMultipleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ protected function getRule(): Rule
],
],
],
[
'attribute' => '#[\Attributes\AttributeClass()]',
'allowIn' => [
'../src/disallowed-allow/ClassWithAttributesAllow.php',
],
],
]
);
}
Expand All @@ -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'], [
[
Expand Down
10 changes: 10 additions & 0 deletions tests/Usages/AttributeUsagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ protected function getRule(): Rule
],
],
],
[
'attribute' => '#[\Attributes\AttributeClass()]',
'allowIn' => [
'../src/disallowed-allow/ClassWithAttributesAllow.php',
],
],
]
);
}
Expand All @@ -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'], []);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/libs/AttributeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types = 1);

namespace Attributes;

use Attribute;

#[Attribute]
class AttributeClass
{
}
6 changes: 6 additions & 0 deletions tests/src/disallowed-allow/ClassWithAttributesAllow.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ public function hasKetchup(): bool
{
}


#[AttributeClass()] // allowed by path in all tests
public function hasPineapple(): bool
{
}

}
6 changes: 6 additions & 0 deletions tests/src/disallowed/ClassWithAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ public function hasKetchup(): bool
{
}


#[AttributeClass()] // disallowed
public function hasPineapple(): bool
{
}

}

0 comments on commit ce2fe73

Please sign in to comment.