Skip to content

Commit ad365b1

Browse files
committed
FPPP: Fix code block detection during removal
Instead of checking whether there is a {/} before/after the removed note, check whether {/} occurs in the between-node range. Dropping that is what we're really concerned about here.
1 parent 4bc8243 commit ad365b1

File tree

3 files changed

+66
-14
lines changed

3 files changed

+66
-14
lines changed

lib/PhpParser/Internal/TokenStream.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function skipRightWhitespace(int $pos) {
170170
return $pos;
171171
}
172172

173-
public function findRight($pos, $findTokenType) {
173+
public function findRight(int $pos, $findTokenType) {
174174
$tokens = $this->tokens;
175175
for ($count = \count($tokens); $pos < $count; $pos++) {
176176
$type = $tokens[$pos][0];
@@ -181,6 +181,29 @@ public function findRight($pos, $findTokenType) {
181181
return -1;
182182
}
183183

184+
/**
185+
* Whether the given position range contains a certain token type.
186+
*
187+
* @param int $startPos Starting position (inclusive)
188+
* @param int $endPos Ending position (exclusive)
189+
* @param int|string $tokenType Token type to look for
190+
* @return bool Whether the token occurs in the given range
191+
*/
192+
public function haveTokenInRange(int $startPos, int $endPos, $tokenType) {
193+
$tokens = $this->tokens;
194+
for ($pos = $startPos; $pos < $endPos; $pos++) {
195+
if ($tokens[$pos][0] === $tokenType) {
196+
return true;
197+
}
198+
}
199+
return false;
200+
}
201+
202+
public function haveBracesInRange(int $startPos, int $endPos) {
203+
return $this->haveTokenInRange($startPos, $endPos, '{')
204+
|| $this->haveTokenInRange($startPos, $endPos, '}');
205+
}
206+
184207
/**
185208
* Get indentation before token position.
186209
*

lib/PhpParser/PrettyPrinterAbstract.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,13 @@ protected function pArray(
802802
} else if (!$skipRemovedNode) {
803803
$result .= $this->origTokens->getTokenCode(
804804
$pos, $itemStartPos, $indentAdjustment);
805+
} else {
806+
if ($isStmtList && $this->origTokens->haveBracesInRange($pos, $itemStartPos)) {
807+
// We'd remove the brace of a code block.
808+
// TODO: Preserve formatting.
809+
$this->setIndentLevel($origIndentLevel);
810+
return null;
811+
}
805812
}
806813

807814
if ($commentsChanged && $comments) {
@@ -865,20 +872,8 @@ protected function pArray(
865872
$result .= $this->origTokens->getTokenCode(
866873
$pos, $itemStartPos, $indentAdjustment);
867874
$skipRemovedNode = true;
868-
869-
if ($isStmtList
870-
&& ($this->origTokens->haveTokenImmediatelyAfter($itemEndPos, '{')
871-
|| $this->origTokens->haveTokenImmediatelyAfter($itemEndPos, '}'))
872-
) {
873-
// We'd remove the brace of a code block.
874-
// TODO: Preserve formatting.
875-
return null;
876-
}
877875
} else {
878-
if ($isStmtList
879-
&& ($this->origTokens->haveTokenImmediatelyBefore($itemStartPos, '{')
880-
|| $this->origTokens->haveTokenImmediatelyBefore($itemStartPos, '}'))
881-
) {
876+
if ($isStmtList && $this->origTokens->haveBracesInRange($pos, $itemStartPos)) {
882877
// We'd remove the brace of a code block.
883878
// TODO: Preserve formatting.
884879
return null;

test/code/formatPreservation/listRemoval.test

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,37 @@ try {
174174
} catch (\Throwable $e) {
175175

176176
}
177+
178+
-----
179+
<?php
180+
class Foo
181+
{
182+
private $id;
183+
184+
public function getId()
185+
{
186+
}
187+
188+
public function getFoo()
189+
{
190+
}
191+
}
192+
-----
193+
$stmts[0]->stmts[2] = new Node\Stmt\ClassMethod('getBar');
194+
$stmts[0]->stmts[3] = new Node\Stmt\ClassMethod('getBaz');
195+
-----
196+
<?php
197+
class Foo
198+
{
199+
private $id;
200+
201+
public function getId()
202+
{
203+
}
204+
function getBar()
205+
{
206+
}
207+
function getBaz()
208+
{
209+
}
210+
}

0 commit comments

Comments
 (0)