-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add removeClassAttribute method for class attributes #58
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
base: master
Are you sure you want to change the base?
Changes from all commits
16c51cf
9ecd149
b6220fa
799fe92
8cd3f12
b6b6178
87d54c6
37cc406
eb2719b
6c77fe6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Visitors; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Attribute; | ||
| use PhpParser\Node\Stmt\Class_; | ||
| use PhpParser\Node\Stmt\Nop; | ||
| use RonasIT\Larabuilder\Exceptions\NodeNotExistException; | ||
|
|
||
| class RemoveClassAttribute extends AbstractNodeVisitor | ||
| { | ||
| protected array $allowedParentNodesTypes = [ | ||
| Class_::class, | ||
| ]; | ||
|
|
||
| public function __construct( | ||
| protected string $className, | ||
| protected string $attributeName, | ||
| ) { | ||
| } | ||
|
|
||
| protected function modify(Node $node): Node | ||
| { | ||
| /** @var Class_ $node */ | ||
| $this->validateClassName($node); | ||
|
|
||
| $this->removeMatchingAttributes($node); | ||
|
|
||
| return $node; | ||
| } | ||
|
|
||
| protected function validateClassName(Class_ $node): void | ||
| { | ||
| if ($this->className !== $node->name->name) { | ||
| throw new NodeNotExistException('Class', $this->className); | ||
|
Comment on lines
+35
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Multiple classes in a single file violates |
||
| } | ||
| } | ||
|
Comment on lines
+33
to
+38
|
||
|
|
||
| protected function removeMatchingAttributes(Class_ $node): void | ||
| { | ||
| foreach ($node->attrGroups as $key => $attrGroup) { | ||
| if ($attrGroup instanceof Nop) { | ||
| continue; | ||
| } | ||
|
|
||
| $attrGroup->attrs = array_filter($attrGroup->attrs, fn (Attribute $attr) => $attr->name->name !== $this->attributeName); | ||
|
|
||
| // Replace with Nop to avoid leftover `#[]` and preserve original blank lines (full removal shifts token offsets in Printer) | ||
| if (empty($attrGroup->attrs)) { | ||
| $node->attrGroups[$key] = new Nop(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Tests\Support; | ||
|
|
||
| use App\MyAttribute; | ||
| use App\SetUp; | ||
|
|
||
| #[MyAttribute] | ||
| #[AnotherAttribute] | ||
| class SomeClass | ||
| { | ||
| #[MyAttribute] | ||
| public int $prop; | ||
|
|
||
| #[SetUp] | ||
| public function someMethod() | ||
| { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Tests\Support; | ||
|
|
||
| use App\MyAttribute; | ||
| use App\AnotherAttribute; | ||
| use App\SetUp; | ||
|
|
||
| #[MyAttribute, AnotherAttribute] | ||
| class SomeClass | ||
| { | ||
| #[SetUp] | ||
| public function someMethod() | ||
| { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Tests\Support; | ||
|
|
||
| use App\MyAttribute; | ||
| use App\SetUp; | ||
|
|
||
| #[MyAttribute(1234)] | ||
| #[MyAttribute(5678)] | ||
| class SomeClass | ||
| { | ||
| #[MyAttribute] | ||
| public int $prop; | ||
|
|
||
| #[SetUp] | ||
| public function someMethod() | ||
| { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Tests\Support; | ||
|
|
||
| use App\MyAttribute; | ||
| use App\SetUp; | ||
|
|
||
| #[MyAttribute] | ||
| #[AnotherAttribute] | ||
| class SomeClass | ||
| { | ||
| #[MyAttribute] | ||
| public int $prop; | ||
|
|
||
| #[SetUp] | ||
| public function someMethod() | ||
| { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Tests\Support; | ||
|
|
||
| use App\MyAttribute; | ||
| use App\AnotherAttribute; | ||
| use App\SetUp; | ||
|
|
||
| #[AnotherAttribute] | ||
| class SomeClass | ||
| { | ||
| #[SetUp] | ||
| public function someMethod() | ||
| { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| namespace RonasIT\Larabuilder\Tests\Support; | ||
|
|
||
| use App\MyAttribute; | ||
| use App\SetUp; | ||
|
|
||
| class SomeClass | ||
| { | ||
| #[MyAttribute] | ||
| public int $prop; | ||
|
|
||
| #[SetUp] | ||
| public function someMethod() | ||
| { | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fresh evidence: even in a PSR-1 single-class file, a nested
new class {}creates aClass_node with no name, and this comparison dereferences$node->nameunconditionally. WhenremoveClassAttribute()traverses such a file, it fails before reaching the intended class (or throwsNodeNotExistExceptionon the wrong node), so attribute removal is impossible for valid PHP files that include anonymous classes.Useful? React with 👍 / 👎.