Skip to content

Commit 935fcff

Browse files
committed
Updated Rector to commit e7381c2a1031f57a7e80eacf9884033c07d8c4a4
rectorphp/rector-src@e7381c2 [type-declaration] Add AddParamStringTypeFromSprintfUseRector (#7477)
1 parent d168fc0 commit 935fcff

8 files changed

Lines changed: 168 additions & 75 deletions

File tree

rules/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

rules/Privatization/TypeManipulator/TypeNormalizer.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,6 @@ public function __construct(TypeFactory $typeFactory, StaticTypeMapper $staticTy
5353
$this->arrayTypeLeastCommonDenominatorResolver = $arrayTypeLeastCommonDenominatorResolver;
5454
$this->typeHasher = $typeHasher;
5555
}
56-
/**
57-
* @deprecated This method is deprecated and will be removed in the next major release.
58-
* Use @see generalizeConstantTypes() instead.
59-
*/
60-
public function generalizeConstantBoolTypes(Type $type): Type
61-
{
62-
return $this->generalizeConstantTypes($type);
63-
}
6456
/**
6557
* Generalize false/true constantArrayType to bool,
6658
* as mostly default value but accepts both
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\TypeDeclaration\NodeAnalyzer;
5+
6+
use RectorPrefix202510\Nette\Utils\Strings;
7+
use PhpParser\Node\Arg;
8+
use PhpParser\Node\Expr\FuncCall;
9+
use PhpParser\Node\Expr\Variable;
10+
use PhpParser\Node\Scalar\String_;
11+
use PhpParser\Node\Stmt\ClassMethod;
12+
use PhpParser\Node\Stmt\Function_;
13+
use Rector\NodeNameResolver\NodeNameResolver;
14+
use Rector\PhpParser\Node\BetterNodeFinder;
15+
final class VariableInSprintfMaskMatcher
16+
{
17+
/**
18+
* @readonly
19+
*/
20+
private BetterNodeFinder $betterNodeFinder;
21+
/**
22+
* @readonly
23+
*/
24+
private NodeNameResolver $nodeNameResolver;
25+
public function __construct(BetterNodeFinder $betterNodeFinder, NodeNameResolver $nodeNameResolver)
26+
{
27+
$this->betterNodeFinder = $betterNodeFinder;
28+
$this->nodeNameResolver = $nodeNameResolver;
29+
}
30+
/**
31+
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike
32+
*/
33+
public function matchMask($functionLike, string $variableName, string $mask): bool
34+
{
35+
$funcCalls = $this->betterNodeFinder->findInstancesOfScoped((array) $functionLike->stmts, FuncCall::class);
36+
foreach ($funcCalls as $funcCall) {
37+
if (!$this->nodeNameResolver->isName($funcCall->name, 'sprintf')) {
38+
continue;
39+
}
40+
if ($funcCall->isFirstClassCallable()) {
41+
continue;
42+
}
43+
$args = $funcCall->getArgs();
44+
if (count($args) < 2) {
45+
continue;
46+
}
47+
/** @var Arg $messageArg */
48+
$messageArg = array_shift($args);
49+
if (!$messageArg->value instanceof String_) {
50+
continue;
51+
}
52+
$string = $messageArg->value;
53+
// match all %s, %d types by position
54+
$masks = Strings::match($string->value, '#%[sd]#');
55+
foreach ($args as $position => $arg) {
56+
if (!$arg->value instanceof Variable) {
57+
continue;
58+
}
59+
if (!$this->nodeNameResolver->isName($arg->value, $variableName)) {
60+
continue;
61+
}
62+
if (!isset($masks[$position])) {
63+
continue;
64+
}
65+
$knownMaskOnPosition = $masks[$position];
66+
if ($knownMaskOnPosition !== $mask) {
67+
continue;
68+
}
69+
return \true;
70+
}
71+
}
72+
return \false;
73+
}
74+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\TypeDeclaration\Rector\ClassMethod;
5+
6+
use PhpParser\Node\Identifier;
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\ClassMethod;
9+
use PhpParser\Node\Stmt\Function_;
10+
use Rector\Rector\AbstractRector;
11+
use Rector\TypeDeclaration\NodeAnalyzer\VariableInSprintfMaskMatcher;
12+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
13+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
14+
/**
15+
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector\AddParamStringTypeFromSprintfUseRectorTest
16+
*/
17+
final class AddParamStringTypeFromSprintfUseRector extends AbstractRector
18+
{
19+
/**
20+
* @readonly
21+
*/
22+
private VariableInSprintfMaskMatcher $variableInSprintfMaskMatcher;
23+
public function __construct(VariableInSprintfMaskMatcher $variableInSprintfMaskMatcher)
24+
{
25+
$this->variableInSprintfMaskMatcher = $variableInSprintfMaskMatcher;
26+
}
27+
public function getRuleDefinition(): RuleDefinition
28+
{
29+
return new RuleDefinition('Add string type to parameters used in sprintf calls', [new CodeSample(<<<'CODE_SAMPLE'
30+
class SomeClass
31+
{
32+
public function formatMessage($name)
33+
{
34+
return sprintf('My name is %s', $name);
35+
}
36+
}
37+
CODE_SAMPLE
38+
, <<<'CODE_SAMPLE'
39+
class SomeClass
40+
{
41+
public function formatMessage(string $name)
42+
{
43+
return sprintf('My name is %s', $name);
44+
}
45+
}
46+
CODE_SAMPLE
47+
)]);
48+
}
49+
/**
50+
* @return array<class-string<Node>>
51+
*/
52+
public function getNodeTypes(): array
53+
{
54+
return [ClassMethod::class, Function_::class];
55+
}
56+
/**
57+
* @param ClassMethod|Function_ $node
58+
* @return \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|null
59+
*/
60+
public function refactor(Node $node)
61+
{
62+
if ($node->stmts === null) {
63+
return null;
64+
}
65+
if ($node->getParams() === []) {
66+
return null;
67+
}
68+
$hasChanged = \false;
69+
foreach ($node->getParams() as $param) {
70+
if ($param->type instanceof Node) {
71+
continue;
72+
}
73+
/** @var string $variableName */
74+
$variableName = $this->getName($param->var);
75+
if (!$this->variableInSprintfMaskMatcher->matchMask($node, $variableName, '%s')) {
76+
continue;
77+
}
78+
$param->type = new Identifier('string');
79+
$hasChanged = \true;
80+
}
81+
if (!$hasChanged) {
82+
return null;
83+
}
84+
return $node;
85+
}
86+
}

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 = '08c953e98fd3a00e77caeefebb09daa4f5b9127f';
22+
public const PACKAGE_VERSION = 'e7381c2a1031f57a7e80eacf9884033c07d8c4a4';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2025-10-11 22:23:37';
27+
public const RELEASE_DATE = '2025-10-11 19:53:22';
2828
/**
2929
* @var int
3030
*/

src/Config/Level/TypeDeclarationLevel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Rector\TypeDeclaration\Rector\Class_\TypedPropertyFromJMSSerializerAttributeTypeRector;
1717
use Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector;
1818
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamFromDimFetchKeyUseRector;
19+
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamStringTypeFromSprintfUseRector;
1920
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeBasedOnPHPUnitDataProviderRector;
2021
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeFromPropertyTypeRector;
2122
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationBasedOnParentClassMethodRector;
@@ -131,6 +132,7 @@ final class TypeDeclarationLevel
131132
// array parameter from dim fetch assign inside
132133
StrictArrayParamDimFetchRector::class,
133134
AddParamFromDimFetchKeyUseRector::class,
135+
AddParamStringTypeFromSprintfUseRector::class,
134136
// possibly based on docblocks, but also helpful, intentionally last
135137
AddArrayFunctionClosureParamTypeRector::class,
136138
TypedPropertyFromDocblockSetUpDefinedRector::class,

vendor/composer/autoload_classmap.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,6 @@
11681168
'Rector\\CodeQuality\\Rector\\Class_\\DynamicDocBlockPropertyToNativePropertyRector' => $baseDir . '/rules/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector.php',
11691169
'Rector\\CodeQuality\\Rector\\Class_\\InlineConstructorDefaultToPropertyRector' => $baseDir . '/rules/CodeQuality/Rector/Class_/InlineConstructorDefaultToPropertyRector.php',
11701170
'Rector\\CodeQuality\\Rector\\Class_\\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector' => $baseDir . '/rules/CodeQuality/Rector/Class_/RemoveReadonlyPropertyVisibilityOnReadonlyClassRector.php',
1171-
'Rector\\CodeQuality\\Rector\\Class_\\StaticToSelfStaticMethodCallOnFinalClassRector' => $baseDir . '/rules/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector.php',
11721171
'Rector\\CodeQuality\\Rector\\Concat\\JoinStringConcatRector' => $baseDir . '/rules/CodeQuality/Rector/Concat/JoinStringConcatRector.php',
11731172
'Rector\\CodeQuality\\Rector\\Empty_\\SimplifyEmptyCheckOnEmptyArrayRector' => $baseDir . '/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php',
11741173
'Rector\\CodeQuality\\Rector\\Equal\\UseIdenticalOverEqualWithSameTypeRector' => $baseDir . '/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php',
@@ -2729,6 +2728,7 @@
27292728
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnTypeAnalyzer\\StrictNativeFunctionReturnTypeAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictNativeFunctionReturnTypeAnalyzer.php',
27302729
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnTypeAnalyzer\\StrictReturnNewAnalyzer' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictReturnNewAnalyzer.php',
27312730
'Rector\\TypeDeclaration\\NodeAnalyzer\\TypeNodeUnwrapper' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/TypeNodeUnwrapper.php',
2731+
'Rector\\TypeDeclaration\\NodeAnalyzer\\VariableInSprintfMaskMatcher' => $baseDir . '/rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php',
27322732
'Rector\\TypeDeclaration\\NodeManipulator\\AddNeverReturnType' => $baseDir . '/rules/TypeDeclaration/NodeManipulator/AddNeverReturnType.php',
27332733
'Rector\\TypeDeclaration\\NodeManipulator\\AddReturnTypeFromCast' => $baseDir . '/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromCast.php',
27342734
'Rector\\TypeDeclaration\\NodeManipulator\\AddReturnTypeFromParam' => $baseDir . '/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromParam.php',
@@ -2744,6 +2744,7 @@
27442744
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddMethodCallBasedStrictParamTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector.php',
27452745
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamArrayDocblockBasedOnCallableNativeFuncCallRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector.php',
27462746
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamFromDimFetchKeyUseRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector.php',
2747+
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamStringTypeFromSprintfUseRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector.php',
27472748
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeBasedOnPHPUnitDataProviderRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeBasedOnPHPUnitDataProviderRector.php',
27482749
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php',
27492750
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeFromPropertyTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeFromPropertyTypeRector.php',

vendor/composer/autoload_static.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,6 @@ class ComposerStaticInit100ccc9176c32b7be45ab2960a573d86
14161416
'Rector\\CodeQuality\\Rector\\Class_\\DynamicDocBlockPropertyToNativePropertyRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Class_/DynamicDocBlockPropertyToNativePropertyRector.php',
14171417
'Rector\\CodeQuality\\Rector\\Class_\\InlineConstructorDefaultToPropertyRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Class_/InlineConstructorDefaultToPropertyRector.php',
14181418
'Rector\\CodeQuality\\Rector\\Class_\\RemoveReadonlyPropertyVisibilityOnReadonlyClassRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Class_/RemoveReadonlyPropertyVisibilityOnReadonlyClassRector.php',
1419-
'Rector\\CodeQuality\\Rector\\Class_\\StaticToSelfStaticMethodCallOnFinalClassRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Class_/StaticToSelfStaticMethodCallOnFinalClassRector.php',
14201419
'Rector\\CodeQuality\\Rector\\Concat\\JoinStringConcatRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Concat/JoinStringConcatRector.php',
14211420
'Rector\\CodeQuality\\Rector\\Empty_\\SimplifyEmptyCheckOnEmptyArrayRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Empty_/SimplifyEmptyCheckOnEmptyArrayRector.php',
14221421
'Rector\\CodeQuality\\Rector\\Equal\\UseIdenticalOverEqualWithSameTypeRector' => __DIR__ . '/../..' . '/rules/CodeQuality/Rector/Equal/UseIdenticalOverEqualWithSameTypeRector.php',
@@ -2977,6 +2976,7 @@ class ComposerStaticInit100ccc9176c32b7be45ab2960a573d86
29772976
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnTypeAnalyzer\\StrictNativeFunctionReturnTypeAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictNativeFunctionReturnTypeAnalyzer.php',
29782977
'Rector\\TypeDeclaration\\NodeAnalyzer\\ReturnTypeAnalyzer\\StrictReturnNewAnalyzer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/ReturnTypeAnalyzer/StrictReturnNewAnalyzer.php',
29792978
'Rector\\TypeDeclaration\\NodeAnalyzer\\TypeNodeUnwrapper' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/TypeNodeUnwrapper.php',
2979+
'Rector\\TypeDeclaration\\NodeAnalyzer\\VariableInSprintfMaskMatcher' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeAnalyzer/VariableInSprintfMaskMatcher.php',
29802980
'Rector\\TypeDeclaration\\NodeManipulator\\AddNeverReturnType' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeManipulator/AddNeverReturnType.php',
29812981
'Rector\\TypeDeclaration\\NodeManipulator\\AddReturnTypeFromCast' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromCast.php',
29822982
'Rector\\TypeDeclaration\\NodeManipulator\\AddReturnTypeFromParam' => __DIR__ . '/../..' . '/rules/TypeDeclaration/NodeManipulator/AddReturnTypeFromParam.php',
@@ -2992,6 +2992,7 @@ class ComposerStaticInit100ccc9176c32b7be45ab2960a573d86
29922992
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddMethodCallBasedStrictParamTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddMethodCallBasedStrictParamTypeRector.php',
29932993
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamArrayDocblockBasedOnCallableNativeFuncCallRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamArrayDocblockBasedOnCallableNativeFuncCallRector.php',
29942994
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamFromDimFetchKeyUseRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamFromDimFetchKeyUseRector.php',
2995+
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamStringTypeFromSprintfUseRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamStringTypeFromSprintfUseRector.php',
29952996
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeBasedOnPHPUnitDataProviderRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeBasedOnPHPUnitDataProviderRector.php',
29962997
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeDeclarationRector.php',
29972998
'Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddParamTypeFromPropertyTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/AddParamTypeFromPropertyTypeRector.php',

0 commit comments

Comments
 (0)