Skip to content

Commit cf4b50e

Browse files
authored
Analyse dev-tools (#249)
1 parent 16ea6c7 commit cf4b50e

File tree

10 files changed

+97
-33
lines changed

10 files changed

+97
-33
lines changed

dev-tools/composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
"composer validate --strict --working-dir=..",
2222
"composer normalize --dry-run ../composer.json",
2323
"composer-require-checker check ../composer.json",
24-
"phpcs --exclude=Generic.Files.LineLength --report-full --standard=PSR2 ../src ../dev-tools/src ../tests",
25-
"types-checker ../src ../dev-tools/src ../tests",
24+
"phpcs --exclude=Generic.Files.LineLength --report-full --standard=PSR2 ./src ../src ../tests",
25+
"types-checker ./src ../src ../tests",
2626
"phpmd ../src text ./phpmd.xml",
2727
"phpstan analyse --no-progress",
2828
"psalm --no-progress --shepherd"
2929
],
3030
"fix": [
3131
"composer normalize ../composer.json",
32-
"phpcbf --exclude=Generic.Files.LineLength --report-full --standard=PSR2 ../src ../dev-tools/src ../tests || exit 0"
32+
"phpcbf --exclude=Generic.Files.LineLength --report-full --standard=PSR2 ./src ../src ../tests || exit 0"
3333
],
3434
"infection": [
3535
"infection run --ansi --min-msi=100 --only-covered --threads=16"

dev-tools/phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
parameters:
22
autoload_files:
33
- ../vendor/autoload.php
4+
excludes_analyse:
5+
- ./src/Readme
46
ignoreErrors:
57
- '#^Method PhpCsFixerCustomFixers\\Fixer\\[a-zA-Z]+::configure\(\) has parameter \$configuration with no value type specified in iterable type array\.$#'
68
- '#^Parameter \#\d+ \$[a-zA-Z]+ of method PhpCsFixer\\Tokenizer\\Tokens::insertAt\(\) expects#'
79
- '#^Parameter \#\d+ \$[a-zA-Z]+ of method PhpCsFixer\\Tokenizer\\Tokens::overrideRange\(\) expects#'
810
- '#has parameter \$tokens with no value type specified in iterable type PhpCsFixer\\Tokenizer\\Tokens\.#'
911
level: max
1012
paths:
13+
- ./src
1114
- ../src
1215

1316
includes:

dev-tools/psalm.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
xmlns='https://getpsalm.org/schema/config'
55
xsi:schemaLocation='https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd'
66
allowStringToStandInForClass='true'
7+
autoloader='../vendor/autoload.php'
78
findUnusedCode='true'
89
findUnusedVariablesAndParams='true'
910
strictBinaryOperands='true'
1011
totallyTyped='true'
1112
usePhpDocMethodsWithoutMagicCall='true'
1213
>
1314
<projectFiles>
15+
<directory name='./src' />
1416
<directory name='../src' />
17+
<ignoreFiles>
18+
<directory name='./src/Readme' />
19+
</ignoreFiles>
1520
</projectFiles>
1621

1722
<issueHandlers>
@@ -22,6 +27,7 @@
2227
<LoopInvalidation errorLevel='suppress' />
2328
<MoreSpecificReturnType>
2429
<errorLevel type='suppress'>
30+
<file name='./src/Fixers.php' />
2531
<file name='../src/Fixers.php' />
2632
</errorLevel>
2733
</MoreSpecificReturnType>
@@ -30,6 +36,7 @@
3036
<PossiblyNullOperand errorLevel='suppress' />
3137
<PossiblyUnusedMethod>
3238
<errorLevel type='suppress'>
39+
<file name='./src/Fixer/OrderedClassElementsInternalFixer.php' />
3340
<file name='../src/Analyzer/Analysis/SwitchAnalysis.php' />
3441
<file name='../src/Fixer/DeprecatingFixerInterface.php' />
3542
</errorLevel>

dev-tools/src/Fixer/OrderedClassElementsInternalFixer.php

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,43 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
7171

7272
/**
7373
* @internal
74+
*
75+
* @param array<array<string>> $elements
7476
*/
7577
function usort(array &$elements): void
7678
{
77-
\usort($elements, static function (array $a, array $b) {
78-
if ($a['type'] === 'method' && $a['visibility'] === 'public'
79-
&& $b['type'] === 'method' && $b['visibility'] === 'public'
80-
&& isset($a['name'], $b['name'])) {
81-
if (!\in_array($a['name'], OrderedClassElementsInternalFixer::PUBLIC_METHODS_ORDER, true)) {
82-
throw new \Exception(\sprintf('Method %s not in order list', $a['name']));
83-
}
84-
if (!\in_array($b['name'], OrderedClassElementsInternalFixer::PUBLIC_METHODS_ORDER, true)) {
85-
throw new \Exception(\sprintf('Method %s not in order list', $b['name']));
86-
}
87-
foreach (OrderedClassElementsInternalFixer::PUBLIC_METHODS_ORDER as $name) {
88-
if ($a['name'] === $name) {
89-
return -1;
79+
\usort(
80+
$elements,
81+
/**
82+
* @param string[] $a
83+
* @param string[] $b
84+
*/
85+
static function (array $a, array $b): int {
86+
if ($a['type'] === 'method' && $a['visibility'] === 'public'
87+
&& $b['type'] === 'method' && $b['visibility'] === 'public'
88+
&& isset($a['name'], $b['name'])) {
89+
if (!\in_array($a['name'], OrderedClassElementsInternalFixer::PUBLIC_METHODS_ORDER, true)) {
90+
throw new \Exception(\sprintf('Method %s not in order list', $a['name']));
9091
}
91-
if ($b['name'] === $name) {
92-
return 1;
92+
if (!\in_array($b['name'], OrderedClassElementsInternalFixer::PUBLIC_METHODS_ORDER, true)) {
93+
throw new \Exception(\sprintf('Method %s not in order list', $b['name']));
94+
}
95+
foreach (OrderedClassElementsInternalFixer::PUBLIC_METHODS_ORDER as $name) {
96+
if ($a['name'] === $name) {
97+
return -1;
98+
}
99+
if ($b['name'] === $name) {
100+
return 1;
101+
}
93102
}
94103
}
95-
}
96104

97-
if ($a['position'] === $b['position']) {
98-
return $a['start'] <=> $b['start'];
99-
}
105+
if ($a['position'] === $b['position']) {
106+
return $a['start'] <=> $b['start'];
107+
}
100108

101-
return $a['position'] <=> $b['position'];
102-
});
109+
return $a['position'] <=> $b['position'];
110+
}
111+
);
103112
}
104113
}

dev-tools/src/Fixer/PriorityInternalFixer.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,31 @@ public function isRisky(): bool
4343

4444
public function fix(\SplFileInfo $file, Tokens $tokens): void
4545
{
46+
/** @var int[] $indices */
4647
$indices = $tokens->findSequence([[T_EXTENDS], [T_STRING, 'AbstractFixer']]);
4748

48-
$classNameIndex = $tokens->getPrevMeaningfulToken(\key($indices));
49+
/** @var int $sequencesStartIndex */
50+
$sequencesStartIndex = \key($indices);
51+
52+
/** @var int $classNameIndex */
53+
$classNameIndex = $tokens->getPrevMeaningfulToken($sequencesStartIndex);
54+
4955
$className = $tokens[$classNameIndex]->getContent();
5056

5157
/** @var int $startIndex */
52-
$startIndex = $tokens->getNextTokenOfKind(\key($indices), ['{']);
58+
$startIndex = $tokens->getNextTokenOfKind($sequencesStartIndex, ['{']);
59+
5360
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startIndex);
5461

62+
/** @var int[] $indices */
5563
$indices = $tokens->findSequence([[T_PUBLIC], [T_FUNCTION], [T_STRING, 'getPriority']], $startIndex, $endIndex);
5664

65+
/** @var int $sequencesStartIndex */
66+
$sequencesStartIndex = \key($indices);
67+
5768
/** @var int $startIndex */
58-
$startIndex = $tokens->getNextTokenOfKind(\key($indices), ['{']);
69+
$startIndex = $tokens->getNextTokenOfKind($sequencesStartIndex, ['{']);
70+
5971
$endIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $startIndex);
6072

6173
$commentsToInsert = $this->getCommentsToInsert($className);
@@ -73,6 +85,7 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
7385
$tokens[$index] = \array_shift($commentsToInsert);
7486
}
7587

88+
/** @var int $returnIndex */
7689
$returnIndex = $tokens->getNextTokenOfKind($startIndex, [[T_RETURN]]);
7790

7891
foreach (\array_reverse($commentsToInsert) as $comment) {
@@ -85,11 +98,18 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
8598
);
8699
}
87100

