Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

Commit

Permalink
Merge pull request #28 from TomasVotruba/rector
Browse files Browse the repository at this point in the history
[CI] Add Rector basic sets
  • Loading branch information
olivernybroe committed Aug 5, 2020
2 parents bdc435c + 42fe9ee commit 29a031c
Show file tree
Hide file tree
Showing 38 changed files with 125 additions and 76 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
- name: PHPStan
run: 'composer phpstan'

- name: Rector
run: 'composer rector'

steps:
- name: Checkout code
uses: actions/checkout@v2
Expand Down
2 changes: 0 additions & 2 deletions bin/drift
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ $status = $kernel->handle(
|
*/

$kernel->terminate();

exit($status);


Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"scripts": {
"check-cs": "vendor/bin/ecs check --ansi",
"fix-cs": "vendor/bin/ecs check --fix --ansi",
"phpstan": "vendor/bin/phpstan analyze --ansi --error-format symplify"
"phpstan": "vendor/bin/phpstan analyze --ansi --error-format symplify",
"rector": "vendor/bin/rector process --config rector-ci.php --ansi",
"rector-dry": "vendor/bin/rector process --config rector-ci.php --ansi --dry-run"
}
}
9 changes: 8 additions & 1 deletion ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@
__DIR__ . '/tests',
__DIR__ . '/config',
__DIR__ . '/ecs.php',
__DIR__ . '/rector-ci.php',
]);

$parameters->set(Option::SETS, [SetList::PSR_12, SetList::PHP_71, SetList::PHP_70, SetList::COMMON]);
$parameters->set(Option::SETS, [
SetList::PSR_12,
SetList::PHP_71,
SetList::PHP_70,
SetList::COMMON,
SetList::DEAD_CODE,
]);
};
28 changes: 28 additions & 0 deletions rector-ci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
$parameters->set(Option::SETS, [
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
SetList::PHP_70,
SetList::PHP_71,
SetList::SOLID,
]);

$parameters->set(Option::PATHS, [__DIR__ . '/src', __DIR__ . '/tests']);

$parameters->set(Option::EXCLUDE_PATHS, [__DIR__ . '/tests/fixtures/*']);

$parameters->set(Option::EXCLUDE_RECTORS, [
// buggy, needs fix
\Rector\SOLID\Rector\ClassMethod\ChangeReadOnlyVariableWithDefaultValueToConstantRector::class,
]);
};
6 changes: 1 addition & 5 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

