Skip to content

Commit

Permalink
Merge 0fcc5c6 into b5554ce
Browse files Browse the repository at this point in the history
  • Loading branch information
OndraM committed May 19, 2021
2 parents b5554ce + 0fcc5c6 commit 466b899
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<!-- There is always Unreleased section on the top. Subsections (Added, Changed, Fixed, Removed) should be added as needed. -->

## Unreleased
- Fix SpecifyArgSeparatorFixer not compatible with php-cs-fixer 3.0.

## 3.1.0 - 2021-05-13
- Use php-cs-fixer 3.0.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"nette/utils": "^3.2",
"slevomat/coding-standard": "^6.4.1",
"squizlabs/php_codesniffer": "^3.6",
"symplify/easy-coding-standard": "^9.3.10"
"symplify/easy-coding-standard": "^9.3.12"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.13.2",
Expand Down
2 changes: 0 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ parameters:
- vendor/symplify/easy-coding-standard/vendor/squizlabs/php_codesniffer/autoload.php
- vendor/symplify/easy-coding-standard/vendor/squizlabs/php_codesniffer/src/Util/Tokens.php
ignoreErrors:
- message: '#Parameter \#(2|3) \$(argumentStart|argumentEnd) of method PhpCsFixer\\Tokenizer\\Analyzer\\ArgumentsAnalyzer::getArgumentInfo\(\) expects int#'
path: %currentWorkingDirectory%/src/Fixer/SpecifyArgSeparatorFixer.php
- message: '#Parameter \#1 \$code of static method PhpCsFixer\\Tokenizer\\Tokens::fromCode\(\) expects string, string\|false given#'
path: %currentWorkingDirectory%/tests/Fixer/SpecifyArgSeparatorFixerTest.php
- message: '#Parameter \#1 \$filename of function file_get_contents expects string, string\|false given.#'
Expand Down
33 changes: 21 additions & 12 deletions src/Fixer/SpecifyArgSeparatorFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Analyzer\Analysis\TypeAnalysis;
use PhpCsFixer\Tokenizer\Analyzer\ArgumentsAnalyzer;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Token;
Expand Down Expand Up @@ -91,13 +90,14 @@ private function fixFunction(Tokens $tokens, int $functionIndex): void

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

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

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

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

/**
* @param Tokens<Token> $tokens
* @return array<int, Token>
*/
private function getThirdArgumentInfo(
private function getThirdArgumentTokenTuple(
Tokens $tokens,
int $openParenthesisIndex,
int $closeParenthesisIndex
): ?TypeAnalysis {
): array {
$argumentsAnalyzer = new ArgumentsAnalyzer();
$allArguments = $argumentsAnalyzer->getArguments($tokens, $openParenthesisIndex, $closeParenthesisIndex);
$thirdArgumentIndex = array_slice($allArguments, 2, 1, true);

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

return $argumentInfo->getTypeAnalysis();
if (!$token->isWhitespace() && !$token->isComment()) {
return [$index => $token];
}
}

return [];
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixer/Fixtures/Correct.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class Correct
{
$queryString1 = http_build_query(['foo' => 'bar'], null, '&');

$queryString1WithMultipleWhitespaces = http_build_query(['foo' => 'bar'], null, '&');

$queryString1WithWhitespacesAndComments = http_build_query(['foo' => 'bar'], null, /* comment */ '&' /* x */);

$queryString1WithComment = http_build_query(['foo' => 'bar'], /* Comment, with commas, */ null , '&');

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

0 comments on commit 466b899

Please sign in to comment.