Skip to content

Commit

Permalink
Add doctrine migration clean fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremie Samson committed May 5, 2020
1 parent fd0496a commit 6ceac5b
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 55 deletions.
47 changes: 4 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,15 @@ composer require jsamson/php-cs-custom-fixer --dev

# Fixers

## Jsamson/sensio_to_symfony_route
## [Jsamson/sensio_to_symfony_route](doc/sensio_to_symfony_route.md)

Replace deprecated Sensio\Bundle\FrameworkExtraBundle\Configuration\Route by Symfony\Component\Routing\Annotation\Route

### Configuration examples

```php
$config = PhpCsFixer\Config::create()
->setRules([
'Jsamson/sensio_to_symfony_route' => true,

])
->registerCustomFixers([
new JSamson\CS\Fixer\Deprecation\SensioToSymfonyRouteFixer,
])
;
```

### Fixes

```diff
+use Symfony\Component\Routing\Annotation\Route;
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
```

## Jsamson/method_to_route_annotation
## [Jsamson/method_to_route_annotation](doc/method_to_route_annotation.md)

Remove deprecated `@Method` annotation and move content to `@Route` args

### Configuration examples

```php
$config = PhpCsFixer\Config::create()
->setRules([
'Jsamson/method_to_route_annotation' => true,

])
->registerCustomFixers([
new JSamson\CS\Fixer\Deprecation\MethodToRouteAnnotationFixer,
])
;
```

### Fixes
## [Jsamson/doctrine_migration_clean](doc/doctrine_migration_clean.md)

```diff
/**
- * @Route(name="foo", path="foo")
+ * @Route(name="foo", path="foo", methods={"GET"})
*/
```
Remove declare(strict_types=1), auto-generated comments, and abortIf calls from doctrine migration generated files.
15 changes: 15 additions & 0 deletions doc/doctrine_migration_clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# doctrine_migration_clean

### Configuration examples

```php
$config = PhpCsFixer\Config::create()
->setRules([
'Jsamson/doctrine_migration_clean' => true,

])
->registerCustomFixers([
new JSamson\CS\Fixer\Doctrine\DoctrineMigrationCleanFixer,
])
;
```
24 changes: 24 additions & 0 deletions doc/method_to_route_annotation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# method_to_route_annotation

### Configuration examples

```php
$config = PhpCsFixer\Config::create()
->setRules([
'Jsamson/method_to_route_annotation' => true,

])
->registerCustomFixers([
new JSamson\CS\Fixer\Deprecation\MethodToRouteAnnotationFixer,
])
;
```

### Fixes

```diff
/**
- * @Route(name="foo", path="foo")
+ * @Route(name="foo", path="foo", methods={"GET"})
*/
```
22 changes: 22 additions & 0 deletions doc/sensio_to_symfony_route.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# sensio_to_symfony_route

### Configuration examples

```php
$config = PhpCsFixer\Config::create()
->setRules([
'Jsamson/sensio_to_symfony_route' => true,

])
->registerCustomFixers([
new JSamson\CS\Fixer\Deprecation\SensioToSymfonyRouteFixer,
])
;
```

### Fixes

```diff
+use Symfony\Component\Routing\Annotation\Route;
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
```
11 changes: 11 additions & 0 deletions src/JSamson/CS/Fixer/AbstractFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace JSamson\CS\Fixer;

abstract class AbstractFixer extends \PhpCsFixer\AbstractFixer
{
public function getName()
{
return 'Jsamson/'.parent::getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

namespace JSamson\CS\Fixer\Deprecation;

use PhpCsFixer\AbstractFixer;
use JSamson\CS\Fixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

final class MethodToRouteAnnotationFixer extends AbstractFixer
{
public function getName(): string
{
return 'Jsamson/'.parent::getName();
}

public function getDefinition(): FixerDefinition
{
return new FixerDefinition(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

namespace JSamson\CS\Fixer\Deprecation;

use PhpCsFixer\AbstractFixer;
use JSamson\CS\Fixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;

final class SensioToSymfonyRouteFixer extends AbstractFixer
{
public function getName()
{
return 'Jsamson/'.parent::getName();
}

public function getDefinition()
{
return new FixerDefinition(
Expand Down
102 changes: 102 additions & 0 deletions src/JSamson/CS/Fixer/Doctrine/DoctrineMigrationCleanFixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace JSamson\CS\Fixer\Doctrine;

use JSamson\CS\Fixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Tokenizer\Tokens;

final class DoctrineMigrationCleanFixer extends AbstractFixer
{
private $candidateTokens = [\T_DECLARE, \T_DOC_COMMENT, \T_COMMENT, \T_STRING];

public function getDefinition(): FixerDefinition
{
return new FixerDefinition(
'Remove declare(strict_types=1), auto-generated comments, and abortIf calls from doctrine migration generated files.',
[
new CodeSample(
'<?php
declare(strict_types=1);
namespace Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20190306110954 extends AbstractMigration
{
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== \'mysql\', \'Migration can only be executed safely on \'mysql\'.\');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== \'mysql\', \'Migration can only be executed safely on \'mysql\'.\');
}
}'
),
]
);
}

public function isRisky(): bool
{
return true;
}

public function supports(\SplFileInfo $file): bool
{
return preg_match("/^Version\d{14}/", $file->getBasename());
}

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAllTokenKindsFound($this->candidateTokens);
}

public function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
foreach ($tokens as $index => $token) {
if ($token->isGivenKind(\T_DECLARE)) {
$tokens->clearRange(
$index,
$tokens->getTokenOfKindSibling($index, 1, [[\T_WHITESPACE]])
);
}

if ($token->isGivenKind(\T_DOC_COMMENT)
&& false !== strpos($token->getContent(), 'Auto-generated Migration: Please modify to your needs!')
) {
$tokens->clearRange(
$index,
$tokens->getTokenOfKindSibling($index, 1, [[\T_WHITESPACE]])
);
}

if ($token->isGivenKind(\T_COMMENT)
&& false !== strpos($token->getContent(), 'auto-generated')
) {
$tokens->clearRange(
$index,
$tokens->getTokenOfKindSibling($index, 1, [[\T_WHITESPACE]])
);
}

if ($token->isGivenKind(\T_STRING) && 'abortIf' === $token->getContent()) {
$tokens->clearRange(
$tokens->getTokenOfKindSibling($index, -1, [[\T_WHITESPACE]]),
$endOfTheLineTokenIndex = $tokens->getNextTokenOfKind($index, [';'])
);
}
}
}
}
28 changes: 28 additions & 0 deletions tests/Fixer/Doctrine/DoctrineMigrationCleanFixerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Fixer\Doctrine;

use JSamson\CS\Fixer\Deprecation\SensioToSymfonyRouteFixer;
use JSamson\CS\Fixer\Doctrine\DoctrineMigrationCleanFixer;
use PhpCsFixer\Tests\TestCase;
use PhpCsFixer\Tokenizer\Tokens;

class DoctrineMigrationCleanFixerTest extends TestCase
{
/**
* @dataProvider provideTestSupportsCases
*/
public function testSupports(string $fileName, bool $expected): void
{
$this->assertEquals($expected, (new DoctrineMigrationCleanFixer())->supports(new \SplFileInfo($fileName)));
}

public function provideTestSupportsCases(): \Generator
{
yield ['Version1234.php', false];
yield ['1234Version.php', false];
yield ['Version12345678901234.php', true];
yield ['Version123456789012.php', false];
yield ['Version20200101010101.php', true];
}
}

0 comments on commit 6ceac5b

Please sign in to comment.