mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
[CodeQuality] Add NewStaticToNewSelfRector (#4468)
* [CodeQuality] Add NewStaticToNewSelfRector * regenerated docs * drop patches
This commit is contained in:
parent
4fc93376c6
commit
10ae0d687a
@ -57,9 +57,7 @@
|
||||
"webmozart/assert": "^1.9"
|
||||
},
|
||||
"require-dev": {
|
||||
"cweagans/composer-patches": "^1.7",
|
||||
"friendsofphp/php-cs-fixer": "^2.16",
|
||||
"migrify/vendor-patches": "^0.3.49",
|
||||
"nette/application": "^3.0",
|
||||
"nette/di": "^3.0",
|
||||
"nette/forms": "^3.0",
|
||||
@ -346,12 +344,5 @@
|
||||
"config": {
|
||||
"sort-packages": true,
|
||||
"process-timeout": 0
|
||||
},
|
||||
"extra": {
|
||||
"patches": {
|
||||
"symplify/monorepo-builder": [
|
||||
"patches/symplify-monorepo-builder-packages-split-src-packagetorepositorysplitter-php.patch"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -55,6 +55,7 @@ use Rector\CodeQuality\Rector\Isset_\IssetOnPropertyObjectToPropertyExistsRector
|
||||
use Rector\CodeQuality\Rector\LogicalAnd\AndAssignsToSeparateLinesRector;
|
||||
use Rector\CodeQuality\Rector\LogicalAnd\LogicalToBooleanRector;
|
||||
use Rector\CodeQuality\Rector\Name\FixClassCaseSensitivityNameRector;
|
||||
use Rector\CodeQuality\Rector\New_\NewStaticToNewSelfRector;
|
||||
use Rector\CodeQuality\Rector\NotEqual\CommonNotEqualRector;
|
||||
use Rector\CodeQuality\Rector\Return_\SimplifyUselessVariableRector;
|
||||
use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector;
|
||||
@ -69,157 +70,94 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigura
|
||||
|
||||
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||
$services = $containerConfigurator->services();
|
||||
|
||||
$services->set(CombinedAssignRector::class);
|
||||
|
||||
$services->set(SimplifyEmptyArrayCheckRector::class);
|
||||
|
||||
$services->set(ForeachToInArrayRector::class);
|
||||
|
||||
$services->set(SimplifyForeachToCoalescingRector::class);
|
||||
|
||||
$services->set(InArrayAndArrayKeysToArrayKeyExistsRector::class);
|
||||
|
||||
$services->set(SimplifyFuncGetArgsCountRector::class);
|
||||
|
||||
$services->set(SimplifyInArrayValuesRector::class);
|
||||
|
||||
$services->set(SimplifyStrposLowerRector::class);
|
||||
|
||||
$services->set(GetClassToInstanceOfRector::class);
|
||||
|
||||
$services->set(SimplifyArraySearchRector::class);
|
||||
|
||||
$services->set(SimplifyConditionsRector::class);
|
||||
|
||||
$services->set(SimplifyIfNotNullReturnRector::class);
|
||||
|
||||
$services->set(SimplifyIfReturnBoolRector::class);
|
||||
|
||||
$services->set(SimplifyUselessVariableRector::class);
|
||||
|
||||
$services->set(UnnecessaryTernaryExpressionRector::class);
|
||||
|
||||
$services->set(RemoveExtraParametersRector::class);
|
||||
|
||||
$services->set(SimplifyDeMorganBinaryRector::class);
|
||||
|
||||
$services->set(SimplifyTautologyTernaryRector::class);
|
||||
|
||||
$services->set(SimplifyForeachToArrayFilterRector::class);
|
||||
|
||||
$services->set(SingleInArrayToCompareRector::class);
|
||||
|
||||
$services->set(SimplifyIfElseToTernaryRector::class);
|
||||
|
||||
$services->set(JoinStringConcatRector::class);
|
||||
|
||||
$services->set(ConsecutiveNullCompareReturnsToNullCoalesceQueueRector::class);
|
||||
|
||||
$services->set(SimplifyIfIssetToNullCoalescingRector::class);
|
||||
|
||||
$services->set(ExplicitBoolCompareRector::class);
|
||||
|
||||
$services->set(CombineIfRector::class);
|
||||
|
||||
$services->set(UseIdenticalOverEqualWithSameTypeRector::class);
|
||||
|
||||
$services->set(SimplifyDuplicatedTernaryRector::class);
|
||||
|
||||
$services->set(SimplifyBoolIdenticalTrueRector::class);
|
||||
|
||||
$services->set(SimplifyRegexPatternRector::class);
|
||||
|
||||
$services->set(BooleanNotIdenticalToNotIdenticalRector::class);
|
||||
|
||||
$services->set(CallableThisArrayToAnonymousFunctionRector::class);
|
||||
|
||||
$services->set(AndAssignsToSeparateLinesRector::class);
|
||||
|
||||
$services->set(ForToForeachRector::class);
|
||||
|
||||
$services->set(CompactToVariablesRector::class);
|
||||
|
||||
$services->set(CompleteDynamicPropertiesRector::class);
|
||||
|
||||
$services->set(IsAWithStringWithThirdArgumentRector::class);
|
||||
|
||||
$services->set(StrlenZeroToIdenticalEmptyStringRector::class);
|
||||
|
||||
$services->set(RemoveAlwaysTrueConditionSetInConstructorRector::class);
|
||||
|
||||
$services->set(ThrowWithPreviousExceptionRector::class);
|
||||
|
||||
$services->set(RemoveSoleValueSprintfRector::class);
|
||||
|
||||
$services->set(ShortenElseIfRector::class);
|
||||
|
||||
$services->set(UseInterfaceOverImplementationInConstructorRector::class);
|
||||
|
||||
$services->set(AddPregQuoteDelimiterRector::class);
|
||||
|
||||
$services->set(ArrayMergeOfNonArraysToSimpleArrayRector::class);
|
||||
|
||||
$services->set(IntvalToTypeCastRector::class);
|
||||
|
||||
$services->set(ArrayKeyExistsTernaryThenValueToCoalescingRector::class);
|
||||
|
||||
$services->set(AbsolutizeRequireAndIncludePathRector::class);
|
||||
|
||||
$services->set(ChangeArrayPushToArrayAssignRector::class);
|
||||
|
||||
$services->set(ForRepeatedCountToOwnVariableRector::class);
|
||||
|
||||
$services->set(ForeachItemsAssignToEmptyArrayToAssignRector::class);
|
||||
|
||||
$services->set(InlineIfToExplicitIfRector::class);
|
||||
|
||||
$services->set(ArrayKeysAndInArrayToArrayKeyExistsRector::class);
|
||||
|
||||
$services->set(SplitListAssignToSeparateLineRector::class);
|
||||
|
||||
$services->set(UnusedForeachValueToArrayKeysRector::class);
|
||||
|
||||
$services->set(ArrayThisCallToThisMethodCallRector::class);
|
||||
|
||||
$services->set(CommonNotEqualRector::class);
|
||||
|
||||
$services->set(RenameFunctionRector::class)
|
||||
->call('configure', [[
|
||||
RenameFunctionRector::OLD_FUNCTION_TO_NEW_FUNCTION => [
|
||||
'split' => 'explode',
|
||||
'join' => 'implode',
|
||||
'sizeof' => 'count',
|
||||
# https://www.php.net/manual/en/aliases.php
|
||||
'chop' => 'rtrim',
|
||||
'doubleval' => 'floatval',
|
||||
'gzputs' => 'gzwrites',
|
||||
'fputs' => 'fwrite',
|
||||
'ini_alter' => 'ini_set',
|
||||
'is_double' => 'is_float',
|
||||
'is_integer' => 'is_int',
|
||||
'is_long' => 'is_int',
|
||||
'is_real' => 'is_float',
|
||||
'is_writeable' => 'is_writable',
|
||||
'key_exists' => 'array_key_exists',
|
||||
'pos' => 'current',
|
||||
'strchr' => 'strstr',
|
||||
# mb
|
||||
'mbstrcut' => 'mb_strcut',
|
||||
'mbstrlen' => 'mb_strlen',
|
||||
'mbstrpos' => 'mb_strpos',
|
||||
'mbstrrpos' => 'mb_strrpos',
|
||||
'mbsubstr' => 'mb_substr',
|
||||
],
|
||||
]]);
|
||||
|
||||
$services->set(RenameFunctionRector::class)->call('configure', [[
|
||||
RenameFunctionRector::OLD_FUNCTION_TO_NEW_FUNCTION => [
|
||||
'split' => 'explode',
|
||||
'join' => 'implode',
|
||||
'sizeof' => 'count',
|
||||
# https://www.php.net/manual/en/aliases.php
|
||||
'chop' => 'rtrim',
|
||||
'doubleval' => 'floatval',
|
||||
'gzputs' => 'gzwrites',
|
||||
'fputs' => 'fwrite',
|
||||
'ini_alter' => 'ini_set',
|
||||
'is_double' => 'is_float',
|
||||
'is_integer' => 'is_int',
|
||||
'is_long' => 'is_int',
|
||||
'is_real' => 'is_float',
|
||||
'is_writeable' => 'is_writable',
|
||||
'key_exists' => 'array_key_exists',
|
||||
'pos' => 'current',
|
||||
'strchr' => 'strstr',
|
||||
# mb
|
||||
'mbstrcut' => 'mb_strcut',
|
||||
'mbstrlen' => 'mb_strlen',
|
||||
'mbstrpos' => 'mb_strpos',
|
||||
'mbstrrpos' => 'mb_strrpos',
|
||||
'mbsubstr' => 'mb_substr',
|
||||
],
|
||||
]]);
|
||||
$services->set(SetTypeToCastRector::class);
|
||||
|
||||
$services->set(LogicalToBooleanRector::class);
|
||||
|
||||
$services->set(VarToPublicPropertyRector::class);
|
||||
|
||||
$services->set(FixClassCaseSensitivityNameRector::class);
|
||||
|
||||
$services->set(IssetOnPropertyObjectToPropertyExistsRector::class);
|
||||
$services->set(NewStaticToNewSelfRector::class);
|
||||
};
|
||||
|
@ -1,4 +1,4 @@
|
||||
# All 595 Rectors Overview
|
||||
# All 596 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
---
|
||||
@ -8,7 +8,7 @@
|
||||
- [Architecture](#architecture) (2)
|
||||
- [Autodiscovery](#autodiscovery) (4)
|
||||
- [CakePHP](#cakephp) (6)
|
||||
- [CodeQuality](#codequality) (59)
|
||||
- [CodeQuality](#codequality) (60)
|
||||
- [CodingStyle](#codingstyle) (33)
|
||||
- [DeadCode](#deadcode) (41)
|
||||
- [Decouple](#decouple) (1)
|
||||
@ -941,11 +941,12 @@ Change `foreach()` items assign to empty array to direct assign
|
||||
{
|
||||
public function run($items)
|
||||
{
|
||||
$items2 = [];
|
||||
$collectedItems = [];
|
||||
|
||||
- foreach ($items as $item) {
|
||||
- $items2[] = $item;
|
||||
- $collectedItems[] = $item;
|
||||
- }
|
||||
+ $items2 = $items;
|
||||
+ $collectedItems = $items;
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -1122,6 +1123,26 @@ Change OR, AND to ||, && with more common understanding
|
||||
|
||||
<br><br>
|
||||
|
||||
### `NewStaticToNewSelfRector`
|
||||
|
||||
- class: [`Rector\CodeQuality\Rector\New_\NewStaticToNewSelfRector`](/rules/code-quality/src/Rector/New_/NewStaticToNewSelfRector.php)
|
||||
- [test fixtures](/rules/code-quality/tests/Rector/New_/NewStaticToNewSelfRector/Fixture)
|
||||
|
||||
Change unsafe new `static()` to new `self()`
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
public function build()
|
||||
{
|
||||
- return new static();
|
||||
+ return new self();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br><br>
|
||||
|
||||
### `RemoveAlwaysTrueConditionSetInConstructorRector`
|
||||
|
||||
- class: [`Rector\CodeQuality\Rector\FunctionLike\RemoveAlwaysTrueConditionSetInConstructorRector`](/rules/code-quality/src/Rector/FunctionLike/RemoveAlwaysTrueConditionSetInConstructorRector.php)
|
||||
|
@ -1,20 +0,0 @@
|
||||
--- /dev/null
|
||||
+++ ../packages/split/src/PackageToRepositorySplitter.php
|
||||
@@ -80,7 +80,7 @@
|
||||
$this->symfonyStyle->warning($message);
|
||||
}
|
||||
|
||||
- $tag = $this->gitManager->getMostRecentTag($rootDirectory);
|
||||
+ // $tag = $this->gitManager->getMostRecentTag($rootDirectory);
|
||||
}
|
||||
|
||||
// If branch not set, default to current branch
|
||||
@@ -87,7 +87,7 @@
|
||||
$branch = $branch ?? $this->gitManager->getCurrentBranch();
|
||||
|
||||
// If branch doesn't exist on origin, push it
|
||||
- if (! $this->gitManager->doesBranchExistOnRemote($branch)) {
|
||||
+ if (! $this->gitManager->doesBranchExistOnRemote($branch) && $tag === null) {
|
||||
$missingBranchMessage = sprintf('Branch "%s" does not exist on origin, pushing it...', $branch);
|
||||
$this->symfonyStyle->note($missingBranchMessage);
|
||||
$this->symfonyStyle->writeln($this->gitManager->pushBranchToRemoteOrigin($branch));
|
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Rector\New_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
/**
|
||||
* @see https://github.com/phpstan/phpstan-src/blob/699c420f8193da66927e54494a0afa0c323c6458/src/Rules/Classes/NewStaticRule.php
|
||||
*
|
||||
* @see \Rector\CodeQuality\Tests\Rector\New_\NewStaticToNewSelfRector\NewStaticToNewSelfRectorTest
|
||||
*/
|
||||
final class NewStaticToNewSelfRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Change unsafe new static() to new self()', [
|
||||
new CodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function build()
|
||||
{
|
||||
return new static();
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function build()
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [New_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param New_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$class = $node->getAttribute(AttributeKey::CLASS_NODE);
|
||||
if (! $class instanceof Class_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $class->isFinal()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isName($node->class, 'static')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$node->class = new Name('self');
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\New_\NewStaticToNewSelfRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function build()
|
||||
{
|
||||
return new static();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\New_\NewStaticToNewSelfRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public function build()
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\New_\NewStaticToNewSelfRector\Fixture;
|
||||
|
||||
class SkipNonFinal
|
||||
{
|
||||
public function build()
|
||||
{
|
||||
return new static();
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\New_\NewStaticToNewSelfRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\CodeQuality\Rector\New_\NewStaticToNewSelfRector;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class NewStaticToNewSelfRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return NewStaticToNewSelfRector::class;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user