Skip to content

Commit

Permalink
[Config] Make sure only one of type-declaration/dead-code or with*Lev…
Browse files Browse the repository at this point in the history
…el() is used to avoid duplicates (#5578)
  • Loading branch information
TomasVotruba authored Feb 7, 2024
1 parent 5b3d656 commit 4570438
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
20 changes: 1 addition & 19 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,12 @@ parameters:
- src/BetterPhpDocParser/PhpDocParser/DoctrineAnnotationDecorator.php
- src/Parallel/Application/ParallelFileProcessor.php
# rector rules
- rules/CodeQuality/Rector/If_/SimplifyIfNullableReturnRector.php
- rules/DeadCode/Rector/Assign/RemoveDoubleAssignRector.php
- rules/CodeQuality/Rector/Isset_/IssetOnPropertyObjectToPropertyExistsRector.php
- rules/*/Rector/*Rector.php
- utils/Rector/MoveAbstractRectorToChildrenRector.php
- rules/TypeDeclaration/Rector/*
- rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php
- rules/Transform/Rector/FuncCall/FuncCallToMethodCallRector.php
- rules/Renaming/Rector/ClassMethod/RenameAnnotationRector.php
- rules/Php83/Rector/ClassConst/AddTypeToConstRector.php
- rules/EarlyReturn/Rector/Return_/PreparedValueToEarlyReturnRector.php
- rules/Naming/Rector/Foreach_/RenameForeachValueVariableToMatchExprVariableRector.php
- rules/DeadCode/Rector/StaticCall/RemoveParentCallWithoutParentRector.php
- rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php
- rules/CodeQuality/Rector/If_/ConsecutiveNullCompareReturnsToNullCoalesceQueueRector.php
- src/PhpDocParser/PhpDocParser/PhpDocNodeTraverser.php
- src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
- rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
- rules/Php80/Rector/Class_/ClassPropertyAssignToConstructorPromotionRector.php
- rules/EarlyReturn/Rector/If_/ChangeAndIfToEarlyReturnRector.php
- rules/DeadCode/Rector/Node/RemoveNonExistingVarAnnotationRector.php
- rules/CodeQuality/Rector/Foreach_/SimplifyForeachToCoalescingRector.php
- rules/CodeQuality/Rector/Foreach_/ForeachToInArrayRector.php
- src/Configuration/RectorConfigBuilder.php
- rules/Transform/Rector/FileWithoutNamespace/RectorConfigBuilderRector.php

# is nested expr
-
Expand Down
2 changes: 0 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
earlyReturn: true,
naming: true
)
// @experimental since 0.19.7 for more smooth Rector integration to new project
->withTypeCoverageLevel(10)
->withPhpSets()
->withRules([DeclareStrictTypesRector::class])
->withPaths([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,12 @@ public function refactor(Node $node): ?Node
$rules->items = array_merge($rules->items, $rectorConfigStmt->expr->getArgs()[0]->value->items);
} elseif ($this->isName($rectorConfigStmt->expr->name, 'paths')) {
Assert::isAOf($rectorConfigStmt->expr->getArgs()[0]->value, Array_::class);
$paths = $rectorConfigStmt->expr->getArgs()[0]->value;
$paths = $rectorConfigStmt->expr->getArgs()[0]
->value;
} elseif ($this->isName($rectorConfigStmt->expr->name, 'skip')) {
Assert::isAOf($rectorConfigStmt->expr->getArgs()[0]->value, Array_::class);
$skips = $rectorConfigStmt->expr->getArgs()[0]->value;
$skips = $rectorConfigStmt->expr->getArgs()[0]
->value;
} else {
// implementing method by method
return null;
Expand Down
26 changes: 26 additions & 0 deletions src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,32 @@ final class RectorConfigBuilder

private ?string $symfonyContainerPhpFile = null;

/**
* To make sure type declarations set and level are not duplicated,
* as both contain same rules
*/
private bool $isTypeCoverageLevelUsed = false;

private bool $isDeadCodeLevelUsed = false;

public function __invoke(RectorConfig $rectorConfig): void
{
$uniqueSets = array_unique($this->sets);

if (in_array(SetList::TYPE_DECLARATION, $uniqueSets, true) && $this->isTypeCoverageLevelUsed) {
throw new InvalidConfigurationException(sprintf(
'Your config already enables type declarations set.%sRemove "->withTypeCoverageLevel()" as it only duplicates it, or remove type declaration set.',
PHP_EOL
));
}

if (in_array(SetList::DEAD_CODE, $uniqueSets, true) && $this->isDeadCodeLevelUsed) {
throw new InvalidConfigurationException(sprintf(
'Your config already enables dead code set.%sRemove "->withDeadCodeLevel()" as it only duplicates it, or remove dead code set.',
PHP_EOL
));
}

$rectorConfig->sets($uniqueSets);

if ($this->paths !== []) {
Expand Down Expand Up @@ -596,6 +618,8 @@ public function withSymfonyContainerPhp(string $symfonyContainerPhpFile): self
*/
public function withDeadCodeLevel(int $level): self
{
$this->isDeadCodeLevelUsed = true;

$levelRules = LevelRulesResolver::resolve(
$level,
DeadCodeLevel::RULE_LIST,
Expand All @@ -613,6 +637,8 @@ public function withDeadCodeLevel(int $level): self
*/
public function withTypeCoverageLevel(int $level): self
{
$this->isTypeCoverageLevelUsed = true;

$levelRules = LevelRulesResolver::resolve(
$level,
TypeCoverageLevel::RULE_LIST,
Expand Down
14 changes: 11 additions & 3 deletions src/Console/Command/DetectNodeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Console\Command;

use Throwable;
use Nette\Utils\Strings;
use Rector\CustomRules\SimpleNodeDumper;
use Rector\PhpParser\Parser\SimplePhpParser;
Expand All @@ -14,6 +13,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;

final class DetectNodeCommand extends Command
{
Expand Down Expand Up @@ -64,10 +64,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
private function addConsoleColors(string $contents): string
{
// decorate class names
$colorContents = Strings::replace($contents, self::CLASS_NAME_REGEX, static fn(array $match): string => '<fg=green>' . $match['class_name'] . '</>(');
$colorContents = Strings::replace(
$contents,
self::CLASS_NAME_REGEX,
static fn (array $match): string => '<fg=green>' . $match['class_name'] . '</>('
);

// decorate keys
return Strings::replace($colorContents, self::PROPERTY_KEY_REGEX, static fn(array $match): string => '<fg=yellow>' . $match['key'] . '</>:');
return Strings::replace(
$colorContents,
self::PROPERTY_KEY_REGEX,
static fn (array $match): string => '<fg=yellow>' . $match['key'] . '</>:'
);
}

private function askQuestionAndDumpNode(): void
Expand Down

0 comments on commit 4570438

Please sign in to comment.