Skip to content

Commit 074622b

Browse files
committed
PEAR/FunctionDeclaration: ignore multi-line promoted properties
Similar to (multi-line) arrays in a multi-line function declaration, ignore (multi-line) attributes for the purposes of the indentation checks in this sniff. This does mean that inconsistent indentation within multi-line attributes/ for an attribute closer will not be fixed, but that should be handled by a dedicated attribute formatting sniff in my opinion. Includes unit tests. Fixes 3424
1 parent b10c327 commit 074622b

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,13 @@ public function processArgumentList($phpcsFile, $stackPtr, $indent, $type='funct
507507
$lastLine = $tokens[$i]['line'];
508508
continue;
509509
}
510+
511+
if ($tokens[$i]['code'] === T_ATTRIBUTE) {
512+
// Skip attributes as they have their own indentation rules.
513+
$i = $tokens[$i]['attribute_closer'];
514+
$lastLine = $tokens[$i]['line'];
515+
continue;
516+
}
510517
}//end for
511518

512519
}//end processArgumentList()

src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,47 @@ private string $private,
372372
) {
373373
}
374374
}
375+
376+
class ConstructorPropertyPromotionMultiLineAttributesOK
377+
public function __construct(
378+
#[ORM\ManyToOne(
379+
Something: true,
380+
SomethingElse: 'text',
381+
)]
382+
#[Groups([
383+
'ArrayEntry',
384+
'Another.ArrayEntry',
385+
])]
386+
#[MoreGroups(
387+
[
388+
'ArrayEntry',
389+
'Another.ArrayEntry',
390+
]
391+
)]
392+
private Type $property
393+
) {
394+
// Do something.
395+
}
396+
}
397+
398+
class ConstructorPropertyPromotionMultiLineAttributesIncorrectIndent
399+
public function __construct(
400+
#[ORM\ManyToOne(
401+
Something: true,
402+
SomethingElse: 'text',
403+
)]
404+
#[Groups([
405+
'ArrayEntry',
406+
'Another.ArrayEntry',
407+
])]
408+
#[MoreGroups(
409+
[
410+
'ArrayEntry',
411+
'Another.ArrayEntry',
412+
]
413+
)]
414+
private Type $property
415+
) {
416+
// Do something.
417+
}
418+
}

src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc.fixed

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,47 @@ class ConstructorPropertyPromotionMultiLineDocblockAndAttributeIncorrectIndent
370370
) {
371371
}
372372
}
373+
374+
class ConstructorPropertyPromotionMultiLineAttributesOK
375+
public function __construct(
376+
#[ORM\ManyToOne(
377+
Something: true,
378+
SomethingElse: 'text',
379+
)]
380+
#[Groups([
381+
'ArrayEntry',
382+
'Another.ArrayEntry',
383+
])]
384+
#[MoreGroups(
385+
[
386+
'ArrayEntry',
387+
'Another.ArrayEntry',
388+
]
389+
)]
390+
private Type $property
391+
) {
392+
// Do something.
393+
}
394+
}
395+
396+
class ConstructorPropertyPromotionMultiLineAttributesIncorrectIndent
397+
public function __construct(
398+
#[ORM\ManyToOne(
399+
Something: true,
400+
SomethingElse: 'text',
401+
)]
402+
#[Groups([
403+
'ArrayEntry',
404+
'Another.ArrayEntry',
405+
])]
406+
#[MoreGroups(
407+
[
408+
'ArrayEntry',
409+
'Another.ArrayEntry',
410+
]
411+
)]
412+
private Type $property
413+
) {
414+
// Do something.
415+
}
416+
}

src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public function getErrorList($testFile='FunctionDeclarationUnitTest.inc')
9797
369 => 1,
9898
370 => 1,
9999
371 => 1,
100+
400 => 1,
101+
404 => 1,
100102
];
101103
} else {
102104
$errors = [

0 commit comments

Comments
 (0)