Skip to content

Commit 7a96a27

Browse files
committed
Updated Rector to commit 6dcc12669e87748c5c11607bb41580c61477fd35
rectorphp/rector-src@6dcc126 [TypeDeclaration] Deprecate StrictStringParamConcatRector as producing too many false positives (#8090)
1 parent ff9faa5 commit 7a96a27

3 files changed

Lines changed: 7 additions & 137 deletions

File tree

rules/TypeDeclaration/Rector/ClassMethod/StrictStringParamConcatRector.php

Lines changed: 5 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,21 @@
44
namespace Rector\TypeDeclaration\Rector\ClassMethod;
55

66
use PhpParser\Node;
7-
use PhpParser\Node\Expr;
8-
use PhpParser\Node\Expr\Assign;
9-
use PhpParser\Node\Expr\AssignOp\Concat;
107
use PhpParser\Node\Expr\Closure;
11-
use PhpParser\Node\Expr\Variable;
12-
use PhpParser\Node\FunctionLike;
13-
use PhpParser\Node\Identifier;
14-
use PhpParser\Node\NullableType;
15-
use PhpParser\Node\Param;
16-
use PhpParser\Node\Stmt\Class_;
178
use PhpParser\Node\Stmt\ClassMethod;
189
use PhpParser\Node\Stmt\Function_;
19-
use PhpParser\NodeVisitor;
20-
use PHPStan\Type\MixedType;
21-
use PHPStan\Type\Type;
22-
use PHPStan\Type\TypeCombinator;
23-
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
10+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
11+
use Rector\Exception\ShouldNotHappenException;
2412
use Rector\Rector\AbstractRector;
2513
use Rector\ValueObject\PhpVersionFeature;
26-
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
2714
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
2815
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2916
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
3017
/**
31-
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector\StrictStringParamConcatRectorTest
18+
* @deprecated This rule is deprecated as it produces too many false positives.
3219
*/
33-
final class StrictStringParamConcatRector extends AbstractRector implements MinPhpVersionInterface
20+
final class StrictStringParamConcatRector extends AbstractRector implements MinPhpVersionInterface, DeprecatedInterface
3421
{
35-
/**
36-
* @readonly
37-
*/
38-
private ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard;
39-
/**
40-
* @readonly
41-
*/
42-
private PhpDocInfoFactory $phpDocInfoFactory;
43-
public function __construct(ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard, PhpDocInfoFactory $phpDocInfoFactory)
44-
{
45-
$this->parentClassMethodTypeOverrideGuard = $parentClassMethodTypeOverrideGuard;
46-
$this->phpDocInfoFactory = $phpDocInfoFactory;
47-
}
4822
public function getRuleDefinition(): RuleDefinition
4923
{
5024
return new RuleDefinition('Add string type based on concat use', [new CodeSample(<<<'CODE_SAMPLE'
@@ -83,108 +57,6 @@ public function provideMinPhpVersion(): int
8357
*/
8458
public function refactor(Node $node): ?Node
8559
{
86-
if ($node instanceof ClassMethod && $this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($node)) {
87-
return null;
88-
}
89-
$hasChanged = \false;
90-
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
91-
foreach ($node->getParams() as $param) {
92-
if ($param->type instanceof Node) {
93-
continue;
94-
}
95-
$variableConcattedFromParam = $this->resolveVariableConcattedFromParam($param, $node);
96-
if (!$variableConcattedFromParam instanceof Variable) {
97-
continue;
98-
}
99-
$paramDocType = $phpDocInfo->getParamType($this->getName($param));
100-
if (!$paramDocType instanceof MixedType && !$paramDocType->isString()->yes()) {
101-
continue;
102-
}
103-
$nativeType = $this->nodeTypeResolver->getNativeType($variableConcattedFromParam);
104-
if (!$nativeType instanceof MixedType) {
105-
continue;
106-
}
107-
$subtractedType = $nativeType->getSubtractedType();
108-
if (!$subtractedType instanceof Type) {
109-
$param->type = new Identifier('string');
110-
$hasChanged = \true;
111-
continue;
112-
}
113-
if (TypeCombinator::containsNull($subtractedType)) {
114-
$param->type = new NullableType(new Identifier('string'));
115-
$hasChanged = \true;
116-
}
117-
}
118-
if ($hasChanged) {
119-
return $node;
120-
}
121-
return null;
122-
}
123-
/**
124-
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
125-
*/
126-
private function resolveVariableConcattedFromParam(Param $param, $functionLike): ?Variable
127-
{
128-
if ($functionLike->stmts === null) {
129-
return null;
130-
}
131-
if ($param->default instanceof Expr && !$this->getType($param->default)->isString()->yes()) {
132-
return null;
133-
}
134-
$paramName = $this->getName($param);
135-
$variableConcatted = null;
136-
$this->traverseNodesWithCallable($functionLike->stmts, function (Node $node) use ($paramName, &$variableConcatted): ?int {
137-
// skip nested class and function nodes
138-
if ($node instanceof FunctionLike || $node instanceof Class_) {
139-
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
140-
}
141-
if ($node instanceof Assign && $node->var instanceof Variable && $this->isName($node->var, $paramName)) {
142-
$variableConcatted = null;
143-
return NodeVisitor::STOP_TRAVERSAL;
144-
}
145-
$expr = $this->resolveAssignConcatVariable($node, $paramName);
146-
if ($expr instanceof Variable) {
147-
$variableConcatted = $expr;
148-
}
149-
$variableBinaryConcat = $this->resolveBinaryConcatVariable($node, $paramName);
150-
if ($variableBinaryConcat instanceof Variable) {
151-
$variableConcatted = $variableBinaryConcat;
152-
}
153-
return null;
154-
});
155-
return $variableConcatted;
156-
}
157-
private function isVariableWithSameParam(Expr $expr, string $paramName): bool
158-
{
159-
if (!$expr instanceof Variable) {
160-
return \false;
161-
}
162-
return $this->isName($expr, $paramName);
163-
}
164-
private function resolveAssignConcatVariable(Node $node, string $paramName): ?Expr
165-
{
166-
if (!$node instanceof Concat) {
167-
return null;
168-
}
169-
if ($this->isVariableWithSameParam($node->var, $paramName)) {
170-
return $node->var;
171-
}
172-
if ($this->isVariableWithSameParam($node->expr, $paramName)) {
173-
return $node->expr;
174-
}
175-
return null;
176-
}
177-
private function resolveBinaryConcatVariable(Node $node, string $paramName): ?Expr
178-
{
179-
if (!$node instanceof Expr\BinaryOp\Concat) {
180-
return null;
181-
}
182-
if ($this->isVariableWithSameParam($node->left, $paramName)) {
183-
return $node->left;
184-
}
185-
if ($this->isVariableWithSameParam($node->right, $paramName)) {
186-
return $node->right;
187-
}
188-
return null;
60+
throw new ShouldNotHappenException(sprintf('"%s" is deprecated as it produces too many false positives. Add the type manually where needed instead', self::class));
18961
}
19062
}

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 = '26a142f6e5c0d4289b3d4a72fda8245cad6fa254';
22+
public const PACKAGE_VERSION = '6dcc12669e87748c5c11607bb41580c61477fd35';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2026-06-27 14:54:13';
27+
public const RELEASE_DATE = '2026-06-27 17:14:24';
2828
/**
2929
* @var int
3030
*/

src/Config/Level/TypeDeclarationLevel.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromSymfonySerializerRector;
5050
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;
5151
use Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector;
52-
use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector;
5352
use Rector\TypeDeclaration\Rector\ClassMethod\StringReturnTypeFromStrictScalarReturnsRector;
5453
use Rector\TypeDeclaration\Rector\ClassMethod\StringReturnTypeFromStrictStringReturnsRector;
5554
use Rector\TypeDeclaration\Rector\Closure\AddClosureNeverReturnTypeRector;
@@ -140,7 +139,6 @@ final class TypeDeclarationLevel
140139
AddReturnTypeDeclarationBasedOnParentClassMethodRector::class,
141140
ReturnTypeFromStrictFluentReturnRector::class,
142141
ReturnNeverTypeRector::class,
143-
StrictStringParamConcatRector::class,
144142
// jms attributes
145143
ObjectTypedPropertyFromJMSSerializerAttributeTypeRector::class,
146144
ScalarTypedPropertyFromJMSSerializerAttributeTypeRector::class,

0 commit comments

Comments
 (0)