Updated Rector to commit 09398c3d4f07bb6ad664a55b87cab955278a467e

09398c3d4f Decouple AddFunctionVoidReturnTypeWhereNoReturnRector to allow leveling by simple node first (#5563)
This commit is contained in:
Tomas Votruba 2024-02-05 16:07:43 +00:00
parent d00ebb838c
commit 428336c22c
7 changed files with 86 additions and 23 deletions

View File

@ -34,11 +34,12 @@ use Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector;
use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
use Rector\TypeDeclaration\Rector\Function_\AddFunctionVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector;
use Rector\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector;
return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->rules([AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, AddClosureVoidReturnTypeWhereNoReturnRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromStrictFluentReturnRector::class, ReturnTypeFromReturnNewRector::class, AddMethodCallBasedStrictParamTypeRector::class, ReturnTypeFromStrictBoolReturnExprRector::class, ReturnTypeFromStrictNativeCallRector::class, ReturnTypeFromStrictNewArrayRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, ReturnTypeFromStrictParamRector::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, ReturnUnionTypeRector::class, StrictStringParamConcatRector::class, MergeDateTimePropertyTypeDeclarationRector::class]);
$rectorConfig->rules([AddArrowFunctionReturnTypeRector::class, ParamTypeByMethodCallTypeRector::class, TypedPropertyFromAssignsRector::class, AddReturnTypeDeclarationBasedOnParentClassMethodRector::class, ReturnTypeFromStrictTypedPropertyRector::class, TypedPropertyFromStrictConstructorRector::class, AddClosureVoidReturnTypeWhereNoReturnRector::class, AddFunctionVoidReturnTypeWhereNoReturnRector::class, AddVoidReturnTypeWhereNoReturnRector::class, ReturnTypeFromStrictFluentReturnRector::class, ReturnTypeFromReturnNewRector::class, AddMethodCallBasedStrictParamTypeRector::class, ReturnTypeFromStrictBoolReturnExprRector::class, ReturnTypeFromStrictNativeCallRector::class, ReturnTypeFromStrictNewArrayRector::class, ReturnTypeFromStrictScalarReturnExprRector::class, ReturnTypeFromStrictParamRector::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, ReturnUnionTypeRector::class, StrictStringParamConcatRector::class, MergeDateTimePropertyTypeDeclarationRector::class]);
};

View File

@ -4,10 +4,8 @@ declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Throw_;
use Rector\NodeAnalyzer\MagicClassMethodAnalyzer;
use Rector\Rector\AbstractRector;
@ -79,10 +77,10 @@ CODE_SAMPLE
*/
public function getNodeTypes() : array
{
return [ClassMethod::class, Function_::class, Closure::class];
return [ClassMethod::class];
}
/**
* @param ClassMethod|Function_|Closure $node
* @param ClassMethod $node
*/
public function refactor(Node $node) : ?Node
{
@ -96,7 +94,7 @@ CODE_SAMPLE
if (!$this->silentVoidResolver->hasExclusiveVoid($node)) {
return null;
}
if ($node instanceof ClassMethod && $this->classMethodReturnVendorLockResolver->isVendorLocked($node)) {
if ($this->classMethodReturnVendorLockResolver->isVendorLocked($node)) {
return null;
}
$node->returnType = new Identifier('void');
@ -106,32 +104,26 @@ CODE_SAMPLE
{
return PhpVersionFeature::VOID_TYPE;
}
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
*/
private function shouldSkipClassMethod($functionLike) : bool
private function shouldSkipClassMethod(ClassMethod $classMethod) : bool
{
if (!$functionLike instanceof ClassMethod) {
return \false;
}
if ($this->magicClassMethodAnalyzer->isUnsafeOverridden($functionLike)) {
if ($this->magicClassMethodAnalyzer->isUnsafeOverridden($classMethod)) {
return \true;
}
if ($functionLike->isAbstract()) {
if ($classMethod->isAbstract()) {
return \true;
}
// is not final and has only exception? possibly implemented by child
if ($this->isNotFinalAndHasExceptionOnly($functionLike)) {
if ($this->isNotFinalAndHasExceptionOnly($classMethod)) {
return \true;
}
// possibly required by child implementation
if ($this->isNotFinalAndEmpty($functionLike)) {
if ($this->isNotFinalAndEmpty($classMethod)) {
return \true;
}
if ($functionLike->isProtected()) {
return !$this->classModifierChecker->isInsideFinalClass($functionLike);
if ($classMethod->isProtected()) {
return !$this->classModifierChecker->isInsideFinalClass($classMethod);
}
return $this->classModifierChecker->isInsideAbstractClass($functionLike) && $functionLike->getStmts() === [];
return $this->classModifierChecker->isInsideAbstractClass($classMethod) && $classMethod->getStmts() === [];
}
private function isNotFinalAndHasExceptionOnly(ClassMethod $classMethod) : bool
{

View File

@ -0,0 +1,67 @@
<?php
declare (strict_types=1);
namespace Rector\TypeDeclaration\Rector\Function_;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Function_;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\Function_\AddFunctionVoidReturnTypeWhereNoReturnRector\AddFunctionVoidReturnTypeWhereNoReturnRectorTest
*/
final class AddFunctionVoidReturnTypeWhereNoReturnRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
* @var \Rector\TypeDeclaration\TypeInferer\SilentVoidResolver
*/
private $silentVoidResolver;
public function __construct(SilentVoidResolver $silentVoidResolver)
{
$this->silentVoidResolver = $silentVoidResolver;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Add function return type void if there is no return', [new CodeSample(<<<'CODE_SAMPLE'
function restore() {
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
function restore(): void {
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Function_::class];
}
/**
* @param Function_ $node
*/
public function refactor(Node $node) : ?Node
{
// already has return type → skip
if ($node->returnType instanceof Node) {
return null;
}
if (!$this->silentVoidResolver->hasExclusiveVoid($node)) {
return null;
}
$node->returnType = new Identifier('void');
return $node;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::VOID_TYPE;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '56c250771518c2aa2e17bc64c9986c2dbe9b6fd8';
public const PACKAGE_VERSION = '09398c3d4f07bb6ad664a55b87cab955278a467e';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-02-05 16:54:04';
public const RELEASE_DATE = '2024-02-05 17:05:25';
/**
* @var int
*/

View File

@ -28,6 +28,7 @@ use Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector;
use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
use Rector\TypeDeclaration\Rector\Function_\AddFunctionVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector;
use Rector\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
@ -54,7 +55,7 @@ final class TypeCoverageLevel
// php 7.0
// start with closure first, as safest
AddClosureVoidReturnTypeWhereNoReturnRector::class,
// @todo continue with functions
AddFunctionVoidReturnTypeWhereNoReturnRector::class,
AddVoidReturnTypeWhereNoReturnRector::class,
// php 7.4
AddArrowFunctionReturnTypeRector::class,

View File

@ -2360,6 +2360,7 @@ return array(
'Rector\\TypeDeclaration\\Rector\\Empty_\\EmptyOnNullableObjectToInstanceOfRector' => $baseDir . '/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.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',
'Rector\\TypeDeclaration\\Rector\\Property\\AddPropertyTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php',
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromAssignsRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php',
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictConstructorRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector.php',

View File

@ -2574,6 +2574,7 @@ class ComposerStaticInitf637847380e2ddf55dcae18dded1d2b3
'Rector\\TypeDeclaration\\Rector\\Empty_\\EmptyOnNullableObjectToInstanceOfRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.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',
'Rector\\TypeDeclaration\\Rector\\Property\\AddPropertyTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php',
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromAssignsRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php',
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictConstructorRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector.php',