class Application implements ContainerInterface
final class Application implements ContainerInterface
{
private ContainerInterface $container;

Expand Down Expand Up @@ -48,8 +48,4 @@ public function has($id): bool
{
return $this->container->has($id);
}

public function terminate(): void
{
}
}
2 changes: 1 addition & 1 deletion src/Commands/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class MigrateCommand extends Command
final class MigrateCommand extends Command
{
private RectorRunner $rectorRunner;

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/PolishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class PolishCommand extends Command
final class PolishCommand extends Command
{
private RectorRunner $rectorRunner;

Expand Down
13 changes: 1 addition & 12 deletions src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Kernel
final class Kernel
{
private Application $container;

Expand All @@ -19,14 +19,8 @@ public function __construct(Application $container)
$this->container = $container;
}

public function bootstrap(): void
{
}

public function handle(InputInterface $input, OutputInterface $output): int
{
$this->bootstrap();

$kernelApplication = new \Symfony\Component\Console\Application('Drift - A Pest migration tool');

$kernelApplication->setCommandLoader(new ContainerCommandLoader($this->container, [
Expand All @@ -36,9 +30,4 @@ public function handle(InputInterface $input, OutputInterface $output): int

return $kernelApplication->run($input, $output);
}

public function terminate(): void
{
$this->container->terminate();
}
}
2 changes: 1 addition & 1 deletion src/PHPUnit/ClassMethod/AfterClassToAfterAllRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\NodeTypeResolver\Node\AttributeKey;

class AfterClassToAfterAllRector extends AbstractClassMethodRector
final class AfterClassToAfterAllRector extends AbstractClassMethodRector
{
public ?string $type = PestCollector::AFTER_ALL;

Expand Down
2 changes: 1 addition & 1 deletion src/PHPUnit/ClassMethod/BeforeClassToBeforeAllRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\NodeTypeResolver\Node\AttributeKey;

class BeforeClassToBeforeAllRector extends AbstractClassMethodRector
final class BeforeClassToBeforeAllRector extends AbstractClassMethodRector
{
public ?string $type = PestCollector::BEFORE_ALL;

Expand Down
2 changes: 1 addition & 1 deletion src/PHPUnit/ClassMethod/DataProviderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PHPUnit\Framework\TestCase;

class DataProviderRector extends AbstractPHPUnitToPestRector
final class DataProviderRector extends AbstractPHPUnitToPestRector
{
public function getNodeTypes(): array
{
Expand Down
5 changes: 3 additions & 2 deletions src/PHPUnit/ClassMethod/HelperMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PHPUnit\Framework\TestCase;
use Rector\Core\Exception\ShouldNotHappenException;
use ReflectionClass;

class HelperMethodRector extends AbstractPHPUnitToPestRector
final class HelperMethodRector extends AbstractPHPUnitToPestRector
{
public function getNodeTypes(): array
{
Expand Down Expand Up @@ -124,7 +125,7 @@ private function migratePhpUnitMethodHelpers(Node $pestNode, ClassMethod $method

if ($pestNode instanceof Function_) {
$stmts = $pestNode->getStmts();
} elseif ($pestNode instanceof Node\Stmt\Expression) {
} elseif ($pestNode instanceof Expression) {
$stmts = $pestNode->expr->args[1]->value->stmts;
} else {
throw new ShouldNotHappenException("Can't this node yet.");
Expand Down
19 changes: 11 additions & 8 deletions src/PHPUnit/ClassMethod/MethodToPestTestRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\AttributeKey;

class MethodToPestTestRector extends AbstractClassMethodRector
final class MethodToPestTestRector extends AbstractClassMethodRector
{
/**
* @var string
*/
private const TEST = 'test';

public ?string $type = PestCollector::TEST_METHODS;

public function classMethodRefactor(Class_ $classNode, ClassMethod $classMethodNode): ?Node
Expand All @@ -40,16 +45,14 @@ public function classMethodRefactor(Class_ $classNode, ClassMethod $classMethodN

$pestTestNode = $this->migrateSkipCall($classMethodNode, $pestTestNode);

$pestTestNode = $this->migratePhpDocDepends($classMethodNode, $pestTestNode);

return $pestTestNode;
return $this->migratePhpDocDepends($classMethodNode, $pestTestNode);
}

public function isTestMethod(ClassMethod $classMethod): bool
{
/** @var PhpDocInfo|null $phpDoc */
$phpDoc = $classMethod->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDoc && $phpDoc->hasByName('test')) {
if ($phpDoc && $phpDoc->hasByName(self::TEST)) {
return true;
}

Expand All @@ -58,7 +61,7 @@ public function isTestMethod(ClassMethod $classMethod): bool
return false;
}

return Strings::startsWith($classMethodName, 'test');
return Strings::startsWith($classMethodName, self::TEST);
}

/**
Expand Down Expand Up @@ -108,7 +111,7 @@ private function createPestTest(ClassMethod $classMethod): FuncCall
]),
];

return $this->builderFactory->funcCall('test', $arguments);
return $this->builderFactory->funcCall(self::TEST, $arguments);
}

private function getExpectExceptionCall(ClassMethod $method): ?MethodCallWithPosition
Expand Down Expand Up @@ -159,7 +162,7 @@ private function migrateDataProvider(ClassMethod $method, Expr $pestTestNode)
* @param FuncCall|MethodCall $pestTestNode
* @return FuncCall|MethodCall
*/
private function migratePhpDocGroup(ClassMethod $method, Expr $pestTestNode): \PhpParser\Node
private function migratePhpDocGroup(ClassMethod $method, Expr $pestTestNode): Node
{
$groups = $this->getPhpDocGroupNames($method);
if ($groups !== []) {
Expand Down
2 changes: 1 addition & 1 deletion src/PHPUnit/ClassMethod/SetUpToBeforeEachRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;

class SetUpToBeforeEachRector extends AbstractClassMethodRector
final class SetUpToBeforeEachRector extends AbstractClassMethodRector
{
public ?string $type = PestCollector::BEFORE_EACH;

Expand Down
2 changes: 1 addition & 1 deletion src/PHPUnit/ClassMethod/TearDownToAfterEachRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;

class TearDownToAfterEachRector extends AbstractClassMethodRector
final class TearDownToAfterEachRector extends AbstractClassMethodRector
{
public ?string $type = PestCollector::AFTER_EACH;

Expand Down
2 changes: 1 addition & 1 deletion src/PHPUnit/Class_/CustomTestCaseToUsesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PhpParser\Node\Stmt\Class_;
use PHPUnit\Framework\TestCase;

class CustomTestCaseToUsesRector extends AbstractPHPUnitToPestRector
final class CustomTestCaseToUsesRector extends AbstractPHPUnitToPestRector
{
public function getNodeTypes(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\NodeTypeResolver\Node\AttributeKey;

class PhpDocGroupOnClassToFileScopeGroupRector extends AbstractPHPUnitToPestRector
final class PhpDocGroupOnClassToFileScopeGroupRector extends AbstractPHPUnitToPestRector
{
public function getNodeTypes(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/PHPUnit/Class_/RemovePHPUnitClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpParser\Node\Stmt\Class_;
use PHPUnit\Framework\TestCase;

class RemovePHPUnitClassRector extends AbstractPHPUnitToPestRector
final class RemovePHPUnitClassRector extends AbstractPHPUnitToPestRector
{
public function getNodeTypes(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/PHPUnit/Class_/TraitUsesToUsesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use PhpParser\Node\Stmt\TraitUse;
use PHPUnit\Framework\TestCase;

class TraitUsesToUsesRector extends AbstractPHPUnitToPestRector
final class TraitUsesToUsesRector extends AbstractPHPUnitToPestRector
{
public function getNodeTypes(): array
{
Expand Down
14 changes: 10 additions & 4 deletions src/Pest/FuncCall/PestItNamingRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;

class PestItNamingRector extends AbstractRector
final class PestItNamingRector extends AbstractRector
{
public function getNodeTypes(): array
{
Expand All @@ -29,16 +29,22 @@ public function refactor(Node $node): ?Node
return null;
}

if (count($node->args) < 1 || ! is_string($this->getValue($node->args[0]->value))) {
$args = (array) $node->args;
if (count($args) === 0) {
return null;
}

if (! Strings::startsWith($this->getValue($node->args[0]->value), 'it')) {
$firstArgumentValue = $this->getValue($args[0]->value);
if (! is_string($firstArgumentValue)) {
return null;
}

if (! Strings::startsWith($firstArgumentValue, 'it')) {
return null;
}

$node->name = new Name('it');
$node->args[0]->value = new String_(trim(substr($this->getValue($node->args[0]->value), 2)));
$node->args[0]->value = new String_(trim(substr($firstArgumentValue, 2)));

return $node;
}
Expand Down
23 changes: 17 additions & 6 deletions src/Pest/FuncCall/PestTestNamingRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;

class PestTestNamingRector extends AbstractRector
final class PestTestNamingRector extends AbstractRector
{
/**
* @var string
*/
private const TEST = 'test';

public function getNodeTypes(): array
{
return [FuncCall::class];
Expand All @@ -25,20 +30,26 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'test')) {
if (! $this->isName($node, self::TEST)) {
return null;
}

$args = (array) $node->args;
if (count($args) === 0) {
return null;
}

if (count($node->args) < 1 || ! is_string($this->getValue($node->args[0]->value))) {
$firstArgumentValue = $this->getValue($args[0]->value);
if (! is_string($firstArgumentValue)) {
return null;
}

if (! Strings::startsWith($this->getValue($node->args[0]->value), 'test')) {
if (! Strings::startsWith($firstArgumentValue, self::TEST)) {
return null;
}

$node->name = new Name('test');
$node->args[0]->value = new String_(trim(substr($this->getValue($node->args[0]->value), 4)));
$node->name = new Name(self::TEST);
$node->args[0]->value = new String_(trim(substr($firstArgumentValue, 4)));

return $node;
}
Expand Down
Loading

0 comments on commit 29a031c

Please sign in to comment.