Skip to content

Commit

Permalink
Add enums (#326)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jun 28, 2021
1 parent 62bbaa5 commit 5282793
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 150 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"doctrine/inflector": "^2.0",
"ergebnis/json-printer": "^3.1",
"idiosyncratic/editorconfig": "^0.1.3",
"myclabs/php-enum": "^1.8",
"nette/utils": "^3.2",
"nikic/php-parser": "4.10.5",
"phpstan/phpdoc-parser": "^0.5.5",
Expand Down Expand Up @@ -53,15 +54,16 @@
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-nette": "^0.12.19",
"phpunit/phpunit": "^9.5",
"rector/rector-generator": "^0.1.7",
"rector/phpstan-rules": "^0.3.3",
"rector/rector-generator": "^0.1.7",
"symplify/coding-standard": "^9.3.22",
"symplify/easy-ci": "^9.3.22",
"symplify/easy-coding-standard": "^9.3.22",
"symplify/easy-testing": "^9.3.22",
"symplify/phpstan-extensions": "^9.3.22",
"symplify/phpstan-rules": "^9.3.22",
"symplify/rule-doc-generator": "^9.3.22"
"symplify/rule-doc-generator": "^9.3.22",
"timeweb/phpstan-enum": "^2.3"
},
"replace": {
"rector/rector": "self.version"
Expand Down
2 changes: 1 addition & 1 deletion config/services-rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
$services->alias(PSR4AutoloadNamespaceMatcherInterface::class, PSR4NamespaceMatcher::class);

$services->load('Rector\\', __DIR__ . '/../rules')
->exclude([__DIR__ . '/../rules/*/{ValueObject,Rector,Contract,Exception}']);
->exclude([__DIR__ . '/../rules/*/{ValueObject,Rector,Contract,Exception,Enum}']);
};
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,9 @@ parameters:
- src/Application/FileProcessor.php
- rules/CodingStyle/Rector/Assign/ManualJsonStringToJsonEncodeArrayRector.php
- rules/CodeQuality/Rector/Return_/SimplifyUselessVariableRector.php

# is fixed in next symplify version
-
message: '#Do not use @method tag in class docblock#'
paths:
- rules/*/Enum/*
4 changes: 2 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Rector\CodingStyle\Enum\PreferenceSelfThis;
use Rector\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector;
use Rector\CodingStyle\Rector\String_\SplitStringClassConstantToClassConstFetchRector;
use Rector\CodingStyle\ValueObject\PreferenceSelfThis;
use Rector\Core\Configuration\Option;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector;
Expand Down Expand Up @@ -54,7 +54,7 @@
$services->set(PreferThisOrSelfMethodCallRector::class)
->call('configure', [[
PreferThisOrSelfMethodCallRector::TYPE_TO_PREFERENCE => [
TestCase::class => PreferenceSelfThis::PREFER_THIS,
TestCase::class => ValueObjectInliner::inline(PreferenceSelfThis::PREFER_THIS()),
],
]]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
declare(strict_types=1);

use PHPUnit\Framework\TestCase;

use Rector\CodingStyle\Enum\PreferenceSelfThis;
use Rector\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector;
use Rector\CodingStyle\ValueObject\PreferenceSelfThis;
use Rector\Tests\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector\Source\AbstractTestCase;
use Rector\Tests\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector\Source\BeLocalClass;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(PreferThisOrSelfMethodCallRector::class)
->call('configure', [[
PreferThisOrSelfMethodCallRector::TYPE_TO_PREFERENCE => [
AbstractTestCase::class => PreferenceSelfThis::PREFER_SELF,
BeLocalClass::class => PreferenceSelfThis::PREFER_THIS,
TestCase::class => PreferenceSelfThis::PREFER_SELF,
AbstractTestCase::class => ValueObjectInliner::inline(PreferenceSelfThis::PREFER_SELF()),
BeLocalClass::class => ValueObjectInliner::inline(PreferenceSelfThis::PREFER_THIS()),
TestCase::class => ValueObjectInliner::inline(PreferenceSelfThis::PREFER_SELF()),
],
]]);
};
26 changes: 26 additions & 0 deletions rules/CodingStyle/Enum/PreferenceSelfThis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Rector\CodingStyle\Enum;

use MyCLabs\Enum\Enum;

/**
* @method static PreferenceSelfThis PREFER_THIS()
* @method static PreferenceSelfThis PREFER_SELF()
*/
final class PreferenceSelfThis extends Enum
{
/**
* @api
* @var string
*/
private const PREFER_THIS = 'prefer_this';

/**
* @api
* @var string
*/
private const PREFER_SELF = 'prefer_self';
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Type\ObjectType;
use Rector\CodingStyle\ValueObject\PreferenceSelfThis;
use Rector\CodingStyle\Enum\PreferenceSelfThis;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Exception\Configuration\InvalidConfigurationException;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -33,7 +32,7 @@ final class PreferThisOrSelfMethodCallRector extends AbstractRector implements C
private const SELF = 'self';

/**
* @var array<class-string, string>
* @var array<class-string, PreferenceSelfThis>
*/
private array $typeToPreference = [];

Expand Down Expand Up @@ -63,7 +62,7 @@ public function run()
,
[
self::TYPE_TO_PREFERENCE => [
'PHPUnit\Framework\TestCase' => PreferenceSelfThis::PREFER_SELF,
'PHPUnit\Framework\TestCase' => PreferenceSelfThis::PREFER_SELF(),
],
]
),
Expand All @@ -88,7 +87,8 @@ public function refactor(Node $node): ?Node
continue;
}