88-
$priorityStartIndex = $tokens->getNextTokenOfKind($startIndex, [[T_RETURN]]) + 2;
101+
/** @var int $nextIndex */
102+
$nextIndex = $tokens->getNextTokenOfKind($startIndex, [[T_RETURN]]);
103+
104+
$priorityStartIndex = $nextIndex + 2;
89105
if ($tokens[$priorityStartIndex]->isGivenKind(T_VARIABLE)) {
90106
return;
91107
}
92-
$priorityEndIndex = $tokens->getNextTokenOfKind($priorityStartIndex, [';']) - 1;
108+
109+
/** @var int $nextIndex */
110+
$nextIndex = $tokens->getNextTokenOfKind($priorityStartIndex, [';']);
111+
112+
$priorityEndIndex = $nextIndex - 1;
93113

94114
$priorityCollection = PriorityCollection::create();
95115
$priority = $priorityCollection->getPriorityFixer($className)->getPriority();

dev-tools/src/Fixers.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,28 @@
44

55
namespace PhpCsFixerCustomFixersDev;
66

7+
use PhpCsFixer\Fixer\FixerInterface;
78
use Symfony\Component\Finder\Finder;
9+
use Symfony\Component\Finder\SplFileInfo;
810

911
/**
12+
* @implements \IteratorAggregate<FixerInterface>
13+
*
1014
* @internal
1115
*/
1216
final class Fixers implements \IteratorAggregate
1317
{
18+
/**
19+
* @return \Generator<FixerInterface>
20+
*/
1421
public function getIterator(): \Generator
1522
{
1623
$finder = Finder::create()
1724
->files()
1825
->in(__DIR__ . '/Fixer/')
1926
->sortByName();
2027

28+
/** @var SplFileInfo $fileInfo */
2129
foreach ($finder as $fileInfo) {
2230
$className = __NAMESPACE__ . '\\Fixer\\' . $fileInfo->getBasename('.php');
2331
yield new $className();

dev-tools/src/Priority/PriorityCollection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ final class PriorityCollection
1616

1717
public static function create(): self
1818
{
19+
/** @var null|self $instance */
1920
static $instance;
2021

2122
if ($instance === null) {
@@ -36,8 +37,7 @@ public function __construct()
3637
$this->priorityFixers[(new \ReflectionObject($fixer))->getShortName()] = new PriorityFixer($fixer, null);
3738
}
3839

39-
$priorityTest = new PriorityTest();
40-
foreach ($priorityTest->providePriorityCases() as [$firstFixer, $secondFixer]) {
40+
foreach (PriorityTest::providePriorityCases() as [$firstFixer, $secondFixer]) {
4141
$this->priorityFixer($firstFixer)->addFixerToRunAfter($this->priorityFixer($secondFixer));
4242
$this->priorityFixer($secondFixer)->addFixerToRunBefore($this->priorityFixer($firstFixer));
4343
}
@@ -54,7 +54,7 @@ public function __construct()
5454

5555
foreach ($this->priorityFixers as $priorityFixer) {
5656
if (!$priorityFixer->hasPriority()) {
57-
$anythingChanged |= $priorityFixer->calculatePriority(true);
57+
$anythingChanged = $anythingChanged || $priorityFixer->calculatePriority(true);
5858
}
5959
}
6060
}

dev-tools/src/Priority/PriorityFixer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,27 @@ public function getPriority(): int
5050
return $this->priority;
5151
}
5252

53+
/**
54+
* @return string[]
55+
*/
5356
public function getFixerToRunAfterNames(): array
5457
{
5558
return $this->getFixerNames($this->fixersToRunAfter);
5659
}
5760

61+
/**
62+
* @return string[]
63+
*/
5864
public function getFixerToRunBeforeNames(): array
5965
{
6066
return $this->getFixerNames($this->fixersToRunBefore);
6167
}
6268

69+
/**
70+
* @param self[] $priorityFixers
71+
*
72+
* @return string[]
73+
*/
6374
private function getFixerNames(array $priorityFixers): array
6475
{
6576
$fixers = \array_map(

dev-tools/src/Readme/ReadmeCommand.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpCsFixer\FixerDefinition\CodeSample;
1212
use PhpCsFixer\StdinFileInfo;
1313
use PhpCsFixer\Tokenizer\Tokens;
14+
use PhpCsFixerCustomFixers\Fixer\AbstractFixer;
1415
use PhpCsFixerCustomFixers\Fixer\DeprecatingFixerInterface;
1516
use PhpCsFixerCustomFixers\Fixers;
1617
use SebastianBergmann\Diff\Differ;
@@ -29,7 +30,7 @@ final class ReadmeCommand extends BaseCommand
2930

3031
private const SHIELDS_HOST = 'https://img.shields.io';
3132

32-
protected function execute(InputInterface $input, OutputInterface $output): ?int
33+
protected function execute(InputInterface $input, OutputInterface $output): int
3334
{
3435
$output->write(
3536
\sprintf('# %s', self::NAME) . "\n\n"
@@ -166,6 +167,7 @@ private function fixers(): string
166167
{
167168
$output = '## Fixers';
168169

170+
/** @var AbstractFixer $fixer */
169171
foreach (new Fixers() as $fixer) {
170172
$reflection = new \ReflectionClass($fixer);
171173

@@ -200,6 +202,7 @@ private function fixers(): string
200202
return \sprintf('\'%s\'', $value);
201203
}, $option->getAllowedValues());
202204
} else {
205+
/** @var string[] $allowed */
203206
$allowed = $option->getAllowedTypes();
204207
}
205208
$output .= \sprintf(

tests/PriorityTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ static function (array $case): string {
110110
self::assertSame($sorted, $cases);
111111
}
112112

113+
/**
114+
* @return array<array<FixerInterface>>
115+
*/
113116
public static function providePriorityCases(): iterable
114117
{
115118
yield [

0 commit comments

Comments
 (0)