mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
[CodingStyle] Remove YieldClassMethodToArrayClassMethodRector, rather PHPStorm job (#576)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
00fb10f8a5
commit
b3480900f3
|
@ -1,4 +1,4 @@
|
|||
# 476 Rules Overview
|
||||
# 475 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -14,7 +14,7 @@
|
|||
|
||||
- [CodeQualityStrict](#codequalitystrict) (1)
|
||||
|
||||
- [CodingStyle](#codingstyle) (39)
|
||||
- [CodingStyle](#codingstyle) (38)
|
||||
|
||||
- [Composer](#composer) (6)
|
||||
|
||||
|
@ -2375,7 +2375,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
$services->set(ReturnArrayClassMethodToYieldRector::class)
|
||||
->call('configure', [[
|
||||
ReturnArrayClassMethodToYieldRector::METHODS_TO_YIELDS => ValueObjectInliner::inline([
|
||||
new ReturnArrayClassMethodToYield('EventSubscriberInterface', 'getSubscribedEvents'),
|
||||
new ReturnArrayClassMethodToYield('PHPUnit\Framework\TestCase', '*provide*'),
|
||||
]),
|
||||
]]);
|
||||
};
|
||||
|
@ -2384,12 +2384,14 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
↓
|
||||
|
||||
```diff
|
||||
class SomeEventSubscriber implements EventSubscriberInterface
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class SomeTest implements TestCase
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
public static function provideData()
|
||||
{
|
||||
- return ['event' => 'callback'];
|
||||
+ yield 'event' => 'callback';
|
||||
- return [['some text']];
|
||||
+ yield ['some text'];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -2670,45 +2672,6 @@ Wrap encapsed variables in curly braces
|
|||
|
||||
<br>
|
||||
|
||||
### YieldClassMethodToArrayClassMethodRector
|
||||
|
||||
Turns yield return to array return in specific type and method
|
||||
|
||||
:wrench: **configure it!**
|
||||
|
||||
- class: [`Rector\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector`](../rules/CodingStyle/Rector/ClassMethod/YieldClassMethodToArrayClassMethodRector.php)
|
||||
|
||||
```php
|
||||
use Rector\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
|
||||
$services->set(YieldClassMethodToArrayClassMethodRector::class)
|
||||
->call('configure', [[
|
||||
YieldClassMethodToArrayClassMethodRector::METHODS_BY_TYPE => [
|
||||
'EventSubscriberInterface' => ['getSubscribedEvents'],
|
||||
|
||||
], ]]);
|
||||
};
|
||||
```
|
||||
|
||||
↓
|
||||
|
||||
```diff
|
||||
class SomeEventSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
- yield 'event' => 'callback';
|
||||
+ return ['event' => 'callback'];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## Composer
|
||||
|
||||
### AddPackageToRequireComposerRector
|
||||
|
|
|
@ -38,16 +38,16 @@
|
|||
"symfony/http-kernel": "^5.3",
|
||||
"symfony/process": "^5.3",
|
||||
"symfony/yaml": "^5.3",
|
||||
"symplify/astral": "^9.4.14",
|
||||
"symplify/autowire-array-parameter": "^9.4.14",
|
||||
"symplify/composer-json-manipulator": "^9.4.14",
|
||||
"symplify/console-color-diff": "^9.4.14",
|
||||
"symplify/package-builder": "^9.4.14",
|
||||
"symplify/rule-doc-generator-contracts": "^9.4.14",
|
||||
"symplify/simple-php-doc-parser": "^9.4.14",
|
||||
"symplify/skipper": "^9.4.14",
|
||||
"symplify/smart-file-system": "^9.4.14",
|
||||
"symplify/symfony-php-config": "^9.4.14",
|
||||
"symplify/astral": "^9.4.24",
|
||||
"symplify/autowire-array-parameter": "^9.4.24",
|
||||
"symplify/composer-json-manipulator": "^9.4.24",
|
||||
"symplify/console-color-diff": "^9.4.24",
|
||||
"symplify/package-builder": "^9.4.24",
|
||||
"symplify/rule-doc-generator-contracts": "^9.4.24",
|
||||
"symplify/simple-php-doc-parser": "^9.4.24",
|
||||
"symplify/skipper": "^9.4.24",
|
||||
"symplify/smart-file-system": "^9.4.24",
|
||||
"symplify/symfony-php-config": "^9.4.24",
|
||||
"tracy/tracy": "^2.8",
|
||||
"webmozart/assert": "^1.10"
|
||||
},
|
||||
|
@ -58,13 +58,13 @@
|
|||
"phpunit/phpunit": "^9.5",
|
||||
"rector/phpstan-rules": "^0.3.3",
|
||||
"rector/rector-generator": "^0.3",
|
||||
"symplify/coding-standard": "^9.4.14",
|
||||
"symplify/easy-ci": "^9.4.14",
|
||||
"symplify/coding-standard": "^9.4.24",
|
||||
"symplify/easy-ci": "^9.4.23",
|
||||
"symplify/easy-coding-standard": "^9.4.7",
|
||||
"symplify/easy-testing": "dev-main#049d83e as 9.5.x-dev",
|
||||
"symplify/phpstan-extensions": "^9.4.14",
|
||||
"symplify/phpstan-rules": "^9.4.14",
|
||||
"symplify/rule-doc-generator": "^9.4.14",
|
||||
"symplify/easy-testing": "^9.4.24",
|
||||
"symplify/phpstan-extensions": "^9.4.24",
|
||||
"symplify/phpstan-rules": "^9.4.24",
|
||||
"symplify/rule-doc-generator": "^9.4.24",
|
||||
"timeweb/phpstan-enum": "^2.3"
|
||||
},
|
||||
"replace": {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare(strict_types=1);
|
||||
|
||||
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
|
||||
use Rector\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector;
|
||||
use Rector\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector;
|
||||
use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
|
||||
use Rector\CodingStyle\Rector\MethodCall\UseMessageVariableForSprintfInSymfonyStyleRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
|
|
@ -7,12 +7,18 @@ namespace Rector\NodeTypeResolver\Contract;
|
|||
use PhpParser\Node;
|
||||
use PHPStan\Type\Type;
|
||||
|
||||
/**
|
||||
* @template TNode as Node
|
||||
*/
|
||||
interface NodeTypeResolverInterface
|
||||
{
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
* @return array<class-string<TNode>>
|
||||
*/
|
||||
public function getNodeClasses(): array;
|
||||
|
||||
/**
|
||||
* @param TNode $node
|
||||
*/
|
||||
public function resolve(Node $node): Type;
|
||||
}
|
||||
|
|
|
@ -36,13 +36,13 @@ final class PropertyTypeResolver implements NodeTypeResolverInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* @param Property $propertyNode
|
||||
* @param Property $node
|
||||
*/
|
||||
public function resolve(Node $propertyNode): Type
|
||||
public function resolve(Node $node): Type
|
||||
{
|
||||
// fake property to local PropertyFetch → PHPStan understands that
|
||||
$propertyFetch = new PropertyFetch(new Variable('this'), (string) $propertyNode->props[0]->name);
|
||||
$propertyFetch->setAttribute(AttributeKey::SCOPE, $propertyNode->getAttribute(AttributeKey::SCOPE));
|
||||
$propertyFetch = new PropertyFetch(new Variable('this'), (string) $node->props[0]->name);
|
||||
$propertyFetch->setAttribute(AttributeKey::SCOPE, $node->getAttribute(AttributeKey::SCOPE));
|
||||
|
||||
return $this->nodeTypeResolver->resolve($propertyFetch);
|
||||
}
|
||||
|
|
|
@ -32,11 +32,11 @@ final class TraitTypeResolver implements NodeTypeResolverInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* @param Trait_ $traitNode
|
||||
* @param Trait_ $node
|
||||
*/
|
||||
public function resolve(Node $traitNode): Type
|
||||
public function resolve(Node $node): Type
|
||||
{
|
||||
$traitName = (string) $traitNode->namespacedName;
|
||||
$traitName = (string) $node->namespacedName;
|
||||
if (! $this->reflectionProvider->hasClass($traitName)) {
|
||||
return new MixedType();
|
||||
}
|
||||
|
|
|
@ -31,5 +31,5 @@ interface TypeMapperInterface
|
|||
* @param T $type
|
||||
* @return Name|NullableType|UnionType|null
|
||||
*/
|
||||
public function mapToPhpParserNode(Type $type, TypeKind $typeTypeKind): ?Node;
|
||||
public function mapToPhpParserNode(Type $type, TypeKind $typeKind): ?Node;
|
||||
}
|
||||
|
|
11
phpstan.neon
11
phpstan.neon
|
@ -88,7 +88,6 @@ parameters:
|
|||
paths:
|
||||
- rules/Php70/EregToPcreTransformer.php
|
||||
- packages/NodeTypeResolver/NodeTypeResolver.php
|
||||
- rules/CodeQualityStrict/Rector/Variable/MoveVariableDeclarationNearReferenceRector.php
|
||||
- rules/Php80/Rector/If_/NullsafeOperatorRector.php
|
||||
- rules/Renaming/NodeManipulator/ClassRenamer.php
|
||||
- rules/DowngradePhp72/Rector/FuncCall/DowngradePregUnmatchedAsNullConstantRector.php
|
||||
|
@ -536,3 +535,13 @@ parameters:
|
|||
message: '#"@\\var_export\(new \\Rector\\Caching\\ValueObject\\CacheItem\(\$variableKey, \$data\), true\)" is forbidden to use#'
|
||||
paths:
|
||||
- packages/Caching/ValueObject/Storage/FileCacheStorage.php
|
||||
|
||||
-
|
||||
message: '#Arguments names conflicts with parent class method\: "\$(.*?)" should be "\$(.*?)"\. This will break named arguments#'
|
||||
paths:
|
||||
- src/PhpParser/Printer/BetterStandardPrinter.php
|
||||
- packages/BetterPhpDocParser/PhpDocParser/BetterPhpDocParser.php
|
||||
- packages/BetterPhpDocParser/PhpDocParser/BetterTypeParser.php
|
||||
|
||||
# resolve later
|
||||
- '#Variables "\$(.*?)" are overridden\. This can lead to unwanted bugs, please pick a different name to avoid it#'
|
||||
|
|
11
rector.php
11
rector.php
|
@ -4,8 +4,10 @@ declare(strict_types=1);
|
|||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Rector\CodingStyle\Enum\PreferenceSelfThis;
|
||||
use Rector\CodingStyle\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector;
|
||||
use Rector\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector;
|
||||
use Rector\CodingStyle\Rector\String_\SplitStringClassConstantToClassConstFetchRector;
|
||||
use Rector\CodingStyle\ValueObject\ReturnArrayClassMethodToYield;
|
||||
use Rector\Core\Configuration\Option;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Nette\Set\NetteSetList;
|
||||
|
@ -41,12 +43,12 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
]);
|
||||
|
||||
$services = $containerConfigurator->services();
|
||||
|
||||
$services->set(InferParamFromClassMethodReturnRector::class)
|
||||
->call('configure', [[
|
||||
InferParamFromClassMethodReturnRector::INFER_PARAMS_FROM_CLASS_METHOD_RETURNS => $configuration,
|
||||
]]);
|
||||
|
||||
// phpunit
|
||||
$services->set(PreferThisOrSelfMethodCallRector::class)
|
||||
->call('configure', [[
|
||||
PreferThisOrSelfMethodCallRector::TYPE_TO_PREFERENCE => [
|
||||
|
@ -54,6 +56,13 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
],
|
||||
]]);
|
||||
|
||||
$services->set(ReturnArrayClassMethodToYieldRector::class)
|
||||
->call('configure', [[
|
||||
ReturnArrayClassMethodToYieldRector::METHODS_TO_YIELDS => ValueObjectInliner::inline([
|
||||
new ReturnArrayClassMethodToYield('PHPUnit\Framework\TestCase', '*provide*'),
|
||||
]),
|
||||
]]);
|
||||
|
||||
$parameters = $containerConfigurator->parameters();
|
||||
|
||||
$parameters->set(Option::PATHS, [
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
||||
|
@ -17,7 +17,7 @@ class Fixture
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class Fixture
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class FixtureUsedOnlyAfterIf
|
||||
{
|
||||
|
@ -18,7 +18,7 @@ class FixtureUsedOnlyAfterIf
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class FixtureUsedOnlyAfterIf
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
function variableInClosure()
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ function variableInClosure()
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
function variableInClosure()
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
function variableInClosureWithReturn()
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ function variableInClosureWithReturn()
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
function variableInClosureWithReturn()
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
function variableWithMultipleUsages()
|
||||
{
|
||||
|
@ -15,7 +15,7 @@ function variableWithMultipleUsages()
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
function variableWithMultipleUsages()
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipArrayDimFetchNext
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipArrayDimFetchNextAsKey
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
final class SkipAssignExprArrayDimFetch
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipDeclaredInExtraParentPrevious
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipExprReused
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipExprVarUsed
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipInsideCondition
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipInsideLoop
|
||||
{
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
function test() {
|
||||
$title = 'abc';
|
||||
?>
|
||||
<h1>
|
||||
<?php echo $title; ?>
|
||||
</h1>
|
||||
<?php
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
/**
|
||||
* @see https://github.com/codeigniter4/CodeIgniter4/blob/2c3f9897762fba3c18f95237686c1297822cdff4/tests/system/Database/Live/FabricatorLiveTest.php#L68-L72
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipNotUsed
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
final class SkipObGetContents
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
final class SkipPassBcdiv
|
||||
{
|
||||
|
@ -10,4 +10,4 @@ final class SkipPassBcdiv
|
|||
echo bcdiv('105', '6.55957');
|
||||
echo $a;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
final class SkipPassJsonLastError
|
||||
{
|
||||
|
@ -14,4 +14,4 @@ final class SkipPassJsonLastError
|
|||
|
||||
echo $result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipPropertyInExpr
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipReassign
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipUsageInMethodCall
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipUsageInMultipleLevels
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipUsageInMultipleLevelsHasStmtBetween
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipUsageInStaticCall
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipUsedInCatch
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipUsedInNextCaseOfSwitch
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipUsedInNextElse
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipUsedInNextTernary
|
||||
{
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
class SkipUsedMultipleTimes
|
||||
{
|
|
@ -1,8 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
use Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Source\MyException;
|
||||
use Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Source\MyException;
|
||||
|
||||
class StaticCallNextException
|
||||
{
|
||||
|
@ -20,9 +20,9 @@ class StaticCallNextException
|
|||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
use Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Source\MyException;
|
||||
use Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Source\MyException;
|
||||
|
||||
class StaticCallNextException
|
||||
{
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Source;
|
||||
namespace Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Source;
|
||||
|
||||
class MyException extends \Exception
|
||||
{
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector;
|
||||
use Rector\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
|
@ -1,14 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\Fixture;
|
||||
|
||||
function test() {
|
||||
$title = 'abc';
|
||||
?>
|
||||
<h1>
|
||||
<?php echo $title; ?>
|
||||
</h1>
|
||||
<?php
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector\Fixture;
|
||||
|
||||
use Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector\Source\EventSubscriberInterface;
|
||||
|
||||
class SomeEventSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
yield 'event' => 'callback';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector\Fixture;
|
||||
|
||||
use Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector\Source\EventSubscriberInterface;
|
||||
|
||||
class SomeEventSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return ['event' => 'callback'];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector\Fixture;
|
||||
|
||||
use Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector\Source\EventSubscriberInterface;
|
||||
|
||||
class TypeDeclaration implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents(): \Iterator
|
||||
{
|
||||
yield 'event' => 'callback';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector\Fixture;
|
||||
|
||||
use Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector\Source\EventSubscriberInterface;
|
||||
|
||||
class TypeDeclaration implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return ['event' => 'callback'];
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector\Source;
|
||||
|
||||
interface EventSubscriberInterface
|
||||
{
|
||||
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class YieldClassMethodToArrayClassMethodRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Iterator<SmartFileInfo>
|
||||
*/
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
public function provideConfigFilePath(): string
|
||||
{
|
||||
return __DIR__ . '/config/configured_rule.php';
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Rector\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector;
|
||||
use Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector\Source\EventSubscriberInterface;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
$services->set(YieldClassMethodToArrayClassMethodRector::class)
|
||||
->call('configure', [[
|
||||
YieldClassMethodToArrayClassMethodRector::METHODS_BY_TYPE => [
|
||||
EventSubscriberInterface::class => ['getSubscribedEvents'],
|
||||
],
|
||||
]]);
|
||||
};
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQualityStrict\Rector\Variable;
|
||||
namespace Rector\CodeQuality\Rector\Variable;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
|
@ -18,13 +18,9 @@ use PhpParser\Node\Stmt\Expression;
|
|||
use PhpParser\Node\Stmt\For_;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\InlineHTML;
|
||||
use PhpParser\Node\Stmt\Switch_;
|
||||
use PhpParser\Node\Stmt\TryCatch;
|
||||
use PhpParser\Node\Stmt\While_;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\CodeQuality\UsageFinder\UsageInNextStmtFinder;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
|
||||
use Rector\NodeNestingScope\NodeFinder\ScopeAwareNodeFinder;
|
||||
use Rector\NodeNestingScope\ParentFinder;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
@ -32,15 +28,14 @@ use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @see \Rector\Tests\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector\MoveVariableDeclarationNearReferenceRectorTest
|
||||
* @see \Rector\Tests\CodeQuality\Rector\Variable\MoveVariableDeclarationNearReferenceRector\MoveVariableDeclarationNearReferenceRectorTest
|
||||
*/
|
||||
final class MoveVariableDeclarationNearReferenceRector extends AbstractRector
|
||||
{
|
||||
public function __construct(
|
||||
private ScopeAwareNodeFinder $scopeAwareNodeFinder,
|
||||
private SideEffectNodeDetector $sideEffectNodeDetector,
|
||||
private ReflectionProvider $reflectionProvider,
|
||||
private ParentFinder $parentFinder
|
||||
private ParentFinder $parentFinder,
|
||||
private UsageInNextStmtFinder $usageInNextStmtFinder
|
||||
) {
|
||||
}
|
||||
|
||||
|
@ -99,7 +94,7 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
if ($this->hasPropertyInExpr($expression, $parent->expr)) {
|
||||
if ($this->hasPropertyInExpr($parent->expr)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -107,13 +102,11 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
$variable = $this->getUsageInNextStmts($expression, $node);
|
||||
if (! $variable instanceof Variable) {
|
||||
$usageStmt = $this->findUsageStmt($expression, $node);
|
||||
if (! $usageStmt instanceof Node) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Node $usageStmt */
|
||||
$usageStmt = $variable->getAttribute(AttributeKey::CURRENT_STATEMENT);
|
||||
if ($this->isInsideLoopStmts($usageStmt)) {
|
||||
return null;
|
||||
}
|
||||
|
@ -134,7 +127,7 @@ CODE_SAMPLE
|
|||
return $this->isInsideCondition($expression);
|
||||
}
|
||||
|
||||
private function hasPropertyInExpr(Expression $expression, Expr $expr): bool
|
||||
private function hasPropertyInExpr(Expr $expr): bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirst(
|
||||
$expr,
|
||||
|
@ -151,39 +144,6 @@ CODE_SAMPLE
|
|||
return $this->hasReAssign($expression, $assign->expr);
|
||||
}
|
||||
|
||||
private function getUsageInNextStmts(Expression $expression, Variable $variable): ?Variable
|
||||
{
|
||||
/** @var Node|null $next */
|
||||
$next = $expression->getAttribute(AttributeKey::NEXT_NODE);
|
||||
if (! $next instanceof Node) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($next instanceof InlineHTML) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->hasCall($next)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$countFound = $this->getCountFound($next, $variable);
|
||||
if ($countFound === 0) {
|
||||
return null;
|
||||
}
|
||||
if ($countFound >= 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$nextVariable = $this->getSameVarName([$next], $variable);
|
||||
|
||||
if ($nextVariable instanceof Variable) {
|
||||
return $nextVariable;
|
||||
}
|
||||
|
||||
return $this->getSameVarNameInNexts($next, $variable);
|
||||
}
|
||||
|
||||
private function isInsideLoopStmts(Node $node): bool
|
||||
{
|
||||
$loopNode = $this->parentFinder->findByTypes(
|
||||
|
@ -262,78 +222,6 @@ CODE_SAMPLE
|
|||
return false;
|
||||
}
|
||||
|
||||
private function hasCall(Node $node): bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirst(
|
||||
$node,
|
||||
fn (Node $n): bool => $this->sideEffectNodeDetector->detectCallExpr($n)
|
||||
);
|
||||
}
|
||||
|
||||
private function getCountFound(Node $node, Variable $variable): int
|
||||
{
|
||||
$countFound = 0;
|
||||
while ($node) {
|
||||
$isFound = (bool) $this->getSameVarName([$node], $variable);
|
||||
|
||||
if ($isFound) {
|
||||
++$countFound;
|
||||
}
|
||||
|
||||
$countFound = $this->countWithElseIf($node, $variable, $countFound);
|
||||
$countFound = $this->countWithTryCatch($node, $variable, $countFound);
|
||||
$countFound = $this->countWithSwitchCase($node, $variable, $countFound);
|
||||
|
||||
/** @var Node|null $node */
|
||||
$node = $node->getAttribute(AttributeKey::NEXT_NODE);
|
||||
}
|
||||
|
||||
return $countFound;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, Node|null> $multiNodes
|
||||
*/
|
||||
private function getSameVarName(array $multiNodes, Variable $variable): ?Variable
|
||||
{
|
||||
foreach ($multiNodes as $multiNode) {
|
||||
if ($multiNode === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var Variable|null $found */
|
||||
$found = $this->betterNodeFinder->findFirst($multiNode, function (Node $n) use ($variable): bool {
|
||||
$n = $this->mayBeArrayDimFetch($n);
|
||||
if (! $n instanceof Variable) {
|
||||
return false;
|
||||
}
|
||||
return $this->isName($n, (string) $this->getName($variable));
|
||||
});
|
||||
|
||||
if ($found !== null) {
|
||||
return $found;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function getSameVarNameInNexts(Node $node, Variable $variable): ?Variable
|
||||
{
|
||||
while ($node) {
|
||||
$found = $this->getSameVarName([$node], $variable);
|
||||
|
||||
if ($found instanceof Variable) {
|
||||
return $found;
|
||||
}
|
||||
|
||||
/** @var Node|null $node */
|
||||
$node = $node->getAttribute(AttributeKey::NEXT_NODE);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function mayBeArrayDimFetch(Node $node): Node
|
||||
{
|
||||
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
|
@ -344,50 +232,13 @@ CODE_SAMPLE
|
|||
return $node;
|
||||
}
|
||||
|
||||
private function countWithElseIf(Node $node, Variable $variable, int $countFound): int
|
||||
private function findUsageStmt(Expression $expression, Variable $variable): Node | null
|
||||
{
|
||||
if (! $node instanceof If_) {
|
||||
return $countFound;
|
||||
$nextVariable = $this->usageInNextStmtFinder->getUsageInNextStmts($expression, $variable);
|
||||
if (! $nextVariable instanceof Variable) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$isFoundElseIf = (bool) $this->getSameVarName($node->elseifs, $variable);
|
||||
$isFoundElse = (bool) $this->getSameVarName([$node->else], $variable);
|
||||
|
||||
if ($isFoundElseIf || $isFoundElse) {
|
||||
++$countFound;
|
||||
}
|
||||
|
||||
return $countFound;
|
||||
}
|
||||
|
||||
private function countWithTryCatch(Node $node, Variable $variable, int $countFound): int
|
||||
{
|
||||
if (! $node instanceof TryCatch) {
|
||||
return $countFound;
|
||||
}
|
||||
|
||||
$isFoundInCatch = (bool) $this->getSameVarName($node->catches, $variable);
|
||||
$isFoundInFinally = (bool) $this->getSameVarName([$node->finally], $variable);
|
||||
|
||||
if ($isFoundInCatch || $isFoundInFinally) {
|
||||
++$countFound;
|
||||
}
|
||||
|
||||
return $countFound;
|
||||
}
|
||||
|
||||
private function countWithSwitchCase(Node $node, Variable $variable, int $countFound): int
|
||||
{
|
||||
if (! $node instanceof Switch_) {
|
||||
return $countFound;
|
||||
}
|
||||
|
||||
$isFoundInCases = (bool) $this->getSameVarName($node->cases, $variable);
|
||||
|
||||
if ($isFoundInCases) {
|
||||
++$countFound;
|
||||
}
|
||||
|
||||
return $countFound;
|
||||
return $nextVariable->getAttribute(AttributeKey::CURRENT_STATEMENT);
|
||||
}
|
||||
}
|
195
rules/CodeQuality/UsageFinder/UsageInNextStmtFinder.php
Normal file
195
rules/CodeQuality/UsageFinder/UsageInNextStmtFinder.php
Normal file
|
@ -0,0 +1,195 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\UsageFinder;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\Node\Stmt\InlineHTML;
|
||||
use PhpParser\Node\Stmt\Switch_;
|
||||
use PhpParser\Node\Stmt\TryCatch;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
final class UsageInNextStmtFinder
|
||||
{
|
||||
public function __construct(
|
||||
private SideEffectNodeDetector $sideEffectNodeDetector,
|
||||
private BetterNodeFinder $betterNodeFinder,
|
||||
private NodeNameResolver $nodeNameResolver
|
||||
) {
|
||||
}
|
||||
|
||||
public function getUsageInNextStmts(Expression $expression, Variable $variable): ?Variable
|
||||
{
|
||||
/** @var Node|null $next */
|
||||
$next = $expression->getAttribute(AttributeKey::NEXT_NODE);
|
||||
if (! $next instanceof Node) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($next instanceof InlineHTML) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->hasCall($next)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$countFound = $this->getCountFound($next, $variable);
|
||||
if ($countFound === 0) {
|
||||
return null;
|
||||
}
|
||||
if ($countFound >= 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$nextVariable = $this->getSameVarName([$next], $variable);
|
||||
|
||||
if ($nextVariable instanceof Variable) {
|
||||
return $nextVariable;
|
||||
}
|
||||
|
||||
return $this->getSameVarNameInNexts($next, $variable);
|
||||
}
|
||||
|
||||
private function hasCall(Node $node): bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirst(
|
||||
$node,
|
||||
fn (Node $n): bool => $this->sideEffectNodeDetector->detectCallExpr($n)
|
||||
);
|
||||
}
|
||||
|
||||
private function getCountFound(Node $node, Variable $variable): int
|
||||
{
|
||||
$countFound = 0;
|
||||
while ($node) {
|
||||
$isFound = (bool) $this->getSameVarName([$node], $variable);
|
||||
|
||||
if ($isFound) {
|
||||
++$countFound;
|
||||
}
|
||||
|
||||
$countFound = $this->countWithElseIf($node, $variable, $countFound);
|
||||
$countFound = $this->countWithTryCatch($node, $variable, $countFound);
|
||||
$countFound = $this->countWithSwitchCase($node, $variable, $countFound);
|
||||
|
||||
/** @var Node|null $node */
|
||||
$node = $node->getAttribute(AttributeKey::NEXT_NODE);
|
||||
}
|
||||
|
||||
return $countFound;
|
||||
}
|
||||
|
||||
private function getSameVarNameInNexts(Node $node, Variable $variable): ?Variable
|
||||
{
|
||||
while ($node) {
|
||||
$found = $this->getSameVarName([$node], $variable);
|
||||
|
||||
if ($found instanceof Variable) {
|
||||
return $found;
|
||||
}
|
||||
|
||||
/** @var Node|null $node */
|
||||
$node = $node->getAttribute(AttributeKey::NEXT_NODE);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function countWithElseIf(Node $node, Variable $variable, int $countFound): int
|
||||
{
|
||||
if (! $node instanceof If_) {
|
||||
return $countFound;
|
||||
}
|
||||
|
||||
$isFoundElseIf = (bool) $this->getSameVarName($node->elseifs, $variable);
|
||||
$isFoundElse = (bool) $this->getSameVarName([$node->else], $variable);
|
||||
|
||||
if ($isFoundElseIf || $isFoundElse) {
|
||||
++$countFound;
|
||||
}
|
||||
|
||||
return $countFound;
|
||||
}
|
||||
|
||||
private function countWithTryCatch(Node $node, Variable $variable, int $countFound): int
|
||||
{
|
||||
if (! $node instanceof TryCatch) {
|
||||
return $countFound;
|
||||
}
|
||||
|
||||
$isFoundInCatch = (bool) $this->getSameVarName($node->catches, $variable);
|
||||
$isFoundInFinally = (bool) $this->getSameVarName([$node->finally], $variable);
|
||||
|
||||
if ($isFoundInCatch || $isFoundInFinally) {
|
||||
++$countFound;
|
||||
}
|
||||
|
||||
return $countFound;
|
||||
}
|
||||
|
||||
private function countWithSwitchCase(Node $node, Variable $variable, int $countFound): int
|
||||
{
|
||||
if (! $node instanceof Switch_) {
|
||||
return $countFound;
|
||||
}
|
||||
|
||||
$isFoundInCases = (bool) $this->getSameVarName($node->cases, $variable);
|
||||
|
||||
if ($isFoundInCases) {
|
||||
++$countFound;
|
||||
}
|
||||
|
||||
return $countFound;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, Node|null> $multiNodes
|
||||
*/
|
||||
private function getSameVarName(array $multiNodes, Variable $variable): ?Variable
|
||||
{
|
||||
foreach ($multiNodes as $multiNode) {
|
||||
if ($multiNode === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var Variable|null $found */
|
||||
$found = $this->betterNodeFinder->findFirst($multiNode, function (Node $currentNode) use ($variable): bool {
|
||||
$currentNode = $this->unwrapArrayDimFetch($currentNode);
|
||||
if (! $currentNode instanceof Variable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->nodeNameResolver->isName(
|
||||
$currentNode,
|
||||
(string) $this->nodeNameResolver->getName($variable)
|
||||
);
|
||||
});
|
||||
|
||||
if ($found !== null) {
|
||||
return $found;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function unwrapArrayDimFetch(Node $node): Node
|
||||
{
|
||||
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
|
||||
while ($parent instanceof ArrayDimFetch) {
|
||||
$node = $parent->var;
|
||||
$parent = $parent->getAttribute(AttributeKey::PARENT_NODE);
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
|
@ -43,13 +43,6 @@ final class ReturnArrayClassMethodToYieldRector extends AbstractRector implement
|
|||
private NodeTransformer $nodeTransformer,
|
||||
private CommentsMerger $commentsMerger
|
||||
) {
|
||||
// default values
|
||||
$this->methodsToYields = [
|
||||
new ReturnArrayClassMethodToYield('PHPUnit\Framework\TestCase', 'provideData'),
|
||||
new ReturnArrayClassMethodToYield('PHPUnit\Framework\TestCase', 'provideData*'),
|
||||
new ReturnArrayClassMethodToYield('PHPUnit\Framework\TestCase', 'dataProvider'),
|
||||
new ReturnArrayClassMethodToYield('PHPUnit\Framework\TestCase', 'dataProvider*'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
|
@ -57,28 +50,32 @@ final class ReturnArrayClassMethodToYieldRector extends AbstractRector implement
|
|||
return new RuleDefinition('Turns array return to yield return in specific type and method', [
|
||||
new ConfiguredCodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeEventSubscriber implements EventSubscriberInterface
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class SomeTest implements TestCase
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
public static function provideData()
|
||||
{
|
||||
return ['event' => 'callback'];
|
||||
return [['some text']];
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeEventSubscriber implements EventSubscriberInterface
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class SomeTest implements TestCase
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
public static function provideData()
|
||||
{
|
||||
yield 'event' => 'callback';
|
||||
yield ['some text'];
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
[
|
||||
self::METHODS_TO_YIELDS => [
|
||||
new ReturnArrayClassMethodToYield('EventSubscriberInterface', 'getSubscribedEvents'),
|
||||
new ReturnArrayClassMethodToYield('PHPUnit\Framework\TestCase', '*provide*'),
|
||||
],
|
||||
]
|
||||
),
|
||||
|
|
|
@ -1,144 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Yield_;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\PhpParser\NodeTransformer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
||||
/**
|
||||
* @changelog https://medium.com/tech-tajawal/use-memory-gently-with-yield-in-php-7e62e2480b8d
|
||||
* @see https://3v4l.org/5PJid
|
||||
*
|
||||
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector\YieldClassMethodToArrayClassMethodRectorTest
|
||||
*/
|
||||
final class YieldClassMethodToArrayClassMethodRector extends AbstractRector implements ConfigurableRectorInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const METHODS_BY_TYPE = 'methods_by_type';
|
||||
|
||||
/**
|
||||
* @param array<class-string, string[]> $methodsByType
|
||||
*/
|
||||
public function __construct(
|
||||
private NodeTransformer $nodeTransformer,
|
||||
private array $methodsByType = []
|
||||
) {
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Turns yield return to array return in specific type and method', [
|
||||
new ConfiguredCodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeEventSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
yield 'event' => 'callback';
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeEventSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return ['event' => 'callback'];
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
[
|
||||
self::METHODS_BY_TYPE => [
|
||||
'EventSubscriberInterface' => ['getSubscribedEvents'],
|
||||
],
|
||||
]
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassMethod::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
*/
|
||||
public function refactor(Node $node): ClassMethod
|
||||
{
|
||||
foreach ($this->methodsByType as $type => $methods) {
|
||||
if (! $this->isObjectType($node, new ObjectType($type))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($methods as $method) {
|
||||
if (! $this->isName($node, $method)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$yieldNodes = $this->collectYieldNodesFromClassMethod($node);
|
||||
if ($yieldNodes === []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$arrayNode = $this->nodeTransformer->transformYieldsToArray($yieldNodes);
|
||||
$this->removeNodes($yieldNodes);
|
||||
|
||||
$node->returnType = new Identifier('array');
|
||||
|
||||
$returnExpression = new Return_($arrayNode);
|
||||
$node->stmts = array_merge((array) $node->stmts, [$returnExpression]);
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
public function configure(array $configuration): void
|
||||
{
|
||||
$this->methodsByType = $configuration[self::METHODS_BY_TYPE] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Yield_[]
|
||||
*/
|
||||
private function collectYieldNodesFromClassMethod(ClassMethod $classMethod): array
|
||||
{
|
||||
$yieldNodes = [];
|
||||
|
||||
if ($classMethod->stmts === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
foreach ($classMethod->stmts as $statement) {
|
||||
if (! $statement instanceof Expression) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($statement->expr instanceof Yield_) {
|
||||
$yieldNodes[] = $statement->expr;
|
||||
}
|
||||
}
|
||||
|
||||
return $yieldNodes;
|
||||
}
|
||||
}
|
|
@ -32,7 +32,7 @@ final class PreferThisOrSelfMethodCallRector extends AbstractRector implements C
|
|||
private const SELF = 'self';
|
||||
|
||||
/**
|
||||
* @var array<class-string, PreferenceSelfThis>
|
||||
* @var array<PreferenceSelfThis>
|
||||
*/
|
||||
private array $typeToPreference = [];
|
||||
|
||||
|
@ -99,7 +99,7 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
/**
|
||||
* @param array<string, array<class-string, PreferenceSelfThis>> $configuration
|
||||
* @param array<string, PreferenceSelfThis[]> $configuration
|
||||
*/
|
||||
public function configure(array $configuration): void
|
||||
{
|
||||
|
|
|
@ -12,7 +12,6 @@ use PhpParser\Node\Expr\Variable;
|
|||
use Rector\Core\NodeAnalyzer\CompactFuncCallAnalyzer;
|
||||
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
||||
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
|
||||
final class ExprUsedInNodeAnalyzer
|
||||
{
|
||||
|
@ -20,7 +19,6 @@ final class ExprUsedInNodeAnalyzer
|
|||
private NodeComparator $nodeComparator,
|
||||
private UsedVariableNameAnalyzer $usedVariableNameAnalyzer,
|
||||
private CompactFuncCallAnalyzer $compactFuncCallAnalyzer,
|
||||
private NodeNameResolver $nodeNameResolver,
|
||||
private BetterStandardPrinter $betterStandardPrinter
|
||||
) {
|
||||
}
|
||||
|
|
|
@ -95,7 +95,7 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
if ($this->propertyManipulator->isPropertyUsedInReadContext($param)) {
|
||||
return null;
|
||||
continue;
|
||||
}
|
||||
|
||||
// only private local scope; removing public property might be dangerous
|
||||
|
|
|
@ -14,7 +14,6 @@ use PhpParser\Node\Expr\List_;
|
|||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Foreach_;
|
||||
use Rector\CodeQuality\NodeAnalyzer\ForeachAnalyzer;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Naming\ExpectedNameResolver\InflectorSingularResolver;
|
||||
use Rector\Naming\Naming\VariableNaming;
|
||||
|
@ -29,7 +28,6 @@ final class DowngradeKeysInListRector extends AbstractRector
|
|||
{
|
||||
public function __construct(
|
||||
private InflectorSingularResolver $inflectorSingularResolver,
|
||||
private ForeachAnalyzer $foreachAnalyzer,
|
||||
private VariableNaming $variableNaming
|
||||
) {
|
||||
}
|
||||
|
|
|
@ -10,10 +10,8 @@ use PHPStan\Type\ObjectType;
|
|||
use PHPStan\Type\StaticType;
|
||||
use PHPStan\Type\Type;
|
||||
use PHPStan\Type\TypeWithClassName;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Naming\RectorNamingInflector;
|
||||
use Rector\Naming\ValueObject\ExpectedName;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\NodeTypeResolver;
|
||||
use Rector\PHPStanStaticTypeMapper\Utils\TypeUnwrapper;
|
||||
use Rector\StaticTypeMapper\ValueObject\Type\SelfObjectType;
|
||||
|
@ -51,8 +49,6 @@ final class PropertyNaming
|
|||
public function __construct(
|
||||
private TypeUnwrapper $typeUnwrapper,
|
||||
private RectorNamingInflector $rectorNamingInflector,
|
||||
private BetterNodeFinder $betterNodeFinder,
|
||||
private NodeNameResolver $nodeNameResolver,
|
||||
private NodeTypeResolver $nodeTypeResolver,
|
||||
private ReflectionProvider $reflectionProvider
|
||||
) {
|
||||
|
|
|
@ -17,15 +17,13 @@ use PhpParser\Node\UnionType;
|
|||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\Core\PhpParser\AstResolver;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
|
||||
final class CallerParamMatcher
|
||||
{
|
||||
public function __construct(
|
||||
private NodeNameResolver $nodeNameResolver,
|
||||
private AstResolver $astResolver,
|
||||
private BetterNodeFinder $betterNodeFinder
|
||||
private AstResolver $astResolver
|
||||
) {
|
||||
}
|
||||
|
||||
|
|
|
@ -17,17 +17,15 @@ use PHPStan\Type\StringType;
|
|||
use PHPStan\Type\Type;
|
||||
use PHPStan\Type\TypeTraverser;
|
||||
use PHPStan\Type\UnionType;
|
||||
use Symplify\PackageBuilder\Parameter\ParameterProvider;
|
||||
|
||||
final class GenericClassStringTypeNormalizer
|
||||
{
|
||||
public function __construct(
|
||||
private ReflectionProvider $reflectionProvider,
|
||||
private ParameterProvider $parameterProvider
|
||||
private ReflectionProvider $reflectionProvider
|
||||
) {
|
||||
}
|
||||
|
||||
public function normalize(Type $type): Type
|
||||
public function normalize(Type $type): ArrayType | UnionType | Type
|
||||
{
|
||||
$type = TypeTraverser::map($type, function (Type $type, $callback): Type {
|
||||
if (! $type instanceof ConstantStringType) {
|
||||
|
|
|
@ -11,16 +11,12 @@ use PhpParser\Node\Param;
|
|||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\Parser;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
|
||||
use Rector\Core\PhpParser\AstResolver;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Core\Reflection\ReflectionResolver;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
|
||||
use Symplify\SmartFileSystem\SmartFileSystem;
|
||||
|
||||
final class PropertyFetchFinder
|
||||
{
|
||||
|
@ -28,10 +24,6 @@ final class PropertyFetchFinder
|
|||
private BetterNodeFinder $betterNodeFinder,
|
||||
private NodeNameResolver $nodeNameResolver,
|
||||
private ReflectionProvider $reflectionProvider,
|
||||
private ReflectionResolver $reflectionResolver,
|
||||
private SmartFileSystem $smartFileSystem,
|
||||
private Parser $parser,
|
||||
private NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator,
|
||||
private AstResolver $astResolver,
|
||||
private ClassAnalyzer $classAnalyzer
|
||||
) {
|
||||
|
|
|
@ -59,31 +59,6 @@ final class NodeTransformer
|
|||
return new Array_($arrayMessageParts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Yield_[]|Expression[] $yieldNodes
|
||||
*/
|
||||
public function transformYieldsToArray(array $yieldNodes): Array_
|
||||
{
|
||||
$arrayItems = [];
|
||||
foreach ($yieldNodes as $yieldNode) {
|
||||
if ($yieldNode instanceof Expression) {
|
||||
$yieldNode = $yieldNode->expr;
|
||||
}
|
||||
|
||||
if (! $yieldNode instanceof Yield_) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($yieldNode->value === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$arrayItems[] = new ArrayItem($yieldNode->value, $yieldNode->key);
|
||||
}
|
||||
|
||||
return new Array_($arrayItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Expression[]
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue
Block a user