Skip to content

Commit 428336c

Browse files
committed
Updated Rector to commit 09398c3d4f07bb6ad664a55b87cab955278a467e
rectorphp/rector-src@09398c3 Decouple AddFunctionVoidReturnTypeWhereNoReturnRector to allow leveling by simple node first (#5563)
1 parent d00ebb8 commit 428336c

File tree

7 files changed

+86
-23
lines changed

7 files changed

+86
-23
lines changed

config/set/type-declaration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector;
3535
use Rector\TypeDeclaration\Rector\Closure\AddClosureVoidReturnTypeWhereNoReturnRector;
3636
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
37+
use Rector\TypeDeclaration\Rector\Function_\AddFunctionVoidReturnTypeWhereNoReturnRector;
3738
use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector;
3839
use Rector\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector;
3940
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;
4041
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
4142
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictSetUpRector;
4243
return static function (RectorConfig $rectorConfig) : void {
43-
$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]);
44+
$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]);
4445
};

rules/TypeDeclaration/Rector/ClassMethod/AddVoidReturnTypeWhereNoReturnRector.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
namespace Rector\TypeDeclaration\Rector\ClassMethod;
55

66
use PhpParser\Node;
7-
use PhpParser\Node\Expr\Closure;
87
use PhpParser\Node\Identifier;
98
use PhpParser\Node\Stmt\ClassMethod;
10-
use PhpParser\Node\Stmt\Function_;
119
use PhpParser\Node\Stmt\Throw_;
1210
use Rector\NodeAnalyzer\MagicClassMethodAnalyzer;
1311
use Rector\Rector\AbstractRector;
@@ -79,10 +77,10 @@ public function getValues(): void
7977
*/
8078
public function getNodeTypes() : array
8179
{
82-
return [ClassMethod::class, Function_::class, Closure::class];
80+
return [ClassMethod::class];
8381
}
8482
/**
85-
* @param ClassMethod|Function_|Closure $node
83+
* @param ClassMethod $node
8684
*/
8785
public function refactor(Node $node) : ?Node
8886
{
@@ -96,7 +94,7 @@ public function refactor(Node $node) : ?Node
9694
if (!$this->silentVoidResolver->hasExclusiveVoid($node)) {
9795
return null;
9896
}
99-
if ($node instanceof ClassMethod && $this->classMethodReturnVendorLockResolver->isVendorLocked($node)) {
97+
if ($this->classMethodReturnVendorLockResolver->isVendorLocked($node)) {
10098
return null;
10199
}
102100
$node->returnType = new Identifier('void');
@@ -106,32 +104,26 @@ public function provideMinPhpVersion() : int
106104
{
107105
return PhpVersionFeature::VOID_TYPE;
108106
}
109-
/**
110-
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\Closure $functionLike
111-
*/
112-
private function shouldSkipClassMethod($functionLike) : bool
107+
private function shouldSkipClassMethod(ClassMethod $classMethod) : bool
113108
{
114-
if (!$functionLike instanceof ClassMethod) {
115-
return \false;
116-
}
117-
if ($this->magicClassMethodAnalyzer->isUnsafeOverridden($functionLike)) {
109+
if ($this->magicClassMethodAnalyzer->isUnsafeOverridden($classMethod)) {
118110
return \true;
119111
}
120-
if ($functionLike->isAbstract()) {
112+
if ($classMethod->isAbstract()) {
121113
return \true;
122114
}
123115
// is not final and has only exception? possibly implemented by child
124-
if ($this->isNotFinalAndHasExceptionOnly($functionLike)) {
116+
if ($this->isNotFinalAndHasExceptionOnly($classMethod)) {
125117
return \true;
126118
}
127119
// possibly required by child implementation
128-
if ($this->isNotFinalAndEmpty($functionLike)) {
120+
if ($this->isNotFinalAndEmpty($classMethod)) {
129121
return \true;
130122
}
131-
if ($functionLike->isProtected()) {
132-
return !$this->classModifierChecker->isInsideFinalClass($functionLike);
123+
if ($classMethod->isProtected()) {
124+
return !$this->classModifierChecker->isInsideFinalClass($classMethod);
133125
}
134-
return $this->classModifierChecker->isInsideAbstractClass($functionLike) && $functionLike->getStmts() === [];
126+
return $this->classModifierChecker->isInsideAbstractClass($classMethod) && $classMethod->getStmts() === [];
135127
}
136128
private function isNotFinalAndHasExceptionOnly(ClassMethod $classMethod) : bool
137129
{
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
declare (strict_types=1);
4+
namespace Rector\TypeDeclaration\Rector\Function_;
5+
6+
use PhpParser\Node;
7+
use PhpParser\Node\Identifier;
8+
use PhpParser\Node\Stmt\Function_;
9+
use Rector\Rector\AbstractRector;
10+
use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver;
11+
use Rector\ValueObject\PhpVersionFeature;
12+
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
13+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
14+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
15+
/**
16+
* @see \Rector\Tests\TypeDeclaration\Rector\Function_\AddFunctionVoidReturnTypeWhereNoReturnRector\AddFunctionVoidReturnTypeWhereNoReturnRectorTest
17+
*/
18+
final class AddFunctionVoidReturnTypeWhereNoReturnRector extends AbstractRector implements MinPhpVersionInterface
19+
{
20+
/**
21+
* @readonly
22+
* @var \Rector\TypeDeclaration\TypeInferer\SilentVoidResolver
23+
*/
24+
private $silentVoidResolver;
25+
public function __construct(SilentVoidResolver $silentVoidResolver)
26+
{
27+
$this->silentVoidResolver = $silentVoidResolver;
28+
}
29+
public function getRuleDefinition() : RuleDefinition
30+
{
31+
return new RuleDefinition('Add function return type void if there is no return', [new CodeSample(<<<'CODE_SAMPLE'
32+
function restore() {
33+
}
34+
CODE_SAMPLE
35+
, <<<'CODE_SAMPLE'
36+
function restore(): void {
37+
}
38+
CODE_SAMPLE
39+
)]);
40+
}
41+
/**
42+
* @return array<class-string<Node>>
43+
*/
44+
public function getNodeTypes() : array
45+
{
46+
return [Function_::class];
47+
}
48+
/**
49+
* @param Function_ $node
50+
*/
51+
public function refactor(Node $node) : ?Node
52+
{
53+
// already has return type → skip
54+
if ($node->returnType instanceof Node) {
55+
return null;
56+
}
57+
if (!$this->silentVoidResolver->hasExclusiveVoid($node)) {
58+
return null;
59+
}
60+
$node->returnType = new Identifier('void');
61+
return $node;
62+
}
63+
public function provideMinPhpVersion() : int
64+
{
65+
return PhpVersionFeature::VOID_TYPE;
66+
}
67+
}

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 = '56c250771518c2aa2e17bc64c9986c2dbe9b6fd8';
22+
public const PACKAGE_VERSION = '09398c3d4f07bb6ad664a55b87cab955278a467e';
2323
/**
2424
* @api
2525
* @var string
2626
*/
27-
public const RELEASE_DATE = '2024-02-05 16:54:04';
27+
public const RELEASE_DATE = '2024-02-05 17:05:25';
2828
/**
2929
* @var int
3030
*/

src/Configuration/Levels/TypeCoverageLevel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector;
2929
use Rector\TypeDeclaration\Rector\Closure\AddClosureVoidReturnTypeWhereNoReturnRector;
3030
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;
31+
use Rector\TypeDeclaration\Rector\Function_\AddFunctionVoidReturnTypeWhereNoReturnRector;
3132
use Rector\TypeDeclaration\Rector\FunctionLike\AddParamTypeSplFixedArrayRector;
3233
use Rector\TypeDeclaration\Rector\FunctionLike\AddReturnTypeDeclarationFromYieldsRector;
3334
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector;
@@ -54,7 +55,7 @@ final class TypeCoverageLevel
5455
// php 7.0
5556
// start with closure first, as safest
5657
AddClosureVoidReturnTypeWhereNoReturnRector::class,
57-
// @todo continue with functions
58+
AddFunctionVoidReturnTypeWhereNoReturnRector::class,
5859
AddVoidReturnTypeWhereNoReturnRector::class,
5960
// php 7.4
6061
AddArrowFunctionReturnTypeRector::class,

vendor/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,6 +2360,7 @@
23602360
'Rector\\TypeDeclaration\\Rector\\Empty_\\EmptyOnNullableObjectToInstanceOfRector' => $baseDir . '/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php',
23612361
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddParamTypeSplFixedArrayRector' => $baseDir . '/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector.php',
23622362
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddReturnTypeDeclarationFromYieldsRector' => $baseDir . '/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php',
2363+
'Rector\\TypeDeclaration\\Rector\\Function_\\AddFunctionVoidReturnTypeWhereNoReturnRector' => $baseDir . '/rules/TypeDeclaration/Rector/Function_/AddFunctionVoidReturnTypeWhereNoReturnRector.php',
23632364
'Rector\\TypeDeclaration\\Rector\\Property\\AddPropertyTypeDeclarationRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php',
23642365
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromAssignsRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php',
23652366
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictConstructorRector' => $baseDir . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector.php',

vendor/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,6 +2574,7 @@ class ComposerStaticInitf637847380e2ddf55dcae18dded1d2b3
25742574
'Rector\\TypeDeclaration\\Rector\\Empty_\\EmptyOnNullableObjectToInstanceOfRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Empty_/EmptyOnNullableObjectToInstanceOfRector.php',
25752575
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddParamTypeSplFixedArrayRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/FunctionLike/AddParamTypeSplFixedArrayRector.php',
25762576
'Rector\\TypeDeclaration\\Rector\\FunctionLike\\AddReturnTypeDeclarationFromYieldsRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/FunctionLike/AddReturnTypeDeclarationFromYieldsRector.php',
2577+
'Rector\\TypeDeclaration\\Rector\\Function_\\AddFunctionVoidReturnTypeWhereNoReturnRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Function_/AddFunctionVoidReturnTypeWhereNoReturnRector.php',
25772578
'Rector\\TypeDeclaration\\Rector\\Property\\AddPropertyTypeDeclarationRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/AddPropertyTypeDeclarationRector.php',
25782579
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromAssignsRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromAssignsRector.php',
25792580
'Rector\\TypeDeclaration\\Rector\\Property\\TypedPropertyFromStrictConstructorRector' => __DIR__ . '/../..' . '/rules/TypeDeclaration/Rector/Property/TypedPropertyFromStrictConstructorRector.php',

0 commit comments

Comments
 (0)