mirror of https://github.com/rectorphp/rector.git
Compare commits
181 Commits
Author | SHA1 | Date |
---|---|---|
Tomas Votruba | 4cadabfe9a | |
Tomas Votruba | 3f56884569 | |
Tomas Votruba | 22d3d1949d | |
Tomas Votruba | 62a900a1c0 | |
Tomas Votruba | b98d0a8754 | |
Tomas Votruba | 7ac6f7bf7e | |
Tomas Votruba | 556509e2dc | |
Tomas Votruba | dfaafdbd3a | |
Tomas Votruba | dd1dd510f5 | |
Tomas Votruba | c6998bfca9 | |
Tomas Votruba | 264e16fdae | |
Tomas Votruba | 1931c635c0 | |
Tomas Votruba | df8a377557 | |
Tomas Votruba | 6372b90b19 | |
Tomas Votruba | 6cf705918e | |
Tomas Votruba | 3c1e41f006 | |
Tomas Votruba | 7d70c5853e | |
Tomas Votruba | 049336a81a | |
Tomas Votruba | efe89cff77 | |
Tomas Votruba | 89f91b63be | |
Tomas Votruba | 5dfd1c664c | |
Tomas Votruba | 4bcc8bbea0 | |
Tomas Votruba | faba2d1971 | |
Tomas Votruba | c74c9ca74f | |
Tomas Votruba | b5b172a4b9 | |
Tomas Votruba | 49d837e2a6 | |
Tomas Votruba | af81b5db1b | |
Tomas Votruba | 5ca50de6b7 | |
Tomas Votruba | efb3c476c7 | |
Tomas Votruba | 73eb63e4f9 | |
Tomas Votruba | 322f3ad55c | |
Tomas Votruba | fe891363d5 | |
Tomas Votruba | f7723ed647 | |
Tomas Votruba | ed1310b8dc | |
Tomas Votruba | 9da012cd97 | |
Tomas Votruba | dcdff77130 | |
Tomas Votruba | 582a864f33 | |
Tomas Votruba | 2dc9d28496 | |
Tomas Votruba | 5c55f54e78 | |
Tomas Votruba | 87c26777cb | |
Tomas Votruba | 19b93eeeab | |
Tomas Votruba | fb4bddec05 | |
Tomas Votruba | ee17c27a72 | |
Tomas Votruba | 346b5723b4 | |
Tomas Votruba | 029097acda | |
Tomas Votruba | 12c115e6f6 | |
Tomas Votruba | b70df4cb6b | |
Tomas Votruba | 78e3d54584 | |
Tomas Votruba | c747b603a3 | |
Tomas Votruba | aff937886f | |
Tomas Votruba | 343a691722 | |
Tomas Votruba | e9ca0b2469 | |
Tomas Votruba | 7141ae6e56 | |
Tomas Votruba | d5e203a6e9 | |
Tomas Votruba | a3b725126c | |
Tomas Votruba | 09acbdda13 | |
Tomas Votruba | fdfd07f062 | |
Tomas Votruba | c041415e42 | |
Tomas Votruba | 3e1d355024 | |
Tomas Votruba | 6cbc8ec497 | |
Tomas Votruba | 1c323223b2 | |
Tomas Votruba | 655c2aa498 | |
Tomas Votruba | 584e892a51 | |
Tomas Votruba | b9839a24cb | |
Tomas Votruba | 1e8aeb3de9 | |
Tomas Votruba | 8aefc5e967 | |
Tomas Votruba | 6db496ec6b | |
Tomas Votruba | 24418026e3 | |
Tomas Votruba | e01fd7276e | |
Tomas Votruba | 61037dab6c | |
Tomas Votruba | f42da12667 | |
Tomas Votruba | 83bfbc18b8 | |
Tomas Votruba | b7b08c4396 | |
Tomas Votruba | 4f94d24171 | |
Tomas Votruba | 59f76a0fe3 | |
Tomas Votruba | ea0464beb9 | |
Tomas Votruba | f3e4fee4bc | |
Tomas Votruba | 284531ceed | |
Tomas Votruba | 2b9acd03a1 | |
Tomas Votruba | 885a3f6ed3 | |
Tomas Votruba | 7d4734bb64 | |
Tomas Votruba | 7f89a08ca2 | |
Tomas Votruba | 51fa5a731d | |
Tomas Votruba | 3a03381bf8 | |
Tomas Votruba | 6059061e25 | |
Tomas Votruba | f2cac5e2a0 | |
Tomas Votruba | 76a58d63f0 | |
Tomas Votruba | 0c2b95ea62 | |
Tomas Votruba | f53efcffe8 | |
Tomas Votruba | 67791a9582 | |
Tomas Votruba | 9427cdb84c | |
Tomas Votruba | d495dc4fe7 | |
Tomas Votruba | e56fb5adb0 | |
Tomas Votruba | 415f0bbf34 | |
Tomas Votruba | 8fba5b95f7 | |
Tomas Votruba | 53fb366ec6 | |
Tomas Votruba | 6fb3352189 | |
Tomas Votruba | 530c01c201 | |
Tomas Votruba | 707e3d8942 | |
Tomas Votruba | b8bbd024cd | |
Tomas Votruba | 6678ca05d4 | |
Tomas Votruba | 6e04d0eb08 | |
Tomas Votruba | d59efe6a88 | |
Tomas Votruba | e004423e08 | |
Tomas Votruba | f81fa92915 | |
Tomas Votruba | d36c8ad1a5 | |
Tomas Votruba | b352c8473a | |
Tomas Votruba | 6e65fef1e7 | |
Tomas Votruba | 81fd362a59 | |
Tomas Votruba | a210882e56 | |
Tomas Votruba | 77aab2e2a3 | |
Tomas Votruba | d4fd813b33 | |
Tomas Votruba | 36ab30b476 | |
Tomas Votruba | 3fdaba944f | |
Tomas Votruba | dc2feca4d5 | |
Tomas Votruba | 717507e265 | |
Tomas Votruba | 9f2e48fa07 | |
Tomas Votruba | bc02e10427 | |
Tomas Votruba | dc2205094c | |
Tomas Votruba | 664f4e3a6b | |
Tomas Votruba | 755e0f4a8c | |
Tomas Votruba | 9369a862b6 | |
Tomas Votruba | 9b731daab1 | |
Tomas Votruba | 0a7d532276 | |
Tomas Votruba | f2feb272ed | |
Tomas Votruba | 42078c4127 | |
Tomas Votruba | 7af3ed15e0 | |
Tomas Votruba | 30adaf3fc0 | |
Tomas Votruba | c23b2384d3 | |
Tomas Votruba | e538a96c9b | |
Tomas Votruba | 045db2fd56 | |
Tomas Votruba | 014254f0df | |
Tomas Votruba | 4c2b400cdb | |
Tomas Votruba | cc37bbf217 | |
Tomas Votruba | be393d497b | |
Tomas Votruba | 078e4b92d7 | |
Tomas Votruba | ca1cc37af8 | |
Tomas Votruba | 5d8e196d67 | |
Tomas Votruba | 52addb1dde | |
Tomas Votruba | e14c8a528b | |
Tomas Votruba | bb8b99d70c | |
Tomas Votruba | 0e57251e46 | |
Tomas Votruba | e18e5ef364 | |
Tomas Votruba | 74b8ed7cd4 | |
Tomas Votruba | e6bfc79aa2 | |
Tomas Votruba | 024ebc62a1 | |
Tomas Votruba | e10d636f62 | |
Tomas Votruba | ce9cc3aafc | |
Tomas Votruba | dd51f7ceed | |
Tomas Votruba | 11b9220a05 | |
Tomas Votruba | 9811962c70 | |
Tomas Votruba | de2b2cf2b4 | |
Tomas Votruba | 1a710b92cc | |
Tomas Votruba | c1338c4c07 | |
Tomas Votruba | 50c2e61f1d | |
Tomas Votruba | 7a69e542ca | |
Tomas Votruba | c950e4d97b | |
Tomas Votruba | 00697fa4b6 | |
Tomas Votruba | 817ab2f7cb | |
Tomas Votruba | b38b8f3e85 | |
Tomas Votruba | e94ff85194 | |
Tomas Votruba | 5ac9de7a83 | |
Tomas Votruba | ceff1cad27 | |
Tomas Votruba | ac990f2560 | |
Tomas Votruba | dac0717196 | |
Tomas Votruba | d028a05e61 | |
Tomas Votruba | b1eb18f499 | |
Tomas Votruba | 0268ca2b1c | |
Tomas Votruba | 8d1aee03a3 | |
Tomas Votruba | 51090bc214 | |
Tomas Votruba | b50605a40a | |
Tomas Votruba | 2bf9bab235 | |
Tomas Votruba | 29535d04eb | |
Tomas Votruba | dd371cdca2 | |
Tomas Votruba | d5d197b9bf | |
Tomas Votruba | 9ab2b8bc07 | |
Tomas Votruba | ae50c0e5a3 | |
Tomas Votruba | d3357ab3eb | |
Tomas Votruba | c0820093ad | |
Tomas Votruba | edd8901ea0 | |
Tomas Votruba | d7faacd4a0 |
|
@ -101,6 +101,7 @@ Among there projects belong:
|
|||
* [cakephp/upgrade](https://github.com/cakephp/upgrade)
|
||||
* [driftingly/rector-laravel](https://github.com/driftingly/rector-laravel)
|
||||
* [contao/contao-rector](https://github.com/contao/contao-rector)
|
||||
* [php-static-analysis/rector-rule](https://github.com/php-static-analysis/rector-rule)
|
||||
|
||||
<br>
|
||||
|
||||
|
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
// this is part of downgrade build
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\FileSystem;
|
||||
use RectorPrefix202403\Nette\Utils\Json;
|
||||
use RectorPrefix202405\Nette\Utils\FileSystem;
|
||||
use RectorPrefix202405\Nette\Utils\Json;
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
$composerJsonFileContents = FileSystem::read(__DIR__ . '/../composer.json');
|
||||
$composerJson = Json::decode($composerJsonFileContents, Json::FORCE_ARRAY);
|
||||
$composerJson = Json::decode($composerJsonFileContents, \true);
|
||||
$composerJson['replace']['phpstan/phpstan'] = $composerJson['require']['phpstan/phpstan'];
|
||||
$modifiedComposerJsonFileContents = Json::encode($composerJson, Json::PRETTY);
|
||||
$modifiedComposerJsonFileContents = Json::encode($composerJson, \true);
|
||||
FileSystem::write(__DIR__ . '/../composer.json', $modifiedComposerJsonFileContents, null);
|
||||
echo 'Done!' . \PHP_EOL;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#!/usr/bin/env php
|
||||
<?php
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
require_once __DIR__ . '/rector.php';
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Json;
|
||||
use RectorPrefix202405\Nette\Utils\Json;
|
||||
use Rector\Bootstrap\RectorConfigsResolver;
|
||||
use Rector\ChangesReporting\Output\JsonOutputFormatter;
|
||||
use Rector\Configuration\Option;
|
||||
|
@ -11,9 +11,9 @@ use Rector\Console\Style\SymfonyStyleFactory;
|
|||
use Rector\DependencyInjection\LazyContainerFactory;
|
||||
use Rector\DependencyInjection\RectorContainerFactory;
|
||||
use Rector\Util\Reflection\PrivatesAccessor;
|
||||
use RectorPrefix202403\Symfony\Component\Console\Application;
|
||||
use RectorPrefix202403\Symfony\Component\Console\Command\Command;
|
||||
use RectorPrefix202403\Symfony\Component\Console\Input\ArgvInput;
|
||||
use RectorPrefix202405\Symfony\Component\Console\Application;
|
||||
use RectorPrefix202405\Symfony\Component\Console\Command\Command;
|
||||
use RectorPrefix202405\Symfony\Component\Console\Input\ArgvInput;
|
||||
// @ intentionally: continue anyway
|
||||
@\ini_set('memory_limit', '-1');
|
||||
// Performance boost
|
||||
|
@ -93,7 +93,7 @@ final class AutoloadIncluder
|
|||
require_once $filePath;
|
||||
}
|
||||
}
|
||||
\class_alias('RectorPrefix202403\\AutoloadIncluder', 'AutoloadIncluder', \false);
|
||||
\class_alias('RectorPrefix202405\\AutoloadIncluder', 'AutoloadIncluder', \false);
|
||||
if (\file_exists(__DIR__ . '/../preload.php') && \is_dir(__DIR__ . '/../vendor')) {
|
||||
require_once __DIR__ . '/../preload.php';
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
],
|
||||
"require": {
|
||||
"php": "^7.2|^8.0",
|
||||
"phpstan/phpstan": "^1.10.57"
|
||||
"phpstan/phpstan": "^1.11"
|
||||
},
|
||||
"autoload": {
|
||||
"files": [
|
||||
|
@ -22,5 +22,8 @@
|
|||
"rector/rector-downgrade-php": "*"
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"prefer-stable": true
|
||||
"prefer-stable": true,
|
||||
"suggest": {
|
||||
"ext-dom": "To manipulate phpunit.xml via the custom-rule command"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use RectorPrefix202403\OndraM\CiDetector\CiDetector;
|
||||
use RectorPrefix202405\OndraM\CiDetector\CiDetector;
|
||||
use Rector\Bootstrap\ExtensionConfigResolver;
|
||||
use Rector\Caching\ValueObject\Storage\MemoryCacheStorage;
|
||||
use Rector\Config\RectorConfig;
|
||||
|
@ -31,4 +31,6 @@ return static function (RectorConfig $rectorConfig) : void {
|
|||
foreach ($extensionConfigResolver->provide() as $extensionConfigFile) {
|
||||
$rectorConfig->import($extensionConfigFile);
|
||||
}
|
||||
// use original php-parser printer to avoid BC break on fluent call
|
||||
$rectorConfig->newLineOnFluentCall(\false);
|
||||
};
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector;
|
||||
use Rector\CodeQuality\Rector\Assign\CombinedAssignRector;
|
||||
|
@ -14,6 +14,7 @@ use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
|
|||
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
|
||||
use Rector\CodeQuality\Rector\Class_\StaticToSelfStaticMethodCallOnFinalClassRector;
|
||||
use Rector\CodeQuality\Rector\ClassConstFetch\ConvertStaticPrivateConstantToSelfRector;
|
||||
use Rector\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector;
|
||||
use Rector\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector;
|
||||
use Rector\CodeQuality\Rector\ClassMethod\LocallyCalledStaticMethodToNonStaticRector;
|
||||
use Rector\CodeQuality\Rector\ClassMethod\OptionalParametersAfterRequiredRector;
|
||||
|
@ -28,13 +29,10 @@ use Rector\CodeQuality\Rector\Foreach_\ForeachToInArrayRector;
|
|||
use Rector\CodeQuality\Rector\Foreach_\SimplifyForeachToCoalescingRector;
|
||||
use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\ArrayMergeOfNonArraysToSimpleArrayRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\BoolvalToTypeCastRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\CallUserFuncWithArrowFunctionToInlineRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\CompactToVariablesRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\FloatvalToTypeCastRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\InlineIsAInstanceOfRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\IsAWithStringWithThirdArgumentRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\RemoveSoleValueSprintfRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\SetTypeToCastRector;
|
||||
|
@ -43,7 +41,6 @@ use Rector\CodeQuality\Rector\FuncCall\SimplifyInArrayValuesRector;
|
|||
use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\SingleInArrayToCompareRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\StrvalToTypeCastRector;
|
||||
use Rector\CodeQuality\Rector\FuncCall\UnwrapSprintfOneArgumentRector;
|
||||
use Rector\CodeQuality\Rector\FunctionLike\SimplifyUselessVariableRector;
|
||||
use Rector\CodeQuality\Rector\Identical\BooleanNotIdenticalToNotIdenticalRector;
|
||||
|
@ -111,5 +108,5 @@ return static function (RectorConfig $rectorConfig) : void {
|
|||
'mbstrrpos' => 'mb_strrpos',
|
||||
'mbsubstr' => 'mb_substr',
|
||||
]);
|
||||
$rectorConfig->rules([CombinedAssignRector::class, SimplifyEmptyArrayCheckRector::class, ReplaceMultipleBooleanNotRector::class, ForeachToInArrayRector::class, SimplifyForeachToCoalescingRector::class, SimplifyFuncGetArgsCountRector::class, SimplifyInArrayValuesRector::class, SimplifyStrposLowerRector::class, GetClassToInstanceOfRector::class, SimplifyArraySearchRector::class, SimplifyConditionsRector::class, SimplifyIfNotNullReturnRector::class, SimplifyIfReturnBoolRector::class, SimplifyUselessVariableRector::class, UnnecessaryTernaryExpressionRector::class, RemoveExtraParametersRector::class, SimplifyDeMorganBinaryRector::class, SimplifyTautologyTernaryRector::class, SingleInArrayToCompareRector::class, SimplifyIfElseToTernaryRector::class, JoinStringConcatRector::class, ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class, ExplicitBoolCompareRector::class, CombineIfRector::class, UseIdenticalOverEqualWithSameTypeRector::class, SimplifyBoolIdenticalTrueRector::class, SimplifyRegexPatternRector::class, BooleanNotIdenticalToNotIdenticalRector::class, StrvalToTypeCastRector::class, FloatvalToTypeCastRector::class, CallableThisArrayToAnonymousFunctionRector::class, AndAssignsToSeparateLinesRector::class, CompactToVariablesRector::class, CompleteDynamicPropertiesRector::class, IsAWithStringWithThirdArgumentRector::class, StrlenZeroToIdenticalEmptyStringRector::class, ThrowWithPreviousExceptionRector::class, RemoveSoleValueSprintfRector::class, ShortenElseIfRector::class, ArrayMergeOfNonArraysToSimpleArrayRector::class, IntvalToTypeCastRector::class, BoolvalToTypeCastRector::class, ArrayKeyExistsTernaryThenValueToCoalescingRector::class, AbsolutizeRequireAndIncludePathRector::class, ChangeArrayPushToArrayAssignRector::class, ForRepeatedCountToOwnVariableRector::class, ForeachItemsAssignToEmptyArrayToAssignRector::class, InlineIfToExplicitIfRector::class, UnusedForeachValueToArrayKeysRector::class, CommonNotEqualRector::class, SetTypeToCastRector::class, LogicalToBooleanRector::class, VarToPublicPropertyRector::class, IssetOnPropertyObjectToPropertyExistsRector::class, NewStaticToNewSelfRector::class, UnwrapSprintfOneArgumentRector::class, SwitchNegatedTernaryRector::class, SingularSwitchToIfRector::class, SimplifyIfNullableReturnRector::class, FuncGetArgsToVariadicParamRector::class, CallUserFuncToMethodCallRector::class, CallUserFuncWithArrowFunctionToInlineRector::class, CountArrayToEmptyArrayComparisonRector::class, FlipTypeControlToUseExclusiveTypeRector::class, InlineArrayReturnAssignRector::class, InlineIsAInstanceOfRector::class, TernaryFalseExpressionToIfRector::class, InlineConstructorDefaultToPropertyRector::class, TernaryEmptyArrayArrayDimFetchToCoalesceRector::class, OptionalParametersAfterRequiredRector::class, SimplifyEmptyCheckOnEmptyArrayRector::class, SwitchTrueToIfRector::class, CleanupUnneededNullsafeOperatorRector::class, DisallowedEmptyRuleFixerRector::class, ConvertStaticPrivateConstantToSelfRector::class, LocallyCalledStaticMethodToNonStaticRector::class, NumberCompareToMaxFuncCallRector::class, CompleteMissingIfElseBracketRector::class, RemoveUselessIsObjectCheckRector::class, StaticToSelfStaticMethodCallOnFinalClassRector::class]);
|
||||
$rectorConfig->rules([CombinedAssignRector::class, SimplifyEmptyArrayCheckRector::class, ReplaceMultipleBooleanNotRector::class, ForeachToInArrayRector::class, SimplifyForeachToCoalescingRector::class, SimplifyFuncGetArgsCountRector::class, SimplifyInArrayValuesRector::class, SimplifyStrposLowerRector::class, GetClassToInstanceOfRector::class, SimplifyArraySearchRector::class, SimplifyConditionsRector::class, SimplifyIfNotNullReturnRector::class, SimplifyIfReturnBoolRector::class, SimplifyUselessVariableRector::class, UnnecessaryTernaryExpressionRector::class, RemoveExtraParametersRector::class, SimplifyDeMorganBinaryRector::class, SimplifyTautologyTernaryRector::class, SingleInArrayToCompareRector::class, SimplifyIfElseToTernaryRector::class, JoinStringConcatRector::class, ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class, ExplicitBoolCompareRector::class, CombineIfRector::class, UseIdenticalOverEqualWithSameTypeRector::class, SimplifyBoolIdenticalTrueRector::class, SimplifyRegexPatternRector::class, BooleanNotIdenticalToNotIdenticalRector::class, CallableThisArrayToAnonymousFunctionRector::class, AndAssignsToSeparateLinesRector::class, CompactToVariablesRector::class, CompleteDynamicPropertiesRector::class, IsAWithStringWithThirdArgumentRector::class, StrlenZeroToIdenticalEmptyStringRector::class, ThrowWithPreviousExceptionRector::class, RemoveSoleValueSprintfRector::class, ShortenElseIfRector::class, ExplicitReturnNullRector::class, ArrayMergeOfNonArraysToSimpleArrayRector::class, ArrayKeyExistsTernaryThenValueToCoalescingRector::class, AbsolutizeRequireAndIncludePathRector::class, ChangeArrayPushToArrayAssignRector::class, ForRepeatedCountToOwnVariableRector::class, ForeachItemsAssignToEmptyArrayToAssignRector::class, InlineIfToExplicitIfRector::class, UnusedForeachValueToArrayKeysRector::class, CommonNotEqualRector::class, SetTypeToCastRector::class, LogicalToBooleanRector::class, VarToPublicPropertyRector::class, IssetOnPropertyObjectToPropertyExistsRector::class, NewStaticToNewSelfRector::class, UnwrapSprintfOneArgumentRector::class, SwitchNegatedTernaryRector::class, SingularSwitchToIfRector::class, SimplifyIfNullableReturnRector::class, FuncGetArgsToVariadicParamRector::class, CallUserFuncToMethodCallRector::class, CallUserFuncWithArrowFunctionToInlineRector::class, CountArrayToEmptyArrayComparisonRector::class, FlipTypeControlToUseExclusiveTypeRector::class, InlineArrayReturnAssignRector::class, InlineIsAInstanceOfRector::class, TernaryFalseExpressionToIfRector::class, InlineConstructorDefaultToPropertyRector::class, TernaryEmptyArrayArrayDimFetchToCoalesceRector::class, OptionalParametersAfterRequiredRector::class, SimplifyEmptyCheckOnEmptyArrayRector::class, SwitchTrueToIfRector::class, CleanupUnneededNullsafeOperatorRector::class, DisallowedEmptyRuleFixerRector::class, ConvertStaticPrivateConstantToSelfRector::class, LocallyCalledStaticMethodToNonStaticRector::class, NumberCompareToMaxFuncCallRector::class, CompleteMissingIfElseBracketRector::class, RemoveUselessIsObjectCheckRector::class, StaticToSelfStaticMethodCallOnFinalClassRector::class]);
|
||||
};
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;
|
||||
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector;
|
||||
use Rector\Carbon\Rector\MethodCall\DateTimeMethodCallToCarbonRector;
|
||||
use Rector\Carbon\Rector\New_\DateTimeInstanceToCarbonRector;
|
||||
use Rector\Config\RectorConfig;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->rules([DateFuncCallToCarbonRector::class, DateTimeInstanceToCarbonRector::class, DateTimeMethodCallToCarbonRector::class]);
|
||||
};
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\Level\DeadCodeLevel;
|
||||
use Rector\Config\RectorConfig;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Renaming\Rector\MethodCall\RenameMethodRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\CodeQuality\Rector\FuncCall\InlineIsAInstanceOfRector;
|
||||
use Rector\CodeQuality\Rector\Identical\FlipTypeControlToUseExclusiveTypeRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\SetList;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Set\ValueObject\LevelSetList;
|
||||
use Rector\Set\ValueObject\SetList;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->sets([SetList::PHP_84, LevelSetList::UP_TO_PHP_83]);
|
||||
};
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Naming\Rector\Assign\RenameVariableToMatchMethodCallReturnTypeRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php52\Rector\Property\VarToPublicPropertyRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php53\Rector\FuncCall\DirNameFileConstantToDirConstantRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php54\Rector\Array_\LongArrayToShortArrayRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php55\Rector\Class_\ClassConstantToSelfClassRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php56\Rector\FuncCall\PowToExpRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php70\Rector\Assign\ListSplitStringRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php71\Rector\Assign\AssignArrayToStringRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php72\Rector\Assign\ListEachRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php52\Rector\Switch_\ContinueToBreakInSwitchRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php74\Rector\ArrayDimFetch\CurlyToSquareBracketArrayStringRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Arguments\Rector\ClassMethod\ArgumentAdderRector;
|
||||
use Rector\Arguments\Rector\FuncCall\FunctionArgumentDefaultValueReplacerRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php82\Rector\Class_\ReadOnlyClassRector;
|
||||
use Rector\Php82\Rector\Encapsed\VariableInStringInterpolationFixerRector;
|
||||
use Rector\Php82\Rector\FuncCall\Utf8DecodeEncodeToMbConvertEncodingRector;
|
||||
use Rector\Php82\Rector\New_\FilesystemIteratorSkipDotsRector;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->rules([ReadOnlyClassRector::class, Utf8DecodeEncodeToMbConvertEncodingRector::class, FilesystemIteratorSkipDotsRector::class]);
|
||||
$rectorConfig->rules([ReadOnlyClassRector::class, Utf8DecodeEncodeToMbConvertEncodingRector::class, FilesystemIteratorSkipDotsRector::class, VariableInStringInterpolationFixerRector::class]);
|
||||
};
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector;
|
||||
return static function (RectorConfig $rectorConfig) : void {
|
||||
$rectorConfig->rules([ExplicitNullableParamTypeRector::class]);
|
||||
};
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\RectorConfig;
|
||||
use Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace RectorPrefix202403;
|
||||
namespace RectorPrefix202405;
|
||||
|
||||
use Rector\Config\Level\TypeDeclarationLevel;
|
||||
use Rector\Config\RectorConfig;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# 364 Rules Overview
|
||||
# 374 Rules Overview
|
||||
|
||||
<br>
|
||||
|
||||
|
@ -6,11 +6,13 @@
|
|||
|
||||
- [Arguments](#arguments) (4)
|
||||
|
||||
- [CodeQuality](#codequality) (74)
|
||||
- [Carbon](#carbon) (3)
|
||||
|
||||
- [CodeQuality](#codequality) (75)
|
||||
|
||||
- [CodingStyle](#codingstyle) (28)
|
||||
|
||||
- [DeadCode](#deadcode) (42)
|
||||
- [DeadCode](#deadcode) (44)
|
||||
|
||||
- [EarlyReturn](#earlyreturn) (9)
|
||||
|
||||
|
@ -42,10 +44,12 @@
|
|||
|
||||
- [Php81](#php81) (9)
|
||||
|
||||
- [Php82](#php82) (4)
|
||||
- [Php82](#php82) (5)
|
||||
|
||||
- [Php83](#php83) (3)
|
||||
|
||||
- [Php84](#php84) (1)
|
||||
|
||||
- [Privatization](#privatization) (5)
|
||||
|
||||
- [Removing](#removing) (5)
|
||||
|
@ -54,9 +58,9 @@
|
|||
|
||||
- [Strict](#strict) (5)
|
||||
|
||||
- [Transform](#transform) (24)
|
||||
- [Transform](#transform) (25)
|
||||
|
||||
- [TypeDeclaration](#typedeclaration) (45)
|
||||
- [TypeDeclaration](#typedeclaration) (46)
|
||||
|
||||
- [Visibility](#visibility) (3)
|
||||
|
||||
|
@ -140,6 +144,59 @@ Replaces defined map of arguments in defined methods and their calls.
|
|||
|
||||
<br>
|
||||
|
||||
## Carbon
|
||||
|
||||
### DateFuncCallToCarbonRector
|
||||
|
||||
Convert `date()` function call to Carbon::*()
|
||||
|
||||
- class: [`Rector\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector`](../rules/Carbon/Rector/FuncCall/DateFuncCallToCarbonRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- $date = date('Y-m-d');
|
||||
+ $date = \Carbon\Carbon::now()->format('Y-m-d')
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### DateTimeInstanceToCarbonRector
|
||||
|
||||
Convert new `DateTime()` to Carbon::*()
|
||||
|
||||
- class: [`Rector\Carbon\Rector\New_\DateTimeInstanceToCarbonRector`](../rules/Carbon/Rector/New_/DateTimeInstanceToCarbonRector.php)
|
||||
|
||||
```diff
|
||||
-$date = new \DateTime('today');
|
||||
+$date = \Carbon\Carbon::today();
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### DateTimeMethodCallToCarbonRector
|
||||
|
||||
Convert new `DateTime()` with a method call to Carbon::*()
|
||||
|
||||
- class: [`Rector\Carbon\Rector\MethodCall\DateTimeMethodCallToCarbonRector`](../rules/Carbon/Rector/MethodCall/DateTimeMethodCallToCarbonRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
- $date = (new \DateTime('today +20 day'))->format('Y-m-d');
|
||||
+ $date = \Carbon\Carbon::today()->addDays(20)->format('Y-m-d')
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## CodeQuality
|
||||
|
||||
### AbsolutizeRequireAndIncludePathRector
|
||||
|
@ -547,6 +604,32 @@ Make if conditions more explicit
|
|||
|
||||
<br>
|
||||
|
||||
### ExplicitReturnNullRector
|
||||
|
||||
Add explicit return null to method/function that returns a value, but missed main return
|
||||
|
||||
- class: [`Rector\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector`](../rules/CodeQuality/Rector/ClassMethod/ExplicitReturnNullRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
- * @return string|void
|
||||
+ * @return string|null
|
||||
*/
|
||||
public function run(int $number)
|
||||
{
|
||||
if ($number > 50) {
|
||||
return 'yes';
|
||||
}
|
||||
+
|
||||
+ return null;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### FlipTypeControlToUseExclusiveTypeRector
|
||||
|
||||
Flip type control from null compare to use exclusive instanceof object
|
||||
|
@ -2155,6 +2238,29 @@ Removes recasting of the same type
|
|||
|
||||
<br>
|
||||
|
||||
### ReduceAlwaysFalseIfOrRector
|
||||
|
||||
Reduce always false in a if ( || ) condition
|
||||
|
||||
- class: [`Rector\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector`](../rules/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector.php)
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function run(int $number)
|
||||
{
|
||||
- if (! is_int($number) || $number > 50) {
|
||||
+ if ($number > 50) {
|
||||
return 'yes';
|
||||
}
|
||||
|
||||
return 'no';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### RemoveAlwaysTrueIfConditionRector
|
||||
|
||||
Remove if condition that is always true
|
||||
|
@ -2835,6 +2941,29 @@ Remove `@param` docblock with same type as parameter type
|
|||
|
||||
<br>
|
||||
|
||||
### RemoveUselessReadOnlyTagRector
|
||||
|
||||
Remove useless `@readonly` annotation on native readonly type
|
||||
|
||||
- class: [`Rector\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector`](../rules/DeadCode/Rector/Property/RemoveUselessReadOnlyTagRector.php)
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
- /**
|
||||
- * @readonly
|
||||
- */
|
||||
private readonly string $name;
|
||||
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### RemoveUselessReturnExprInConstructRector
|
||||
|
||||
Remove useless return Expr in `__construct()`
|
||||
|
@ -5272,6 +5401,20 @@ Change deprecated utf8_decode and utf8_encode to mb_convert_encoding
|
|||
|
||||
<br>
|
||||
|
||||
### VariableInStringInterpolationFixerRector
|
||||
|
||||
Replace deprecated "${var}" to "{$var}"
|
||||
|
||||
- class: [`Rector\Php82\Rector\Encapsed\VariableInStringInterpolationFixerRector`](../rules/Php82/Rector/Encapsed/VariableInStringInterpolationFixerRector.php)
|
||||
|
||||
```diff
|
||||
$c = "football";
|
||||
-echo "I like playing ${c}";
|
||||
+echo "I like playing {$c}";
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## Php83
|
||||
|
||||
### AddOverrideAttributeToOverriddenMethodsRector
|
||||
|
@ -5301,7 +5444,7 @@ Add override attribute to overridden methods
|
|||
|
||||
### AddTypeToConstRector
|
||||
|
||||
Add const to type
|
||||
Add type to constants
|
||||
|
||||
- class: [`Rector\Php83\Rector\ClassConst\AddTypeToConstRector`](../rules/Php83/Rector/ClassConst/AddTypeToConstRector.php)
|
||||
|
||||
|
@ -5328,6 +5471,21 @@ Combine separated host and port on `ldap_connect()` args
|
|||
|
||||
<br>
|
||||
|
||||
## Php84
|
||||
|
||||
### ExplicitNullableParamTypeRector
|
||||
|
||||
Make implicit nullable param to explicit
|
||||
|
||||
- class: [`Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector`](../rules/Php84/Rector/Param/ExplicitNullableParamTypeRector.php)
|
||||
|
||||
```diff
|
||||
-function foo(string $param = null) {}
|
||||
+function foo(?string $param = null) {}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## Privatization
|
||||
|
||||
### FinalizeClassesWithoutChildrenRector
|
||||
|
@ -5356,10 +5514,10 @@ PHPUnit test case will be finalized
|
|||
- class: [`Rector\Privatization\Rector\Class_\FinalizeTestCaseClassRector`](../rules/Privatization/Rector/Class_/FinalizeTestCaseClassRector.php)
|
||||
|
||||
```diff
|
||||
-use PHPUnit\Framework\TestCase;
|
||||
+final use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SomeClass extends TestCase
|
||||
-class SomeClass extends TestCase
|
||||
+final class SomeClass extends TestCase
|
||||
{
|
||||
}
|
||||
```
|
||||
|
@ -5852,6 +6010,21 @@ Add interface by used trait
|
|||
|
||||
<br>
|
||||
|
||||
### ArrayDimFetchToMethodCallRector
|
||||
|
||||
Change array dim fetch to method call
|
||||
|
||||
:wrench: **configure it!**
|
||||
|
||||
- class: [`Rector\Transform\Rector\ArrayDimFetch\ArrayDimFetchToMethodCallRector`](../rules/Transform/Rector/ArrayDimFetch/ArrayDimFetchToMethodCallRector.php)
|
||||
|
||||
```diff
|
||||
-$app['someService'];
|
||||
+$app->make('someService');
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### AttributeKeyToClassConstFetchRector
|
||||
|
||||
Replace key value on specific attribute to class constant
|
||||
|
@ -6649,7 +6822,7 @@ Change return type based on strict returns type operations
|
|||
|
||||
### ChildDoctrineRepositoryClassTypeRector
|
||||
|
||||
Add return type to classes that extend `Doctrine\ORM\EntityRepository`
|
||||
Add return type to classes that extend `Doctrine\ORM\EntityRepository` based on return Doctrine method names
|
||||
|
||||
- class: [`Rector\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector`](../rules/TypeDeclaration/Rector/Class_/ChildDoctrineRepositoryClassTypeRector.php)
|
||||
|
||||
|
@ -6712,6 +6885,24 @@ Change `empty()` on nullable object to instanceof check
|
|||
|
||||
<br>
|
||||
|
||||
### IncreaseDeclareStrictTypesRector
|
||||
|
||||
Add declare strict types to a limited amount of classes at a time, to try out in the wild and increase level gradually
|
||||
|
||||
:wrench: **configure it!**
|
||||
|
||||
- class: [`Rector\TypeDeclaration\Rector\StmtsAwareInterface\IncreaseDeclareStrictTypesRector`](../rules/TypeDeclaration/Rector/StmtsAwareInterface/IncreaseDeclareStrictTypesRector.php)
|
||||
|
||||
```diff
|
||||
+declare(strict_types=1);
|
||||
+
|
||||
function someFunction()
|
||||
{
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### MergeDateTimePropertyTypeDeclarationRector
|
||||
|
||||
Set DateTime to DateTimeInterface for DateTime property with DateTimeInterface docblock
|
||||
|
|
|
@ -291,6 +291,9 @@ require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/InvalidTagV
|
|||
require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/MethodTagValueNode.php';
|
||||
require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/MethodTagValueParameterNode.php';
|
||||
require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/MixinTagValueNode.php';
|
||||
require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/ParamClosureThisTagValueNode.php';
|
||||
require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/ParamImmediatelyInvokedCallableTagValueNode.php';
|
||||
require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/ParamLaterInvokedCallableTagValueNode.php';
|
||||
require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/ParamOutTagValueNode.php';
|
||||
require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/ParamTagValueNode.php';
|
||||
require_once __DIR__ . '/vendor/phpstan/phpdoc-parser/src/Ast/PhpDoc/PhpDocChildNode.php';
|
||||
|
|
|
@ -29,7 +29,7 @@ use Rector\Rector\AbstractRector;
|
|||
use Rector\StaticTypeMapper\StaticTypeMapper;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use RectorPrefix202403\Webmozart\Assert\Assert;
|
||||
use RectorPrefix202405\Webmozart\Assert\Assert;
|
||||
/**
|
||||
* @see \Rector\Tests\Arguments\Rector\ClassMethod\ArgumentAdderRector\ArgumentAdderRectorTest
|
||||
*/
|
||||
|
|
|
@ -15,7 +15,7 @@ use Rector\Rector\AbstractRector;
|
|||
use Rector\ValueObject\MethodName;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use RectorPrefix202403\Webmozart\Assert\Assert;
|
||||
use RectorPrefix202405\Webmozart\Assert\Assert;
|
||||
/**
|
||||
* @api used in rector-symfony
|
||||
* @see \Rector\Tests\Arguments\Rector\ClassMethod\ReplaceArgumentDefaultValueRector\ReplaceArgumentDefaultValueRectorTest
|
||||
|
|
|
@ -11,7 +11,7 @@ use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use RectorPrefix202403\Webmozart\Assert\Assert;
|
||||
use RectorPrefix202405\Webmozart\Assert\Assert;
|
||||
/**
|
||||
* @changelog https://php.watch/versions/8.1/version_compare-operator-restrictions
|
||||
* @changelog https://github.com/rectorphp/rector/issues/6271
|
||||
|
|
|
@ -11,7 +11,7 @@ use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use RectorPrefix202403\Webmozart\Assert\Assert;
|
||||
use RectorPrefix202405\Webmozart\Assert\Assert;
|
||||
/**
|
||||
* @see \Rector\Tests\Arguments\Rector\MethodCall\RemoveMethodCallParamRector\RemoveMethodCallParamRectorTest
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Carbon\NodeFactory;
|
||||
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Scalar\LNumber;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
final class CarbonCallFactory
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
* @see https://regex101.com/r/9vGt8r/1
|
||||
*/
|
||||
private const DAY_COUNT_REGEX = '#\\+(\\s+)?(?<count>\\d+)(\\s+)?(day|days)#';
|
||||
/**
|
||||
* @var string
|
||||
* @see https://regex101.com/r/6VUUQF/1
|
||||
*/
|
||||
private const MONTH_COUNT_REGEX = '#\\+(\\s+)?(?<count>\\d+)(\\s+)?(month|months)#';
|
||||
/**
|
||||
* @var array<self::*_REGEX, string>
|
||||
*/
|
||||
private const REGEX_TO_METHOD_NAME_MAP = [self::DAY_COUNT_REGEX => 'addDays', self::MONTH_COUNT_REGEX => 'addMonths'];
|
||||
/**
|
||||
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall
|
||||
*/
|
||||
public function createFromDateTimeString(FullyQualified $carbonFullyQualified, String_ $string)
|
||||
{
|
||||
$dateTimeValue = $string->value;
|
||||
if ($dateTimeValue === 'now') {
|
||||
return new StaticCall($carbonFullyQualified, new Identifier('now'));
|
||||
}
|
||||
if ($dateTimeValue === 'today') {
|
||||
return new StaticCall($carbonFullyQualified, new Identifier('today'));
|
||||
}
|
||||
$hasToday = Strings::match($dateTimeValue, '#today#');
|
||||
if ($hasToday !== null) {
|
||||
$carbonCall = new StaticCall($carbonFullyQualified, new Identifier('today'));
|
||||
} else {
|
||||
$carbonCall = new StaticCall($carbonFullyQualified, new Identifier('now'));
|
||||
}
|
||||
foreach (self::REGEX_TO_METHOD_NAME_MAP as $regex => $methodName) {
|
||||
$match = Strings::match($dateTimeValue, $regex);
|
||||
if ($match === null) {
|
||||
continue;
|
||||
}
|
||||
$countLNumber = new LNumber((int) $match['count']);
|
||||
$carbonCall = new MethodCall($carbonCall, new Identifier($methodName), [new Arg($countLNumber)]);
|
||||
}
|
||||
return $carbonCall;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Carbon\Rector\FuncCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\Carbon\Rector\FuncCall\DateFuncCallToCarbonRector\DateFuncCallToCarbonRectorTest
|
||||
*/
|
||||
final class DateFuncCallToCarbonRector extends AbstractRector
|
||||
{
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Convert date() function call to Carbon::*()', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$date = date('Y-m-d');
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$date = \Carbon\Carbon::now()->format('Y-m-d')
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [FuncCall::class];
|
||||
}
|
||||
/**
|
||||
* @param FuncCall $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if (!$this->isName($node->name, 'date')) {
|
||||
return null;
|
||||
}
|
||||
if ($node->isFirstClassCallable()) {
|
||||
return null;
|
||||
}
|
||||
if (\count($node->getArgs()) !== 1) {
|
||||
return null;
|
||||
}
|
||||
$firstArg = $node->getArgs()[0];
|
||||
if (!$firstArg->value instanceof String_) {
|
||||
return null;
|
||||
}
|
||||
// create now and format()
|
||||
$nowStaticCall = new StaticCall(new FullyQualified('Carbon\\Carbon'), 'now');
|
||||
return new MethodCall($nowStaticCall, 'format', [new Arg($firstArg->value)]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Carbon\Rector\MethodCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Carbon\NodeFactory\CarbonCallFactory;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\Carbon\Rector\MethodCall\DateTimeMethodCallToCarbonRector\DateTimeMethodCallToCarbonRectorTest
|
||||
*/
|
||||
final class DateTimeMethodCallToCarbonRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Carbon\NodeFactory\CarbonCallFactory
|
||||
*/
|
||||
private $carbonCallFactory;
|
||||
public function __construct(CarbonCallFactory $carbonCallFactory)
|
||||
{
|
||||
$this->carbonCallFactory = $carbonCallFactory;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Convert new DateTime() with a method call to Carbon::*()', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$date = (new \DateTime('today +20 day'))->format('Y-m-d');
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
$date = \Carbon\Carbon::today()->addDays(20)->format('Y-m-d')
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [MethodCall::class];
|
||||
}
|
||||
/**
|
||||
* @param MethodCall $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if (!$node->var instanceof New_) {
|
||||
return null;
|
||||
}
|
||||
$new = $node->var;
|
||||
if (!$new->class instanceof Name) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->isName($new->class, 'DateTime')) {
|
||||
return null;
|
||||
}
|
||||
if ($new->isFirstClassCallable()) {
|
||||
return null;
|
||||
}
|
||||
if (\count($new->getArgs()) !== 1) {
|
||||
// @todo handle in separate static call
|
||||
return null;
|
||||
}
|
||||
$firstArg = $new->getArgs()[0];
|
||||
if (!$firstArg->value instanceof String_) {
|
||||
return null;
|
||||
}
|
||||
$carbonFullyQualified = new FullyQualified('Carbon\\Carbon');
|
||||
$carbonCall = $this->carbonCallFactory->createFromDateTimeString($carbonFullyQualified, $firstArg->value);
|
||||
$node->var = $carbonCall;
|
||||
return $node;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\Carbon\Rector\New_;
|
||||
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Carbon\NodeFactory\CarbonCallFactory;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @changelog https://github.com/briannesbitt/Carbon/issues/231
|
||||
*
|
||||
* @see \Rector\Tests\Carbon\Rector\New_\DateTimeInstanceToCarbonRector\DateTimeInstanceToCarbonRectorTest
|
||||
*/
|
||||
final class DateTimeInstanceToCarbonRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Carbon\NodeFactory\CarbonCallFactory
|
||||
*/
|
||||
private $carbonCallFactory;
|
||||
public function __construct(CarbonCallFactory $carbonCallFactory)
|
||||
{
|
||||
$this->carbonCallFactory = $carbonCallFactory;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Convert new DateTime() to Carbon::*()', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
$date = new \DateTime('today');
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
$date = \Carbon\Carbon::today();
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [New_::class];
|
||||
}
|
||||
/**
|
||||
* @param New_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if ($this->isName($node->class, 'DateTime')) {
|
||||
return $this->refactorWithClass($node, 'Carbon\\Carbon');
|
||||
}
|
||||
if ($this->isName($node->class, 'DateTimeImmutable')) {
|
||||
return $this->refactorWithClass($node, 'Carbon\\CarbonImmutable');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|null
|
||||
*/
|
||||
public function refactorWithClass(New_ $new, string $className)
|
||||
{
|
||||
if ($new->isFirstClassCallable()) {
|
||||
return null;
|
||||
}
|
||||
// no arg? ::now()
|
||||
$carbonFullyQualified = new FullyQualified($className);
|
||||
if ($new->args === []) {
|
||||
return new StaticCall($carbonFullyQualified, new Identifier('now'));
|
||||
}
|
||||
if (\count($new->getArgs()) === 1) {
|
||||
$firstArg = $new->getArgs()[0];
|
||||
if ($firstArg->value instanceof String_) {
|
||||
return $this->carbonCallFactory->createFromDateTimeString($carbonFullyQualified, $firstArg->value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -5,6 +5,9 @@ namespace Rector\CodeQuality\Rector\Array_;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\Php\PhpMethodReflection;
|
||||
use Rector\NodeCollector\NodeAnalyzer\ArrayCallableMethodMatcher;
|
||||
|
@ -89,13 +92,17 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Array_::class];
|
||||
return [Property::class, ClassConst::class, Array_::class];
|
||||
}
|
||||
/**
|
||||
* @param Array_ $node
|
||||
* @param Property|ClassConst|Array_ $node
|
||||
* @return null|int|\PhpParser\Node
|
||||
*/
|
||||
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
||||
public function refactorWithScope(Node $node, Scope $scope)
|
||||
{
|
||||
if ($node instanceof Property || $node instanceof ClassConst) {
|
||||
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
|
||||
}
|
||||
if ($this->shouldSkipTwigExtension($scope)) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,174 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PHPStan\Type\NullType;
|
||||
use PHPStan\Type\UnionType;
|
||||
use PHPStan\Type\VoidType;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
|
||||
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
|
||||
use Rector\TypeDeclaration\TypeInferer\SilentVoidResolver;
|
||||
use Rector\ValueObject\MethodName;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\CodeQuality\Rector\ClassMethod\ExplicitReturnNullRector\ExplicitReturnNullRectorTest
|
||||
*/
|
||||
final class ExplicitReturnNullRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\TypeInferer\SilentVoidResolver
|
||||
*/
|
||||
private $silentVoidResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
||||
*/
|
||||
private $phpDocInfoFactory;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
|
||||
*/
|
||||
private $typeFactory;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
|
||||
*/
|
||||
private $phpDocTypeChanger;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer
|
||||
*/
|
||||
private $returnTypeInferer;
|
||||
public function __construct(SilentVoidResolver $silentVoidResolver, PhpDocInfoFactory $phpDocInfoFactory, TypeFactory $typeFactory, PhpDocTypeChanger $phpDocTypeChanger, ReturnTypeInferer $returnTypeInferer)
|
||||
{
|
||||
$this->silentVoidResolver = $silentVoidResolver;
|
||||
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
||||
$this->typeFactory = $typeFactory;
|
||||
$this->phpDocTypeChanger = $phpDocTypeChanger;
|
||||
$this->returnTypeInferer = $returnTypeInferer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Add explicit return null to method/function that returns a value, but missed main return', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @return string|void
|
||||
*/
|
||||
public function run(int $number)
|
||||
{
|
||||
if ($number > 50) {
|
||||
return 'yes';
|
||||
}
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function run(int $number)
|
||||
{
|
||||
if ($number > 50) {
|
||||
return 'yes';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [ClassMethod::class, Function_::class];
|
||||
}
|
||||
/**
|
||||
* @param ClassMethod|Function_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
// known return type, nothing to improve
|
||||
if ($node->returnType instanceof Node) {
|
||||
return null;
|
||||
}
|
||||
if ($node instanceof ClassMethod && $this->isName($node, MethodName::CONSTRUCT)) {
|
||||
return null;
|
||||
}
|
||||
$returnType = $this->returnTypeInferer->inferFunctionLike($node);
|
||||
if (!$returnType instanceof UnionType) {
|
||||
return null;
|
||||
}
|
||||
$hasChanged = \false;
|
||||
$this->traverseNodesWithCallable((array) $node->stmts, static function (Node $node) use(&$hasChanged) {
|
||||
if ($node instanceof Class_ || $node instanceof Function_ || $node instanceof Closure) {
|
||||
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
|
||||
}
|
||||
if ($node instanceof Return_ && !$node->expr instanceof Expr) {
|
||||
$hasChanged = \true;
|
||||
$node->expr = new ConstFetch(new Name('null'));
|
||||
return $node;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
if (!$this->silentVoidResolver->hasSilentVoid($node)) {
|
||||
if ($hasChanged) {
|
||||
$this->transformDocUnionVoidToUnionNull($node);
|
||||
return $node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
$node->stmts[] = new Return_(new ConstFetch(new Name('null')));
|
||||
$this->transformDocUnionVoidToUnionNull($node);
|
||||
return $node;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $node
|
||||
*/
|
||||
private function transformDocUnionVoidToUnionNull($node) : void
|
||||
{
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
||||
$returnType = $phpDocInfo->getReturnType();
|
||||
if (!$returnType instanceof UnionType) {
|
||||
return;
|
||||
}
|
||||
$newTypes = [];
|
||||
$hasChanged = \false;
|
||||
foreach ($returnType->getTypes() as $type) {
|
||||
if ($type instanceof VoidType) {
|
||||
$type = new NullType();
|
||||
$hasChanged = \true;
|
||||
}
|
||||
$newTypes[] = $type;
|
||||
}
|
||||
if (!$hasChanged) {
|
||||
return;
|
||||
}
|
||||
$type = $this->typeFactory->createMixedPassedOrUnionTypeAndKeepConstant($newTypes);
|
||||
if (!$type instanceof UnionType) {
|
||||
return;
|
||||
}
|
||||
$this->phpDocTypeChanger->changeReturnType($node, $phpDocInfo, $type);
|
||||
}
|
||||
}
|
|
@ -4,12 +4,16 @@ declare (strict_types=1);
|
|||
namespace Rector\CodeQuality\Rector\ClassMethod;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Reflection\FunctionReflection;
|
||||
use PHPStan\Reflection\MethodReflection;
|
||||
use PHPStan\Reflection\Native\NativeFunctionReflection;
|
||||
use Rector\CodingStyle\Reflection\VendorLocationDetector;
|
||||
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
|
||||
use Rector\Php80\NodeResolver\ArgumentSorter;
|
||||
|
@ -81,41 +85,49 @@ CODE_SAMPLE
|
|||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [ClassMethod::class, New_::class, MethodCall::class, StaticCall::class];
|
||||
return [ClassMethod::class, Function_::class, New_::class, MethodCall::class, StaticCall::class, FuncCall::class];
|
||||
}
|
||||
/**
|
||||
* @param ClassMethod|New_|MethodCall|StaticCall $node
|
||||
* @return \PhpParser\Node\Stmt\ClassMethod|null|\PhpParser\Node\Expr\New_|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall
|
||||
* @param ClassMethod|Function_|New_|MethodCall|StaticCall|FuncCall $node
|
||||
* @return \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|null|\PhpParser\Node\Expr\New_|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall
|
||||
*/
|
||||
public function refactorWithScope(Node $node, Scope $scope)
|
||||
{
|
||||
if ($node instanceof ClassMethod) {
|
||||
return $this->refactorClassMethod($node, $scope);
|
||||
if ($node instanceof ClassMethod || $node instanceof Function_) {
|
||||
return $this->refactorClassMethodOrFunction($node, $scope);
|
||||
}
|
||||
if ($node instanceof New_) {
|
||||
return $this->refactorNew($node, $scope);
|
||||
}
|
||||
return $this->refactorMethodCall($node, $scope);
|
||||
return $this->refactorMethodCallOrFuncCall($node, $scope);
|
||||
}
|
||||
private function refactorClassMethod(ClassMethod $classMethod, Scope $scope) : ?ClassMethod
|
||||
/**
|
||||
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_ $node
|
||||
* @return \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|null
|
||||
*/
|
||||
private function refactorClassMethodOrFunction($node, Scope $scope)
|
||||
{
|
||||
if ($classMethod->params === []) {
|
||||
if ($node->params === []) {
|
||||
return null;
|
||||
}
|
||||
if ($classMethod->getAttribute(self::HAS_SWAPPED_PARAMS, \false) === \true) {
|
||||
if ($node->getAttribute(self::HAS_SWAPPED_PARAMS, \false) === \true) {
|
||||
return null;
|
||||
}
|
||||
$classMethodReflection = $this->reflectionResolver->resolveMethodReflectionFromClassMethod($classMethod, $scope);
|
||||
if (!$classMethodReflection instanceof MethodReflection) {
|
||||
if ($node instanceof ClassMethod) {
|
||||
$reflection = $this->reflectionResolver->resolveMethodReflectionFromClassMethod($node, $scope);
|
||||
} else {
|
||||
$reflection = $this->reflectionResolver->resolveFunctionReflectionFromFunction($node, $scope);
|
||||
}
|
||||
if (!$reflection instanceof MethodReflection && !$reflection instanceof FunctionReflection) {
|
||||
return null;
|
||||
}
|
||||
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($classMethodReflection, $classMethod, $scope);
|
||||
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($reflection, $node, $scope);
|
||||
if ($expectedArgOrParamOrder === null) {
|
||||
return null;
|
||||
}
|
||||
$classMethod->params = $this->argumentSorter->sortArgsByExpectedParamOrder($classMethod->params, $expectedArgOrParamOrder);
|
||||
$classMethod->setAttribute(self::HAS_SWAPPED_PARAMS, \true);
|
||||
return $classMethod;
|
||||
$node->params = $this->argumentSorter->sortArgsByExpectedParamOrder($node->params, $expectedArgOrParamOrder);
|
||||
$node->setAttribute(self::HAS_SWAPPED_PARAMS, \true);
|
||||
return $node;
|
||||
}
|
||||
private function refactorNew(New_ $new, Scope $scope) : ?New_
|
||||
{
|
||||
|
@ -137,39 +149,46 @@ CODE_SAMPLE
|
|||
return $new;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $methodCall
|
||||
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|null
|
||||
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall $node
|
||||
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall|null
|
||||
*/
|
||||
private function refactorMethodCall($methodCall, Scope $scope)
|
||||
private function refactorMethodCallOrFuncCall($node, Scope $scope)
|
||||
{
|
||||
if ($methodCall->isFirstClassCallable()) {
|
||||
if ($node->isFirstClassCallable()) {
|
||||
return null;
|
||||
}
|
||||
$methodReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($methodCall);
|
||||
if (!$methodReflection instanceof MethodReflection) {
|
||||
$reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
|
||||
if (!$reflection instanceof MethodReflection && !$reflection instanceof FunctionReflection) {
|
||||
return null;
|
||||
}
|
||||
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($methodReflection, $methodCall, $scope);
|
||||
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($reflection, $node, $scope);
|
||||
if ($expectedArgOrParamOrder === null) {
|
||||
return null;
|
||||
}
|
||||
$newArgs = $this->argumentSorter->sortArgsByExpectedParamOrder($methodCall->getArgs(), $expectedArgOrParamOrder);
|
||||
if ($methodCall->args === $newArgs) {
|
||||
$newArgs = $this->argumentSorter->sortArgsByExpectedParamOrder($node->getArgs(), $expectedArgOrParamOrder);
|
||||
if ($node->args === $newArgs) {
|
||||
return null;
|
||||
}
|
||||
$methodCall->args = $newArgs;
|
||||
return $methodCall;
|
||||
$node->args = $newArgs;
|
||||
return $node;
|
||||
}
|
||||
/**
|
||||
* @return int[]|null
|
||||
* @param \PhpParser\Node\Expr\New_|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\StaticCall $node
|
||||
* @param \PHPStan\Reflection\MethodReflection|\PHPStan\Reflection\FunctionReflection $reflection
|
||||
* @param \PhpParser\Node\Expr\New_|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\FuncCall $node
|
||||
*/
|
||||
private function resolveExpectedArgParamOrderIfDifferent(MethodReflection $methodReflection, $node, Scope $scope) : ?array
|
||||
private function resolveExpectedArgParamOrderIfDifferent($reflection, $node, Scope $scope) : ?array
|
||||
{
|
||||
if ($this->vendorLocationDetector->detectMethodReflection($methodReflection)) {
|
||||
if ($reflection instanceof NativeFunctionReflection) {
|
||||
return null;
|
||||
}
|
||||
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($methodReflection, $node, $scope);
|
||||
if ($reflection instanceof MethodReflection && $this->vendorLocationDetector->detectMethodReflection($reflection)) {
|
||||
return null;
|
||||
}
|
||||
if ($reflection instanceof FunctionReflection && $this->vendorLocationDetector->detectFunctionReflection($reflection)) {
|
||||
return null;
|
||||
}
|
||||
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($reflection, $node, $scope);
|
||||
$expectedParameterReflections = $this->requireOptionalParamResolver->resolveFromParametersAcceptor($parametersAcceptor);
|
||||
if ($expectedParameterReflections === $parametersAcceptor->getParameters()) {
|
||||
return null;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\Rector\Concat;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\Concat;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
|
|
|
@ -6,10 +6,11 @@ namespace Rector\CodeQuality\Rector\Expression;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Ternary;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
|
||||
use Rector\NodeAnalyzer\ExprAnalyzer;
|
||||
use Rector\Rector\AbstractScopeAwareRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
@ -20,12 +21,12 @@ final class TernaryFalseExpressionToIfRector extends AbstractScopeAwareRector
|
|||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\DeadCode\SideEffect\SideEffectNodeDetector
|
||||
* @var \Rector\NodeAnalyzer\ExprAnalyzer
|
||||
*/
|
||||
private $sideEffectNodeDetector;
|
||||
public function __construct(SideEffectNodeDetector $sideEffectNodeDetector)
|
||||
private $exprAnalyzer;
|
||||
public function __construct(ExprAnalyzer $exprAnalyzer)
|
||||
{
|
||||
$this->sideEffectNodeDetector = $sideEffectNodeDetector;
|
||||
$this->exprAnalyzer = $exprAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
|
@ -70,7 +71,7 @@ CODE_SAMPLE
|
|||
if (!$ternary->if instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
if ($this->sideEffectNodeDetector->detect($ternary->else, $scope) || $this->sideEffectNodeDetector->detectCallExpr($ternary->else, $scope)) {
|
||||
if (!$ternary->else instanceof Variable && $this->exprAnalyzer->isDynamicExpr($ternary->else)) {
|
||||
return null;
|
||||
}
|
||||
return new If_($ternary->cond, ['stmts' => [new Expression($ternary->if)]]);
|
||||
|
|
|
@ -13,13 +13,14 @@ use PhpParser\Node\Expr\Variable;
|
|||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\For_;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use Rector\Rector\AbstractScopeAwareRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector\ForRepeatedCountToOwnVariableRectorTest
|
||||
*/
|
||||
final class ForRepeatedCountToOwnVariableRector extends AbstractRector
|
||||
final class ForRepeatedCountToOwnVariableRector extends AbstractScopeAwareRector
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
|
@ -63,8 +64,11 @@ CODE_SAMPLE
|
|||
* @param For_ $node
|
||||
* @return Stmt[]|null
|
||||
*/
|
||||
public function refactor(Node $node) : ?array
|
||||
public function refactorWithScope(Node $node, Scope $scope) : ?array
|
||||
{
|
||||
if ($scope->hasVariableType(self::COUNTER_NAME)->yes()) {
|
||||
return null;
|
||||
}
|
||||
$countInCond = null;
|
||||
$counterVariable = new Variable(self::COUNTER_NAME);
|
||||
foreach ($node->cond as $condExpr) {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\Rector\FuncCall;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\NodeNameResolver\Regex\RegexPatternDetector;
|
||||
|
|
|
@ -3,9 +3,11 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\Rector\FuncCall;
|
||||
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
|
@ -14,6 +16,11 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
*/
|
||||
final class SimplifyStrposLowerRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
* @see https://regex101.com/r/Jokjt8/1
|
||||
*/
|
||||
private const UPPERCASE_REGEX = '#[A-Z]#';
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Simplify strpos(strtolower(), "...") calls', [new CodeSample('strpos(strtolower($var), "...")', 'stripos($var, "...")')]);
|
||||
|
@ -36,10 +43,11 @@ final class SimplifyStrposLowerRector extends AbstractRector
|
|||
if ($node->isFirstClassCallable()) {
|
||||
return null;
|
||||
}
|
||||
if (!isset($node->getArgs()[0])) {
|
||||
$args = $node->getArgs();
|
||||
if (!isset($args[0], $args[1])) {
|
||||
return null;
|
||||
}
|
||||
$firstArg = $node->getArgs()[0];
|
||||
$firstArg = $args[0];
|
||||
if (!$firstArg->value instanceof FuncCall) {
|
||||
return null;
|
||||
}
|
||||
|
@ -48,6 +56,13 @@ final class SimplifyStrposLowerRector extends AbstractRector
|
|||
if (!$this->isName($innerFuncCall, 'strtolower')) {
|
||||
return null;
|
||||
}
|
||||
$secondArg = $args[1];
|
||||
if (!$secondArg->value instanceof String_) {
|
||||
return null;
|
||||
}
|
||||
if (Strings::match($secondArg->value->value, self::UPPERCASE_REGEX) !== null) {
|
||||
return null;
|
||||
}
|
||||
// pop 1 level up
|
||||
$node->args[0] = $innerFuncCall->getArgs()[0];
|
||||
$node->name = new Name('stripos');
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodeQuality\Rector\Include_;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\Concat;
|
||||
use PhpParser\Node\Expr\Include_;
|
||||
|
|
|
@ -53,6 +53,6 @@ final class SimplifyTautologyTernaryRector extends AbstractRector
|
|||
if (!$twoNodeMatch instanceof TwoNodeMatch) {
|
||||
return null;
|
||||
}
|
||||
return $node->if;
|
||||
return $node->cond instanceof NotIdentical ? $node->if : $node->else;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\Application;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Declare_;
|
||||
|
|
|
@ -3,11 +3,10 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\ClassNameImport\ClassNameImportSkipVoter;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
|
||||
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
|
||||
use Rector\Configuration\RenamedClassesDataCollector;
|
||||
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
||||
use Rector\ValueObject\Application\File;
|
||||
/**
|
||||
|
@ -26,15 +25,9 @@ final class FullyQualifiedNameClassNameImportSkipVoter implements ClassNameImpor
|
|||
* @var \Rector\CodingStyle\ClassNameImport\ShortNameResolver
|
||||
*/
|
||||
private $shortNameResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Configuration\RenamedClassesDataCollector
|
||||
*/
|
||||
private $renamedClassesDataCollector;
|
||||
public function __construct(ShortNameResolver $shortNameResolver, RenamedClassesDataCollector $renamedClassesDataCollector)
|
||||
public function __construct(ShortNameResolver $shortNameResolver)
|
||||
{
|
||||
$this->shortNameResolver = $shortNameResolver;
|
||||
$this->renamedClassesDataCollector = $renamedClassesDataCollector;
|
||||
}
|
||||
public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType, Node $node) : bool
|
||||
{
|
||||
|
@ -43,7 +36,6 @@ final class FullyQualifiedNameClassNameImportSkipVoter implements ClassNameImpor
|
|||
$shortNamesToFullyQualifiedNames = $this->shortNameResolver->resolveFromFile($file);
|
||||
$fullyQualifiedObjectTypeShortName = $fullyQualifiedObjectType->getShortName();
|
||||
$className = $fullyQualifiedObjectType->getClassName();
|
||||
$removedUses = $this->renamedClassesDataCollector->getOldClasses();
|
||||
foreach ($shortNamesToFullyQualifiedNames as $shortName => $fullyQualifiedName) {
|
||||
if ($fullyQualifiedObjectTypeShortName !== $shortName) {
|
||||
$shortName = $this->cleanShortName($shortName);
|
||||
|
@ -52,10 +44,7 @@ final class FullyQualifiedNameClassNameImportSkipVoter implements ClassNameImpor
|
|||
continue;
|
||||
}
|
||||
$fullyQualifiedName = \ltrim($fullyQualifiedName, '\\');
|
||||
if ($className === $fullyQualifiedName) {
|
||||
return \false;
|
||||
}
|
||||
return !\in_array($fullyQualifiedName, $removedUses, \true);
|
||||
return $className !== $fullyQualifiedName;
|
||||
}
|
||||
return \false;
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\ClassNameImport;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Reflection;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
|
@ -12,8 +11,6 @@ use PhpParser\Node\Stmt\ClassLike;
|
|||
use PhpParser\Node\Stmt\Namespace_;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
||||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
||||
use Rector\CodingStyle\NodeAnalyzer\UseImportNameMatcher;
|
||||
|
@ -24,7 +21,6 @@ use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
|
|||
use Rector\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
|
||||
use Rector\ValueObject\Application\File;
|
||||
use ReflectionClass;
|
||||
/**
|
||||
* @see \Rector\Tests\CodingStyle\ClassNameImport\ShortNameResolver\ShortNameResolverTest
|
||||
*/
|
||||
|
@ -40,11 +36,6 @@ final class ShortNameResolver
|
|||
* @var \Rector\NodeNameResolver\NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \PHPStan\Reflection\ReflectionProvider
|
||||
*/
|
||||
private $reflectionProvider;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
||||
|
@ -64,11 +55,10 @@ final class ShortNameResolver
|
|||
* @var array<string, string[]>
|
||||
*/
|
||||
private $shortNamesByFilePath = [];
|
||||
public function __construct(SimpleCallableNodeTraverser $simpleCallableNodeTraverser, NodeNameResolver $nodeNameResolver, ReflectionProvider $reflectionProvider, BetterNodeFinder $betterNodeFinder, UseImportNameMatcher $useImportNameMatcher, PhpDocInfoFactory $phpDocInfoFactory)
|
||||
public function __construct(SimpleCallableNodeTraverser $simpleCallableNodeTraverser, NodeNameResolver $nodeNameResolver, BetterNodeFinder $betterNodeFinder, UseImportNameMatcher $useImportNameMatcher, PhpDocInfoFactory $phpDocInfoFactory)
|
||||
{
|
||||
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->reflectionProvider = $reflectionProvider;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->useImportNameMatcher = $useImportNameMatcher;
|
||||
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
||||
|
@ -152,7 +142,6 @@ final class ShortNameResolver
|
|||
*/
|
||||
private function resolveFromStmtsDocBlocks(array $stmts) : array
|
||||
{
|
||||
$classReflection = $this->resolveClassReflection($stmts);
|
||||
$shortNames = [];
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use(&$shortNames) {
|
||||
// speed up for nodes that are
|
||||
|
@ -176,42 +165,22 @@ final class ShortNameResolver
|
|||
});
|
||||
return null;
|
||||
});
|
||||
return $this->fqnizeShortNames($shortNames, $classReflection, $stmts);
|
||||
}
|
||||
/**
|
||||
* @param Node[] $stmts
|
||||
*/
|
||||
private function resolveClassReflection(array $stmts) : ?ClassReflection
|
||||
{
|
||||
$firstClassLike = $this->betterNodeFinder->findFirstInstanceOf($stmts, ClassLike::class);
|
||||
if (!$firstClassLike instanceof ClassLike) {
|
||||
return null;
|
||||
}
|
||||
$className = (string) $this->nodeNameResolver->getName($firstClassLike);
|
||||
if (!$this->reflectionProvider->hasClass($className)) {
|
||||
return null;
|
||||
}
|
||||
return $this->reflectionProvider->getClass($className);
|
||||
return $this->fqnizeShortNames($shortNames, $stmts);
|
||||
}
|
||||
/**
|
||||
* @param string[] $shortNames
|
||||
* @param Stmt[] $stmts
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function fqnizeShortNames(array $shortNames, ?ClassReflection $classReflection, array $stmts) : array
|
||||
private function fqnizeShortNames(array $shortNames, array $stmts) : array
|
||||
{
|
||||
$shortNamesToFullyQualifiedNames = [];
|
||||
$nativeReflectionClass = $classReflection instanceof ClassReflection && !$classReflection->isAnonymous() ? $classReflection->getNativeReflection() : null;
|
||||
foreach ($shortNames as $shortName) {
|
||||
$stmtsMatchedName = $this->useImportNameMatcher->matchNameWithStmts($shortName, $stmts);
|
||||
if ($nativeReflectionClass instanceof ReflectionClass) {
|
||||
$fullyQualifiedName = Reflection::expandClassName($shortName, $nativeReflectionClass);
|
||||
} elseif (\is_string($stmtsMatchedName)) {
|
||||
$fullyQualifiedName = $stmtsMatchedName;
|
||||
} else {
|
||||
$fullyQualifiedName = $shortName;
|
||||
if ($stmtsMatchedName == null) {
|
||||
continue;
|
||||
}
|
||||
$shortNamesToFullyQualifiedNames[$shortName] = $fullyQualifiedName;
|
||||
$shortNamesToFullyQualifiedNames[$shortName] = $stmtsMatchedName;
|
||||
}
|
||||
return $shortNamesToFullyQualifiedNames;
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\Naming;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\NodeAnalyzer;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\GroupUse;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\Rector\Catch_;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\Rector\Encapsed;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr;
|
||||
|
|
|
@ -123,7 +123,7 @@ CODE_SAMPLE
|
|||
}
|
||||
/**
|
||||
* @param int|float $rangeLine
|
||||
* @return int|float
|
||||
* @return float|int
|
||||
*/
|
||||
private function resolveRangeLineFromComment($rangeLine, int $line, int $endLine, Stmt $nextStmt)
|
||||
{
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\Rector\Stmt;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\Rector\String_;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ClassConstFetch;
|
||||
use PhpParser\Node\Name\FullyQualified;
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\CodingStyle\Reflection;
|
||||
|
||||
use PHPStan\Reflection\FunctionReflection;
|
||||
use PHPStan\Reflection\MethodReflection;
|
||||
use Rector\FileSystem\FilePathHelper;
|
||||
final class VendorLocationDetector
|
||||
|
@ -20,6 +21,15 @@ final class VendorLocationDetector
|
|||
{
|
||||
$declaringClassReflection = $methodReflection->getDeclaringClass();
|
||||
$fileName = $declaringClassReflection->getFileName();
|
||||
return $this->detect($fileName);
|
||||
}
|
||||
public function detectFunctionReflection(FunctionReflection $functionReflection) : bool
|
||||
{
|
||||
$fileName = $functionReflection->getFileName();
|
||||
return $this->detect($fileName);
|
||||
}
|
||||
private function detect(?string $fileName = null) : bool
|
||||
{
|
||||
// probably internal
|
||||
if ($fileName === null) {
|
||||
return \false;
|
||||
|
|
|
@ -4,6 +4,7 @@ declare (strict_types=1);
|
|||
namespace Rector\DeadCode\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\NullsafeMethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Name;
|
||||
|
@ -29,7 +30,7 @@ final class CallCollectionAnalyzer
|
|||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
}
|
||||
/**
|
||||
* @param StaticCall[]|MethodCall[] $calls
|
||||
* @param StaticCall[]|MethodCall[]|NullsafeMethodCall[] $calls
|
||||
*/
|
||||
public function isExists(array $calls, string $classMethodName, string $className) : bool
|
||||
{
|
||||
|
@ -52,14 +53,14 @@ final class CallCollectionAnalyzer
|
|||
return \false;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall $call
|
||||
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\NullsafeMethodCall $call
|
||||
*/
|
||||
private function isSelfStatic($call) : bool
|
||||
{
|
||||
return $call instanceof StaticCall && $call->class instanceof Name && \in_array($call->class->toString(), [ObjectReference::SELF, ObjectReference::STATIC], \true);
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall $call
|
||||
* @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\NullsafeMethodCall $call
|
||||
*/
|
||||
private function shouldSkip($call, string $classMethodName) : bool
|
||||
{
|
||||
|
|
|
@ -9,6 +9,7 @@ use PhpParser\Node\Expr\Array_;
|
|||
use PhpParser\Node\Expr\ArrayItem;
|
||||
use PhpParser\Node\Expr\CallLike;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\NullsafeMethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
|
@ -88,11 +89,15 @@ final class IsClassMethodUsedAnalyzer
|
|||
if ($this->isClassMethodCalledInLocalMethodCall($class, $classMethodName)) {
|
||||
return \true;
|
||||
}
|
||||
// 2. direct static calls
|
||||
// 2. direct null-safe calls
|
||||
if ($this->isClassMethodCalledInLocalNullsafeMethodCall($class, $classMethodName)) {
|
||||
return \true;
|
||||
}
|
||||
// 3. direct static calls
|
||||
if ($this->isClassMethodUsedInLocalStaticCall($class, $classMethodName)) {
|
||||
return \true;
|
||||
}
|
||||
// 3. magic array calls!
|
||||
// 4. magic array calls!
|
||||
if ($this->isClassMethodCalledInLocalArrayCall($class, $classMethod, $scope)) {
|
||||
return \true;
|
||||
}
|
||||
|
@ -113,6 +118,13 @@ final class IsClassMethodUsedAnalyzer
|
|||
$methodCalls = $this->betterNodeFinder->findInstanceOf($class, MethodCall::class);
|
||||
return $this->callCollectionAnalyzer->isExists($methodCalls, $classMethodName, $className);
|
||||
}
|
||||
private function isClassMethodCalledInLocalNullsafeMethodCall(Class_ $class, string $classMethodName) : bool
|
||||
{
|
||||
$className = (string) $this->nodeNameResolver->getName($class);
|
||||
/** @var Node\Expr\NullsafeMethodCall[] $methodCalls */
|
||||
$methodCalls = $this->betterNodeFinder->findInstanceOf($class, NullsafeMethodCall::class);
|
||||
return $this->callCollectionAnalyzer->isExists($methodCalls, $classMethodName, $className);
|
||||
}
|
||||
private function isInArrayMap(Class_ $class, Array_ $array) : bool
|
||||
{
|
||||
if (!$array->getAttribute(ArrayMapArgVisitor::ATTRIBUTE_NAME) instanceof Arg) {
|
||||
|
@ -134,11 +146,12 @@ final class IsClassMethodUsedAnalyzer
|
|||
{
|
||||
/** @var Array_[] $arrays */
|
||||
$arrays = $this->betterNodeFinder->findInstanceOf($class, Array_::class);
|
||||
$classMethodName = $this->nodeNameResolver->getName($classMethod);
|
||||
foreach ($arrays as $array) {
|
||||
if ($this->isInArrayMap($class, $array)) {
|
||||
return \true;
|
||||
}
|
||||
$arrayCallable = $this->arrayCallableMethodMatcher->match($array, $scope);
|
||||
$arrayCallable = $this->arrayCallableMethodMatcher->match($array, $scope, $classMethodName);
|
||||
if ($arrayCallable instanceof ArrayCallableDynamicMethod) {
|
||||
return \true;
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace Rector\DeadCode\NodeAnalyzer;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\NullsafePropertyFetch;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\StaticPropertyFetch;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
|
@ -24,7 +25,7 @@ final class PropertyWriteonlyAnalyzer
|
|||
public function hasClassDynamicPropertyNames(Class_ $class) : bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirst($class, static function (Node $node) : bool {
|
||||
if (!$node instanceof PropertyFetch) {
|
||||
if (!$node instanceof PropertyFetch && !$node instanceof NullsafePropertyFetch) {
|
||||
return \false;
|
||||
}
|
||||
// has dynamic name - could be anything
|
||||
|
@ -34,7 +35,7 @@ final class PropertyWriteonlyAnalyzer
|
|||
/**
|
||||
* The property fetches are always only assigned to, nothing else
|
||||
*
|
||||
* @param array<PropertyFetch|StaticPropertyFetch> $propertyFetches
|
||||
* @param array<PropertyFetch|StaticPropertyFetch|NullsafePropertyFetch> $propertyFetches
|
||||
*/
|
||||
public function arePropertyFetchesExclusivelyBeingAssignedTo(array $propertyFetches) : bool
|
||||
{
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\DeadCode\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
||||
use PhpParser\Node\Expr\Instanceof_;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\StaticPropertyFetch;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\NodeAnalyzer\ExprAnalyzer;
|
||||
use Rector\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Reflection\ReflectionResolver;
|
||||
final class SafeLeftTypeBooleanAndOrAnalyzer
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeAnalyzer\ExprAnalyzer
|
||||
*/
|
||||
private $exprAnalyzer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Reflection\ReflectionResolver
|
||||
*/
|
||||
private $reflectionResolver;
|
||||
public function __construct(BetterNodeFinder $betterNodeFinder, ExprAnalyzer $exprAnalyzer, ReflectionResolver $reflectionResolver)
|
||||
{
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->exprAnalyzer = $exprAnalyzer;
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
}
|
||||
/**
|
||||
* @param \PhpParser\Node\Expr\BinaryOp\BooleanAnd|\PhpParser\Node\Expr\BinaryOp\BooleanOr $booleanAnd
|
||||
*/
|
||||
public function isSafe($booleanAnd) : bool
|
||||
{
|
||||
$hasNonTypedFromParam = (bool) $this->betterNodeFinder->findFirst($booleanAnd->left, function (Node $node) : bool {
|
||||
return $node instanceof Variable && $this->exprAnalyzer->isNonTypedFromParam($node);
|
||||
});
|
||||
if ($hasNonTypedFromParam) {
|
||||
return \false;
|
||||
}
|
||||
$hasPropertyFetchOrArrayDimFetch = (bool) $this->betterNodeFinder->findFirst($booleanAnd->left, static function (Node $node) : bool {
|
||||
return $node instanceof PropertyFetch || $node instanceof StaticPropertyFetch || $node instanceof ArrayDimFetch;
|
||||
});
|
||||
// get type from Property and ArrayDimFetch is unreliable
|
||||
if ($hasPropertyFetchOrArrayDimFetch) {
|
||||
return \false;
|
||||
}
|
||||
// skip trait this
|
||||
$classReflection = $this->reflectionResolver->resolveClassReflection($booleanAnd);
|
||||
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
|
||||
return !$booleanAnd->left instanceof Instanceof_;
|
||||
}
|
||||
return \true;
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
|
|||
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
|
||||
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
|
||||
use Rector\DeadCode\PhpDoc\Guard\StandaloneTypeRemovalGuard;
|
||||
use Rector\DeadCode\TypeNodeAnalyzer\GenericTypeNodeAnalyzer;
|
||||
use Rector\DeadCode\TypeNodeAnalyzer\MixedArrayTypeNodeAnalyzer;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
|
@ -47,7 +48,12 @@ final class DeadParamTagValueNodeAnalyzer
|
|||
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger
|
||||
*/
|
||||
private $phpDocTypeChanger;
|
||||
public function __construct(NodeNameResolver $nodeNameResolver, TypeComparator $typeComparator, GenericTypeNodeAnalyzer $genericTypeNodeAnalyzer, MixedArrayTypeNodeAnalyzer $mixedArrayTypeNodeAnalyzer, ParamAnalyzer $paramAnalyzer, PhpDocTypeChanger $phpDocTypeChanger)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\DeadCode\PhpDoc\Guard\StandaloneTypeRemovalGuard
|
||||
*/
|
||||
private $standaloneTypeRemovalGuard;
|
||||
public function __construct(NodeNameResolver $nodeNameResolver, TypeComparator $typeComparator, GenericTypeNodeAnalyzer $genericTypeNodeAnalyzer, MixedArrayTypeNodeAnalyzer $mixedArrayTypeNodeAnalyzer, ParamAnalyzer $paramAnalyzer, PhpDocTypeChanger $phpDocTypeChanger, StandaloneTypeRemovalGuard $standaloneTypeRemovalGuard)
|
||||
{
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->typeComparator = $typeComparator;
|
||||
|
@ -55,6 +61,7 @@ final class DeadParamTagValueNodeAnalyzer
|
|||
$this->mixedArrayTypeNodeAnalyzer = $mixedArrayTypeNodeAnalyzer;
|
||||
$this->paramAnalyzer = $paramAnalyzer;
|
||||
$this->phpDocTypeChanger = $phpDocTypeChanger;
|
||||
$this->standaloneTypeRemovalGuard = $standaloneTypeRemovalGuard;
|
||||
}
|
||||
public function isDead(ParamTagValueNode $paramTagValueNode, FunctionLike $functionLike) : bool
|
||||
{
|
||||
|
@ -78,11 +85,15 @@ final class DeadParamTagValueNodeAnalyzer
|
|||
return \false;
|
||||
}
|
||||
if (!$paramTagValueNode->type instanceof BracketsAwareUnionTypeNode) {
|
||||
return \true;
|
||||
return $this->standaloneTypeRemovalGuard->isLegal($paramTagValueNode->type, $param->type);
|
||||
}
|
||||
if ($this->mixedArrayTypeNodeAnalyzer->hasMixedArrayType($paramTagValueNode->type)) {
|
||||
return $this->isAllowedBracketAwareUnion($paramTagValueNode->type);
|
||||
}
|
||||
private function isAllowedBracketAwareUnion(BracketsAwareUnionTypeNode $bracketsAwareUnionTypeNode) : bool
|
||||
{
|
||||
if ($this->mixedArrayTypeNodeAnalyzer->hasMixedArrayType($bracketsAwareUnionTypeNode)) {
|
||||
return \false;
|
||||
}
|
||||
return !$this->genericTypeNodeAnalyzer->hasGenericType($paramTagValueNode->type);
|
||||
return !$this->genericTypeNodeAnalyzer->hasGenericType($bracketsAwareUnionTypeNode);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use PhpParser\Node\FunctionLike;
|
|||
use PHPStan\PhpDocParser\Ast\Node;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
||||
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
|
||||
use Rector\DeadCode\PhpDoc\DeadParamTagValueNodeAnalyzer;
|
||||
|
@ -29,11 +29,11 @@ final class ParamTagRemover
|
|||
$this->deadParamTagValueNodeAnalyzer = $deadParamTagValueNodeAnalyzer;
|
||||
$this->docBlockUpdater = $docBlockUpdater;
|
||||
}
|
||||
public function removeParamTagsIfUseless(PhpDocInfo $phpDocInfo, FunctionLike $functionLike) : bool
|
||||
public function removeParamTagsIfUseless(PhpDocInfo $phpDocInfo, FunctionLike $functionLike, ?Type $type = null) : bool
|
||||
{
|
||||
$hasChanged = \false;
|
||||
$phpDocNodeTraverser = new PhpDocNodeTraverser();
|
||||
$phpDocNodeTraverser->traverseWithCallable($phpDocInfo->getPhpDocNode(), '', function (Node $docNode) use($functionLike, &$hasChanged) : ?int {
|
||||
$phpDocNodeTraverser->traverseWithCallable($phpDocInfo->getPhpDocNode(), '', function (Node $docNode) use($functionLike, &$hasChanged, $type, $phpDocInfo) : ?int {
|
||||
if (!$docNode instanceof PhpDocTagNode) {
|
||||
return null;
|
||||
}
|
||||
|
@ -44,8 +44,12 @@ final class ParamTagRemover
|
|||
if ($docNode->name !== '@param') {
|
||||
return null;
|
||||
}
|
||||
// skip union types
|
||||
if ($docNode->value->type instanceof UnionTypeNode) {
|
||||
$paramType = $phpDocInfo->getParamType($docNode->value->parameterName);
|
||||
if ($type instanceof Type) {
|
||||
if ($type->equals($paramType)) {
|
||||
$hasChanged = \true;
|
||||
return PhpDocNodeTraverser::NODE_REMOVE;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (!$this->deadParamTagValueNodeAnalyzer->isDead($docNode->value, $functionLike)) {
|
||||
|
|
|
@ -93,7 +93,7 @@ CODE_SAMPLE
|
|||
if (!isset(self::CAST_CLASS_TO_NODE_TYPE[$nodeClass])) {
|
||||
return null;
|
||||
}
|
||||
$nodeType = $this->getType($node->expr);
|
||||
$nodeType = $this->nodeTypeResolver->getNativeType($node->expr);
|
||||
if ($nodeType instanceof MixedType) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ use Rector\Contract\Rector\ConfigurableRectorInterface;
|
|||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use RectorPrefix202403\Webmozart\Assert\Assert;
|
||||
use RectorPrefix202405\Webmozart\Assert\Assert;
|
||||
/**
|
||||
* @see \Rector\Tests\DeadCode\Rector\ClassLike\RemoveAnnotationRector\RemoveAnnotationRectorTest
|
||||
*/
|
||||
|
|
|
@ -6,8 +6,10 @@ namespace Rector\DeadCode\Rector\ClassMethod;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use Rector\NodeAnalyzer\ParamAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\Reflection\ReflectionResolver;
|
||||
use Rector\Removing\NodeManipulator\ComplexNodeRemover;
|
||||
use Rector\ValueObject\MethodName;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -27,10 +29,16 @@ final class RemoveUnusedConstructorParamRector extends AbstractRector
|
|||
* @var \Rector\Removing\NodeManipulator\ComplexNodeRemover
|
||||
*/
|
||||
private $complexNodeRemover;
|
||||
public function __construct(ParamAnalyzer $paramAnalyzer, ComplexNodeRemover $complexNodeRemover)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Reflection\ReflectionResolver
|
||||
*/
|
||||
private $reflectionResolver;
|
||||
public function __construct(ParamAnalyzer $paramAnalyzer, ComplexNodeRemover $complexNodeRemover, ReflectionResolver $reflectionResolver)
|
||||
{
|
||||
$this->paramAnalyzer = $paramAnalyzer;
|
||||
$this->complexNodeRemover = $complexNodeRemover;
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
|
@ -83,6 +91,16 @@ CODE_SAMPLE
|
|||
if ($constructorClassMethod->isAbstract()) {
|
||||
return null;
|
||||
}
|
||||
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
|
||||
if (!$classReflection instanceof ClassReflection) {
|
||||
return null;
|
||||
}
|
||||
$interfaces = $classReflection->getInterfaces();
|
||||
foreach ($interfaces as $interface) {
|
||||
if ($interface->hasNativeMethod(MethodName::CONSTRUCT)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
$changedConstructorClassMethod = $this->processRemoveParams($constructorClassMethod);
|
||||
if (!$changedConstructorClassMethod instanceof ClassMethod) {
|
||||
return null;
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\DeadCode\Rector\If_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PHPStan\Type\Constant\ConstantBooleanType;
|
||||
use Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector\ReduceAlwaysFalseIfOrRectorTest
|
||||
*/
|
||||
final class ReduceAlwaysFalseIfOrRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer
|
||||
*/
|
||||
private $safeLeftTypeBooleanAndOrAnalyzer;
|
||||
public function __construct(SafeLeftTypeBooleanAndOrAnalyzer $safeLeftTypeBooleanAndOrAnalyzer)
|
||||
{
|
||||
$this->safeLeftTypeBooleanAndOrAnalyzer = $safeLeftTypeBooleanAndOrAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Reduce always false in a if ( || ) condition', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run(int $number)
|
||||
{
|
||||
if (! is_int($number) || $number > 50) {
|
||||
return 'yes';
|
||||
}
|
||||
|
||||
return 'no';
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function run(int $number)
|
||||
{
|
||||
if ($number > 50) {
|
||||
return 'yes';
|
||||
}
|
||||
|
||||
return 'no';
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [If_::class];
|
||||
}
|
||||
/**
|
||||
* @param If_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if (!$node->cond instanceof BooleanOr) {
|
||||
return null;
|
||||
}
|
||||
$booleanOr = $node->cond;
|
||||
$conditionStaticType = $this->getType($booleanOr->left);
|
||||
if (!$conditionStaticType instanceof ConstantBooleanType) {
|
||||
return null;
|
||||
}
|
||||
if ($conditionStaticType->getValue()) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->safeLeftTypeBooleanAndOrAnalyzer->isSafe($booleanOr)) {
|
||||
return null;
|
||||
}
|
||||
$node->cond = $booleanOr->right;
|
||||
return $node;
|
||||
}
|
||||
}
|
|
@ -5,7 +5,9 @@ namespace Rector\DeadCode\Rector\If_;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\StaticPropertyFetch;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
|
@ -13,12 +15,11 @@ use PhpParser\Node\Stmt;
|
|||
use PhpParser\Node\Stmt\Else_;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Type\Constant\ConstantBooleanType;
|
||||
use Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer;
|
||||
use Rector\NodeAnalyzer\ExprAnalyzer;
|
||||
use Rector\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\Reflection\ReflectionResolver;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
|
@ -26,11 +27,6 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
*/
|
||||
final class RemoveAlwaysTrueIfConditionRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Reflection\ReflectionResolver
|
||||
*/
|
||||
private $reflectionResolver;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\NodeAnalyzer\ExprAnalyzer
|
||||
|
@ -41,11 +37,16 @@ final class RemoveAlwaysTrueIfConditionRector extends AbstractRector
|
|||
* @var \Rector\PhpParser\Node\BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
public function __construct(ReflectionResolver $reflectionResolver, ExprAnalyzer $exprAnalyzer, BetterNodeFinder $betterNodeFinder)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\DeadCode\NodeAnalyzer\SafeLeftTypeBooleanAndOrAnalyzer
|
||||
*/
|
||||
private $safeLeftTypeBooleanAndOrAnalyzer;
|
||||
public function __construct(ExprAnalyzer $exprAnalyzer, BetterNodeFinder $betterNodeFinder, SafeLeftTypeBooleanAndOrAnalyzer $safeLeftTypeBooleanAndOrAnalyzer)
|
||||
{
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
$this->exprAnalyzer = $exprAnalyzer;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
$this->safeLeftTypeBooleanAndOrAnalyzer = $safeLeftTypeBooleanAndOrAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
|
@ -84,10 +85,13 @@ CODE_SAMPLE
|
|||
}
|
||||
/**
|
||||
* @param If_ $node
|
||||
* @return int|null|Stmt[]
|
||||
* @return int|null|Stmt[]|If_
|
||||
*/
|
||||
public function refactor(Node $node)
|
||||
{
|
||||
if ($node->cond instanceof BooleanAnd) {
|
||||
return $this->refactorIfWithBooleanAnd($node);
|
||||
}
|
||||
if ($node->else instanceof Else_) {
|
||||
return null;
|
||||
}
|
||||
|
@ -102,7 +106,7 @@ CODE_SAMPLE
|
|||
if (!$conditionStaticType->getValue()) {
|
||||
return null;
|
||||
}
|
||||
if ($this->shouldSkipPropertyFetch($node->cond)) {
|
||||
if ($this->shouldSkipExpr($node->cond)) {
|
||||
return null;
|
||||
}
|
||||
if ($this->shouldSkipFromParam($node->cond)) {
|
||||
|
@ -128,25 +132,27 @@ CODE_SAMPLE
|
|||
}
|
||||
return \false;
|
||||
}
|
||||
private function shouldSkipPropertyFetch(Expr $expr) : bool
|
||||
private function shouldSkipExpr(Expr $expr) : bool
|
||||
{
|
||||
/** @var PropertyFetch[]|StaticPropertyFetch[] $propertyFetches */
|
||||
$propertyFetches = $this->betterNodeFinder->findInstancesOf($expr, [PropertyFetch::class, StaticPropertyFetch::class]);
|
||||
foreach ($propertyFetches as $propertyFetch) {
|
||||
$classReflection = $this->reflectionResolver->resolveClassReflectionSourceObject($propertyFetch);
|
||||
if (!$classReflection instanceof ClassReflection) {
|
||||
// cannot get parent Trait_ from Property Fetch
|
||||
return \true;
|
||||
}
|
||||
$propertyName = (string) $this->nodeNameResolver->getName($propertyFetch);
|
||||
if (!$classReflection->hasNativeProperty($propertyName)) {
|
||||
continue;
|
||||
}
|
||||
$nativeProperty = $classReflection->getNativeProperty($propertyName);
|
||||
if (!$nativeProperty->hasNativeType()) {
|
||||
return \true;
|
||||
}
|
||||
return (bool) $this->betterNodeFinder->findInstancesOf($expr, [PropertyFetch::class, StaticPropertyFetch::class, ArrayDimFetch::class]);
|
||||
}
|
||||
private function refactorIfWithBooleanAnd(If_ $if) : ?If_
|
||||
{
|
||||
if (!$if->cond instanceof BooleanAnd) {
|
||||
return null;
|
||||
}
|
||||
return \false;
|
||||
$booleanAnd = $if->cond;
|
||||
$leftType = $this->getType($booleanAnd->left);
|
||||
if (!$leftType instanceof ConstantBooleanType) {
|
||||
return null;
|
||||
}
|
||||
if (!$leftType->getValue()) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->safeLeftTypeBooleanAndOrAnalyzer->isSafe($booleanAnd)) {
|
||||
return null;
|
||||
}
|
||||
$if->cond = $booleanAnd->right;
|
||||
return $if;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace Rector\DeadCode\Rector\If_;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
||||
use PhpParser\Node\Expr\BooleanNot;
|
||||
use PhpParser\Node\Expr\CallLike;
|
||||
use PhpParser\Node\Expr\Instanceof_;
|
||||
|
@ -16,9 +17,11 @@ use PhpParser\Node\Stmt;
|
|||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\If_;
|
||||
use PhpParser\NodeTraverser;
|
||||
use PHPStan\Reflection\ClassReflection;
|
||||
use PHPStan\Type\MixedType;
|
||||
use Rector\NodeManipulator\IfManipulator;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\Reflection\ReflectionResolver;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
|
@ -31,9 +34,15 @@ final class RemoveDeadInstanceOfRector extends AbstractRector
|
|||
* @var \Rector\NodeManipulator\IfManipulator
|
||||
*/
|
||||
private $ifManipulator;
|
||||
public function __construct(IfManipulator $ifManipulator)
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Reflection\ReflectionResolver
|
||||
*/
|
||||
private $reflectionResolver;
|
||||
public function __construct(IfManipulator $ifManipulator, ReflectionResolver $reflectionResolver)
|
||||
{
|
||||
$this->ifManipulator = $ifManipulator;
|
||||
$this->reflectionResolver = $reflectionResolver;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
|
@ -64,7 +73,7 @@ CODE_SAMPLE
|
|||
}
|
||||
/**
|
||||
* @param If_ $node
|
||||
* @return Stmt[]|null|int
|
||||
* @return Stmt[]|null|int|If_
|
||||
*/
|
||||
public function refactor(Node $node)
|
||||
{
|
||||
|
@ -74,6 +83,9 @@ CODE_SAMPLE
|
|||
if ($node->cond instanceof BooleanNot && $node->cond->expr instanceof Instanceof_) {
|
||||
return $this->refactorStmtAndInstanceof($node, $node->cond->expr);
|
||||
}
|
||||
if ($node->cond instanceof BooleanAnd) {
|
||||
return $this->refactorIfWithBooleanAnd($node);
|
||||
}
|
||||
if ($node->cond instanceof Instanceof_) {
|
||||
return $this->refactorStmtAndInstanceof($node, $node->cond);
|
||||
}
|
||||
|
@ -84,17 +96,7 @@ CODE_SAMPLE
|
|||
*/
|
||||
private function refactorStmtAndInstanceof(If_ $if, Instanceof_ $instanceof)
|
||||
{
|
||||
if (!$instanceof->class instanceof Name) {
|
||||
return null;
|
||||
}
|
||||
// handle in another rule
|
||||
if ($this->isPropertyFetch($instanceof->expr) || $instanceof->expr instanceof CallLike) {
|
||||
return null;
|
||||
}
|
||||
$classType = $this->nodeTypeResolver->getType($instanceof->class);
|
||||
$exprType = $this->nodeTypeResolver->getType($instanceof->expr);
|
||||
$isSameStaticTypeOrSubtype = $classType->equals($exprType) || $classType->isSuperTypeOf($exprType)->yes();
|
||||
if (!$isSameStaticTypeOrSubtype) {
|
||||
if ($this->isInstanceofTheSameType($instanceof) !== \true) {
|
||||
return null;
|
||||
}
|
||||
if ($this->shouldSkipFromNotTypedParam($instanceof)) {
|
||||
|
@ -125,4 +127,40 @@ CODE_SAMPLE
|
|||
}
|
||||
return $expr instanceof StaticPropertyFetch;
|
||||
}
|
||||
private function isInstanceofTheSameType(Instanceof_ $instanceof) : ?bool
|
||||
{
|
||||
if (!$instanceof->class instanceof Name) {
|
||||
return null;
|
||||
}
|
||||
// handled in another rule
|
||||
if ($this->isPropertyFetch($instanceof->expr) || $instanceof->expr instanceof CallLike) {
|
||||
return null;
|
||||
}
|
||||
$classReflection = $this->reflectionResolver->resolveClassReflection($instanceof);
|
||||
if ($classReflection instanceof ClassReflection && $classReflection->isTrait()) {
|
||||
return null;
|
||||
}
|
||||
$classType = $this->nodeTypeResolver->getType($instanceof->class);
|
||||
$exprType = $this->nodeTypeResolver->getNativeType($instanceof->expr);
|
||||
if ($classType->equals($exprType)) {
|
||||
return \true;
|
||||
}
|
||||
return $classType->isSuperTypeOf($exprType)->yes();
|
||||
}
|
||||
private function refactorIfWithBooleanAnd(If_ $if) : ?\PhpParser\Node\Stmt\If_
|
||||
{
|
||||
if (!$if->cond instanceof BooleanAnd) {
|
||||
return null;
|
||||
}
|
||||
$booleanAnd = $if->cond;
|
||||
if (!$booleanAnd->left instanceof Instanceof_) {
|
||||
return null;
|
||||
}
|
||||
$instanceof = $booleanAnd->left;
|
||||
if ($this->isInstanceofTheSameType($instanceof) !== \true) {
|
||||
return null;
|
||||
}
|
||||
$if->cond = $booleanAnd->right;
|
||||
return $if;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,107 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\DeadCode\Rector\Property;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
||||
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
|
||||
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\Tests\DeadCode\Rector\Property\RemoveUselessReadOnlyTagRector\RemoveUselessReadOnlyTagRectorTest
|
||||
*/
|
||||
final class RemoveUselessReadOnlyTagRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Privatization\NodeManipulator\VisibilityManipulator
|
||||
*/
|
||||
private $visibilityManipulator;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
||||
*/
|
||||
private $phpDocInfoFactory;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Comments\NodeDocBlock\DocBlockUpdater
|
||||
*/
|
||||
private $docBlockUpdater;
|
||||
public function __construct(VisibilityManipulator $visibilityManipulator, PhpDocInfoFactory $phpDocInfoFactory, DocBlockUpdater $docBlockUpdater)
|
||||
{
|
||||
$this->visibilityManipulator = $visibilityManipulator;
|
||||
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
||||
$this->docBlockUpdater = $docBlockUpdater;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Remove useless @readonly annotation on native readonly type', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
*/
|
||||
private readonly string $name;
|
||||
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
final class SomeClass
|
||||
{
|
||||
private readonly string $name;
|
||||
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Class_::class, Property::class, Param::class];
|
||||
}
|
||||
/**
|
||||
* @param Class_|Property|Param $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
// for param, only on property promotion
|
||||
if ($node instanceof Param && $node->flags === 0) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->visibilityManipulator->isReadonly($node)) {
|
||||
return null;
|
||||
}
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
||||
$readonlyDoc = $phpDocInfo->getByName('readonly');
|
||||
if (!$readonlyDoc instanceof PhpDocTagNode) {
|
||||
return null;
|
||||
}
|
||||
if (!$readonlyDoc->value instanceof GenericTagValueNode) {
|
||||
return null;
|
||||
}
|
||||
if ($readonlyDoc->value->value !== '') {
|
||||
return null;
|
||||
}
|
||||
$phpDocInfo->removeByName('readonly');
|
||||
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
|
||||
return $node;
|
||||
}
|
||||
}
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\DeadCode\SideEffect;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\ArrayDimFetch;
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace Rector\EarlyReturn\Rector\Return_;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\AssignOp;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
|
@ -98,6 +99,9 @@ CODE_SAMPLE
|
|||
$initialAssign = null;
|
||||
$initialAssignPosition = null;
|
||||
foreach ($node->stmts as $key => $stmt) {
|
||||
if ($stmt instanceof Expression && $stmt->expr instanceof AssignOp) {
|
||||
return null;
|
||||
}
|
||||
if ($stmt instanceof If_) {
|
||||
$ifs[$key] = $stmt;
|
||||
continue;
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\Naming\ExpectedNameResolver;
|
||||
|
||||
use RectorPrefix202403\Doctrine\Inflector\Inflector;
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Doctrine\Inflector\Inflector;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use Rector\Util\StringUtils;
|
||||
/**
|
||||
* @see \Rector\Tests\Naming\ExpectedNameResolver\InflectorSingularResolverTest
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\Naming\Naming;
|
||||
|
||||
use DateTimeInterface;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
|
@ -93,6 +94,9 @@ final class ExpectedNameResolver
|
|||
}
|
||||
$className = $this->nodeNameResolver->getName($new->class);
|
||||
$fullyQualifiedObjectType = new FullyQualifiedObjectType($className);
|
||||
if ($fullyQualifiedObjectType->isInstanceOf(DateTimeInterface::class)->yes()) {
|
||||
return null;
|
||||
}
|
||||
$expectedName = $this->propertyNaming->getExpectedNameFromType($fullyQualifiedObjectType);
|
||||
if (!$expectedName instanceof ExpectedName) {
|
||||
return null;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\Naming\Naming;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PHPStan\Type\Generic\GenericObjectType;
|
||||
use PHPStan\Type\ObjectType;
|
||||
use PHPStan\Type\StaticType;
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\Naming\Rector\Assign;
|
||||
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
declare (strict_types=1);
|
||||
namespace Rector\Naming;
|
||||
|
||||
use RectorPrefix202403\Doctrine\Inflector\Inflector;
|
||||
use RectorPrefix202403\Nette\Utils\Strings;
|
||||
use RectorPrefix202405\Doctrine\Inflector\Inflector;
|
||||
use RectorPrefix202405\Nette\Utils\Strings;
|
||||
final class RectorNamingInflector
|
||||
{
|
||||
/**
|
||||
|
|
|
@ -7,7 +7,7 @@ use PhpParser\Node\Stmt\ClassLike;
|
|||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\Naming\ValueObject\PropertyRename;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use RectorPrefix202403\Webmozart\Assert\InvalidArgumentException;
|
||||
use RectorPrefix202405\Webmozart\Assert\InvalidArgumentException;
|
||||
final class PropertyRenameFactory
|
||||
{
|
||||
/**
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace Rector\Php55\Rector\FuncCall;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Closure;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
use PhpParser\Node\Name;
|
||||
|
@ -89,13 +90,11 @@ CODE_SAMPLE
|
|||
$firstArgument = $node->getArgs()[0];
|
||||
$firstArgumentValue = $firstArgument->value;
|
||||
$patternWithoutEExpr = $this->regexMatcher->resolvePatternExpressionWithoutEIfFound($firstArgumentValue);
|
||||
if ($patternWithoutEExpr === null) {
|
||||
if (!$patternWithoutEExpr instanceof Expr) {
|
||||
return null;
|
||||
}
|
||||
/** @var Arg $secondArgument */
|
||||
$secondArgument = $node->args[1];
|
||||
$secondArgumentValue = $secondArgument->value;
|
||||
$anonymousFunction = $this->anonymousFunctionFactory->createAnonymousFunctionFromExpr($secondArgumentValue);
|
||||
$secondArgument = $node->getArgs()[1];
|
||||
$anonymousFunction = $this->createAnonymousFunction($secondArgument);
|
||||
if (!$anonymousFunction instanceof Closure) {
|
||||
return null;
|
||||
}
|
||||
|
@ -104,4 +103,8 @@ CODE_SAMPLE
|
|||
$secondArgument->value = $anonymousFunction;
|
||||
return $node;
|
||||
}
|
||||
private function createAnonymousFunction(Arg $arg) : ?Closure
|
||||
{
|
||||
return $this->anonymousFunctionFactory->createAnonymousFunctionFromExpr($arg->value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ use Rector\ValueObject\PhpVersionFeature;
|
|||
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use RectorPrefix202403\Webmozart\Assert\Assert;
|
||||
use RectorPrefix202405\Webmozart\Assert\Assert;
|
||||
/**
|
||||
* @changelog https://wiki.php.net/rfc/class_name_scalars https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.8.md#form
|
||||
*
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue