Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 965c911

Browse files
committed
Fix SpecifyArgSeparatorFixer incompatible with php-cs-fixer 3.0
1 parent b5554ce commit 965c911

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<!-- There is always Unreleased section on the top. Subsections (Added, Changed, Fixed, Removed) should be added as needed. -->
66

77
## Unreleased
8+
- Fix SpecifyArgSeparatorFixer not compatible with php-cs-fixer 3.0.
89

910
## 3.1.0 - 2021-05-13
1011
- Use php-cs-fixer 3.0.

phpstan.neon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ parameters:
77
- vendor/symplify/easy-coding-standard/vendor/squizlabs/php_codesniffer/autoload.php
88
- vendor/symplify/easy-coding-standard/vendor/squizlabs/php_codesniffer/src/Util/Tokens.php
99
ignoreErrors:
10-
- message: '#Parameter \#(2|3) \$(argumentStart|argumentEnd) of method PhpCsFixer\\Tokenizer\\Analyzer\\ArgumentsAnalyzer::getArgumentInfo\(\) expects int#'
11-
path: %currentWorkingDirectory%/src/Fixer/SpecifyArgSeparatorFixer.php
1210
- message: '#Parameter \#1 \$code of static method PhpCsFixer\\Tokenizer\\Tokens::fromCode\(\) expects string, string\|false given#'
1311
path: %currentWorkingDirectory%/tests/Fixer/SpecifyArgSeparatorFixerTest.php
1412
- message: '#Parameter \#1 \$filename of function file_get_contents expects string, string\|false given.#'

src/Fixer/SpecifyArgSeparatorFixer.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PhpCsFixer\FixerDefinition\CodeSample;
77
use PhpCsFixer\FixerDefinition\FixerDefinition;
88
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
9-
use PhpCsFixer\Tokenizer\Analyzer\Analysis\TypeAnalysis;
109
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
1110
use PhpCsFixer\Tokenizer\CT;
1211
use PhpCsFixer\Tokenizer\Token;
@@ -91,13 +90,14 @@ private function fixFunction(Tokens $tokens, int $functionIndex): void
9190

9291
// When third argument is already present and it is null, override its value.
9392
if ($argumentCount >= 3) {
94-
$thirdArgumentType = $this->getThirdArgumentInfo($tokens, $openParenthesisIndex, $closeParenthesisIndex);
95-
if ($thirdArgumentType === null) {
93+
$thirdArgumentTuple = $this->getThirdArgumentTokenTuple($tokens, $openParenthesisIndex, $closeParenthesisIndex);
94+
if ($thirdArgumentTuple === []) {
9695
return;
9796
}
9897

99-
if ($thirdArgumentType->getName() === 'null') {
100-
$this->setArgumentValueToAmp($tokens, $thirdArgumentType->getStartIndex());
98+
$thirdArgumentToken = reset($thirdArgumentTuple);
99+
if ($thirdArgumentToken->getContent() === 'null') {
100+
$this->setArgumentValueToAmp($tokens, key($thirdArgumentTuple));
101101
}
102102

103103
return;
@@ -112,7 +112,7 @@ private function fixFunction(Tokens $tokens, int $functionIndex): void
112112
$tokensToInsert[] = new Token([T_STRING, 'null']);
113113
}
114114

115-
// Add third argument (arg separator): ", &"
115+
// Add third argument (arg separator): ", '&'"
116116
$tokensToInsert[] = new Token(',');
117117
$tokensToInsert[] = new Token([T_WHITESPACE, ' ']);
118118
$tokensToInsert[] = new Token([T_STRING, "'&'"]);
@@ -139,19 +139,28 @@ private function isFunctionCall(Tokens $tokens, int $index): bool
139139

140140
/**
141141
* @param Tokens<Token> $tokens
142+
* @return array<int, Token>
142143
*/
143-
private function getThirdArgumentInfo(
144+
private function getThirdArgumentTokenTuple(
144145
Tokens $tokens,
145146
int $openParenthesisIndex,
146147
int $closeParenthesisIndex
147-
): ?TypeAnalysis {
148+
): array {
148149
$argumentsAnalyzer = new ArgumentsAnalyzer();
150+
$allArguments = $argumentsAnalyzer->getArguments($tokens, $openParenthesisIndex, $closeParenthesisIndex);
151+
$thirdArgumentIndex = array_slice($allArguments, 2, 1, true);
149152

150-
$arguments = $argumentsAnalyzer->getArguments($tokens, $openParenthesisIndex, $closeParenthesisIndex);
151-
$argumentIndex = array_slice($arguments, 2, 1, true);
152-
$argumentInfo = $argumentsAnalyzer->getArgumentInfo($tokens, key($argumentIndex), reset($argumentIndex));
153+
for ($index = key($thirdArgumentIndex); $index <= reset($thirdArgumentIndex); $index++) {
154+
/** @var int $index */
155+
/** @var Token $token */
156+
$token = $tokens[$index];
153157

154-
return $argumentInfo->getTypeAnalysis();
158+
if (!$token->isWhitespace() && !$token->isComment()) {
159+
return [$index => $token];
160+
}
161+
}
162+
163+
return [];
155164
}
156165

157166
/**

tests/Fixer/Fixtures/Correct.php.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class Correct
88
{
99
$queryString1 = http_build_query(['foo' => 'bar'], null, '&');
1010

11+
$queryString1WithMultipleWhitespaces = http_build_query(['foo' => 'bar'], null, '&');
12+
13+
$queryString1WithWhitespacesAndComments = http_build_query(['foo' => 'bar'], null, /* comment */ '&' /* x */);
14+
1115
$queryString1WithComment = http_build_query(['foo' => 'bar'], /* Comment, with commas, */ null , '&');
1216

1317
$queryString1WithObject = http_build_query((object) ['foo' => 'bar'], null, '&');

0 commit comments

Comments
 (0)