Skip to content

Commit 41e9bc9

Browse files
committed
Replacing class method visibility doesn't work without previous node's attributes
1 parent 0da2d66 commit 41e9bc9

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Replace class method modifier `public` with `protected` without keeping node attributes
2+
-----
3+
<?php
4+
5+
namespace Abc;
6+
7+
class SourceClass
8+
{
9+
/**
10+
* @template T
11+
* @param array<T> $values
12+
* @return list<T>
13+
*/
14+
public function makeAList(array $values): array
15+
{
16+
// some comment
17+
18+
$strings = ['1'];
19+
20+
$ints = array_map(function ($value): int {
21+
return (int) $value;
22+
}, $strings);
23+
24+
$nonEmptyArray = ['1'];
25+
26+
$nonEmptyArrayFromMethod = $this->returnNonEmptyArray();
27+
28+
$inlineNonEmpty = ['1'];
29+
30+
return array_values($values);
31+
}
32+
}
33+
-----
34+
$node = $stmts[0]->stmts[0]->stmts[0];
35+
$stmts[0]->stmts[0]->stmts[0] = new \PhpParser\Node\Stmt\ClassMethod(
36+
$node->name,
37+
[
38+
'flags' => ($node->flags & ~\PhpParser\Modifiers::PUBLIC) | \PhpParser\Modifiers::PROTECTED,
39+
'byRef' => $node->returnsByRef(),
40+
'params' => $node->getParams(),
41+
'returnType' => $node->getReturnType(),
42+
'stmts' => $node->getStmts(),
43+
'attrGroups' => $node->getAttrGroups(),
44+
],
45+
// $node->getAttributes(), <---- does not work without previous node's attributes
46+
);
47+
-----
48+
<?php
49+
50+
namespace Abc;
51+
52+
class SourceClass
53+
{
54+
/**
55+
* @template T
56+
* @param array<T> $values
57+
* @return list<T>
58+
*/
59+
protected function makeAList(array $values): array
60+
{
61+
// some comment
62+
63+
$strings = ['1'];
64+
65+
$ints = array_map(function ($value): int {
66+
return (int) $value;
67+
}, $strings);
68+
69+
$nonEmptyArray = ['1'];
70+
71+
$nonEmptyArrayFromMethod = $this->returnNonEmptyArray();
72+
73+
$inlineNonEmpty = ['1'];
74+
75+
return array_values($values);
76+
}
77+
}

0 commit comments

Comments
 (0)