Skip to content

Commit

Permalink
Do not remove attributes from class methods when visibility is mutated (
Browse files Browse the repository at this point in the history
#1896)

Previously, attributes were removed when e.g. `public` was mutated to `protected`.

Now, attributes are retained.
  • Loading branch information
maks-rafalko committed Nov 8, 2023
1 parent e5bf714 commit 673ce76
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Mutator/FunctionSignature/ProtectedVisibility.php
Expand Up @@ -82,6 +82,7 @@ public function mutate(Node $node): iterable
'params' => $node->getParams(),
'returnType' => $node->getReturnType(),
'stmts' => $node->getStmts(),
'attrGroups' => $node->getAttrGroups(),
],
$node->getAttributes()
);
Expand Down
1 change: 1 addition & 0 deletions src/Mutator/FunctionSignature/PublicVisibility.php
Expand Up @@ -81,6 +81,7 @@ public function mutate(Node $node): iterable
'params' => $node->getParams(),
'returnType' => $node->getReturnType(),
'stmts' => $node->getStmts(),
'attrGroups' => $node->getAttrGroups(),
],
$node->getAttributes()
);
Expand Down
Expand Up @@ -227,5 +227,41 @@ private function anything()
}
PHP
];

yield 'It does not remove attributes' => [
<<<'PHP'
<?php
namespace PublicVisibilityOneClass;
class Test
{
#[SomeAttribute1]
#[SomeAttribute2]
protected function &foo(int $param, $test = 1) : bool
{
echo 1;
return false;
}
}
PHP,
<<<'PHP'
<?php
namespace PublicVisibilityOneClass;
class Test
{
#[SomeAttribute1]
#[SomeAttribute2]
private function &foo(int $param, $test = 1) : bool
{
echo 1;
return false;
}
}
PHP
,
];
}
}
36 changes: 36 additions & 0 deletions tests/phpunit/Mutator/FunctionSignature/PublicVisibilityTest.php
Expand Up @@ -343,5 +343,41 @@ protected function foo() : bool
}];
PHP,
];

yield 'It does not remove attributes' => [
<<<'PHP'
<?php
namespace PublicVisibilityOneClass;
class Test
{
#[SomeAttribute1]
#[SomeAttribute2]
public function &foo(int $param, $test = 1) : bool
{
echo 1;
return false;
}
}
PHP,
<<<'PHP'
<?php
namespace PublicVisibilityOneClass;
class Test
{
#[SomeAttribute1]
#[SomeAttribute2]
protected function &foo(int $param, $test = 1) : bool
{
echo 1;
return false;
}
}
PHP
,
];
}
}

0 comments on commit 673ce76

Please sign in to comment.