Skip to content

Commit b87dafd

Browse files
authored
Merge pull request #609 from PHPCSStandards/feature/608-squiz-multilinefunctiondeclarations-bug-fix-param-attributes
Squiz/MultiLineFunctionDeclaration: bug fix - skip over attributes
2 parents c0e8f4d + f39847a commit b87dafd

4 files changed

+115
-2
lines changed

src/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,21 @@ public function processBracket($phpcsFile, $openBracket, $tokens, $type='functio
227227
// Each line between the brackets should contain a single parameter.
228228
for ($i = ($openBracket + 1); $i < $closeBracket; $i++) {
229229
// Skip brackets, like arrays, as they can contain commas.
230-
if (isset($tokens[$i]['bracket_opener']) === true) {
230+
if (isset($tokens[$i]['bracket_closer']) === true) {
231231
$i = $tokens[$i]['bracket_closer'];
232232
continue;
233233
}
234234

235-
if (isset($tokens[$i]['parenthesis_opener']) === true) {
235+
if (isset($tokens[$i]['parenthesis_closer']) === true) {
236236
$i = $tokens[$i]['parenthesis_closer'];
237237
continue;
238238
}
239239

240+
if (isset($tokens[$i]['attribute_closer']) === true) {
241+
$i = $tokens[$i]['attribute_closer'];
242+
continue;
243+
}
244+
240245
if ($tokens[$i]['code'] !== T_COMMA) {
241246
continue;
242247
}

src/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.inc

+52
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,55 @@ new ExceptionMessage(),
302302
) {
303303
}
304304
}
305+
306+
// Issue #608 - multi-attributes are not handled correctly.
307+
function ParamWithMultiAttributeOnSameLine(
308+
#[AttributeA, AttributeB] string $param,
309+
) {
310+
}
311+
312+
function ParamWithMultiAttributeOnSameLineWithParamsShouldNotBeSeenAsMultipleFnParams(
313+
#[AttributeA(10, 'test'), AttributeB([1, 2, 3,])] string $param,
314+
) {
315+
}
316+
317+
function ParamWithMultiAttributeOnSameLine(
318+
#[AttributeA, AttributeB] string $paramA, int $paramB
319+
) {
320+
}
321+
322+
function ParamWithMultiAttributeOnSameLineWithParamsShouldNotBeSeenAsMultipleFnParams(
323+
#[AttributeA(10, 'test'), AttributeB([1, 2, 3,])] string $param, int $paramB
324+
) {
325+
}
326+
327+
function ParamWithAttributeOnOwnLineShouldNotBeSeenAsMultipleFnParams(
328+
#[Attribute]
329+
string $param,
330+
) {
331+
}
332+
333+
function ParamWithMultipleAttributesOnOwnLineShouldNotBeSeenAsMultipleFnParams(
334+
#[AttributeA]
335+
#[AttributeB]
336+
string $param,
337+
) {
338+
}
339+
340+
function ParamWithAttributeOnOwnLineWithParamsShouldNotBeSeenAsMultipleFnParamse(
341+
#[Attribute(10, 20)]
342+
string $param,
343+
) {
344+
}
345+
346+
function ParamWithMultiAttributeOnOwnLineShouldNotBeSeenAsMultipleFnParams(
347+
#[AttributeA, AttributeB]
348+
string $param,
349+
) {
350+
}
351+
352+
function ParamWithMultiAttributeOnOwnLineWithParamsShouldNotBeSeenAsMultipleFnParams(
353+
#[AttributeA(10, 'test'), AttributeB([1, 2, 3,])]
354+
string $param,
355+
) {
356+
}

src/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.inc.fixed

+54
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,57 @@ new ExceptionMessage(),
314314
) {
315315
}
316316
}
317+
318+
// Issue #608 - multi-attributes are not handled correctly.
319+
function ParamWithMultiAttributeOnSameLine(
320+
#[AttributeA, AttributeB] string $param,
321+
) {
322+
}
323+
324+
function ParamWithMultiAttributeOnSameLineWithParamsShouldNotBeSeenAsMultipleFnParams(
325+
#[AttributeA(10, 'test'), AttributeB([1, 2, 3,])] string $param,
326+
) {
327+
}
328+
329+
function ParamWithMultiAttributeOnSameLine(
330+
#[AttributeA, AttributeB] string $paramA,
331+
int $paramB
332+
) {
333+
}
334+
335+
function ParamWithMultiAttributeOnSameLineWithParamsShouldNotBeSeenAsMultipleFnParams(
336+
#[AttributeA(10, 'test'), AttributeB([1, 2, 3,])] string $param,
337+
int $paramB
338+
) {
339+
}
340+
341+
function ParamWithAttributeOnOwnLineShouldNotBeSeenAsMultipleFnParams(
342+
#[Attribute]
343+
string $param,
344+
) {
345+
}
346+
347+
function ParamWithMultipleAttributesOnOwnLineShouldNotBeSeenAsMultipleFnParams(
348+
#[AttributeA]
349+
#[AttributeB]
350+
string $param,
351+
) {
352+
}
353+
354+
function ParamWithAttributeOnOwnLineWithParamsShouldNotBeSeenAsMultipleFnParamse(
355+
#[Attribute(10, 20)]
356+
string $param,
357+
) {
358+
}
359+
360+
function ParamWithMultiAttributeOnOwnLineShouldNotBeSeenAsMultipleFnParams(
361+
#[AttributeA, AttributeB]
362+
string $param,
363+
) {
364+
}
365+
366+
function ParamWithMultiAttributeOnOwnLineWithParamsShouldNotBeSeenAsMultipleFnParams(
367+
#[AttributeA(10, 'test'), AttributeB([1, 2, 3,])]
368+
string $param,
369+
) {
370+
}

src/Standards/Squiz/Tests/Functions/MultiLineFunctionDeclarationUnitTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public function getErrorList($testFile='')
7575
252 => 1,
7676
253 => 1,
7777
254 => 1,
78+
318 => 1,
79+
323 => 1,
7880
];
7981
} else {
8082
$errors = [

0 commit comments

Comments
 (0)