Skip to content

Commit 792a2cf

Browse files
authored
Merge pull request #455 from rodrigoprimo/test-coverage-disallow-long-array-syntax
Generic/DisallowLongArraySyntax: improve code coverage
2 parents a2c048b + e1f0e46 commit 792a2cf

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

src/Standards/Generic/Sniffs/Arrays/DisallowLongArraySyntaxSniff.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ public function process(File $phpcsFile, $stackPtr)
4545

4646
$error = 'Short array syntax must be used to define arrays';
4747

48-
if (isset($tokens[$stackPtr]['parenthesis_opener']) === false
49-
|| isset($tokens[$stackPtr]['parenthesis_closer']) === false
50-
) {
48+
if (isset($tokens[$stackPtr]['parenthesis_opener'], $tokens[$stackPtr]['parenthesis_closer']) === false) {
5149
// Live coding/parse error, just show the error, don't try and fix it.
5250
$phpcsFile->addError($error, $stackPtr, 'Found');
5351
return;
@@ -61,13 +59,9 @@ public function process(File $phpcsFile, $stackPtr)
6159

6260
$phpcsFile->fixer->beginChangeset();
6361

64-
if ($opener === null) {
65-
$phpcsFile->fixer->replaceToken($stackPtr, '[]');
66-
} else {
67-
$phpcsFile->fixer->replaceToken($stackPtr, '');
68-
$phpcsFile->fixer->replaceToken($opener, '[');
69-
$phpcsFile->fixer->replaceToken($closer, ']');
70-
}
62+
$phpcsFile->fixer->replaceToken($stackPtr, '');
63+
$phpcsFile->fixer->replaceToken($opener, '[');
64+
$phpcsFile->fixer->replaceToken($closer, ']');
7165

7266
$phpcsFile->fixer->endChangeset();
7367
}

src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ $var = array();
33
$var = [1,2,3];
44
$var = array(1,2,3);
55
echo $var[1];
6-
$foo = array($var[1],$var[2]);
6+
$foo = ARRAY($var[1],$var[2]);
77
$foo = array(
88
1,
99
2,
1010
3
1111
);
1212
$var = array/*comment*/(1,2,3);
13-
$var = array;
1413

1514
function foo(array $array) {}
1615

@@ -26,3 +25,9 @@ array_map(
2625
static fn (array $value): array => array_filter($value),
2726
[]
2827
);
28+
29+
class Foo {
30+
function array() {}
31+
}
32+
33+
$obj->array( 1, 2, 3 );

src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.1.inc.fixed

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ $foo = [
1010
3
1111
];
1212
$var = /*comment*/[1,2,3];
13-
$var = array;
1413

1514
function foo(array $array) {}
1615

@@ -26,3 +25,9 @@ array_map(
2625
static fn (array $value): array => array_filter($value),
2726
[]
2827
);
28+
29+
class Foo {
30+
function array() {}
31+
}
32+
33+
$obj->array( 1, 2, 3 );
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// Intentional parse error (long array syntax missing parentheses).
4+
// This should be the only test in this file.
5+
// Testing that the sniff is *not* triggered.
6+
7+
$var = array

src/Standards/Generic/Tests/Arrays/DisallowLongArraySyntaxUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public function getErrorList($testFile='')
4646
2 => 1,
4747
9 => 1,
4848
];
49+
case 'DisallowLongArraySyntaxUnitTest.3.inc':
50+
return [
51+
7 => 1,
52+
];
4953
default:
5054
return [];
5155
}//end switch

0 commit comments

Comments
 (0)