Skip to content
Permalink
Browse files

Add OrderedClassElementsInternalFixer (#169)

  • Loading branch information...
kubawerlos committed Oct 24, 2019
1 parent 1063223 commit fb9c6e844cef07ed5eaa858f023536cc755fde17
Showing with 295 additions and 147 deletions.
  1. +16 −2 .php_cs
  2. +0 −1 composer.json
  3. +4 −5 dev-tools/composer.json
  4. +104 −0 dev-tools/src/Fixer/OrderedClassElementsInternalFixer.php
  5. +26 −0 dev-tools/src/Fixers.php
  6. +3 −0 dev-tools/src/Readme/ReadmeCommand.php
  7. +1 −0 phpunit.xml
  8. +2 −0 psalm.xml
  9. +6 −6 src/Fixer/CommentSurroundedBySpacesFixer.php
  10. +4 −4 src/Fixer/DataProviderNameFixer.php
  11. +5 −5 src/Fixer/DataProviderReturnTypeFixer.php
  12. +5 −5 src/Fixer/ImplodeCallFixer.php
  13. +5 −5 src/Fixer/InternalClassCasingFixer.php
  14. +6 −6 src/Fixer/NoCommentedOutCodeFixer.php
  15. +5 −5 src/Fixer/NoDoctrineMigrationsGeneratedCommentFixer.php
  16. +5 −5 src/Fixer/NoDuplicatedImportsFixer.php
  17. +5 −5 src/Fixer/NoImportFromGlobalNamespaceFixer.php
  18. +6 −6 src/Fixer/NoLeadingSlashInGlobalNamespaceFixer.php
  19. +5 −5 src/Fixer/NoNullableBooleanTypeFixer.php
  20. +5 −5 src/Fixer/NoPhpStormGeneratedCommentFixer.php
  21. +5 −5 src/Fixer/NoReferenceInFunctionDefinitionFixer.php
  22. +5 −5 src/Fixer/NoTwoConsecutiveEmptyLinesFixer.php
  23. +5 −5 src/Fixer/NoUnneededConcatenationFixer.php
  24. +5 −5 src/Fixer/NoUselessClassCommentFixer.php
  25. +5 −5 src/Fixer/NoUselessCommentFixer.php
  26. +5 −5 src/Fixer/NoUselessConstructorCommentFixer.php
  27. +5 −5 src/Fixer/NoUselessDoctrineRepositoryCommentFixer.php
  28. +8 −8 src/Fixer/OperatorLinebreakFixer.php
  29. +5 −5 src/Fixer/PhpdocNoSuperfluousParamFixer.php
  30. +6 −6 src/Fixer/PhpdocOnlyAllowedAnnotationsFixer.php
  31. +7 −7 src/Fixer/PhpdocParamTypeFixer.php
  32. +5 −5 src/Fixer/PhpdocSingleLineVarFixer.php
  33. +5 −5 src/Fixer/PhpdocVarAnnotationCorrectOrderFixer.php
  34. +6 −6 src/Fixer/SingleLineThrowFixer.php
18 .php_cs
@@ -2,7 +2,11 @@
declare(strict_types = 1);
require_once __DIR__ . '/dev-tools/vendor/autoload.php';
return PhpCsFixer\Config::create()
->registerCustomFixers(new PhpCsFixerCustomFixers\Fixers())
->registerCustomFixers(new PhpCsFixerCustomFixersDev\Fixers())
->setFinder(
PhpCsFixer\Finder::create()
->files()
@@ -16,7 +20,6 @@ return PhpCsFixer\Config::create()
])
)
->setRiskyAllowed(true)
->registerCustomFixers(new PhpCsFixerCustomFixers\Fixers())
->setRules([
'@PHP71Migration' => true,
'@PHP71Migration:risky' => true,
@@ -94,7 +97,6 @@ return PhpCsFixer\Config::create()
'no_unset_on_property' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_class_elements' => true,
'ordered_imports' => [
'sortAlgorithm' => 'alpha',
],
@@ -129,6 +131,10 @@ return PhpCsFixer\Config::create()
] + \array_reduce(
\iterator_to_array(new PhpCsFixerCustomFixers\Fixers()),
static function (array $carry, PhpCsFixer\Fixer\DefinedFixerInterface $fixer): array {
if ($fixer instanceof PhpCsFixerCustomFixers\Fixer\NoReferenceInFunctionDefinitionFixer) {
return $carry;
}
if (!$fixer instanceof PhpCsFixer\Fixer\DeprecatedFixerInterface) {
$carry[$fixer->getName()] = true;
}
@@ -148,4 +154,12 @@ return PhpCsFixer\Config::create()
return $carry;
},
[]
) + \array_reduce(
\iterator_to_array(new PhpCsFixerCustomFixersDev\Fixers()),
static function (array $carry, PhpCsFixer\Fixer\FixerInterface $fixer): array {
$carry[$fixer->getName()] = true;
return $carry;
},
[]
));
@@ -29,7 +29,6 @@
"./dev-tools/vendor/bin/phpcs --exclude=Generic.Files.LineLength --report-full --standard=PSR2 ./src ./dev-tools/src ./tests",
"./dev-tools/vendor/bin/types-checker ./src ./dev-tools/src ./tests",
"PHP_CS_FIXER_FUTURE_MODE=1 ./vendor/bin/php-cs-fixer fix --ansi --diff --dry-run --verbose",
"./dev-tools/vendor/bin/phpcpd ./src",
"./dev-tools/vendor/bin/phpmd ./src text ./phpmd.xml",
"./dev-tools/vendor/bin/phpstan analyse --ansi",
"./dev-tools/vendor/bin/psalm --find-dead-code --shepherd"
@@ -1,20 +1,19 @@
{
"require-dev": {
"infection/infection": "^0.13.6 || 0.14.1",
"infection/infection": "^0.13.6 || ^0.14.2",
"johnkary/phpunit-speedtrap": "^3.1",
"kubawerlos/types-checker": "^1.1",
"localheinz/composer-normalize": "^1.3",
"maglnet/composer-require-checker": "^2.0",
"mi-schi/phpmd-extension": "^4.3",
"phpmd/phpmd": "^2.7",
"phpstan/phpstan": "^0.11.16",
"phpstan/phpstan": "^0.11.19",
"phpstan/phpstan-strict-rules": "^0.11.1",
"phpunit/phpunit": "^7.4 || ^8.3",
"phpunit/phpunit": "^7.4 || ^8.4",
"sebastian/diff": "^3.0",
"sebastian/phpcpd": "^4.1",
"squizlabs/php_codesniffer": "^3.5",
"symfony/console": "^4.3",
"vimeo/psalm": "^3.5",
"vimeo/psalm": "^3.6",
"wikimedia/composer-merge-plugin": "^1.4"
},
"extra": {
@@ -0,0 +1,104 @@
<?php
declare(strict_types = 1);
namespace PhpCsFixerCustomFixersDev\Fixer {
use PhpCsFixer\Fixer\ClassNotation\OrderedClassElementsFixer;
use PhpCsFixer\Fixer\FixerInterface;
use PhpCsFixer\Tokenizer\Tokens;
/**
* @internal
*/
final class OrderedClassElementsInternalFixer implements FixerInterface
{
public const PUBLIC_METHODS_ORDER = [
'getDefinition',
'getConfigurationDefinition',
'configure',
'getName',
'getPriority',
'getPullRequestId',
'supports',
'isCandidate',
'isRisky',
'fix',
'getSuccessorsNames',
];
/** @var OrderedClassElementsFixer */
private $orderedClassElementsFixer;
public function __construct()
{
$this->orderedClassElementsFixer = new OrderedClassElementsFixer();
}
public function getName(): string
{
return 'Internal/' . $this->orderedClassElementsFixer->getName();
}
public function getPriority(): int
{
return $this->orderedClassElementsFixer->getPriority();
}
public function supports(\SplFileInfo $file): bool
{
return $this->orderedClassElementsFixer->supports($file);
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->findSequence([[T_EXTENDS], [T_STRING, 'AbstractFixer']]) !== null;
}
public function isRisky(): bool
{
return $this->orderedClassElementsFixer->isRisky();
}
public function fix(\SplFileInfo $file, Tokens $tokens): void
{
$this->orderedClassElementsFixer->fix($file, $tokens);
}
}
}
namespace PhpCsFixer\Fixer\ClassNotation {
use PhpCsFixerCustomFixersDev\Fixer\OrderedClassElementsInternalFixer;
/**
* @internal
*/
function usort(array &$elements): void
{
\usort($elements, static function (array $a, array $b) {
if ($a['type'] === 'method' && $a['visibility'] === 'public'
&& $b['type'] === 'method' && $b['visibility'] === 'public'
&& isset($a['name'], $b['name'])) {
if (!\in_array($a['name'], OrderedClassElementsInternalFixer::PUBLIC_METHODS_ORDER, true)) {
throw new \Exception(\sprintf('Method %s not in order list', $a['name']));
}
if (!\in_array($b['name'], OrderedClassElementsInternalFixer::PUBLIC_METHODS_ORDER, true)) {
throw new \Exception(\sprintf('Method %s not in order list', $b['name']));
}
foreach (OrderedClassElementsInternalFixer::PUBLIC_METHODS_ORDER as $name) {
if ($a['name'] === $name) {
return -1;
}
if ($b['name'] === $name) {
return 1;
}
}
}
if ($a['position'] === $b['position']) {
return $a['start'] <=> $b['start'];
}
return $a['position'] <=> $b['position'];
});
}
}
@@ -0,0 +1,26 @@
<?php
declare(strict_types = 1);
namespace PhpCsFixerCustomFixersDev;
use Symfony\Component\Finder\Finder;
/**
* @internal
*/
final class Fixers implements \IteratorAggregate
{
public function getIterator(): \Generator
{
$finder = Finder::create()
->files()
->in(__DIR__ . '/Fixer/')
->sortByName();
foreach ($finder as $fileInfo) {
$className = __NAMESPACE__ . '\\Fixer\\' . $fileInfo->getBasename('.php');
yield new $className();
}
}
}
@@ -20,6 +20,9 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
/**
* @internal
*/
final class ReadmeCommand extends BaseCommand
{
private const NAME = 'PHP CS Fixer: custom fixers';
@@ -38,6 +38,7 @@

<filter>
<whitelist>
<directory>./dev-tools/src/Readme</directory>
<directory>./src</directory>
<exclude>
<file>./dev-tools/src/Readme/php-cs-fixer.config.after.php</file>
@@ -20,6 +20,7 @@
<InvalidArgument errorLevel='suppress' />
<InvalidStringClass>
<errorLevel type='suppress'>
<file name='./dev-tools/src/Fixers.php' />
<file name='./src/Fixers.php' />
</errorLevel>
</InvalidStringClass>
@@ -29,6 +30,7 @@
<PossiblyNullOperand errorLevel='suppress' />
<PossiblyUnusedMethod>
<errorLevel type='suppress'>
<file name='./dev-tools/src/Fixer/OrderedClassElementsInternalFixer.php' />
<file name='./src/Analyzer/Analysis/SwitchAnalysis.php' />
</errorLevel>
</PossiblyUnusedMethod>
@@ -23,6 +23,12 @@ public function getDefinition(): FixerDefinitionInterface
);
}
public function getPriority(): int
{
// Must be run after MultilineCommentOpeningClosingFixer
return -1;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]);
@@ -57,10 +63,4 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
$tokens[$index] = new Token([\strpos($newContent, '/** ') === 0 ? T_DOC_COMMENT : T_COMMENT, $newContent]);
}
}
public function getPriority(): int
{
// Must be run after MultilineCommentOpeningClosingFixer
return -1;
}
}
@@ -39,14 +39,14 @@ function dataProvider() {}
);
}
public function isCandidate(Tokens $tokens): bool
public function getPriority(): int
{
return $tokens->isTokenKindFound(T_DOC_COMMENT);
return 0;
}
public function getPriority(): int
public function isCandidate(Tokens $tokens): bool
{
return 0;
return $tokens->isTokenKindFound(T_DOC_COMMENT);
}
public function isRisky(): bool
@@ -42,17 +42,17 @@ function provideHappyPathCases(): array {}
);
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_DOC_COMMENT);
}
public function getPriority(): int
{
// must be run before MethodArgumentSpaceFixer
return 0;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_DOC_COMMENT);
}
public function isRisky(): bool
{
return true;
@@ -36,6 +36,11 @@ public function getDefinition(): FixerDefinitionInterface
);
}
public function getPriority(): int
{
return $this->fixer->getPriority();
}
public function isCandidate(Tokens $tokens): bool
{
return $this->fixer->isCandidate($tokens);
@@ -51,11 +56,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
$this->fixer->fix($file, $tokens);
}
public function getPriority(): int
{
return $this->fixer->getPriority();
}
/**
* @return string[]
*/
@@ -22,6 +22,11 @@ public function getDefinition(): FixerDefinitionInterface
);
}
public function getPriority(): int
{
return 0;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_STRING);
@@ -41,11 +46,6 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
}
}
public function getPriority(): int
{
return 0;
}
private function fixCasing(Tokens $tokens, int $startIndex, int $endIndex, bool $isInGlobalNamespace): void
{
for ($index = $startIndex; $index < $endIndex; $index++) {
@@ -22,6 +22,12 @@ public function getDefinition(): FixerDefinitionInterface
);
}
public function getPriority(): int
{
// must be run before NoUnusedImportsFixer
return 0;
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_COMMENT, T_DOC_COMMENT]);
@@ -32,12 +38,6 @@ public function isRisky(): bool
return false;
}
public function getPriority(): int
{
// must be run before NoUnusedImportsFixer
return 0;
}
public function fix(\SplFileInfo $file, Tokens $tokens): void
{
$commentsAnalyzer = new CommentsAnalyzer();

0 comments on commit fb9c6e8

Please sign in to comment.
You can’t perform that action at this time.