mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-29 11:33:33 +01:00
Updated Rector to commit 18a8aecd13cbe87766c38de95dcba08673493d3a
18a8aecd13
[TypeDeclaration] Adds AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector rule (#5547)
This commit is contained in:
parent
08cec1591e
commit
5c2a81061c
@ -56,7 +56,7 @@
|
||||
|
||||
- [Transform](#transform) (23)
|
||||
|
||||
- [TypeDeclaration](#typedeclaration) (43)
|
||||
- [TypeDeclaration](#typedeclaration) (44)
|
||||
|
||||
- [Visibility](#visibility) (3)
|
||||
|
||||
@ -6406,6 +6406,21 @@ Add param types where needed
|
||||
|
||||
<br>
|
||||
|
||||
### AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector
|
||||
|
||||
Add param types where needed
|
||||
|
||||
:wrench: **configure it!**
|
||||
|
||||
- class: [`Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector`](../rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector.php)
|
||||
|
||||
```diff
|
||||
-(new SomeClass)->process(function ($parameter) {});
|
||||
+(new SomeClass)->process(function (string $parameter) {});
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### AddParamTypeFromPropertyTypeRector
|
||||
|
||||
Adds param type declaration based on property type the value is assigned to PHPUnit provider return type declaration
|
||||
|
@ -0,0 +1,186 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\Rector\FunctionLike;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\CallLike;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Param;
|
||||
use PHPStan\Type\MixedType;
|
||||
use PHPStan\Type\StringType;
|
||||
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
|
||||
use Rector\Php\PhpVersionProvider;
|
||||
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\StaticTypeMapper\StaticTypeMapper;
|
||||
use Rector\TypeDeclaration\ValueObject\AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration;
|
||||
use Rector\ValueObject\PhpVersionFeature;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use RectorPrefix202402\Webmozart\Assert\Assert;
|
||||
/**
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRectorTest
|
||||
*/
|
||||
final class AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector extends AbstractRector implements ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator
|
||||
*/
|
||||
private $typeComparator;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Php\PhpVersionProvider
|
||||
*/
|
||||
private $phpVersionProvider;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
||||
*/
|
||||
private $staticTypeMapper;
|
||||
/**
|
||||
* @var AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration[]
|
||||
*/
|
||||
private $addParamTypeForFunctionLikeParamDeclarations = [];
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $hasChanged = \false;
|
||||
public function __construct(TypeComparator $typeComparator, PhpVersionProvider $phpVersionProvider, StaticTypeMapper $staticTypeMapper)
|
||||
{
|
||||
$this->typeComparator = $typeComparator;
|
||||
$this->phpVersionProvider = $phpVersionProvider;
|
||||
$this->staticTypeMapper = $staticTypeMapper;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add param types where needed', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
||||
(new SomeClass)->process(function ($parameter) {});
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
(new SomeClass)->process(function (string $parameter) {});
|
||||
CODE_SAMPLE
|
||||
, [new AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration('SomeClass', 'process', 0, 0, new StringType())])]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [MethodCall::class, StaticCall::class];
|
||||
}
|
||||
/**
|
||||
* @param CallLike $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
$this->hasChanged = \false;
|
||||
foreach ($this->addParamTypeForFunctionLikeParamDeclarations as $addParamTypeForFunctionLikeParamDeclaration) {
|
||||
switch (\true) {
|
||||
case $node instanceof MethodCall:
|
||||
$type = $node->var;
|
||||
break;
|
||||
case $node instanceof StaticCall:
|
||||
$type = $node->class;
|
||||
break;
|
||||
default:
|
||||
$type = null;
|
||||
break;
|
||||
}
|
||||
if ($type === null) {
|
||||
continue;
|
||||
}
|
||||
if (!$this->isObjectType($type, $addParamTypeForFunctionLikeParamDeclaration->getObjectType())) {
|
||||
continue;
|
||||
}
|
||||
if (($node->name ?? null) === null) {
|
||||
continue;
|
||||
}
|
||||
if (!$node->name instanceof Identifier) {
|
||||
continue;
|
||||
}
|
||||
if (!$this->isName($node->name, $addParamTypeForFunctionLikeParamDeclaration->getMethodName())) {
|
||||
continue;
|
||||
}
|
||||
$this->processFunctionLike($node, $addParamTypeForFunctionLikeParamDeclaration);
|
||||
}
|
||||
if (!$this->hasChanged) {
|
||||
return null;
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
/**
|
||||
* @param mixed[] $configuration
|
||||
*/
|
||||
public function configure(array $configuration) : void
|
||||
{
|
||||
Assert::allIsAOf($configuration, AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration::class);
|
||||
$this->addParamTypeForFunctionLikeParamDeclarations = $configuration;
|
||||
}
|
||||
private function processFunctionLike(CallLike $callLike, AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration) : void
|
||||
{
|
||||
if ($callLike->isFirstClassCallable()) {
|
||||
return;
|
||||
}
|
||||
if (\is_int($addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getCallLikePosition())) {
|
||||
if ($callLike->getArgs() === []) {
|
||||
return;
|
||||
}
|
||||
$arg = $callLike->args[$addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getCallLikePosition()] ?? null;
|
||||
if (!$arg instanceof Arg) {
|
||||
return;
|
||||
}
|
||||
// int positions shouldn't have names
|
||||
if ($arg->name !== null) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
$args = \array_filter($callLike->getArgs(), static function (Arg $arg) use($addParamTypeForFunctionLikeWithinCallLikeArgDeclaration) : bool {
|
||||
if ($arg->name === null) {
|
||||
return \false;
|
||||
}
|
||||
return $arg->name->name === $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getCallLikePosition();
|
||||
});
|
||||
if ($args === []) {
|
||||
return;
|
||||
}
|
||||
$arg = \array_values($args)[0];
|
||||
}
|
||||
$functionLike = $arg->value;
|
||||
if (!$functionLike instanceof FunctionLike) {
|
||||
return;
|
||||
}
|
||||
if (!isset($functionLike->params[$addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getFunctionLikePosition()])) {
|
||||
return;
|
||||
}
|
||||
$this->refactorParameter($functionLike->params[$addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getFunctionLikePosition()], $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration);
|
||||
}
|
||||
private function refactorParameter(Param $param, AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration) : void
|
||||
{
|
||||
// already set → no change
|
||||
if ($param->type !== null) {
|
||||
$currentParamType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);
|
||||
if ($this->typeComparator->areTypesEqual($currentParamType, $addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getParamType())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
$paramTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getParamType(), TypeKind::PARAM);
|
||||
$this->hasChanged = \true;
|
||||
// remove it
|
||||
if ($addParamTypeForFunctionLikeWithinCallLikeArgDeclaration->getParamType() instanceof MixedType) {
|
||||
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::MIXED_TYPE)) {
|
||||
$param->type = $paramTypeNode;
|
||||
return;
|
||||
}
|
||||
$param->type = null;
|
||||
return;
|
||||
}
|
||||
$param->type = $paramTypeNode;
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\TypeDeclaration\ValueObject;
|
||||
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\Validation\RectorAssert;
|
||||
/**
|
||||
* @see \Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddParamTypeForFunctionLikeWithinCallLikeDeclarationRector\AddParamTypeForFunctionLikeWithinCallLikeDeclarationRectorTest
|
||||
*/
|
||||
final class AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var string
|
||||
*/
|
||||
private $className;
|
||||
/**
|
||||
* @readonly
|
||||
* @var string
|
||||
*/
|
||||
private $methodName;
|
||||
/**
|
||||
* @var int<0, max>|string
|
||||
* @readonly
|
||||
*/
|
||||
private $callLikePosition;
|
||||
/**
|
||||
* @var int<0, max>
|
||||
* @readonly
|
||||
*/
|
||||
private $functionLikePosition;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \PHPStan\Type\Type
|
||||
*/
|
||||
private $paramType;
|
||||
/**
|
||||
* @param int<0, max>|string $callLikePosition
|
||||
* @param int<0, max> $functionLikePosition
|
||||
*/
|
||||
public function __construct(string $className, string $methodName, $callLikePosition, int $functionLikePosition, Type $paramType)
|
||||
{
|
||||
$this->className = $className;
|
||||
$this->methodName = $methodName;
|
||||
$this->callLikePosition = $callLikePosition;
|
||||
$this->functionLikePosition = $functionLikePosition;
|
||||
$this->paramType = $paramType;
|
||||
RectorAssert::className($className);
|
||||
}
|
||||
public function getObjectType() : ObjectType
|
||||
{
|
||||
return new ObjectType($this->className);
|
||||
}
|
||||
public function getMethodName() : string
|
||||
{
|
||||
return $this->methodName;
|
||||
}
|
||||
/**
|
||||
* @return int<0, max>|string
|
||||
*/
|
||||
public function getCallLikePosition()
|
||||
{
|
||||
return $this->callLikePosition;
|
||||
}
|
||||
/**
|
||||
* @return int<0, max>
|
||||
*/
|
||||
public function getFunctionLikePosition() : int
|
||||
{
|
||||
return $this->functionLikePosition;
|
||||
}
|
||||
public function getParamType() : Type
|
||||
{
|
||||
return $this->paramType;
|
||||
}
|
||||
}
|
@ -19,12 +19,12 @@ final class VersionResolver
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const PACKAGE_VERSION = '4951accb91a39af88d3903930112f2752e497076';
|
||||
public const PACKAGE_VERSION = '18a8aecd13cbe87766c38de95dcba08673493d3a';
|
||||
/**
|
||||
* @api
|
||||
* @var string
|
||||
*/
|
||||
public const RELEASE_DATE = '2024-02-22 10:06:22';
|
||||
public const RELEASE_DATE = '2024-02-22 10:07:14';
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
|
2
vendor/composer/autoload_classmap.php
vendored
2
vendor/composer/autoload_classmap.php
vendored
@ -2388,6 +2388,7 @@ return array(
|
||||
'Rector\\TypeDeclaration\\Rector\\Class_\\ReturnTypeFromStrictTernaryRector' => $baseDir . '/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\Closure\\AddClosureVoidReturnTypeWhereNoReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/Closure/AddClosureVoidReturnTypeWhereNoReturnRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\Empty_\\EmptyOnNullableObjectToInstanceOfRector' => $baseDir . '/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddParamTypeSplFixedArrayRector' => $baseDir . '/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddReturnTypeDeclarationFromYieldsRector' => $baseDir . '/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\Function_\\AddFunctionVoidReturnTypeWhereNoReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/Function_/AddFunctionVoidReturnTypeWhereNoReturnRector.php',
|
||||
@ -2415,6 +2416,7 @@ return array(
|
||||
'Rector\\TypeDeclaration\\TypeInferer\\SplArrayFixedTypeNarrower' => $baseDir . '/rules/TypeDeclaration/TypeInferer/SplArrayFixedTypeNarrower.php',
|
||||
'Rector\\TypeDeclaration\\TypeNormalizer' => $baseDir . '/rules/TypeDeclaration/TypeNormalizer.php',
|
||||
'Rector\\TypeDeclaration\\ValueObject\\AddParamTypeDeclaration' => $baseDir . '/rules/TypeDeclaration/ValueObject/AddParamTypeDeclaration.php',
|
||||
'Rector\\TypeDeclaration\\ValueObject\\AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration' => $baseDir . '/rules/TypeDeclaration/ValueObject/AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration.php',
|
||||
'Rector\\TypeDeclaration\\ValueObject\\AddPropertyTypeDeclaration' => $baseDir . '/rules/TypeDeclaration/ValueObject/AddPropertyTypeDeclaration.php',
|
||||
'Rector\\TypeDeclaration\\ValueObject\\AddReturnTypeDeclaration' => $baseDir . '/rules/TypeDeclaration/ValueObject/AddReturnTypeDeclaration.php',
|
||||
'Rector\\TypeDeclaration\\ValueObject\\AssignToVariable' => $baseDir . '/rules/TypeDeclaration/ValueObject/AssignToVariable.php',
|
||||
|
2
vendor/composer/autoload_static.php
vendored
2
vendor/composer/autoload_static.php
vendored
@ -2607,6 +2607,7 @@ class ComposerStaticInit2d887a2f87c676eb32b3e04612865e54
|
||||
'Rector\\TypeDeclaration\\Rector\\Class_\\ReturnTypeFromStrictTernaryRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\Closure\\AddClosureVoidReturnTypeWhereNoReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Closure/AddClosureVoidReturnTypeWhereNoReturnRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\Empty_\\EmptyOnNullableObjectToInstanceOfRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeForFunctionLikeWithinCallLikeArgDeclarationRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddParamTypeSplFixedArrayRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddReturnTypeDeclarationFromYieldsRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php',
|
||||
'Rector\\TypeDeclaration\\Rector\\Function_\\AddFunctionVoidReturnTypeWhereNoReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Function_/AddFunctionVoidReturnTypeWhereNoReturnRector.php',
|
||||
@ -2634,6 +2635,7 @@ class ComposerStaticInit2d887a2f87c676eb32b3e04612865e54
|
||||
'Rector\\TypeDeclaration\\TypeInferer\\SplArrayFixedTypeNarrower' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeInferer/SplArrayFixedTypeNarrower.php',
|
||||
'Rector\\TypeDeclaration\\TypeNormalizer' => __DIR__ . '/../..' . '/rules/TypeDeclaration/TypeNormalizer.php',
|
||||
'Rector\\TypeDeclaration\\ValueObject\\AddParamTypeDeclaration' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AddParamTypeDeclaration.php',
|
||||
'Rector\\TypeDeclaration\\ValueObject\\AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AddParamTypeForFunctionLikeWithinCallLikeArgDeclaration.php',
|
||||
'Rector\\TypeDeclaration\\ValueObject\\AddPropertyTypeDeclaration' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AddPropertyTypeDeclaration.php',
|
||||
'Rector\\TypeDeclaration\\ValueObject\\AddReturnTypeDeclaration' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AddReturnTypeDeclaration.php',
|
||||
'Rector\\TypeDeclaration\\ValueObject\\AssignToVariable' => __DIR__ . '/../..' . '/rules/TypeDeclaration/ValueObject/AssignToVariable.php',
|
||||
|
Loading…
x
Reference in New Issue
Block a user