if ($preference === PreferenceSelfThis::PREFER_SELF) {
/** @var PreferenceSelfThis $preference */
if ($preference->equals(PreferenceSelfThis::PREFER_SELF())) {
return $this->processToSelf($node);
}

Expand All @@ -99,16 +99,12 @@ public function refactor(Node $node): ?Node
}

/**
* @param array<string, array<class-string, string>> $configuration
* @param array<string, array<class-string, PreferenceSelfThis>> $configuration
*/
public function configure(array $configuration): void
{
$typeToPreference = $configuration[self::TYPE_TO_PREFERENCE] ?? [];
Assert::allString($typeToPreference);

foreach ($typeToPreference as $singleTypeToPreference) {
$this->ensurePreferenceIsValid($singleTypeToPreference);
}
Assert::allIsAOf($typeToPreference, PreferenceSelfThis::class);

$this->typeToPreference = $typeToPreference;
}
Expand Down Expand Up @@ -154,18 +150,4 @@ private function processToThis(Node $node): ?MethodCall

return $this->nodeFactory->createMethodCall('this', $name, $node->args);
}

private function ensurePreferenceIsValid(string $preference): void
{
if (in_array($preference, PreferenceSelfThis::ALLOWED_VALUES, true)) {
return;
}

throw new InvalidConfigurationException(sprintf(
'Preference configuration "%s" for "%s" is not valid. Use one of "%s"',
$preference,
self::class,
implode('", "', PreferenceSelfThis::ALLOWED_VALUES)
));
}
}
28 changes: 0 additions & 28 deletions rules/CodingStyle/ValueObject/PreferenceSelfThis.php

This file was deleted.

36 changes: 36 additions & 0 deletions rules/Php80/Enum/MatchKind.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Rector\Php80\Enum;

use MyCLabs\Enum\Enum;

/**
* @method static MatchKind NORMAL()
* @method static MatchKind ASSIGN()
* @method static MatchKind RETURN()
* @method static MatchKind THROW()
*/
final class MatchKind extends Enum
{
/**
* @var string
*/
private const NORMAL = 'normal';

/**
* @var string
*/
private const ASSIGN = 'assign';

/**
* @var string
*/
private const RETURN = 'return';

/**
* @var string
*/
private const THROW = 'throw';
}
8 changes: 4 additions & 4 deletions rules/Php80/NodeAnalyzer/MatchSwitchAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use PhpParser\Node\Stmt\Throw_;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\Enum\MatchKind;
use Rector\Php80\ValueObject\CondAndExpr;
use Rector\Php80\ValueObject\MatchKind;

