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

Commit b424f7a

Browse files
committed
Fix: SpecifyArgSeparatorFixer type error in PHP 7.4+ (fixes alma-oss#72)
1 parent 00724a1 commit b424f7a

File tree

5 files changed

+48
-29
lines changed

5 files changed

+48
-29
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 type error when adding empty `$numeric_prefix` parameter in PHP 7.4+.
89

910
## 3.2.0 - 2021-05-20
1011
- Fix SpecifyArgSeparatorFixer not compatible with php-cs-fixer 3.0.

src/Fixer/SpecifyArgSeparatorFixer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ private function fixFunction(Tokens $tokens, int $functionIndex): void
8181

8282
// When third argument is already present and it is null, override its value.
8383
if ($argumentCount >= 3) {
84-
$thirdArgumentTuple = $this->getThirdArgumentTokenTuple($tokens, $openParenthesisIndex, $closeParenthesisIndex);
84+
$thirdArgumentTuple = $this->getThirdArgumentTokenTuple(
85+
$tokens,
86+
$openParenthesisIndex,
87+
$closeParenthesisIndex
88+
);
8589
if ($thirdArgumentTuple === []) {
8690
return;
8791
}
@@ -100,7 +104,7 @@ private function fixFunction(Tokens $tokens, int $functionIndex): void
100104
if ($argumentCount === 1) {
101105
$tokensToInsert[] = new Token(',');
102106
$tokensToInsert[] = new Token([T_WHITESPACE, ' ']);
103-
$tokensToInsert[] = new Token([T_STRING, 'null']);
107+
$tokensToInsert[] = new Token([T_STRING, "''"]);
104108
}
105109

106110
// Add third argument (arg separator): ", '&'"

tests/Fixer/Fixtures/Correct.php.inc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ class Correct
66
{
77
public function assembleQueryString(): void
88
{
9-
$queryString1 = http_build_query(['foo' => 'bar'], null, '&');
9+
$queryString1 = http_build_query(['foo' => 'bar'], '', '&');
1010

11-
$queryString1WithMultipleWhitespaces = http_build_query(['foo' => 'bar'], null, '&');
11+
$queryString1WithMultipleWhitespaces = http_build_query(['foo' => 'bar'], 'prefix', '&');
1212

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

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

17-
$queryString1WithObject = http_build_query((object) ['foo' => 'bar'], null, '&');
17+
$queryString1WithObject = http_build_query((object) ['foo' => 'bar'], '', '&');
1818

19-
$queryString2 = http_build_query(['foo' => 'bar', 'baz' => 'bat'], null, '&');
19+
$queryString2 = http_build_query(['foo' => 'bar', 'baz' => 'bat'], '', '&');
2020

21-
$queryString3 = http_build_query(['foo' => 'bar'], null, '&amp;');
21+
$queryString3 = http_build_query(['foo' => 'bar'], '', '&amp;');
2222

23-
$queryString4 = http_build_query(['foo' => 'bar'], null, ';');
23+
$queryString4 = http_build_query(['foo' => 'bar'], '', ';');
2424
}
2525
}

tests/Fixer/Fixtures/Fixed.php.inc

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
<?php declare(strict_types=1);
22

3-
function http_build_query($should, $not, $change): void
4-
{
5-
// this should not be affected
3+
/**
4+
* The function declaration should not be affected
5+
*/
6+
function http_build_query(
7+
$data, // TODO: array|object in PHP 8.0+
8+
string $numeric_prefix = '',
9+
?string $arg_separator = null,
10+
int $encoding_type = PHP_QUERY_RFC1738
11+
): string {
12+
return 'foo';
613
}
714

815
function assembleQueryString(): void
916
{
10-
$queryString1 = http_build_query(['foo' => 'bar'], null, '&');
17+
$queryString1 = http_build_query(['foo' => 'bar'], '', '&');
1118

12-
$queryString2 = http_build_query(['foo' => 'bar'], 333, '&');
19+
$queryString2 = http_build_query(['foo' => 'bar'], '', '&');
1320

14-
$queryString3 = http_build_query(['foo' => 'bar'], 333, '&');
21+
$queryString3 = http_build_query(['foo' => 'bar'], '', '&');
1522

16-
$queryString4 = http_build_query(['foo' => 'bar'], 333, '&', PHP_QUERY_RFC3986);
23+
$queryString4 = http_build_query(['foo' => 'bar'], '', '&', PHP_QUERY_RFC3986);
1724

18-
$queryString5 = http_build_query(['foo' => 'bar'], null, '&', PHP_QUERY_RFC3986);
25+
$queryString5 = http_build_query(['foo' => 'bar'], 'prefix', '&', PHP_QUERY_RFC3986);
1926

20-
$queryString6 = http_build_query(['foo' => 'bar', 'baz' => 'ban'], null, '&', PHP_QUERY_RFC3986);
27+
$queryString6 = http_build_query(['foo' => 'bar', 'baz' => 'ban'], '', '&', PHP_QUERY_RFC3986);
2128

22-
$queryString6 = http_build_query((object) ['foo' => 'bar', 'baz' => 'ban'], null, '&', PHP_QUERY_RFC3986);
29+
$queryString7 = http_build_query((object) ['foo' => 'bar', 'baz' => 'ban'], '', '&', PHP_QUERY_RFC3986);
2330
}

tests/Fixer/Fixtures/Wrong.php.inc

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
<?php declare(strict_types=1);
22

3-
function http_build_query($should, $not, $change): void
4-
{
5-
// this should not be affected
3+
/**
4+
* The function declaration should not be affected
5+
*/
6+
function http_build_query(
7+
$data, // TODO: array|object in PHP 8.0+
8+
string $numeric_prefix = '',
9+
?string $arg_separator = null,
10+
int $encoding_type = PHP_QUERY_RFC1738
11+
): string {
12+
return 'foo';
613
}
714

815
function assembleQueryString(): void
916
{
1017
$queryString1 = http_build_query(['foo' => 'bar']);
1118

12-
$queryString2 = http_build_query(['foo' => 'bar'], 333);
19+
$queryString2 = http_build_query(['foo' => 'bar'], '');
1320

14-
$queryString3 = http_build_query(['foo' => 'bar'], 333, null);
21+
$queryString3 = http_build_query(['foo' => 'bar'], '', null);
1522

16-
$queryString4 = http_build_query(['foo' => 'bar'], 333, null, PHP_QUERY_RFC3986);
23+
$queryString4 = http_build_query(['foo' => 'bar'], '', null, PHP_QUERY_RFC3986);
1724

18-
$queryString5 = http_build_query(['foo' => 'bar'], null, null, PHP_QUERY_RFC3986);
25+
$queryString5 = http_build_query(['foo' => 'bar'], 'prefix', null, PHP_QUERY_RFC3986);
1926

20-
$queryString6 = http_build_query(['foo' => 'bar', 'baz' => 'ban'], null, null, PHP_QUERY_RFC3986);
27+
$queryString6 = http_build_query(['foo' => 'bar', 'baz' => 'ban'], '', null, PHP_QUERY_RFC3986);
2128

22-
$queryString6 = http_build_query((object) ['foo' => 'bar', 'baz' => 'ban'], null, null, PHP_QUERY_RFC3986);
29+
$queryString7 = http_build_query((object) ['foo' => 'bar', 'baz' => 'ban'], '', null, PHP_QUERY_RFC3986);
2330
}

0 commit comments

Comments
 (0)