Skip to content

Commit

Permalink
Merge pull request tempestphp#310 from Treggats/proposal/rector
Browse files Browse the repository at this point in the history
[proposal] introduce Rector to keep code up to date
  • Loading branch information
brendt authored Aug 12, 2024
2 parents 569f0f0 + efb5c20 commit 6865803
Show file tree
Hide file tree
Showing 177 changed files with 547 additions and 452 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"spaze/phpstan-disallowed-calls": "^3.1",
"symplify/monorepo-builder": "^11.2",
"jenssegers/blade": "^2.0",
"illuminate/view": "~11.7.0"
"illuminate/view": "~11.7.0",
"rector/rector": "^1.2"
},
"autoload": {
"files": [
Expand Down Expand Up @@ -74,6 +75,7 @@
"coverage": "vendor/bin/phpunit --coverage-html build/reports/html --coverage-clover build/reports/clover.xml",
"csfixer": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
"phpstan": "vendor/bin/phpstan analyse src tests --memory-limit=1G",
"rector": "vendor/bin/rector process --no-ansi",
"qa": [
"./tempest discovery:clear",
"composer csfixer",
Expand Down
77 changes: 77 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

use Rector\Arguments\Rector\ClassMethod\ArgumentAdderRector;
use Rector\Caching\ValueObject\Storage\FileCacheStorage;
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPublicMethodParameterRector;
use Rector\DeadCode\Rector\PropertyProperty\RemoveNullPropertyInitializationRector;
use Rector\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector;
use Rector\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Php74\Rector\Property\RestoreDefaultNullToNullableTypePropertyRector;
use Rector\Php74\Rector\Ternary\ParenthesizeNestedTernaryRector;
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Php82\Rector\Class_\ReadOnlyClassRector;
use Rector\Php82\Rector\Param\AddSensitiveParameterAttributeRector;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
use Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector;
use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnNeverTypeRector;
use Rector\TypeDeclaration\Rector\Closure\ClosureReturnTypeRector;
use Rector\TypeDeclaration\Rector\Empty_\EmptyOnNullableObjectToInstanceOfRector;

return RectorConfig::configure()
->withCache('./.cache/rector', FileCacheStorage::class)
->withPaths([
__DIR__ . '/public',
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withConfiguredRule(AddSensitiveParameterAttributeRector::class, [
'sensitive_parameters' => [
'password',
'secret',
],
])
->withRules([
ParenthesizeNestedTernaryRector::class,
ExplicitNullableParamTypeRector::class,
])
->withSkip([
AddOverrideAttributeToOverriddenMethodsRector::class,
ArgumentAdderRector::class,
ClosureToArrowFunctionRector::class,
EmptyOnNullableObjectToInstanceOfRector::class,
FirstClassCallableRector::class,
NullToStrictStringFuncCallArgRector::class,
ReadOnlyClassRector::class,
ReadOnlyPropertyRector::class,
RemoveNullPropertyInitializationRector::class,
RemoveUnreachableStatementRector::class,
AddSensitiveParameterAttributeRector::class,
RemoveUnusedPublicMethodParameterRector::class,
RestoreDefaultNullToNullableTypePropertyRector::class,
ReturnNeverTypeRector::class,
StaticCallOnNonStaticToInstanceCallRector::class,
ClosureReturnTypeRector::class,
EncapsedStringsToSprintfRector::class,
AddArrowFunctionReturnTypeRector::class,
])
->withSkipPath('src/Tempest/Framework/Exceptions/')
->withParallel(300, 10, 10)
->withPreparedSets(
codeQuality: false,
codingStyle: true,
privatization: true,
naming: false,
earlyReturn: true,
)
->withDeadCodeLevel(40)
->withMemoryLimit('3G')
->withPhpSets(php83: true)
->withTypeCoverageLevel(37);
4 changes: 2 additions & 2 deletions src/Tempest/CommandBus/GenericCommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function dispatch(object $command): void
{
$commandHandler = $this->getCommandHandler($command);

if (! $commandHandler) {
if ($commandHandler === null) {
throw new CommandHandlerNotFound($command);
}

Expand All @@ -33,7 +33,7 @@ public function dispatch(object $command): void

private function getCallable(CommandHandler $commandHandler): Closure
{
$callable = function (object $command) use ($commandHandler) {
$callable = function (object $command) use ($commandHandler): void {
$commandHandler->handler->invoke(
$this->container->get($commandHandler->handler->getDeclaringClass()->getName()),
$command,
Expand Down
4 changes: 2 additions & 2 deletions src/Tempest/Console/Commands/ScheduleTaskCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public function __invoke(string $task): void

try {
$reflectionMethod = new ReflectionMethod($class, $method);
} catch (Throwable $e) {
$console->error($e->getMessage());
} catch (Throwable $throwable) {
$console->error($throwable->getMessage());

return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tempest/Console/Commands/TailProjectLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __invoke(): void

$this->console->write('<h1>Project</h1> ');

if (! $appendLogChannel) {
if ($appendLogChannel === null) {
$this->console->error("No AppendLogChannel registered");

return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
final class ConfirmComponent implements InteractiveComponent, HasCursor, HasStaticComponent
{
private bool $answer;

private string $textualAnswer = '';

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
final class MultipleChoiceComponent implements InteractiveComponent, HasStaticComponent
{
public array $selectedOptions = [];

public int $activeOption;

public function __construct(
Expand All @@ -33,7 +34,7 @@ public function render(): string
$output .= $this->isActive($key) ? '> ' : ' ';
$output .= $this->isSelected($key) ? '[x]' : '[ ]';
$output .= $this->isActive($key) ? '<em>' : '';
$output .= " $option";
$output .= " {$option}";
$output .= $this->isActive($key) ? '</em>' : '';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public function __construct(
public function render(): string
{
$output = "<question>{$this->label}</question> ";
$output .= str_repeat('*', strlen($this->password));

return $output;
return $output . str_repeat('*', strlen($this->password));
}

public function renderFooter(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
final class SearchComponent implements InteractiveComponent, HasCursor, HasStaticComponent
{
public Point $cursorPosition;

public string $query = '';

public int $selectedOption = 0;

public array $options = [];

public function __construct(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
final class TextBoxComponent implements InteractiveComponent, HasCursor, HasStaticComponent
{
public Point $cursorPosition;

public string $answer = '';

public function __construct(
Expand Down
12 changes: 5 additions & 7 deletions src/Tempest/Console/Components/InteractiveComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
final class InteractiveComponentRenderer
{
private array $validationErrors = [];

private bool $shouldRerender = true;

public function render(Console $console, InteractiveComponent $component, array $validation = []): mixed
Expand Down Expand Up @@ -63,7 +64,7 @@ private function renderComponent(Console $console, InteractiveComponent $compone

// If we're running within a fiber, we'll suspend here as well so that the parent can continue
// This is needed for our testing helper
if (Fiber::getCurrent()) {
if (Fiber::getCurrent() !== null) {
Fiber::suspend();
}
}
Expand Down Expand Up @@ -127,7 +128,7 @@ private function applyKey(InteractiveComponent $component, Console $console, arr
$failingRule = $this->validate($return, $validation);

// If invalid, we'll remember the validation message and continue
if ($failingRule) {
if ($failingRule !== null) {
$this->validationErrors[] = '<error>' . $failingRule->message() . '</error>';
Fiber::suspend();

Expand Down Expand Up @@ -181,7 +182,6 @@ private function resolveHandlers(InteractiveComponent $component): array
/** @var ReflectionMethod[][] $keyBindings */
$keyBindings = [];

/** @var ReflectionMethod[][] $keyBindings */
$inputHandlers = [];

foreach ((new ReflectionClass($component))->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
Expand All @@ -198,18 +198,16 @@ private function resolveHandlers(InteractiveComponent $component): array
}

/**
* @param mixed $value
* @param \Tempest\Validation\Rule[] $validation
* @return Rule|null
*/
private function validate(mixed $value, array $validation): ?Rule
{
$validator = new Validator();

try {
$validator->validateValue($value, $validation);
} catch (InvalidValueException $e) {
return $e->failingRules[0];
} catch (InvalidValueException $invalidValueException) {
return $invalidValueException->failingRules[0];
}

return null;
Expand Down
8 changes: 0 additions & 8 deletions src/Tempest/Console/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@ public function write(string $contents): self;
public function writeln(string $line = ''): self;

/**
* @param InteractiveComponent $component
* @param \Tempest\Validation\Rule[] $validation
*/
public function component(InteractiveComponent $component, array $validation = []): mixed;

/**
* @param string $question
* @param array|null $options
* @param mixed|null $default
* @param bool $multiple
* @param bool $asList
* @param \Tempest\Validation\Rule[] $validation
* @return string|array
*/
public function ask(
string $question,
Expand All @@ -49,9 +43,7 @@ public function password(string $label = 'Password', bool $confirm = false): str
public function progressBar(iterable $data, Closure $handler): array;

/**
* @param string $label
* @param Closure(string $search): array $search
* @return mixed
*/
public function search(string $label, Closure $search): mixed;

Expand Down
2 changes: 1 addition & 1 deletion src/Tempest/Console/ConsoleInputBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function build(): array
$validArguments[] = $argument;
}

if (count($invalidArguments)) {
if ($invalidArguments !== []) {
throw new InvalidCommandException(
$this->command,
$invalidArguments
Expand Down
2 changes: 0 additions & 2 deletions src/Tempest/Console/ConsoleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
interface ConsoleMiddleware
{
/**
* @param Invocation $invocation
* @param callable(Invocation $invocation): ExitCode $next
* @return ExitCode
*/
public function __invoke(Invocation $invocation, callable $next): ExitCode;
}
6 changes: 1 addition & 5 deletions src/Tempest/Console/Exceptions/ConsoleExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function handle(Throwable $throwable): void
->writeln('<u>' . $throwable->getFile() . ':' . $throwable->getLine() . '</u>')
->writeln();

if ($this->argumentBag->get('-v')) {
if ($this->argumentBag->get('-v') !== null) {
foreach ($throwable->getTrace() as $i => $trace) {
$this->console->writeln("<h2>#{$i}</h2> " . $this->formatTrace($trace));
}
Expand Down Expand Up @@ -76,10 +76,6 @@ private function getCodeSample(Throwable $throwable): string
return PHP_EOL . implode(PHP_EOL, $lines);
}

/**
* @param mixed $trace
* @return string
*/
public function formatTrace(mixed $trace): string
{
if (isset($trace['file'])) {
Expand Down
4 changes: 3 additions & 1 deletion src/Tempest/Console/GenericConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
final class GenericConsole implements Console
{
private ?string $label = null;

private bool $isForced = false;

private ?InteractiveComponentRenderer $componentRenderer = null;

public function __construct(
Expand Down Expand Up @@ -200,7 +202,7 @@ public function search(string $label, Closure $search): mixed

private function interactiveSupported(): bool
{
if (! $this->componentRenderer) {
if ($this->componentRenderer === null) {
return false;
}

Expand Down
7 changes: 0 additions & 7 deletions src/Tempest/Console/HasConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,8 @@ public function writeln(string $line = ''): self
}

/**
* @param string $question
* @param array|null $options
* @param mixed|null $default
* @param bool $multiple
* @param bool $asList
* @param \Tempest\Validation\Rule[] $validation
* @return string|array
*/
public function ask(
string $question,
Expand Down Expand Up @@ -88,9 +83,7 @@ public function progressBar(iterable $data, Closure $handler): array
}

/**
* @param string $label
* @param Closure(string $search): array $search
* @return mixed
*/
public function search(string $label, Closure $search): mixed
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ public function initialize(Container $container): ShellExecutor
$app = $container->get(Application::class);

if (! $app instanceof ConsoleApplication) {
$executor = new NullShellExecutor();
} else {
$executor = new GenericShellExecutor();
return new NullShellExecutor();
}

return $executor;
return new GenericShellExecutor();
}
}
4 changes: 2 additions & 2 deletions src/Tempest/Console/Input/ConsoleArgumentBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
final class ConsoleArgumentBag
{
/** @var ConsoleInputArgument[] */
protected array $arguments = [];
private array $arguments = [];

/** @var string[] */
protected array $path = [];
private array $path = [];

/**
* @param array<string|int, mixed> $arguments
Expand Down
Loading

0 comments on commit 6865803

Please sign in to comment.