Skip to content

Commit 84e97dc

Browse files
committed
Merge branch 'feature/3786-squiz/multilinefunctiondeclarations-allow-for-new-in-initializers' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents c1a30b1 + 6213153 commit 84e97dc

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,19 @@ public function processArgumentList($phpcsFile, $stackPtr, $indent, $type='funct
496496
$lastLine = $tokens[$i]['line'];
497497
}//end if
498498

499+
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS
500+
&& isset($tokens[$i]['parenthesis_closer']) === true
501+
) {
502+
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
503+
if ($tokens[$prevNonEmpty]['code'] !== T_USE) {
504+
// Since PHP 8.1, a default value can contain a class instantiation.
505+
// Skip over these "function calls" as they have their own indentation rules.
506+
$i = $tokens[$i]['parenthesis_closer'];
507+
$lastLine = $tokens[$i]['line'];
508+
continue;
509+
}
510+
}
511+
499512
if ($tokens[$i]['code'] === T_ARRAY || $tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
500513
// Skip arrays as they have their own indentation rules.
501514
if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,3 +418,50 @@ class ConstructorPropertyPromotionMultiLineAttributesIncorrectIndent
418418
// Do something.
419419
}
420420
}
421+
422+
// PHP 8.1: new in initializers means that class instantiations with parameters can occur in a function declaration.
423+
function usingNewInInitializersCallParamsIndented(
424+
int $paramA,
425+
string $paramB,
426+
object $paramC = new SomeClass(
427+
new InjectedDependencyA(),
428+
new InjectedDependencyB
429+
)
430+
) {}
431+
432+
function usingNewInInitializersCallParamsNotIndented(
433+
int $paramA,
434+
string $paramB,
435+
object $paramC = new SomeClass(
436+
new InjectedDependencyA,
437+
new InjectedDependencyB()
438+
)
439+
) {}
440+
441+
function usingNewInInitializersCallParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
442+
int $paramA,
443+
string $paramB,
444+
object $paramC = new SomeClass(
445+
new InjectedDependencyA(), new InjectedDependencyB()
446+
)
447+
) {}
448+
449+
class UsingNewInInitializers {
450+
public function doSomething(
451+
object $paramA,
452+
stdClass $paramB = new stdClass(),
453+
Exception $paramC = new Exception(
454+
new ExceptionMessage(),
455+
new ExceptionCode(),
456+
),
457+
) {
458+
}
459+
460+
public function callParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
461+
Exception $param = new Exception(
462+
new ExceptionMessage(),
463+
new ExceptionCode(),
464+
),
465+
) {
466+
}
467+
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,50 @@ class ConstructorPropertyPromotionMultiLineAttributesIncorrectIndent
416416
// Do something.
417417
}
418418
}
419+
420+
// PHP 8.1: new in initializers means that class instantiations with parameters can occur in a function declaration.
421+
function usingNewInInitializersCallParamsIndented(
422+
int $paramA,
423+
string $paramB,
424+
object $paramC = new SomeClass(
425+
new InjectedDependencyA(),
426+
new InjectedDependencyB
427+
)
428+
) {}
429+
430+
function usingNewInInitializersCallParamsNotIndented(
431+
int $paramA,
432+
string $paramB,
433+
object $paramC = new SomeClass(
434+
new InjectedDependencyA,
435+
new InjectedDependencyB()
436+
)
437+
) {}
438+
439+
function usingNewInInitializersCallParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
440+
int $paramA,
441+
string $paramB,
442+
object $paramC = new SomeClass(
443+
new InjectedDependencyA(), new InjectedDependencyB()
444+
)
445+
) {}
446+
447+
class UsingNewInInitializers {
448+
public function doSomething(
449+
object $paramA,
450+
stdClass $paramB = new stdClass(),
451+
Exception $paramC = new Exception(
452+
new ExceptionMessage(),
453+
new ExceptionCode(),
454+
),
455+
) {
456+
}
457+
458+
public function callParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
459+
Exception $param = new Exception(
460+
new ExceptionMessage(),
461+
new ExceptionCode(),
462+
),
463+
) {
464+
}
465+
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,50 @@ private string $private,
255255
) {
256256
}
257257
}
258+
259+
// PHP 8.1: new in initializers means that class instantiations with parameters can occur in a function declaration.
260+
function usingNewInInitializersCallParamsIndented(
261+
int $paramA,
262+
string $paramB,
263+
object $paramC = new SomeClass(
264+
new InjectedDependencyA(),
265+
new InjectedDependencyB
266+
)
267+
) {}
268+
269+
function usingNewInInitializersCallParamsNotIndented(
270+
int $paramA,
271+
string $paramB,
272+
object $paramC = new SomeClass(
273+
new InjectedDependencyA,
274+
new InjectedDependencyB()
275+
)
276+
) {}
277+
278+
function usingNewInInitializersCallParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
279+
int $paramA,
280+
string $paramB,
281+
object $paramC = new SomeClass(
282+
new InjectedDependencyA(), new InjectedDependencyB()
283+
)
284+
) {}
285+
286+
class UsingNewInInitializers {
287+
public function doSomething(
288+
object $paramA,
289+
stdClass $paramB = new stdClass(),
290+
Exception $paramC = new Exception(
291+
new ExceptionMessage(),
292+
new ExceptionCode(),
293+
),
294+
) {
295+
}
296+
297+
public function callParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
298+
Exception $param = new Exception(
299+
new ExceptionMessage(),
300+
new ExceptionCode(),
301+
),
302+
) {
303+
}
304+
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,50 @@ class ConstructorPropertyPromotionMultiLineDocblockAndAttributeIncorrectIndent
267267
) {
268268
}
269269
}
270+
271+
// PHP 8.1: new in initializers means that class instantiations with parameters can occur in a function declaration.
272+
function usingNewInInitializersCallParamsIndented(
273+
int $paramA,
274+
string $paramB,
275+
object $paramC = new SomeClass(
276+
new InjectedDependencyA(),
277+
new InjectedDependencyB
278+
)
279+
) {}
280+
281+
function usingNewInInitializersCallParamsNotIndented(
282+
int $paramA,
283+
string $paramB,
284+
object $paramC = new SomeClass(
285+
new InjectedDependencyA,
286+
new InjectedDependencyB()
287+
)
288+
) {}
289+
290+
function usingNewInInitializersCallParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
291+
int $paramA,
292+
string $paramB,
293+
object $paramC = new SomeClass(
294+
new InjectedDependencyA(), new InjectedDependencyB()
295+
)
296+
) {}
297+
298+
class UsingNewInInitializers {
299+
public function doSomething(
300+
object $paramA,
301+
stdClass $paramB = new stdClass(),
302+
Exception $paramC = new Exception(
303+
new ExceptionMessage(),
304+
new ExceptionCode(),
305+
),
306+
) {
307+
}
308+
309+
public function callParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
310+
Exception $param = new Exception(
311+
new ExceptionMessage(),
312+
new ExceptionCode(),
313+
),
314+
) {
315+
}
316+
}

0 commit comments

Comments
 (0)