Skip to content

Commit

Permalink
Convert single classes in one step with php artisan enum:to-native (#334
Browse files Browse the repository at this point in the history
)
  • Loading branch information
spawnia authored Aug 3, 2023
1 parent f62430d commit 85db3d4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## 6.6.2

### Fixed

- Convert single classes in one step with `php artisan enum:to-native`

## 6.6.1

### Fixed
Expand Down
34 changes: 23 additions & 11 deletions src/Commands/EnumToNativeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,32 @@ public function handle(): int
->timeout(0) // Unlimited, rector can take quite a while
->run($command, $withPipedOutput);

$usagesConfig = realpath(__DIR__ . '/../Rector/usages.php');
if ($class) {
if (! class_exists($class)) {
$this->error("Class does not exist: {$class}.");

$convertUsages = "vendor/bin/rector process --clear-cache --config={$usagesConfig}";
$this->info("Converting usages, running: {$convertUsages}");
$run($convertUsages);
return 1;
}

$implementationConfig = realpath(__DIR__ . '/../Rector/implementation.php');
$classFileName = $class
? (new \ReflectionClass($class))->getFileName()
: null;
// If a specific class is given, we can do both conversion steps at once
// since the usages can still be recognized by the class name.
$usagesAndImplementationConfig = realpath(__DIR__ . '/../Rector/usages-and-implementation.php');
$convertUsagesAndImplementation = "vendor/bin/rector process --clear-cache --config={$usagesAndImplementationConfig}";
$this->info("Converting {$class}, running: {$convertUsagesAndImplementation}");
$run($convertUsagesAndImplementation);
} else {
// If not, we have to do two steps to avoid partial conversion,
// since the usages conversion relies on the enums extending BenSampo\Enum\Enum.
$usagesConfig = realpath(__DIR__ . '/../Rector/usages.php');
$convertUsages = "vendor/bin/rector process --clear-cache --config={$usagesConfig}";
$this->info("Converting usages, running: {$convertUsages}");
$run($convertUsages);

$convertImplementation = "vendor/bin/rector process --clear-cache --config={$implementationConfig} {$classFileName}";
$this->info("Converting implementation, running: {$convertImplementation}");
$run($convertImplementation);
$implementationConfig = realpath(__DIR__ . '/../Rector/implementation.php');
$convertImplementation = "vendor/bin/rector process --clear-cache --config={$implementationConfig}";
$this->info("Converting implementation, running: {$convertImplementation}");
$run($convertImplementation);
}

return 0;
}
Expand Down
15 changes: 15 additions & 0 deletions src/Rector/usages-and-implementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

use BenSampo\Enum\Commands\EnumToNativeCommand;
use BenSampo\Enum\Rector\ToNativeImplementationRector;
use BenSampo\Enum\Rector\ToNativeUsagesRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(env(EnumToNativeCommand::BASE_RECTOR_CONFIG_PATH_ENV));
$classes = [
env(EnumToNativeCommand::TO_NATIVE_CLASS_ENV),
];
$rectorConfig->ruleWithConfiguration(ToNativeUsagesRector::class, $classes);
$rectorConfig->ruleWithConfiguration(ToNativeImplementationRector::class, $classes);
};
22 changes: 13 additions & 9 deletions tests/EnumToNativeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testAll(): void
$this->assertMatchesRegularExpression(
match ($count) {
1 => '#^vendor/bin/rector process --clear-cache --config=/.+/src/Rector/usages\.php$#',
2 => '#^vendor/bin/rector process --clear-cache --config=/.+/src/Rector/implementation\.php $#',
2 => '#^vendor/bin/rector process --clear-cache --config=/.+/src/Rector/implementation\.php$#',
default => throw new \Exception('Only expected 2 processes'),
},
$process->command
Expand All @@ -53,23 +53,27 @@ public function testClass(): void
$this->artisan('enum:to-native', ['class' => UserType::class])
->assertExitCode(0);

$count = 0;
$process->assertRan(function (PendingProcess $process) use (&$count): bool {
++$count;
$process->assertRan(function (PendingProcess $process): bool {
$this->assertSame([
EnumToNativeCommand::TO_NATIVE_CLASS_ENV => UserType::class,
EnumToNativeCommand::BASE_RECTOR_CONFIG_PATH_ENV => base_path('rector.php'),
], $process->environment);
$this->assertMatchesRegularExpression(
match ($count) {
1 => '#^vendor/bin/rector process --clear-cache --config=/.+/src/Rector/usages\.php$#',
2 => '#^vendor/bin/rector process --clear-cache --config=/.+/src/Rector/implementation\.php /.+/tests/Enums/UserType.php$#',
default => throw new \Exception('Only expected 2 processes'),
},
'#^vendor/bin/rector process --clear-cache --config=/.+/src/Rector/usages-and-implementation\.php$#',
$process->command
);

return true;
});
}

public function testClassDoesNotExist(): void
{
$process = Process::fake();

$this->artisan('enum:to-native', ['class' => 'does-not-exist'])
->assertExitCode(1);

$process->assertNothingRan();
}
}

0 comments on commit 85db3d4

Please sign in to comment.