Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHPStan ^2.1.1] Handle next unreachable statement via UnreachableStatementNode->getNextUnreachableStatements() #6642

Merged
merged 16 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE,
\Rector\NodeTypeResolver\Node\AttributeKey::REPRINT_RAW_VALUE,
\Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::IS_UNREACHABLE,
\Rector\NodeTypeResolver\Node\AttributeKey::PHP_DOC_INFO,
\Rector\NodeTypeResolver\Node\AttributeKey::KIND,
\Rector\NodeTypeResolver\Node\AttributeKey::IS_REGULAR_PATTERN,
Expand All @@ -64,7 +63,6 @@
\Rector\NodeTypeResolver\Node\AttributeKey::SCOPE,
\Rector\NodeTypeResolver\Node\AttributeKey::REPRINT_RAW_VALUE,
\Rector\NodeTypeResolver\Node\AttributeKey::ORIGINAL_NODE,
\Rector\NodeTypeResolver\Node\AttributeKey::IS_UNREACHABLE,
\Rector\NodeTypeResolver\Node\AttributeKey::PHP_DOC_INFO,
\Rector\NodeTypeResolver\Node\AttributeKey::KIND,
\Rector\NodeTypeResolver\Node\AttributeKey::IS_REGULAR_PATTERN,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

if (true) {
return;
}

echo 'test';

class FixtureUnreachableA
{
public function run($order, $oldOrder)
{
return $order || $oldOrder ? true : false;
}
}

$order || $oldOrder ? true : false;

class FixtureUnreachableB
{
public function run($order, $oldOrder)
{
return $order || $oldOrder ? true : false;
}
}

$order || $oldOrder ? true : false;

?>
-----
<?php

if (true) {
return;
}

echo 'test';

class FixtureUnreachableA
{
public function run($order, $oldOrder)
{
return $order || $oldOrder;
}
}

$order || $oldOrder;

class FixtureUnreachableB
{
public function run($order, $oldOrder)
{
return $order || $oldOrder;
}
}

$order || $oldOrder;

?>
5 changes: 0 additions & 5 deletions src/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ final class AttributeKey
*/
public const KIND = 'kind';

/**
* @var string
*/
public const IS_UNREACHABLE = 'isUnreachable';

