Skip to content

Commit 5b48d9e

Browse files
committed
Updated Rector to commit bd510e6e343aaadd95a50e36fecfdbd349af2af8
rectorphp/rector-src@bd510e6 [fix] skip all-but standalone assign on SetTypeToCastRector (#7699)
1 parent 7e2963c commit 5b48d9e

File tree

14 files changed

+82
-77
lines changed

14 files changed

+82
-77
lines changed

rules/CodeQuality/Rector/FuncCall/SetTypeToCastRector.php

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
namespace Rector\CodeQuality\Rector\FuncCall;
55

66
use PhpParser\Node;
7-
use PhpParser\Node\Arg;
8-
use PhpParser\Node\ArrayItem;
9-
use PhpParser\Node\Expr;
107
use PhpParser\Node\Expr\Assign;
118
use PhpParser\Node\Expr\Cast;
129
use PhpParser\Node\Expr\Cast\Array_;
@@ -17,7 +14,6 @@
1714
use PhpParser\Node\Expr\Cast\String_;
1815
use PhpParser\Node\Expr\FuncCall;
1916
use PhpParser\Node\Stmt\Expression;
20-
use PhpParser\NodeVisitor;
2117
use Rector\PhpParser\Node\Value\ValueResolver;
2218
use Rector\Rector\AbstractRector;
2319
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -35,24 +31,18 @@ final class SetTypeToCastRector extends AbstractRector
3531
* @var array<string, class-string<Cast>>
3632
*/
3733
private const TYPE_TO_CAST = ['array' => Array_::class, 'bool' => Bool_::class, 'boolean' => Bool_::class, 'double' => Double::class, 'float' => Double::class, 'int' => Int_::class, 'integer' => Int_::class, 'object' => Object_::class, 'string' => String_::class];
38-
/**
39-
* @var string
40-
*/
41-
private const IS_ARG_VALUE_ITEM_SET_TYPE = 'is_arg_value_item_set_type';
4234
public function __construct(ValueResolver $valueResolver)
4335
{
4436
$this->valueResolver = $valueResolver;
4537
}
4638
public function getRuleDefinition(): RuleDefinition
4739
{
48-
return new RuleDefinition('Change `settype()` to `(type)` where possible', [new CodeSample(<<<'CODE_SAMPLE'
40+
return new RuleDefinition('Change `settype()` to `(type)` on standalone line. `settype()` returns always success/failure bool value', [new CodeSample(<<<'CODE_SAMPLE'
4941
class SomeClass
5042
{
5143
public function run($foo)
5244
{
5345
settype($foo, 'string');
54-
55-
return settype($foo, 'integer');
5646
}
5747
}
5848
CODE_SAMPLE
@@ -62,8 +52,6 @@ class SomeClass
6252
public function run($foo)
6353
{
6454
$foo = (string) $foo;
65-
66-
return (int) $foo;
6755
}
6856
}
6957
CODE_SAMPLE
@@ -74,78 +62,51 @@ public function run($foo)
7462
*/
7563
public function getNodeTypes(): array
7664
{
77-
return [FuncCall::class, Expression::class, Assign::class, ArrayItem::class, Arg::class];
65+
return [Expression::class];
7866
}
7967
/**
80-
* @param FuncCall|Expression|Assign|ArrayItem|Node\Arg $node
81-
* @return null|NodeVisitor::DONT_TRAVERSE_CHILDREN|Expression|Assign|Cast
68+
* @param Expression $node
8269
*/
83-
public function refactor(Node $node)
70+
public function refactor(Node $node): ?\PhpParser\Node\Stmt\Expression
8471
{
85-
if ($node instanceof Arg || $node instanceof ArrayItem) {
86-
if ($this->isSetTypeFuncCall($node->value)) {
87-
$node->value->setAttribute(self::IS_ARG_VALUE_ITEM_SET_TYPE, \true);
88-
}
72+
// skip expr that are not standalone line, as settype() returns success bool value
73+
// and cannot be casted
74+
if (!$node->expr instanceof FuncCall) {
8975
return null;
9076
}
91-
if ($node instanceof Assign) {
92-
if (!$this->isSetTypeFuncCall($node->expr)) {
93-
return null;
94-
}
95-
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
96-
}
97-
if ($node instanceof Expression) {
98-
if (!$node->expr instanceof FuncCall) {
99-
return null;
100-
}
101-
$assignOrCast = $this->refactorFuncCall($node->expr, \true);
102-
if (!$assignOrCast instanceof Expr) {
103-
return null;
104-
}
105-
return new Expression($assignOrCast);
77+
$assign = $this->refactorFuncCall($node->expr);
78+
if (!$assign instanceof Assign) {
79+
return null;
10680
}
107-
return $this->refactorFuncCall($node, \false);
81+
return new Expression($assign);
10882
}
109-
/**
110-
* @return \PhpParser\Node\Expr\Assign|null|\PhpParser\Node\Expr\Cast
111-
*/
112-
private function refactorFuncCall(FuncCall $funcCall, bool $isStandaloneExpression)
83+
private function refactorFuncCall(FuncCall $funcCall): ?\PhpParser\Node\Expr\Assign
11384
{
114-
if (!$this->isSetTypeFuncCall($funcCall)) {
85+
if (!$this->isName($funcCall, 'setType')) {
11586
return null;
11687
}
11788
if ($funcCall->isFirstClassCallable()) {
11889
return null;
11990
}
120-
if ($funcCall->getAttribute(self::IS_ARG_VALUE_ITEM_SET_TYPE) === \true) {
91+
if (count($funcCall->getArgs()) < 2) {
12192
return null;
12293
}
123-
$typeValue = $this->valueResolver->getValue($funcCall->getArgs()[1]->value);
94+
$secondArg = $funcCall->getArgs()[1];
95+
$typeValue = $this->valueResolver->getValue($secondArg->value);
12496
if (!is_string($typeValue)) {
12597
return null;
12698
}
12799
$typeValue = strtolower($typeValue);
128-
$variable = $funcCall->getArgs()[0]->value;
100+
$firstArg = $funcCall->getArgs()[0];
101+
$variable = $firstArg->value;
129102
if (isset(self::TYPE_TO_CAST[$typeValue])) {
130103
$castClass = self::TYPE_TO_CAST[$typeValue];
131104
$castNode = new $castClass($variable);
132-
if (!$isStandaloneExpression) {
133-
return $castNode;
134-
}
135-
// bare expression? → assign
136105
return new Assign($variable, $castNode);
137106
}
138107
if ($typeValue === 'null') {
139108
return new Assign($variable, $this->nodeFactory->createNull());
140109
}
141110
return null;
142111
}
143-
private function isSetTypeFuncCall(Expr $expr): bool
144-
{
145-
// skip assign of settype() calls
146-
if (!$expr instanceof FuncCall) {
147-
return \false;
148-
}
149-
return $this->isName($expr, 'settype');
150-
}
151112
}

src/Application/VersionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ final class VersionResolver
1919
* @api
2020
* @var string
2121
*/
22-
public const PACKAGE_VERSION = '4dee67d65a73ce92f0c626dce8b78a477108aecd';
22+
public const PACKAGE_VERSION = 'bd510e6e343aaadd95a50e36fecfdbd349af2af8';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2025-12-02 08:29:38';
27+
public const RELEASE_DATE = '2025-12-02 07:32:41';
2828
/**
2929
* @var int
3030
*/

src/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ArgNodeVisitor.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor;
55

66
use PhpParser\Node;
7-
use PhpParser\Node\Arg;
87
use PhpParser\Node\Expr\Array_;
98
use PhpParser\Node\Expr\ArrayDimFetch;
109
use PhpParser\Node\Expr\FuncCall;
@@ -22,11 +21,12 @@ public function enterNode(Node $node): ?Node
2221
if (!$node->name instanceof Name) {
2322
return null;
2423
}
24+
// has no args
25+
if ($node->isFirstClassCallable()) {
26+
return null;
27+
}
2528
$funcCallName = $node->name->toString();
26-
foreach ($node->args as $arg) {
27-
if (!$arg instanceof Arg) {
28-
continue;
29-
}
29+
foreach ($node->getArgs() as $arg) {
3030
if ($arg->value instanceof Array_) {
3131
$arg->value->setAttribute(AttributeKey::FROM_FUNC_CALL_NAME, $funcCallName);
3232
continue;

vendor/composer/autoload_classmap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2541,6 +2541,7 @@
25412541
'Rector\\Symfony\\Set\\SetProvider\\Symfony5SetProvider' => $vendorDir . '/rector/rector-symfony/src/Set/SetProvider/Symfony5SetProvider.php',
25422542
'Rector\\Symfony\\Set\\SetProvider\\Symfony6SetProvider' => $vendorDir . '/rector/rector-symfony/src/Set/SetProvider/Symfony6SetProvider.php',
25432543
'Rector\\Symfony\\Set\\SetProvider\\Symfony7SetProvider' => $vendorDir . '/rector/rector-symfony/src/Set/SetProvider/Symfony7SetProvider.php',
2544+
'Rector\\Symfony\\Set\\SetProvider\\Symfony8SetProvider' => $vendorDir . '/rector/rector-symfony/src/Set/SetProvider/Symfony8SetProvider.php',
25442545
'Rector\\Symfony\\Set\\SetProvider\\SymfonySetProvider' => $vendorDir . '/rector/rector-symfony/src/Set/SetProvider/SymfonySetProvider.php',
25452546
'Rector\\Symfony\\Set\\SetProvider\\TwigSetProvider' => $vendorDir . '/rector/rector-symfony/src/Set/SetProvider/TwigSetProvider.php',
25462547
'Rector\\Symfony\\Set\\SwiftMailerSetList' => $vendorDir . '/rector/rector-symfony/src/Set/SwiftMailerSetList.php',
@@ -2628,9 +2629,9 @@
26282629
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\GetFiltersToAsTwigFilterAttributeRector' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/GetFiltersToAsTwigFilterAttributeRector.php',
26292630
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\GetFunctionsToAsTwigFunctionAttributeRector' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/GetFunctionsToAsTwigFunctionAttributeRector.php',
26302631
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\InvokableCommandInputAttributeRector' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector.php',
2631-
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\RemoveEraseCredentialsRector' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/RemoveEraseCredentialsRector.php',
26322632
'Rector\\Symfony\\Symfony73\\ValueObject\\CommandArgument' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/ValueObject/CommandArgument.php',
26332633
'Rector\\Symfony\\Symfony73\\ValueObject\\CommandOption' => $vendorDir . '/rector/rector-symfony/rules/Symfony73/ValueObject/CommandOption.php',
2634+
'Rector\\Symfony\\Symfony80\\Rector\\Class_\\RemoveEraseCredentialsRector' => $vendorDir . '/rector/rector-symfony/rules/Symfony80/Rector/Class_/RemoveEraseCredentialsRector.php',
26342635
'Rector\\Symfony\\Twig134\\Rector\\Return_\\SimpleFunctionAndFilterRector' => $vendorDir . '/rector/rector-symfony/rules/Twig134/Rector/Return_/SimpleFunctionAndFilterRector.php',
26352636
'Rector\\Symfony\\TypeAnalyzer\\ArrayUnionResponseTypeAnalyzer' => $vendorDir . '/rector/rector-symfony/src/TypeAnalyzer/ArrayUnionResponseTypeAnalyzer.php',
26362637
'Rector\\Symfony\\TypeAnalyzer\\ContainerAwareAnalyzer' => $vendorDir . '/rector/rector-symfony/src/TypeAnalyzer/ContainerAwareAnalyzer.php',

vendor/composer/autoload_static.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2789,6 +2789,7 @@ class ComposerStaticInitd7faddeaafe22cf1903ddfcba3e7de0e
27892789
'Rector\\Symfony\\Set\\SetProvider\\Symfony5SetProvider' => __DIR__ . '/..' . '/rector/rector-symfony/src/Set/SetProvider/Symfony5SetProvider.php',
27902790
'Rector\\Symfony\\Set\\SetProvider\\Symfony6SetProvider' => __DIR__ . '/..' . '/rector/rector-symfony/src/Set/SetProvider/Symfony6SetProvider.php',
27912791
'Rector\\Symfony\\Set\\SetProvider\\Symfony7SetProvider' => __DIR__ . '/..' . '/rector/rector-symfony/src/Set/SetProvider/Symfony7SetProvider.php',
2792+
'Rector\\Symfony\\Set\\SetProvider\\Symfony8SetProvider' => __DIR__ . '/..' . '/rector/rector-symfony/src/Set/SetProvider/Symfony8SetProvider.php',
27922793
'Rector\\Symfony\\Set\\SetProvider\\SymfonySetProvider' => __DIR__ . '/..' . '/rector/rector-symfony/src/Set/SetProvider/SymfonySetProvider.php',
27932794
'Rector\\Symfony\\Set\\SetProvider\\TwigSetProvider' => __DIR__ . '/..' . '/rector/rector-symfony/src/Set/SetProvider/TwigSetProvider.php',
27942795
'Rector\\Symfony\\Set\\SwiftMailerSetList' => __DIR__ . '/..' . '/rector/rector-symfony/src/Set/SwiftMailerSetList.php',
@@ -2876,9 +2877,9 @@ class ComposerStaticInitd7faddeaafe22cf1903ddfcba3e7de0e
28762877
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\GetFiltersToAsTwigFilterAttributeRector' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/GetFiltersToAsTwigFilterAttributeRector.php',
28772878
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\GetFunctionsToAsTwigFunctionAttributeRector' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/GetFunctionsToAsTwigFunctionAttributeRector.php',
28782879
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\InvokableCommandInputAttributeRector' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/InvokableCommandInputAttributeRector.php',
2879-
'Rector\\Symfony\\Symfony73\\Rector\\Class_\\RemoveEraseCredentialsRector' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/Rector/Class_/RemoveEraseCredentialsRector.php',
28802880
'Rector\\Symfony\\Symfony73\\ValueObject\\CommandArgument' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/ValueObject/CommandArgument.php',
28812881
'Rector\\Symfony\\Symfony73\\ValueObject\\CommandOption' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony73/ValueObject/CommandOption.php',
2882+
'Rector\\Symfony\\Symfony80\\Rector\\Class_\\RemoveEraseCredentialsRector' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Symfony80/Rector/Class_/RemoveEraseCredentialsRector.php',
28822883
'Rector\\Symfony\\Twig134\\Rector\\Return_\\SimpleFunctionAndFilterRector' => __DIR__ . '/..' . '/rector/rector-symfony/rules/Twig134/Rector/Return_/SimpleFunctionAndFilterRector.php',
28832884
'Rector\\Symfony\\TypeAnalyzer\\ArrayUnionResponseTypeAnalyzer' => __DIR__ . '/..' . '/rector/rector-symfony/src/TypeAnalyzer/ArrayUnionResponseTypeAnalyzer.php',
28842885
'Rector\\Symfony\\TypeAnalyzer\\ContainerAwareAnalyzer' => __DIR__ . '/..' . '/rector/rector-symfony/src/TypeAnalyzer/ContainerAwareAnalyzer.php',

vendor/composer/installed.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,12 +1862,12 @@
18621862
"source": {
18631863
"type": "git",
18641864
"url": "https:\/\/github.com\/rectorphp\/rector-symfony.git",
1865-
"reference": "36488eda3cdd8ea517e5181aae07b3f18256a056"
1865+
"reference": "9775f3d8b38fc6bd129334c45d3dea52526e1fb4"
18661866
},
18671867
"dist": {
18681868
"type": "zip",
1869-
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/36488eda3cdd8ea517e5181aae07b3f18256a056",
1870-
"reference": "36488eda3cdd8ea517e5181aae07b3f18256a056",
1869+
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-symfony\/zipball\/9775f3d8b38fc6bd129334c45d3dea52526e1fb4",
1870+
"reference": "9775f3d8b38fc6bd129334c45d3dea52526e1fb4",
18711871
"shasum": ""
18721872
},
18731873
"require": {
@@ -1899,7 +1899,7 @@
18991899
"tomasvotruba\/unused-public": "^2.1",
19001900
"tracy\/tracy": "^2.11"
19011901
},
1902-
"time": "2025-12-01T17:53:23+00:00",
1902+
"time": "2025-12-02T07:30:06+00:00",
19031903
"default-branch": true,
19041904
"type": "rector-extension",
19051905
"extra": {

0 commit comments

Comments
 (0)