Skip to content

Commit eec7b74

Browse files
authored
Updates (#637)
1 parent 5023ff8 commit eec7b74

File tree

5 files changed

+79
-84
lines changed

5 files changed

+79
-84
lines changed

.dev-tools/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"phpstan/phpstan-strict-rules": "^0.12.11",
1515
"squizlabs/php_codesniffer": "^3.6.1",
1616
"symplify/phpstan-rules": "^9.4.70",
17-
"vimeo/psalm": "^4.11.1"
17+
"vimeo/psalm": "^4.11.2"
1818
},
1919
"autoload": {
2020
"psr-4": {

.dev-tools/composer.lock

Lines changed: 36 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Analyzer/FunctionAnalyzer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ final class FunctionAnalyzer
2424
/**
2525
* @return array<ArgumentAnalysis>
2626
*/
27-
public function getFunctionArguments(Tokens $tokens, int $index): array
27+
public static function getFunctionArguments(Tokens $tokens, int $index): array
2828
{
29-
$argumentsRange = $this->getArgumentsRange($tokens, $index);
29+
$argumentsRange = self::getArgumentsRange($tokens, $index);
3030
if ($argumentsRange === null) {
3131
return [];
3232
}
@@ -53,21 +53,21 @@ public function getFunctionArguments(Tokens $tokens, int $index): array
5353
/** @var int $currentArgumentEnd */
5454
$currentArgumentEnd = $tokens->getPrevMeaningfulToken($index);
5555

56-
$arguments[] = $this->createArgumentAnalysis($tokens, $currentArgumentStart, $currentArgumentEnd);
56+
$arguments[] = self::createArgumentAnalysis($tokens, $currentArgumentStart, $currentArgumentEnd);
5757

5858
/** @var int $currentArgumentStart */
5959
$currentArgumentStart = $tokens->getNextMeaningfulToken($index);
6060
}
6161

62-
$arguments[] = $this->createArgumentAnalysis($tokens, $currentArgumentStart, $argumentsEndIndex);
62+
$arguments[] = self::createArgumentAnalysis($tokens, $currentArgumentStart, $argumentsEndIndex);
6363

6464
return $arguments;
6565
}
6666

6767
/**
6868
* @return null|array{int, int}
6969
*/
70-
private function getArgumentsRange(Tokens $tokens, int $index): ?array
70+
private static function getArgumentsRange(Tokens $tokens, int $index): ?array
7171
{
7272
if (!$tokens[$index]->isGivenKind(\T_STRING)) {
7373
throw new \InvalidArgumentException(\sprintf('Index %d is not a function.', $index));
@@ -98,7 +98,7 @@ private function getArgumentsRange(Tokens $tokens, int $index): ?array
9898
return [$argumentStartIndex, $argumentsEndIndex];
9999
}
100100

101-
private function createArgumentAnalysis(Tokens $tokens, int $startIndex, int $endIndex): ArgumentAnalysis
101+
private static function createArgumentAnalysis(Tokens $tokens, int $startIndex, int $endIndex): ArgumentAnalysis
102102
{
103103
$isConstant = true;
104104

src/Fixer/PhpUnitAssertArgumentsOrderFixer.php

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,54 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
8383

8484
private function fixArgumentsOrder(Tokens $tokens, int $startIndex, int $endIndex): void
8585
{
86-
$functionAnalyzer = new FunctionAnalyzer();
87-
8886
for ($index = $startIndex; $index < $endIndex; $index++) {
8987
if (!self::isAssertionCall($tokens, $index)) {
9088
continue;
9189
}
9290

93-
$arguments = $functionAnalyzer->getFunctionArguments($tokens, $index);
91+
$arguments = FunctionAnalyzer::getFunctionArguments($tokens, $index);
9492

95-
if (!$this->shouldArgumentsBeSwapped($arguments)) {
93+
if (!self::shouldArgumentsBeSwapped($arguments)) {
9694
continue;
9795
}
9896

99-
$this->swapArguments($tokens, $arguments);
97+
self::swapArguments($tokens, $arguments);
98+
}
99+
}
100+
101+
private static function isAssertionCall(Tokens $tokens, int $index): bool
102+
{
103+
static $assertions;
104+
105+
if ($assertions === null) {
106+
$assertions = \array_flip(
107+
\array_map(
108+
static function (string $name): string {
109+
return \strtolower($name);
110+
},
111+
self::ASSERTIONS
112+
)
113+
);
114+
}
115+
116+
if (!isset($assertions[\strtolower($tokens[$index]->getContent())])) {
117+
return false;
100118
}
119+
120+
/** @var int $openingBraceIndex */
121+
$openingBraceIndex = $tokens->getNextMeaningfulToken($index);
122+
123+
if (!$tokens[$openingBraceIndex]->equals('(')) {
124+
return false;
125+
}
126+
127+
return (new FunctionsAnalyzer())->isTheSameClassCall($tokens, $index);
101128
}
102129

103130
/**
104131
* @param array<ArgumentAnalysis> $arguments
105132
*/
106-
private function shouldArgumentsBeSwapped(array $arguments): bool
133+
private static function shouldArgumentsBeSwapped(array $arguments): bool
107134
{
108135
if (\count($arguments) < 2) {
109136
return false;
@@ -119,7 +146,7 @@ private function shouldArgumentsBeSwapped(array $arguments): bool
119146
/**
120147
* @param array<ArgumentAnalysis> $arguments
121148
*/
122-
private function swapArguments(Tokens $tokens, array $arguments): void
149+
private static function swapArguments(Tokens $tokens, array $arguments): void
123150
{
124151
$expectedArgumentTokens = []; // these will be 1st argument
125152
for ($index = $arguments[1]->getStartIndex(); $index <= $arguments[1]->getEndIndex(); $index++) {
@@ -134,33 +161,4 @@ private function swapArguments(Tokens $tokens, array $arguments): void
134161
$tokens->overrideRange($arguments[1]->getStartIndex(), $arguments[1]->getEndIndex(), $actualArgumentTokens);
135162
$tokens->overrideRange($arguments[0]->getStartIndex(), $arguments[0]->getEndIndex(), $expectedArgumentTokens);
136163
}
137-
138-
private static function isAssertionCall(Tokens $tokens, int $index): bool
139-
{
140-
static $assertions;
141-
142-
if ($assertions === null) {
143-
$assertions = \array_flip(
144-
\array_map(
145-
static function (string $name): string {
146-
return \strtolower($name);
147-
},
148-
self::ASSERTIONS
149-
)
150-
);
151-
}
152-
153-
if (!isset($assertions[\strtolower($tokens[$index]->getContent())])) {
154-
return false;
155-
}
156-
157-
/** @var int $openingBraceIndex */
158-
$openingBraceIndex = $tokens->getNextMeaningfulToken($index);
159-
160-
if (!$tokens[$openingBraceIndex]->equals('(')) {
161-
return false;
162-
}
163-
164-
return (new FunctionsAnalyzer())->isTheSameClassCall($tokens, $index);
165-
}
166164
}

tests/Analyzer/FunctionAnalyzerTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ final class FunctionAnalyzerTest extends TestCase
3030
*/
3131
public function testForNotFunction(string $code, int $index): void
3232
{
33-
$analyzer = new FunctionAnalyzer();
34-
3533
$this->expectException(\InvalidArgumentException::class);
3634
$this->expectExceptionMessage(\sprintf('Index %d is not a function.', $index));
3735

38-
$analyzer->getFunctionArguments(Tokens::fromCode($code), $index);
36+
FunctionAnalyzer::getFunctionArguments(Tokens::fromCode($code), $index);
3937
}
4038

4139
/**
@@ -55,9 +53,8 @@ public static function provideForNotFunctionCases(): iterable
5553
public function testGettingArguments(array $expected, string $code, int $index): void
5654
{
5755
$tokens = Tokens::fromCode($code);
58-
$analyzer = new FunctionAnalyzer();
5956

60-
self::assertSame(\serialize($expected), \serialize($analyzer->getFunctionArguments($tokens, $index)));
57+
self::assertSame(\serialize($expected), \serialize(FunctionAnalyzer::getFunctionArguments($tokens, $index)));
6158
}
6259

6360
/**

0 commit comments

Comments
 (0)