/**
* @var string
*/
Expand Down
28 changes: 12 additions & 16 deletions src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PHPStan\NodeVisitor\UnreachableStatementNodeVisitor;
use Rector\Util\Reflection\PrivatesAccessor;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -161,11 +160,9 @@ public function processNodes(

$scope = $formerMutatingScope ?? $this->scopeFactory->createFromFile($filePath);

$hasUnreachableStatementNode = false;
$nodeCallback = function (Node $node, MutatingScope $mutatingScope) use (
&$nodeCallback,
$filePath,
&$hasUnreachableStatementNode
): void {
// the class reflection is resolved AFTER entering to class node
// so we need to get it from the first after this one
Expand All @@ -187,8 +184,7 @@ public function processNodes(
// early check here as UnreachableStatementNode is special VirtualNode
// so node to be checked inside
if ($node instanceof UnreachableStatementNode) {
$this->processUnreachableStatementNode($node, $filePath, $mutatingScope);
$hasUnreachableStatementNode = true;
$this->processUnreachableStatementNode($node, $mutatingScope, $nodeCallback);
return;
}

Expand Down Expand Up @@ -410,12 +406,7 @@ public function processNodes(
RectorNodeScopeResolver::processNodes($stmts, $scope);
}

if (! $hasUnreachableStatementNode) {
return $stmts;
}

$nodeTraverser = new NodeTraverser(new UnreachableStatementNodeVisitor($this, $filePath, $scope));
return $nodeTraverser->traverse($stmts);
return $stmts;
}

private function processYield(Yield_ $yield, MutatingScope $mutatingScope): void
Expand Down Expand Up @@ -582,16 +573,21 @@ private function processTryCatch(TryCatch $tryCatch, MutatingScope $mutatingScop
}
}

/**
* @param callable(Node $node, MutatingScope $scope): void $nodeCallback
*/
private function processUnreachableStatementNode(
UnreachableStatementNode $unreachableStatementNode,
string $filePath,
MutatingScope $mutatingScope
MutatingScope $mutatingScope,
callable $nodeCallback
): void {
$originalStmt = $unreachableStatementNode->getOriginalStatement();
$originalStmt->setAttribute(AttributeKey::IS_UNREACHABLE, true);
$originalStmt->setAttribute(AttributeKey::SCOPE, $mutatingScope);

$this->processNodes([$originalStmt], $filePath, $mutatingScope);
$this->nodeScopeResolverProcessNodes(
array_merge([$originalStmt], $unreachableStatementNode->getNextStatements()),
$mutatingScope,
$nodeCallback
);
}

/**
Expand Down
64 changes: 0 additions & 64 deletions src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ class Fixture
{
$user = $_REQUEST['param'];

// add JSON_THROW_ON_ERROR on NON-exact value
$user = json_decode($user);

$condition = true;
if ($condition) {
$output = [];
// skip add JSON_THROW_ON_ERROR on exact value
echo(json_encode($output));
exit;
}

$output = [];
// skip add JSON_THROW_ON_ERROR on exact value
echo(json_encode($output));
}
}
Expand All @@ -34,17 +37,20 @@ class Fixture
{
$user = $_REQUEST['param'];

// add JSON_THROW_ON_ERROR on NON-exact value
$user = json_decode($user, null, 512, JSON_THROW_ON_ERROR);

$condition = true;
if ($condition) {
$output = [];
// skip add JSON_THROW_ON_ERROR on exact value
echo(json_encode($output));
exit;
}

$output = [];
echo(json_encode($output, JSON_THROW_ON_ERROR));
// skip add JSON_THROW_ON_ERROR on exact value
echo(json_encode($output));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rector\Tests\Issues\ScopeNotAvailable\FixtureJsonThrowCaseSensitiveConstFetch;

class NonExactValueAfterUnreachableStmts
{
/**
* @param 'foo' $foo
*/
public static function run($foo)
{
if ($foo === 'foo') {
return;
}

echo 'some statement 1';
echo 'some statement 2';
echo(json_encode($foo));
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\ScopeNotAvailable\FixtureJsonThrowCaseSensitiveConstFetch;

class NonExactValueAfterUnreachableStmts
{
/**
* @param 'foo' $foo
*/
public static function run($foo)
{
if ($foo === 'foo') {
return;
}

echo 'some statement 1';
echo 'some statement 2';
echo(json_encode($foo, JSON_THROW_ON_ERROR));
}
}

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

namespace Rector\Tests\Issues\ScopeNotAvailable\FixtureJsonThrowCaseSensitiveConstFetch;

class NonExactValueAfterUnreachableStmts
{
/**
* @param 'foo' $foo
*/
public static function run($foo)
{
if ($foo === 'foo') {
return;
}

echo 'some statement 1';
echo 'some statement 2';

function foo($foo) {
echo(json_encode($foo));
}
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\ScopeNotAvailable\FixtureJsonThrowCaseSensitiveConstFetch;

class NonExactValueAfterUnreachableStmts
{
/**
* @param 'foo' $foo
*/
public static function run($foo)
{
if ($foo === 'foo') {
return;
}

echo 'some statement 1';
echo 'some statement 2';

function foo($foo) {
echo(json_encode($foo, JSON_THROW_ON_ERROR));
}
}
}

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

namespace Rector\Tests\Issues\ScopeNotAvailable\FixtureJsonThrowCaseSensitiveConstFetch;

if (true) {
return;
}

echo 'some statement 1';
echo 'some statement 2';

function foo($foo) {
echo(json_encode($foo));
}

?>
-----
<?php

namespace Rector\Tests\Issues\ScopeNotAvailable\FixtureJsonThrowCaseSensitiveConstFetch;

if (true) {
return;
}

echo 'some statement 1';
echo 'some statement 2';

function foo($foo) {
echo(json_encode($foo, JSON_THROW_ON_ERROR));
}

?>
Loading