Updated Rector to commit a1cb33463a604a3b0e232ec423700ee1ff2cf60a

a1cb33463a [DeadCode] Add RemoveUselessAssignFromPropertyPromotionRector (#6643)
This commit is contained in:
Tomas Votruba 2025-01-04 12:25:26 +00:00
parent 79bfc87e90
commit 24b8fc7d89
11 changed files with 160 additions and 32 deletions

View File

@ -0,0 +1,105 @@
<?php
declare (strict_types=1);
namespace Rector\DeadCode\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUselessAssignFromPropertyPromotionRector\RemoveUselessAssignFromPropertyPromotionRectorTest
*/
final class RemoveUselessAssignFromPropertyPromotionRector extends AbstractRector
{
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Remove useless re-assign from property promotion', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(private \stdClass $std)
{
$this->std = $std;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(private \stdClass $std)
{
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node) : ?Node
{
if (!$this->isName($node, MethodName::CONSTRUCT)) {
return null;
}
if ($node->stmts === null || $node->stmts == []) {
return null;
}
$variableNames = [];
foreach ($node->params as $param) {
if (!$param->isPromoted()) {
continue;
}
// re-assign will cause error on the first place, no need to collect names
// on readonly property promotion
if ($param->isReadonly()) {
continue;
}
$variableNames[] = (string) $this->getName($param->var);
}
if ($variableNames === []) {
return null;
}
$removeStmtKeys = [];
foreach ($node->stmts as $key => $stmt) {
// has non direct expression with assign, skip
if (!$stmt instanceof Expression || !$stmt->expr instanceof Assign) {
return null;
}
/** @var Assign $assign */
$assign = $stmt->expr;
// has non property fetches assignments, skip
if (!$assign->var instanceof PropertyFetch) {
return null;
}
// collect first, ensure not removed too early on next non property fetch assignment
// which may have side effect
if ($assign->var->var instanceof Variable && $this->isName($assign->var->var, 'this') && $this->isNames($assign->var->name, $variableNames) && $assign->expr instanceof Variable && $this->isName($assign->expr, (string) $this->getName($assign->var->name))) {
$removeStmtKeys[] = $key;
continue;
}
// early return, if not all are property fetches from $this and its param
return null;
}
// empty data? nothing to remove
if ($removeStmtKeys === []) {
return null;
}
foreach ($removeStmtKeys as $removeStmtKey) {
unset($node->stmts[$removeStmtKey]);
}
return $node;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '66d93167af53e4116e73f6dc011f02ac23efbd6f';
public const PACKAGE_VERSION = 'a1cb33463a604a3b0e232ec423700ee1ff2cf60a';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2025-01-01 23:32:36';
public const RELEASE_DATE = '2025-01-04 13:22:49';
/**
* @var int
*/

View File

@ -20,6 +20,7 @@ use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodParameterRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPublicMethodParameterRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessAssignFromPropertyPromotionRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnExprInConstructRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
@ -85,6 +86,7 @@ final class DeadCodeLevel
RemoveTypedPropertyDeadInstanceOfRector::class,
TernaryToBooleanOrFalseToBooleanAndRector::class,
RemoveDoubleAssignRector::class,
RemoveUselessAssignFromPropertyPromotionRector::class,
RemoveConcatAutocastRector::class,
SimplifyIfElseWithSameContentRector::class,
SimplifyUselessVariableRector::class,

View File

@ -1316,6 +1316,7 @@ return array(
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedPrivateMethodRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedPromotedPropertyRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedPublicMethodParameterRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPublicMethodParameterRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUselessAssignFromPropertyPromotionRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUselessAssignFromPropertyPromotionRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUselessParamTagRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUselessParamTagRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUselessReturnExprInConstructRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnExprInConstructRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUselessReturnTagRector' => $baseDir . '/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnTagRector.php',

View File

@ -1535,6 +1535,7 @@ class ComposerStaticInit4fb50f2d4af661ecee2245f9ff725125
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedPrivateMethodRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPrivateMethodRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedPromotedPropertyRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUnusedPublicMethodParameterRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPublicMethodParameterRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUselessAssignFromPropertyPromotionRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUselessAssignFromPropertyPromotionRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUselessParamTagRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUselessParamTagRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUselessReturnExprInConstructRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnExprInConstructRector.php',
'Rector\\DeadCode\\Rector\\ClassMethod\\RemoveUselessReturnTagRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/ClassMethod/RemoveUselessReturnTagRector.php',

View File

@ -512,8 +512,8 @@
},
{
"name": "illuminate\/container",
"version": "v11.36.1",
"version_normalized": "11.36.1.0",
"version": "v11.37.0",
"version_normalized": "11.37.0.0",
"source": {
"type": "git",
"url": "https:\/\/github.com\/illuminate\/container.git",
@ -569,8 +569,8 @@
},
{
"name": "illuminate\/contracts",
"version": "v11.36.1",
"version_normalized": "11.36.1.0",
"version": "v11.37.0",
"version_normalized": "11.37.0.0",
"source": {
"type": "git",
"url": "https:\/\/github.com\/illuminate\/contracts.git",
@ -1157,36 +1157,36 @@
},
{
"name": "react\/child-process",
"version": "v0.6.5",
"version_normalized": "0.6.5.0",
"version": "v0.6.6",
"version_normalized": "0.6.6.0",
"source": {
"type": "git",
"url": "https:\/\/github.com\/reactphp\/child-process.git",
"reference": "e71eb1aa55f057c7a4a0d08d06b0b0a484bead43"
"reference": "1721e2b93d89b745664353b9cfc8f155ba8a6159"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/reactphp\/child-process\/zipball\/e71eb1aa55f057c7a4a0d08d06b0b0a484bead43",
"reference": "e71eb1aa55f057c7a4a0d08d06b0b0a484bead43",
"url": "https:\/\/api.github.com\/repos\/reactphp\/child-process\/zipball\/1721e2b93d89b745664353b9cfc8f155ba8a6159",
"reference": "1721e2b93d89b745664353b9cfc8f155ba8a6159",
"shasum": ""
},
"require": {
"evenement\/evenement": "^3.0 || ^2.0 || ^1.0",
"php": ">=5.3.0",
"react\/event-loop": "^1.2",
"react\/stream": "^1.2"
"react\/stream": "^1.4"
},
"require-dev": {
"phpunit\/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react\/socket": "^1.8",
"phpunit\/phpunit": "^9.6 || ^5.7 || ^4.8.36",
"react\/socket": "^1.16",
"sebastian\/environment": "^5.0 || ^3.0 || ^2.0 || ^1.0"
},
"time": "2022-09-16T13:41:56+00:00",
"time": "2025-01-01T16:37:48+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
"psr-4": {
"RectorPrefix202501\\React\\ChildProcess\\": "src"
"RectorPrefix202501\\React\\ChildProcess\\": "src\/"
}
},
"notification-url": "https:\/\/packagist.org\/downloads\/",
@ -1223,16 +1223,12 @@
],
"support": {
"issues": "https:\/\/github.com\/reactphp\/child-process\/issues",
"source": "https:\/\/github.com\/reactphp\/child-process\/tree\/v0.6.5"
"source": "https:\/\/github.com\/reactphp\/child-process\/tree\/v0.6.6"
},
"funding": [
{
"url": "https:\/\/github.com\/WyriHaximus",
"type": "github"
},
{
"url": "https:\/\/github.com\/clue",
"type": "github"
"url": "https:\/\/opencollective.com\/reactphp",
"type": "open_collective"
}
],
"install-path": "..\/react\/child-process"

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,16 @@
# Changelog
## 0.6.6 (2025-01-01)
This is a compatibility release that contains backported features from the `0.7.x` branch.
Once v0.7 is released, it will be the way forward for this project.
* Feature: Improve PHP 8.4+ support by avoiding implicitly nullable types.
(#114 by @clue)
* Improve test suite to run tests on latest PHP versions and report failed assertions.
(#113 by @clue)
## 0.6.5 (2022-09-16)
* Feature: Full support for PHP 8.1 and PHP 8.2 release.

View File

@ -588,7 +588,7 @@ The recommended way to install this library is [through Composer](https://getcom
This will install the latest supported version:
```bash
composer require react/child-process:^0.6.5
composer require react/child-process:^0.6.6
```
See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.

View File

@ -33,21 +33,21 @@
"php": ">=5.3.0",
"evenement\/evenement": "^3.0 || ^2.0 || ^1.0",
"react\/event-loop": "^1.2",
"react\/stream": "^1.2"
"react\/stream": "^1.4"
},
"require-dev": {
"phpunit\/phpunit": "^9.3 || ^5.7 || ^4.8.35",
"react\/socket": "^1.8",
"phpunit\/phpunit": "^9.6 || ^5.7 || ^4.8.36",
"react\/socket": "^1.16",
"sebastian\/environment": "^5.0 || ^3.0 || ^2.0 || ^1.0"
},
"autoload": {
"psr-4": {
"RectorPrefix202501\\React\\ChildProcess\\": "src"
"RectorPrefix202501\\React\\ChildProcess\\": "src\/"
}
},
"autoload-dev": {
"psr-4": {
"RectorPrefix202501\\React\\Tests\\ChildProcess\\": "tests"
"RectorPrefix202501\\React\\Tests\\ChildProcess\\": "tests\/"
}
}
}

View File

@ -103,8 +103,16 @@ class Process extends EventEmitter
* @param null|array $fds File descriptors to allocate for this process (or null = default STDIO streams)
* @throws \LogicException On windows or when proc_open() is not installed
*/
public function __construct($cmd, $cwd = null, array $env = null, array $fds = null)
public function __construct($cmd, $cwd = null, $env = null, $fds = null)
{
if ($env !== null && !\is_array($env)) {
// manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #3 ($env) expected null|array');
}
if ($fds !== null && !\is_array($fds)) {
// manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #4 ($fds) expected null|array');
}
if (!\function_exists('proc_open')) {
throw new \LogicException('The Process class relies on proc_open(), which is not available on your PHP installation.');
}
@ -151,8 +159,12 @@ class Process extends EventEmitter
* @param float $interval Interval to periodically monitor process state (seconds)
* @throws \RuntimeException If the process is already running or fails to start
*/
public function start(LoopInterface $loop = null, $interval = 0.1)
public function start($loop = null, $interval = 0.1)
{
if ($loop !== null && !$loop instanceof LoopInterface) {
// manual type check to support legacy PHP < 7.1
throw new \InvalidArgumentException('Argument #1 ($loop) expected null|React\\EventLoop\\LoopInterface');
}
if ($this->isRunning()) {
throw new \RuntimeException('Process is already running');
}