From c6c2e13de0bfed50d93bac7f20066f6e76687a07 Mon Sep 17 00:00:00 2001 From: Ondrej Machulda Date: Tue, 4 Jan 2022 11:53:40 +0100 Subject: [PATCH] Fix: SpecifyArgSeparatorFixer type error in PHP 7.4+ (fixes #72) --- CHANGELOG.md | 1 + src/Fixer/SpecifyArgSeparatorFixer.php | 8 ++++++-- tests/Fixer/Fixtures/Correct.php.inc | 16 +++++++-------- tests/Fixer/Fixtures/Fixed.php.inc | 27 ++++++++++++++++---------- tests/Fixer/Fixtures/Wrong.php.inc | 25 +++++++++++++++--------- 5 files changed, 48 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a45886b..d8f5a75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ## Unreleased +- Fix SpecifyArgSeparatorFixer type error when adding empty `$numeric_prefix` parameter in PHP 7.4+. ## 3.2.0 - 2021-05-20 - Fix SpecifyArgSeparatorFixer not compatible with php-cs-fixer 3.0. diff --git a/src/Fixer/SpecifyArgSeparatorFixer.php b/src/Fixer/SpecifyArgSeparatorFixer.php index 333ca99..0495bbf 100644 --- a/src/Fixer/SpecifyArgSeparatorFixer.php +++ b/src/Fixer/SpecifyArgSeparatorFixer.php @@ -81,7 +81,11 @@ private function fixFunction(Tokens $tokens, int $functionIndex): void // When third argument is already present and it is null, override its value. if ($argumentCount >= 3) { - $thirdArgumentTuple = $this->getThirdArgumentTokenTuple($tokens, $openParenthesisIndex, $closeParenthesisIndex); + $thirdArgumentTuple = $this->getThirdArgumentTokenTuple( + $tokens, + $openParenthesisIndex, + $closeParenthesisIndex + ); if ($thirdArgumentTuple === []) { return; } @@ -100,7 +104,7 @@ private function fixFunction(Tokens $tokens, int $functionIndex): void if ($argumentCount === 1) { $tokensToInsert[] = new Token(','); $tokensToInsert[] = new Token([T_WHITESPACE, ' ']); - $tokensToInsert[] = new Token([T_STRING, 'null']); + $tokensToInsert[] = new Token([T_STRING, "''"]); } // Add third argument (arg separator): ", '&'" diff --git a/tests/Fixer/Fixtures/Correct.php.inc b/tests/Fixer/Fixtures/Correct.php.inc index 4b9e293..726d568 100644 --- a/tests/Fixer/Fixtures/Correct.php.inc +++ b/tests/Fixer/Fixtures/Correct.php.inc @@ -6,20 +6,20 @@ class Correct { public function assembleQueryString(): void { - $queryString1 = http_build_query(['foo' => 'bar'], null, '&'); + $queryString1 = http_build_query(['foo' => 'bar'], '', '&'); - $queryString1WithMultipleWhitespaces = http_build_query(['foo' => 'bar'], null, '&'); + $queryString1WithMultipleWhitespaces = http_build_query(['foo' => 'bar'], 'prefix', '&'); - $queryString1WithWhitespacesAndComments = http_build_query(['foo' => 'bar'], null, /* comment */ '&' /* x */); + $queryString1WithWhitespacesAndComments = http_build_query(['foo' => 'bar'], '', /* comment */ '&' /* x */); - $queryString1WithComment = http_build_query(['foo' => 'bar'], /* Comment, with commas, */ null , '&'); + $queryString1WithComment = http_build_query(['foo' => 'bar'], /* Comment, with commas, */ '' , '&'); - $queryString1WithObject = http_build_query((object) ['foo' => 'bar'], null, '&'); + $queryString1WithObject = http_build_query((object) ['foo' => 'bar'], '', '&'); - $queryString2 = http_build_query(['foo' => 'bar', 'baz' => 'bat'], null, '&'); + $queryString2 = http_build_query(['foo' => 'bar', 'baz' => 'bat'], '', '&'); - $queryString3 = http_build_query(['foo' => 'bar'], null, '&'); + $queryString3 = http_build_query(['foo' => 'bar'], '', '&'); - $queryString4 = http_build_query(['foo' => 'bar'], null, ';'); + $queryString4 = http_build_query(['foo' => 'bar'], '', ';'); } } diff --git a/tests/Fixer/Fixtures/Fixed.php.inc b/tests/Fixer/Fixtures/Fixed.php.inc index 8a095a4..6f09b17 100644 --- a/tests/Fixer/Fixtures/Fixed.php.inc +++ b/tests/Fixer/Fixtures/Fixed.php.inc @@ -1,23 +1,30 @@ 'bar'], null, '&'); + $queryString1 = http_build_query(['foo' => 'bar'], '', '&'); - $queryString2 = http_build_query(['foo' => 'bar'], 333, '&'); + $queryString2 = http_build_query(['foo' => 'bar'], '', '&'); - $queryString3 = http_build_query(['foo' => 'bar'], 333, '&'); + $queryString3 = http_build_query(['foo' => 'bar'], '', '&'); - $queryString4 = http_build_query(['foo' => 'bar'], 333, '&', PHP_QUERY_RFC3986); + $queryString4 = http_build_query(['foo' => 'bar'], '', '&', PHP_QUERY_RFC3986); - $queryString5 = http_build_query(['foo' => 'bar'], null, '&', PHP_QUERY_RFC3986); + $queryString5 = http_build_query(['foo' => 'bar'], 'prefix', '&', PHP_QUERY_RFC3986); - $queryString6 = http_build_query(['foo' => 'bar', 'baz' => 'ban'], null, '&', PHP_QUERY_RFC3986); + $queryString6 = http_build_query(['foo' => 'bar', 'baz' => 'ban'], '', '&', PHP_QUERY_RFC3986); - $queryString6 = http_build_query((object) ['foo' => 'bar', 'baz' => 'ban'], null, '&', PHP_QUERY_RFC3986); + $queryString7 = http_build_query((object) ['foo' => 'bar', 'baz' => 'ban'], '', '&', PHP_QUERY_RFC3986); } diff --git a/tests/Fixer/Fixtures/Wrong.php.inc b/tests/Fixer/Fixtures/Wrong.php.inc index 40f8f5d..b176557 100644 --- a/tests/Fixer/Fixtures/Wrong.php.inc +++ b/tests/Fixer/Fixtures/Wrong.php.inc @@ -1,23 +1,30 @@ 'bar']); - $queryString2 = http_build_query(['foo' => 'bar'], 333); + $queryString2 = http_build_query(['foo' => 'bar'], ''); - $queryString3 = http_build_query(['foo' => 'bar'], 333, null); + $queryString3 = http_build_query(['foo' => 'bar'], '', null); - $queryString4 = http_build_query(['foo' => 'bar'], 333, null, PHP_QUERY_RFC3986); + $queryString4 = http_build_query(['foo' => 'bar'], '', null, PHP_QUERY_RFC3986); - $queryString5 = http_build_query(['foo' => 'bar'], null, null, PHP_QUERY_RFC3986); + $queryString5 = http_build_query(['foo' => 'bar'], 'prefix', null, PHP_QUERY_RFC3986); - $queryString6 = http_build_query(['foo' => 'bar', 'baz' => 'ban'], null, null, PHP_QUERY_RFC3986); + $queryString6 = http_build_query(['foo' => 'bar', 'baz' => 'ban'], '', null, PHP_QUERY_RFC3986); - $queryString6 = http_build_query((object) ['foo' => 'bar', 'baz' => 'ban'], null, null, PHP_QUERY_RFC3986); + $queryString7 = http_build_query((object) ['foo' => 'bar', 'baz' => 'ban'], '', null, PHP_QUERY_RFC3986); }