Skip to content

Commit

Permalink
Refactor the code using Rector
Browse files Browse the repository at this point in the history
  • Loading branch information
nicwortel committed Sep 29, 2023
1 parent ae36f69 commit 5e41ffc
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"phpstan/phpstan": "^1.8",
"phpstan/phpstan-symfony": "^1.2",
"phpunit/phpunit": "^10.3",
"rector/rector": "^0.18.4",
"slevomat/coding-standard": ">=6.3.11",
"squizlabs/php_codesniffer": "^3.5",
"symfony/process": "^6.0"
Expand Down
28 changes: 28 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\Set\PHPUnitLevelSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\Symfony\Set\SymfonySetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

$rectorConfig->importNames();

// register a single rule
// $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
SetList::CODE_QUALITY,
PHPUnitLevelSetList::UP_TO_PHPUNIT_100,
]);
};
7 changes: 2 additions & 5 deletions src/ConsoleUnusedStepDefinitionsPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@

final class ConsoleUnusedStepDefinitionsPrinter implements UnusedStepDefinitionsPrinter
{
private OutputInterface $output;

public function __construct(OutputInterface $output)
public function __construct(private readonly OutputInterface $output)
{
$this->output = $output;
}

/**
* @inheritDoc
*/
public function printUnusedStepDefinitions(array $unusedDefinitions): void
{
if (count($unusedDefinitions) === 0) {
if ($unusedDefinitions === []) {
return;
}

Expand Down
27 changes: 8 additions & 19 deletions src/UnusedStepDefinitionsChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,17 @@

final class UnusedStepDefinitionsChecker implements EventSubscriberInterface
{
private DefinitionFinder $definitionFinder;

private DefinitionRepository $definitionRepository;

private UnusedStepDefinitionsPrinter $printer;

private ?string $filter;

/**
* @var array<Definition>
*/
private array $usedDefinitions = [];

public function __construct(
DefinitionFinder $definitionFinder,
DefinitionRepository $definitionRepository,
UnusedStepDefinitionsPrinter $printer,
?string $filter
private readonly DefinitionFinder $definitionFinder,
private readonly DefinitionRepository $definitionRepository,
private readonly UnusedStepDefinitionsPrinter $printer,
private readonly ?string $filter
) {
$this->definitionFinder = $definitionFinder;
$this->definitionRepository = $definitionRepository;
$this->printer = $printer;
$this->filter = $filter;
}

/**
Expand Down Expand Up @@ -76,9 +64,10 @@ public function checkUnusedStepDefinitions(AfterSuiteTested $event): void
$unusedDefinitions = array_diff($definitions, $this->usedDefinitions);

if ($this->filter) {
$unusedDefinitions = array_filter($unusedDefinitions, function (Definition $definition): bool {
return (bool) preg_match((string) $this->filter, $definition->getPath());
});
$unusedDefinitions = array_filter(
$unusedDefinitions,
fn(Definition $definition): bool => (bool) preg_match((string) $this->filter, $definition->getPath())
);
}

$this->printer->printUnusedStepDefinitions($unusedDefinitions);
Expand Down

0 comments on commit 5e41ffc

Please sign in to comment.