final class MatchSwitchAnalyzer
{
Expand Down Expand Up @@ -97,17 +97,17 @@ public function hasDefaultValue(Match_ $match): bool

/**
* @param CondAndExpr[] $condAndExprs
* @return string[]
* @return MatchKind[]
*/
private function resolveUniqueKindsWithoutThrows(array $condAndExprs): array
{
$condAndExprKinds = [];
foreach ($condAndExprs as $condAndExpr) {
if ($condAndExpr->getKind() === MatchKind::THROW) {
if ($condAndExpr->equalsMatchKind(MatchKind::THROW())) {
continue;
}

$condAndExprKinds[] = $condAndExpr->getKind();
$condAndExprKinds[] = $condAndExpr->getMatchKind();
}

return array_unique($condAndExprKinds);
Expand Down
10 changes: 5 additions & 5 deletions rules/Php80/NodeResolver/SwitchExprsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\Throw_;
use Rector\Php80\Enum\MatchKind;
use Rector\Php80\ValueObject\CondAndExpr;
use Rector\Php80\ValueObject\MatchKind;

final class SwitchExprsResolver
{
Expand Down Expand Up @@ -69,14 +69,14 @@ public function resolve(Switch_ $switch): array
return [];
}

$condAndExpr[] = new CondAndExpr($condExprs, $returnedExpr, MatchKind::RETURN);
$condAndExpr[] = new CondAndExpr($condExprs, $returnedExpr, MatchKind::RETURN());
} elseif ($expr instanceof Assign) {
$condAndExpr[] = new CondAndExpr($condExprs, $expr, MatchKind::ASSIGN);
$condAndExpr[] = new CondAndExpr($condExprs, $expr, MatchKind::ASSIGN());
} elseif ($expr instanceof Expr) {
$condAndExpr[] = new CondAndExpr($condExprs, $expr, MatchKind::NORMAL);
$condAndExpr[] = new CondAndExpr($condExprs, $expr, MatchKind::NORMAL());
} elseif ($expr instanceof Throw_) {
$throwExpr = new Expr\Throw_($expr->expr);
$condAndExpr[] = new CondAndExpr($condExprs, $throwExpr, MatchKind::THROW);
$condAndExpr[] = new CondAndExpr($condExprs, $throwExpr, MatchKind::THROW());
} else {
return [];
}
Expand Down
8 changes: 4 additions & 4 deletions rules/Php80/Rector/Switch_/ChangeSwitchToMatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
use PhpParser\Node\Stmt\Throw_ as ThrowsStmt;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\Enum\MatchKind;
use Rector\Php80\NodeAnalyzer\MatchSwitchAnalyzer;
use Rector\Php80\NodeFactory\MatchFactory;
use Rector\Php80\NodeResolver\SwitchExprsResolver;
use Rector\Php80\ValueObject\CondAndExpr;
use Rector\Php80\ValueObject\MatchKind;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -91,7 +91,7 @@ public function refactor(Node $node): ?Node
$isReturn = false;

foreach ($condAndExprs as $condAndExpr) {
if ($condAndExpr->getKind() === MatchKind::RETURN) {
if ($condAndExpr->equalsMatchKind(MatchKind::RETURN())) {
$isReturn = true;
break;
}
Expand Down Expand Up @@ -186,7 +186,7 @@ private function processImplicitReturnAfterSwitch(Switch_ $switch, Match_ $match

$this->removeNode($nextNode);

$condAndExprs[] = new CondAndExpr([], $returnedExpr, MatchKind::RETURN);
$condAndExprs[] = new CondAndExpr([], $returnedExpr, MatchKind::RETURN());
return $this->matchFactory->createFromCondAndExprs($switch->cond, $condAndExprs);
}

Expand All @@ -208,7 +208,7 @@ private function processImplicitThrowsAfterSwitch(Switch_ $switch, Match_ $match

$throw = new Throw_($nextNode->expr);

$condAndExprs[] = new CondAndExpr([], $throw, MatchKind::RETURN);
$condAndExprs[] = new CondAndExpr([], $throw, MatchKind::RETURN());
return $this->matchFactory->createFromCondAndExprs($switch->cond, $condAndExprs);
}
}
12 changes: 9 additions & 3 deletions rules/Php80/ValueObject/CondAndExpr.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Php80\ValueObject;

use PhpParser\Node\Expr;
use Rector\Php80\Enum\MatchKind;

final class CondAndExpr
{
Expand All @@ -14,7 +15,7 @@ final class CondAndExpr
public function __construct(
private array $condExprs,
private Expr $expr,
private string $kind
private MatchKind $matchKind
) {
}

Expand All @@ -31,8 +32,13 @@ public function getCondExprs(): array
return $this->condExprs;
}

public function getKind(): string
public function getMatchKind(): MatchKind
{
return $this->kind;
return $this->matchKind;
}

public function equalsMatchKind(MatchKind $matchKind): bool
{
return $this->matchKind->equals($matchKind);
}
}

0 comments on commit 5282793

Please sign in to comment.