-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jeremie Samson
committed
May 5, 2020
1 parent
fd0496a
commit 6ceac5b
Showing
9 changed files
with
208 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]) | ||
; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) | ||
*/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/JSamson/CS/Fixer/Doctrine/DoctrineMigrationCleanFixer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, [';']) | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |