Skip to content

Commit 0105ba1

Browse files
committed
FPPP: Fix visibility change with attributes
With the introduction of attributes, the visibility modifier isn't always at the start of the node anymore. As such, preserve any leaving whitespace. Fixes nikic#1117.
1 parent 0da2d66 commit 0105ba1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/PhpParser/PrettyPrinterAbstract.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,10 @@ protected function p(
678678
}
679679

680680
[$printFn, $findToken] = $this->modifierChangeMap[$key];
681+
$skipWSPos = $this->origTokens->skipRightWhitespace($pos);
682+
$result .= $this->origTokens->getTokenCode($pos, $skipWSPos, $indentAdjustment);
681683
$result .= $this->$printFn($subNode);
682-
$pos = $this->origTokens->findRight($pos, $findToken);
684+
$pos = $this->origTokens->findRight($skipWSPos, $findToken);
683685
continue;
684686
}
685687

test/code/formatPreservation/modifierChange.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,22 @@ $stmts[1]->expr->class->flags = 0;
6666
<?php
6767
readonly class {};
6868
class {};
69+
-----
70+
<?php
71+
class X {
72+
#[A]
73+
public function m() {}
74+
/** A */
75+
public function m2() {}
76+
}
77+
-----
78+
$stmts[0]->stmts[0]->flags = Modifiers::PROTECTED;
79+
$stmts[0]->stmts[1]->flags = Modifiers::PROTECTED;
80+
-----
81+
<?php
82+
class X {
83+
#[A]
84+
protected function m() {}
85+
/** A */
86+
protected function m2() {}
87+
}

0 commit comments

Comments
 (0)