diff --git a/config/set/type-declaration.php b/config/set/type-declaration.php index 32ef99279f7..7bd0daf3cf8 100644 --- a/config/set/type-declaration.php +++ b/config/set/type-declaration.php @@ -29,6 +29,7 @@ use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNativeCallRect use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector; use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedPropertyRector; +use Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector; use Rector\TypeDeclaration\Rector\Closure\AddClosureReturnTypeRector; use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector; use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector; @@ -40,5 +41,5 @@ use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictGetterMethodRe use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector; use Rector\TypeDeclaration\Rector\Property\VarAnnotationIncorrectNullableRector; return static function (RectorConfig $rectorConfig) : void { - $rectorConfig->rules([AddClosureReturnTypeRector::class, AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, ReturnAnnotationIncorrectNullableRector::class, VarAnnotationIncorrectNullableRector::class, ParamAnnotationIncorrectNullableRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromReturnNewRector::class, TypedPropertyFromStrictGetterMethodReturnTypeRector::class, AddMethodCallBasedStrictParamTypeRector::class, ArrayShapeFromConstantArrayReturnRector::class, ReturnTypeFromStrictBoolReturnExprRector::class, ReturnTypeFromStrictNativeCallRector::class, ReturnTypeFromStrictNewArrayRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, TypedPropertyFromStrictSetUpRector::class, ParamTypeByParentCallTypeRector::class, AddParamTypeSplFixedArrayRector::class, AddParamTypeBasedOnPHPUnitDataProviderRector::class, AddParamTypeFromPropertyTypeRector::class, AddReturnTypeDeclarationFromYieldsRector::class, ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromStrictConstantReturnRector::class, ReturnTypeFromStrictTypedCallRector::class, ReturnNeverTypeRector::class, EmptyOnNullableObjectToInstanceOfRector::class, PropertyTypeFromStrictSetterGetterRector::class, ReturnTypeFromStrictTernaryRector::class, BoolReturnTypeFromStrictScalarReturnsRector::class, NumericReturnTypeFromStrictScalarReturnsRector::class]); + $rectorConfig->rules([AddClosureReturnTypeRector::class, AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, ReturnAnnotationIncorrectNullableRector::class, VarAnnotationIncorrectNullableRector::class, ParamAnnotationIncorrectNullableRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, ParamTypeFromStrictTypedPropertyRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromReturnNewRector::class, TypedPropertyFromStrictGetterMethodReturnTypeRector::class, AddMethodCallBasedStrictParamTypeRector::class, ArrayShapeFromConstantArrayReturnRector::class, ReturnTypeFromStrictBoolReturnExprRector::class, ReturnTypeFromStrictNativeCallRector::class, ReturnTypeFromStrictNewArrayRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, TypedPropertyFromStrictSetUpRector::class, ParamTypeByParentCallTypeRector::class, AddParamTypeSplFixedArrayRector::class, AddParamTypeBasedOnPHPUnitDataProviderRector::class, AddParamTypeFromPropertyTypeRector::class, AddReturnTypeDeclarationFromYieldsRector::class, ReturnTypeFromReturnDirectArrayRector::class, ReturnTypeFromStrictConstantReturnRector::class, ReturnTypeFromStrictTypedCallRector::class, ReturnNeverTypeRector::class, EmptyOnNullableObjectToInstanceOfRector::class, PropertyTypeFromStrictSetterGetterRector::class, ReturnTypeFromStrictTernaryRector::class, BoolReturnTypeFromStrictScalarReturnsRector::class, NumericReturnTypeFromStrictScalarReturnsRector::class, StrictArrayParamDimFetchRector::class]); }; diff --git a/docs/rector_rules_overview.md b/docs/rector_rules_overview.md index 23adbca74ae..91b9a80d8eb 100644 --- a/docs/rector_rules_overview.md +++ b/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 369 Rules Overview +# 370 Rules Overview
@@ -54,7 +54,7 @@ - [Transform](#transform) (22) -- [TypeDeclaration](#typedeclaration) (41) +- [TypeDeclaration](#typedeclaration) (42) - [Visibility](#visibility) (3) @@ -8368,6 +8368,25 @@ Add return method return type based on strict typed property
+### StrictArrayParamDimFetchRector + +Add array type based on array dim fetch use + +- class: [`Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector`](../rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php) + +```diff + class SomeClass + { +- public function resolve($item) ++ public function resolve(array $item) + { + return $item['name']; + } + } +``` + +
+ ### TypedPropertyFromAssignsRector Add typed property from assigned types diff --git a/packages/Config/RectorConfig.php b/packages/Config/RectorConfig.php index 0d03017b0d9..647304f3183 100644 --- a/packages/Config/RectorConfig.php +++ b/packages/Config/RectorConfig.php @@ -140,6 +140,7 @@ final class RectorConfig extends ContainerConfigurator public function rules(array $rectorClasses) : void { Assert::allString($rectorClasses); + Assert::uniqueValues($rectorClasses); foreach ($rectorClasses as $rectorClass) { $this->rule($rectorClass); } diff --git a/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php new file mode 100644 index 00000000000..fda4db34499 --- /dev/null +++ b/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php @@ -0,0 +1,97 @@ +> + */ + public function getNodeTypes() : array + { + return [ClassMethod::class, Function_::class]; + } + /** + * @param ClassMethod|Function_ $node + */ + public function refactor(Node $node) : ?Node + { + $hasChanged = \false; + foreach ($node->getParams() as $param) { + if ($param->type instanceof Node) { + continue; + } + if (!$this->isParamAccessedArrayDimFetch($param, $node)) { + continue; + } + $param->type = new Identifier('array'); + $hasChanged = \true; + } + if ($hasChanged) { + return $node; + } + return null; + } + /** + * @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $functionLike + */ + private function isParamAccessedArrayDimFetch(Param $param, $functionLike) : bool + { + $paramName = $this->getName($param); + $isParamAccessedArrayDimFetch = \false; + $this->traverseNodesWithCallable($functionLike, function (Node $node) use($paramName, &$isParamAccessedArrayDimFetch) : ?int { + if ($node instanceof FuncCall && $this->isName($node, 'is_array')) { + $firstArg = $node->getArgs()[0]; + if ($this->isName($firstArg->value, $paramName)) { + return NodeTraverser::STOP_TRAVERSAL; + } + } + if (!$node instanceof ArrayDimFetch) { + return null; + } + if (!$this->isName($node->var, $paramName)) { + return null; + } + $isParamAccessedArrayDimFetch = \true; + return null; + }); + return $isParamAccessedArrayDimFetch; + } +} diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index bdb019c81b1..b6a1a4f79d1 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '205df9013af5e05034441aa6088599532f5fef05'; + public const PACKAGE_VERSION = '8324cf427656e1d81f8c0e782885ca65bf592481'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-06-30 10:54:34'; + public const RELEASE_DATE = '2023-06-30 16:28:59'; /** * @var int */ diff --git a/vendor/autoload.php b/vendor/autoload.php index 519c34fcb30..91318400f88 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) { require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInit270aad27721263e728d3e03b2ead6200::getLoader(); +return ComposerAutoloaderInit093835b3742dcca73593de5ba99150ad::getLoader(); diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 79f2e5a05df..c11e1575dc6 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -2776,6 +2776,7 @@ return array( 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictNewArrayRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedPropertyRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php', + 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictArrayParamDimFetchRector' => $baseDir . '/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php', 'Rector\\TypeDeclaration\\Rector\\Class_\\PropertyTypeFromStrictSetterGetterRector' => $baseDir . '/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php', 'Rector\\TypeDeclaration\\Rector\\Class_\\ReturnTypeFromStrictTernaryRector' => $baseDir . '/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php', 'Rector\\TypeDeclaration\\Rector\\Closure\\AddClosureReturnTypeRector' => $baseDir . '/rules/TypeDeclaration/Rector/Closure/AddClosureReturnTypeRector.php', diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 4ea589850ab..a16fecb635c 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInit270aad27721263e728d3e03b2ead6200 +class ComposerAutoloaderInit093835b3742dcca73593de5ba99150ad { private static $loader; @@ -22,17 +22,17 @@ class ComposerAutoloaderInit270aad27721263e728d3e03b2ead6200 return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInit270aad27721263e728d3e03b2ead6200', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInit093835b3742dcca73593de5ba99150ad', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); - spl_autoload_unregister(array('ComposerAutoloaderInit270aad27721263e728d3e03b2ead6200', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInit093835b3742dcca73593de5ba99150ad', 'loadClassLoader')); require __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInit270aad27721263e728d3e03b2ead6200::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInit093835b3742dcca73593de5ba99150ad::getInitializer($loader)); $loader->setClassMapAuthoritative(true); $loader->register(true); - $filesToLoad = \Composer\Autoload\ComposerStaticInit270aad27721263e728d3e03b2ead6200::$files; + $filesToLoad = \Composer\Autoload\ComposerStaticInit093835b3742dcca73593de5ba99150ad::$files; $requireFile = \Closure::bind(static function ($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 1be85a4fff1..cc95e052010 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInit270aad27721263e728d3e03b2ead6200 +class ComposerStaticInit093835b3742dcca73593de5ba99150ad { public static $files = array ( 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', @@ -3028,6 +3028,7 @@ class ComposerStaticInit270aad27721263e728d3e03b2ead6200 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictNewArrayRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedCallRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector.php', 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\ReturnTypeFromStrictTypedPropertyRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedPropertyRector.php', + 'Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictArrayParamDimFetchRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector.php', 'Rector\\TypeDeclaration\\Rector\\Class_\\PropertyTypeFromStrictSetterGetterRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Class_/PropertyTypeFromStrictSetterGetterRector.php', 'Rector\\TypeDeclaration\\Rector\\Class_\\ReturnTypeFromStrictTernaryRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Class_/ReturnTypeFromStrictTernaryRector.php', 'Rector\\TypeDeclaration\\Rector\\Closure\\AddClosureReturnTypeRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Closure/AddClosureReturnTypeRector.php', @@ -3098,9 +3099,9 @@ class ComposerStaticInit270aad27721263e728d3e03b2ead6200 public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit270aad27721263e728d3e03b2ead6200::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit270aad27721263e728d3e03b2ead6200::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit270aad27721263e728d3e03b2ead6200::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInit093835b3742dcca73593de5ba99150ad::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit093835b3742dcca73593de5ba99150ad::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit093835b3742dcca73593de5ba99150ad::$classMap; }, null, ClassLoader::class); }