-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX] Split migration to enums and enum values
- Loading branch information
1 parent
87ea44a
commit eb60fe1
Showing
13 changed files
with
339 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\General\Renaming; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ClassConstFetch; | ||
use Rector\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\Renaming\ValueObject\RenameClassAndConstFetch; | ||
use Rector\ValueObject\PhpVersionFeature; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Ssch\TYPO3Rector\Contract\NoChangelogRequiredInterface; | ||
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @see \Ssch\TYPO3Rector\Tests\Rector\General\Renaming\ConstantsToBackedEnumRector\ConstantsToBackedEnumRectorTest | ||
*/ | ||
final class ConstantsToBackedEnumRector extends AbstractRector implements ConfigurableRectorInterface, MinPhpVersionInterface, NoChangelogRequiredInterface, DocumentedRuleInterface | ||
{ | ||
/** | ||
* @var RenameClassAndConstFetch[] | ||
*/ | ||
private array $renameClassConstFetches = []; | ||
|
||
/** | ||
* @param array<int, RenameClassAndConstFetch> $configuration | ||
*/ | ||
public function configure(array $configuration): void | ||
{ | ||
Assert::allIsAOf($configuration, RenameClassAndConstFetch::class); | ||
$this->renameClassConstFetches = $configuration; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Migrate constants to enum class', [ | ||
new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
\TYPO3\CMS\Core\Imaging\Icon::SIZE_DEFAULT | ||
\TYPO3\CMS\Core\Imaging\Icon::SIZE_SMALL | ||
\TYPO3\CMS\Core\Imaging\Icon::SIZE_MEDIUM | ||
\TYPO3\CMS\Core\Imaging\Icon::SIZE_LARGE | ||
\TYPO3\CMS\Core\Imaging\Icon::SIZE_MEGA | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
TYPO3\CMS\Core\Imaging\IconSize::DEFAULT | ||
TYPO3\CMS\Core\Imaging\IconSize::SMALL | ||
TYPO3\CMS\Core\Imaging\IconSize::MEDIUM | ||
TYPO3\CMS\Core\Imaging\IconSize::LARGE | ||
TYPO3\CMS\Core\Imaging\IconSize::MEGA | ||
CODE_SAMPLE | ||
, | ||
[ | ||
new RenameClassAndConstFetch( | ||
'TYPO3\CMS\Core\Imaging\Icon', | ||
'SIZE_MEDIUM', | ||
'TYPO3\CMS\Core\Imaging\IconSize', | ||
'MEDIUM' | ||
), | ||
] | ||
), | ||
]); | ||
} | ||
|
||
public function getNodeTypes(): array | ||
{ | ||
return [ClassConstFetch::class]; | ||
} | ||
|
||
/** | ||
* @param ClassConstFetch $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
foreach ($this->renameClassConstFetches as $renameClassConstFetch) { | ||
if (! $this->isName($node->name, $renameClassConstFetch->getOldConstant())) { | ||
continue; | ||
} | ||
|
||
if (! $this->isObjectType($node->class, $renameClassConstFetch->getOldObjectType())) { | ||
continue; | ||
} | ||
|
||
return $this->nodeFactory->createClassConstFetch( | ||
$renameClassConstFetch->getNewClass(), | ||
$renameClassConstFetch->getNewConstant() | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::ENUM; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TYPO3\CMS\Core\Authentication; | ||
|
||
use TYPO3\CMS\Core\Type\Enumeration; | ||
|
||
if (class_exists('TYPO3\CMS\Core\Authentication\LoginType')) { | ||
return; | ||
} | ||
|
||
class LoginType extends Enumeration | ||
{ | ||
public const LOGIN = 'login'; | ||
public const LOGOUT = 'logout'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TYPO3\CMS\Core\Type; | ||
|
||
if (class_exists('TYPO3\CMS\Core\Type\Enumeration')) { | ||
return; | ||
} | ||
|
||
abstract class Enumeration | ||
{ | ||
} |
31 changes: 31 additions & 0 deletions
31
...s/Rector/General/Renaming/ConstantsToBackedEnumRector/ConstantsToBackedEnumRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\General\Renaming\ConstantsToBackedEnumRector; | ||
|
||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ConstantsToBackedEnumRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return \Iterator<array<string>> | ||
*/ | ||
public static function provideData(): \Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/Rector/General/Renaming/ConstantsToBackedEnumRector/Fixture/icon_types.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Imaging\Icon; | ||
|
||
$icon1 = Icon::SIZE_DEFAULT; | ||
$icon2 = Icon::SIZE_SMALL; | ||
$icon3 = Icon::SIZE_MEDIUM; | ||
$icon4 = Icon::SIZE_LARGE; | ||
$icon5 = Icon::SIZE_MEGA; | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
use TYPO3\CMS\Core\Imaging\IconSize; | ||
use TYPO3\CMS\Core\Imaging\Icon; | ||
|
||
$icon1 = IconSize::DEFAULT; | ||
$icon2 = IconSize::SMALL; | ||
$icon3 = IconSize::MEDIUM; | ||
$icon4 = IconSize::LARGE; | ||
$icon5 = IconSize::MEGA; | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
tests/Rector/General/Renaming/ConstantsToBackedEnumRector/Source/MyOtherFileType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\General\Renaming\ConstantsToBackedEnumRector\Source; | ||
|
||
final class MyOtherFileType | ||
{ | ||
public const FILETYPE_UNKNOWN = 0; | ||
} |
Oops, something went wrong.