Skip to content

Commit

Permalink
[CodingStyle] Handle complex If_ ElseIf_ Else_ Try Catch Finally on o…
Browse files Browse the repository at this point in the history
…n NewlineAfterStatementRector (#793)

* [CodingStyle] Failing fixture complex if on NewlineAfterStatementRector

* [CodingStyle] Handle complex If_ ElseIf_ Else_ on on NewlineAfterStatementRector

* more example

* refactor

* naming

* fix
  • Loading branch information
samsonasik committed Aug 31, 2021
1 parent 170a09f commit 9fee4dd
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Stmt\NewlineAfterStatementRector\Fixture;

class ComplexIf
{
public function run()
{
if (rand(0, 1)) {
if (rand(0, 1)) {

}
} elseif (rand(0, 1)) {

} else {

}
return true;
}

public function run2()
{
if (rand(0, 1)) {
if (rand(0, 1)) {

}
echo 'test';
} elseif (rand(0, 1)) {

} else {

}
return true;
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Stmt\NewlineAfterStatementRector\Fixture;

class ComplexIf
{
public function run()
{
if (rand(0, 1)) {
if (rand(0, 1)) {

}
} elseif (rand(0, 1)) {

} else {

}

return true;
}

public function run2()
{
if (rand(0, 1)) {
if (rand(0, 1)) {

}

echo 'test';
} elseif (rand(0, 1)) {

} else {

}

return true;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Stmt\NewlineAfterStatementRector\Fixture;

class ComplexTryCatch
{
public function run()
{
try {
if (rand(0, 1)) {

}
} catch (\RuntimeException $e) {
if (rand(0, 1)) {

}
} finally {

}
return true;
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Stmt\NewlineAfterStatementRector\Fixture;

class ComplexTryCatch
{
public function run()
{
try {
if (rand(0, 1)) {

}
} catch (\RuntimeException $e) {
if (rand(0, 1)) {

}
} finally {

}

return true;
}
}

?>
13 changes: 13 additions & 0 deletions rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\Finally_;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
Expand Down Expand Up @@ -120,6 +124,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->shouldSkip($nextNode)) {
return null;
}

$endLine = $node->getEndLine();
$line = $nextNode->getLine();
$rangeLine = $line - $endLine;
Expand All @@ -133,4 +141,9 @@ public function refactor(Node $node): ?Node

return $node;
}

private function shouldSkip(Node $nextNode): bool
{
return $nextNode instanceof Else_ || $nextNode instanceof ElseIf_ || $nextNode instanceof Catch_ || $nextNode instanceof Finally_;
}
}

0 comments on commit 9fee4dd

Please sign in to comment.