Updated Rector to commit e044d50e9b7b04c5ef7454044721dbf16b4b75f8

e044d50e9b [TypeDeclaration] optionally only add types for hard coded return values in `ReturnTypeFromStrictScalarReturnExprRector` (#5364)
This commit is contained in:
Tomas Votruba 2023-12-30 11:58:36 +00:00
parent ae330c0d87
commit b1b95d1c64
12 changed files with 237 additions and 22 deletions

View File

@ -1,4 +1,4 @@
# 354 Rules Overview
# 355 Rules Overview
<br>
@ -44,7 +44,7 @@
- [Php82](#php82) (4)
- [Php83](#php83) (2)
- [Php83](#php83) (3)
- [Privatization](#privatization) (4)
@ -5312,6 +5312,19 @@ Add const to type
<br>
### CombineHostPortLdapUriRector
Combine separated host and port on `ldap_connect()` args
- class: [`Rector\Php83\Rector\FuncCall\CombineHostPortLdapUriRector`](../rules/Php83/Rector/FuncCall/CombineHostPortLdapUriRector.php)
```diff
-ldap_connect('ldap://ldap.example.com', 389);
+ldap_connect('ldap://ldap.example.com:389');
```
<br>
## Privatization
### FinalizeClassesWithoutChildrenRector
@ -6850,13 +6863,15 @@ Add return type based on strict parameter type
Change return type based on strict scalar returns - string, int, float or bool
:wrench: **configure it!**
- class: [`Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector`](../rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictScalarReturnExprRector.php)
```diff
final class SomeClass
{
- public function run($value)
+ public function run($value): string
- public function foo($value)
+ public function foo($value): string
{
if ($value) {
return 'yes';
@ -6864,6 +6879,12 @@ Change return type based on strict scalar returns - string, int, float or bool
return 'no';
}
- public function bar(string $value)
+ public function bar(string $value): int
{
return strlen($value);
}
}
```

View File

@ -5,6 +5,9 @@ namespace Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\UnaryMinus;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Type\Type;
@ -36,7 +39,7 @@ final class StrictScalarReturnTypeAnalyzer
/**
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\Function_ $functionLike
*/
public function matchAlwaysScalarReturnType($functionLike) : ?Type
public function matchAlwaysScalarReturnType($functionLike, bool $hardCodedOnly = \false) : ?Type
{
$returns = $this->alwaysStrictReturnAnalyzer->matchAlwaysStrictReturns($functionLike);
if ($returns === []) {
@ -48,6 +51,9 @@ final class StrictScalarReturnTypeAnalyzer
if (!$return->expr instanceof Expr) {
return null;
}
if ($hardCodedOnly && !$this->isHardCodedExpression($return->expr)) {
return null;
}
$scalarType = $this->alwaysStrictScalarExprAnalyzer->matchStrictScalarExpr($return->expr);
if (!$scalarType instanceof Type) {
return null;
@ -56,4 +62,20 @@ final class StrictScalarReturnTypeAnalyzer
}
return $this->typeFactory->createMixedPassedOrUnionType($scalarTypes);
}
private function isHardCodedExpression(Expr $expr) : bool
{
// Normal scalar values like strings, integers and floats
if ($expr instanceof Scalar) {
return \true;
}
// true / false / null are constants
if ($expr instanceof ConstFetch && \in_array($expr->name->toLowerString(), ['true', 'false', 'null'], \true)) {
return \true;
}
// Negative numbers are wrapped in UnaryMinus, so check expression inside it
if (($expr instanceof UnaryMinus || $expr instanceof Expr\UnaryPlus) && $expr->expr instanceof Scalar) {
return \true;
}
return \false;
}
}

View File

@ -10,6 +10,7 @@ use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\UnionType;
use PHPStan\Analyser\Scope;
use PHPStan\Type\Type;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
@ -17,12 +18,13 @@ use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\NodeAnalyzer\ReturnTypeAnalyzer\StrictScalarReturnTypeAnalyzer;
use Rector\VendorLocker\NodeVendorLocker\ClassMethodReturnTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202312\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\ReturnTypeFromStrictScalarReturnExprRectorTest
*/
final class ReturnTypeFromStrictScalarReturnExprRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
final class ReturnTypeFromStrictScalarReturnExprRector extends AbstractScopeAwareRector implements MinPhpVersionInterface, ConfigurableRectorInterface
{
/**
* @readonly
@ -39,6 +41,14 @@ final class ReturnTypeFromStrictScalarReturnExprRector extends AbstractScopeAwar
* @var \Rector\StaticTypeMapper\StaticTypeMapper
*/
private $staticTypeMapper;
/**
* @var string
*/
public const HARD_CODED_ONLY = 'hard_coded_only';
/**
* @var bool
*/
private $hardCodedOnly = \false;
public function __construct(StrictScalarReturnTypeAnalyzer $strictScalarReturnTypeAnalyzer, ClassMethodReturnTypeOverrideGuard $classMethodReturnTypeOverrideGuard, StaticTypeMapper $staticTypeMapper)
{
$this->strictScalarReturnTypeAnalyzer = $strictScalarReturnTypeAnalyzer;
@ -47,10 +57,10 @@ final class ReturnTypeFromStrictScalarReturnExprRector extends AbstractScopeAwar
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change return type based on strict scalar returns - string, int, float or bool', [new CodeSample(<<<'CODE_SAMPLE'
return new RuleDefinition('Change return type based on strict scalar returns - string, int, float or bool', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
final class SomeClass
{
public function run($value)
public function foo($value)
{
if ($value) {
return 'yes';
@ -58,12 +68,17 @@ final class SomeClass
return 'no';
}
public function bar(string $value)
{
return strlen($value);
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
final class SomeClass
{
public function run($value): string
public function foo($value): string
{
if ($value) {
return 'yes';
@ -71,9 +86,14 @@ final class SomeClass
return 'no';
}
public function bar(string $value): int
{
return strlen($value);
}
}
CODE_SAMPLE
)]);
, [\Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector::HARD_CODED_ONLY => \false])]);
}
/**
* @return array<class-string<Node>>
@ -91,7 +111,7 @@ CODE_SAMPLE
if ($node->returnType instanceof Node) {
return null;
}
$scalarReturnType = $this->strictScalarReturnTypeAnalyzer->matchAlwaysScalarReturnType($node);
$scalarReturnType = $this->strictScalarReturnTypeAnalyzer->matchAlwaysScalarReturnType($node, $this->hardCodedOnly);
if (!$scalarReturnType instanceof Type) {
return null;
}
@ -112,4 +132,10 @@ CODE_SAMPLE
{
return PhpVersion::PHP_70;
}
public function configure(array $configuration) : void
{
$hardCodedOnly = $configuration[self::HARD_CODED_ONLY] ?? \false;
Assert::boolean($hardCodedOnly);
$this->hardCodedOnly = $hardCodedOnly;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = 'e8684d461ed996055fb060592267fcdab258e367';
public const PACKAGE_VERSION = 'e044d50e9b7b04c5ef7454044721dbf16b4b75f8';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-12-29 18:28:57';
public const RELEASE_DATE = '2023-12-30 11:56:25';
/**
* @var int
*/

View File

@ -1761,6 +1761,7 @@ return array(
'Rector\\PHPUnit\\NodeFactory\\ExpectExceptionMethodCallFactory' => $vendorDir . '/rector/rector-phpunit/src/NodeFactory/ExpectExceptionMethodCallFactory.php',
'Rector\\PHPUnit\\NodeFinder\\DataProviderClassMethodFinder' => $vendorDir . '/rector/rector-phpunit/src/NodeFinder/DataProviderClassMethodFinder.php',
'Rector\\PHPUnit\\PHPUnit100\\Rector\\Class_\\AddProphecyTraitRector' => $vendorDir . '/rector/rector-phpunit/rules/PHPUnit100/Rector/Class_/AddProphecyTraitRector.php',
'Rector\\PHPUnit\\PHPUnit100\\Rector\\Class_\\PublicDataProviderClassMethodRector' => $vendorDir . '/rector/rector-phpunit/rules/PHPUnit100/Rector/Class_/PublicDataProviderClassMethodRector.php',
'Rector\\PHPUnit\\PHPUnit100\\Rector\\Class_\\StaticDataProviderClassMethodRector' => $vendorDir . '/rector/rector-phpunit/rules/PHPUnit100/Rector/Class_/StaticDataProviderClassMethodRector.php',
'Rector\\PHPUnit\\PHPUnit100\\Rector\\MethodCall\\PropertyExistsWithoutAssertRector' => $vendorDir . '/rector/rector-phpunit/rules/PHPUnit100/Rector/MethodCall/PropertyExistsWithoutAssertRector.php',
'Rector\\PHPUnit\\PHPUnit100\\Rector\\MethodCall\\RemoveSetMethodsMethodCallRector' => $vendorDir . '/rector/rector-phpunit/rules/PHPUnit100/Rector/MethodCall/RemoveSetMethodsMethodCallRector.php',

View File

@ -1979,6 +1979,7 @@ class ComposerStaticInit8f2270509893f1162053010eaa6e09a5
'Rector\\PHPUnit\\NodeFactory\\ExpectExceptionMethodCallFactory' => __DIR__ . '/..' . '/rector/rector-phpunit/src/NodeFactory/ExpectExceptionMethodCallFactory.php',
'Rector\\PHPUnit\\NodeFinder\\DataProviderClassMethodFinder' => __DIR__ . '/..' . '/rector/rector-phpunit/src/NodeFinder/DataProviderClassMethodFinder.php',
'Rector\\PHPUnit\\PHPUnit100\\Rector\\Class_\\AddProphecyTraitRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/PHPUnit100/Rector/Class_/AddProphecyTraitRector.php',
'Rector\\PHPUnit\\PHPUnit100\\Rector\\Class_\\PublicDataProviderClassMethodRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/PHPUnit100/Rector/Class_/PublicDataProviderClassMethodRector.php',
'Rector\\PHPUnit\\PHPUnit100\\Rector\\Class_\\StaticDataProviderClassMethodRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/PHPUnit100/Rector/Class_/StaticDataProviderClassMethodRector.php',
'Rector\\PHPUnit\\PHPUnit100\\Rector\\MethodCall\\PropertyExistsWithoutAssertRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/PHPUnit100/Rector/MethodCall/PropertyExistsWithoutAssertRector.php',
'Rector\\PHPUnit\\PHPUnit100\\Rector\\MethodCall\\RemoveSetMethodsMethodCallRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/PHPUnit100/Rector/MethodCall/RemoveSetMethodsMethodCallRector.php',

View File

@ -1807,12 +1807,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/rectorphp\/rector-phpunit.git",
"reference": "e5120ffbe0a54af5e505f8cd6b405c46b6a55f28"
"reference": "71c289d02b480b5487e294dd044bea2974f5950c"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/e5120ffbe0a54af5e505f8cd6b405c46b6a55f28",
"reference": "e5120ffbe0a54af5e505f8cd6b405c46b6a55f28",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/71c289d02b480b5487e294dd044bea2974f5950c",
"reference": "71c289d02b480b5487e294dd044bea2974f5950c",
"shasum": ""
},
"require": {
@ -1842,7 +1842,7 @@
"tomasvotruba\/unused-public": "^0.3",
"tracy\/tracy": "^2.10"
},
"time": "2023-12-26T14:34:09+00:00",
"time": "2023-12-29T19:57:13+00:00",
"default-branch": true,
"type": "rector-extension",
"extra": {

File diff suppressed because one or more lines are too long

View File

@ -9,7 +9,7 @@ namespace Rector\RectorInstaller;
*/
final class GeneratedConfig
{
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => NULL, 'version' => 'dev-main a14848d'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => NULL, 'version' => 'dev-main 90fbaf5'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => NULL, 'version' => 'dev-main e5120ff'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => NULL, 'version' => 'dev-main 1f44b34'));
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => NULL, 'version' => 'dev-main a14848d'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => NULL, 'version' => 'dev-main 90fbaf5'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => NULL, 'version' => 'dev-main 71c289d'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => NULL, 'version' => 'dev-main 1f44b34'));
private function __construct()
{
}

View File

@ -5,6 +5,7 @@ namespace RectorPrefix202312;
use Rector\Config\RectorConfig;
use Rector\PHPUnit\PHPUnit100\Rector\Class_\AddProphecyTraitRector;
use Rector\PHPUnit\PHPUnit100\Rector\Class_\PublicDataProviderClassMethodRector;
use Rector\PHPUnit\PHPUnit100\Rector\Class_\StaticDataProviderClassMethodRector;
use Rector\PHPUnit\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector;
use Rector\PHPUnit\PHPUnit100\Rector\MethodCall\RemoveSetMethodsMethodCallRector;
@ -14,7 +15,7 @@ use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
return static function (RectorConfig $rectorConfig) : void {
$rectorConfig->sets([PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES]);
$rectorConfig->rules([StaticDataProviderClassMethodRector::class, PropertyExistsWithoutAssertRector::class, AddProphecyTraitRector::class, WithConsecutiveRector::class, RemoveSetMethodsMethodCallRector::class]);
$rectorConfig->rules([StaticDataProviderClassMethodRector::class, PublicDataProviderClassMethodRector::class, PropertyExistsWithoutAssertRector::class, AddProphecyTraitRector::class, WithConsecutiveRector::class, RemoveSetMethodsMethodCallRector::class]);
$rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [
// https://github.com/sebastianbergmann/phpunit/issues/4087
new MethodCallRename('PHPUnit\\Framework\\Assert', 'assertRegExp', 'assertMatchesRegularExpression'),

View File

@ -1,4 +1,4 @@
# 51 Rules Overview
# 52 Rules Overview
## AddDoesNotPerformAssertionToNonAssertingTestRector
@ -712,6 +712,34 @@ Turns PHPUnit TestCase assertObjectHasAttribute into `property_exists` compariso
<br>
## PublicDataProviderClassMethodRector
Change data provider methods to public
- class: [`Rector\PHPUnit\PHPUnit100\Rector\Class_\PublicDataProviderClassMethodRector`](../rules/PHPUnit100/Rector/Class_/PublicDataProviderClassMethodRector.php)
```diff
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
/**
* @dataProvider provideData()
*/
public function test()
{
}
- protected static function provideData()
+ public static function provideData()
{
yield [1];
}
}
```
<br>
## RemoveDataProviderTestPrefixRector
Data provider methods cannot start with "test" prefix

View File

@ -0,0 +1,115 @@
<?php
declare (strict_types=1);
namespace Rector\PHPUnit\PHPUnit100\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\Rector\AbstractRector;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\NodeFinder\DataProviderClassMethodFinder;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\PublicDataProviderClassMethodRector\PublicDataProviderClassMethodRectorTest
*/
final class PublicDataProviderClassMethodRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer
*/
private $testsNodeAnalyzer;
/**
* @readonly
* @var \Rector\PHPUnit\NodeFinder\DataProviderClassMethodFinder
*/
private $dataProviderClassMethodFinder;
/**
* @readonly
* @var \Rector\Privatization\NodeManipulator\VisibilityManipulator
*/
private $visibilityManipulator;
public function __construct(TestsNodeAnalyzer $testsNodeAnalyzer, DataProviderClassMethodFinder $dataProviderClassMethodFinder, VisibilityManipulator $visibilityManipulator)
{
$this->testsNodeAnalyzer = $testsNodeAnalyzer;
$this->dataProviderClassMethodFinder = $dataProviderClassMethodFinder;
$this->visibilityManipulator = $visibilityManipulator;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change data provider methods to public', [new CodeSample(<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
/**
* @dataProvider provideData()
*/
public function test()
{
}
protected static function provideData()
{
yield [1];
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
/**
* @dataProvider provideData()
*/
public function test()
{
}
public static function provideData()
{
yield [1];
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node) : ?Node
{
if (!$this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
// 1. find all data providers
$dataProviderClassMethods = $this->dataProviderClassMethodFinder->find($node);
$hasChanged = \false;
foreach ($dataProviderClassMethods as $dataProviderClassMethod) {
if ($this->skipMethod($dataProviderClassMethod)) {
continue;
}
$this->visibilityManipulator->makePublic($dataProviderClassMethod);
$hasChanged = \true;
}
if ($hasChanged) {
return $node;
}
return null;
}
private function skipMethod(ClassMethod $classMethod) : bool
{
return $classMethod->isPublic();
}
}