Skip to content

Commit

Permalink
[VendorPatch] Register nikic-php-parser-lib-phpparser-node-stmt-names…
Browse files Browse the repository at this point in the history
…pace-php into vendor-patches list (#2825)

* [VendorPatch] Register nikic-php-parser-lib-phpparser-node-stmt-namespace-php into vendor-patches list

* clean up Namespace_ register

* more fixture

* Fixed 🎉

* clean up
  • Loading branch information
samsonasik committed Aug 24, 2022
1 parent feda30a commit 58edce7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-foreach-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-if-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-case-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-elseif-php.patch"
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-elseif-php.patch",
"https://raw.githubusercontent.com/rectorphp/vendor-patches/main/patches/nikic-php-parser-lib-phpparser-node-stmt-namespace-php.patch"
],
"symfony/dependency-injection": [
"https://raw.githubusercontent.com/symplify/vendor-patch-files/main/patches/generic-php-config-loader.patch",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

calledFunction();
return;

function calledFunction()
{
define("SOMETHING",true);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function refactor(Node $node): ?Node
}

$originalStmts = $node->stmts;
$cleanedStmts = $this->processCleanUpUnreachabelStmts($node->stmts);
$cleanedStmts = $this->processCleanUpUnreachabelStmts($node, $node->stmts);

if ($cleanedStmts === $originalStmts) {
return null;
Expand All @@ -82,7 +82,7 @@ public function refactor(Node $node): ?Node
* @param Stmt[] $stmts
* @return Stmt[]
*/
private function processCleanUpUnreachabelStmts(array $stmts): array
private function processCleanUpUnreachabelStmts(StmtsAwareInterface $stmtsAware, array $stmts): array
{
foreach ($stmts as $key => $stmt) {
if (! isset($stmts[$key - 1])) {
Expand All @@ -93,7 +93,7 @@ private function processCleanUpUnreachabelStmts(array $stmts): array

// unset...

if ($this->terminatedNodeAnalyzer->isAlwaysTerminated($previousStmt, $stmt)) {
if ($this->terminatedNodeAnalyzer->isAlwaysTerminated($stmtsAware, $previousStmt, $stmt)) {
array_splice($stmts, $key);
return $stmts;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Application/ChangedNodeScopeRefresher.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\TryCatch;
Expand Down Expand Up @@ -101,7 +100,7 @@ public function refresh(Node $node, ?MutatingScope $mutatingScope, ?SmartFileInf

private function reIndexNodeAttributes(Node $node): void
{
if (($node instanceof Namespace_ || $node instanceof ClassLike || $node instanceof StmtsAwareInterface) && $node->stmts !== null) {
if (($node instanceof ClassLike || $node instanceof StmtsAwareInterface) && $node->stmts !== null) {
$node->stmts = array_values($node->stmts);
}

Expand Down
11 changes: 10 additions & 1 deletion src/NodeAnalyzer/TerminatedNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@
use PhpParser\Node\Expr\Exit_;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Finally_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Goto_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\Node\Stmt\Label;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\Node\Stmt\TryCatch;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;

final class TerminatedNodeAnalyzer
{
Expand All @@ -45,12 +50,16 @@ final class TerminatedNodeAnalyzer
*/
private const ALLOWED_CONTINUE_CURRENT_STMTS = [InlineHTML::class, Nop::class];

public function isAlwaysTerminated(Stmt $node, Stmt $currentStmt): bool
public function isAlwaysTerminated(StmtsAwareInterface $stmtsAware, Stmt $node, Stmt $currentStmt): bool
{
if (in_array($currentStmt::class, self::ALLOWED_CONTINUE_CURRENT_STMTS, true)) {
return false;
}

if (($stmtsAware instanceof FileWithoutNamespace || $stmtsAware instanceof Namespace_) && ($currentStmt instanceof ClassLike || $currentStmt instanceof Function_)) {
return false;
}

if (! in_array($node::class, self::TERMINABLE_NODES_BY_ITS_STMTS, true)) {
return $this->isTerminatedNode($node, $currentStmt);
}
Expand Down

0 comments on commit 58edce7

Please sign in to comment.