Skip to content

Commit

Permalink
Merge branch 'feature/3786-squiz/multilinefunctiondeclarations-allow-…
Browse files Browse the repository at this point in the history
…for-new-in-initializers' of https://github.com/jrfnl/PHP_CodeSniffer
  • Loading branch information
gsherwood committed May 4, 2023
2 parents c1a30b1 + 6213153 commit 84e97dc
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Standards/PEAR/Sniffs/Functions/FunctionDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,19 @@ public function processArgumentList($phpcsFile, $stackPtr, $indent, $type='funct
$lastLine = $tokens[$i]['line'];
}//end if

if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS
&& isset($tokens[$i]['parenthesis_closer']) === true
) {
$prevNonEmpty = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($i - 1), null, true);
if ($tokens[$prevNonEmpty]['code'] !== T_USE) {
// Since PHP 8.1, a default value can contain a class instantiation.
// Skip over these "function calls" as they have their own indentation rules.
$i = $tokens[$i]['parenthesis_closer'];
$lastLine = $tokens[$i]['line'];
continue;
}
}

if ($tokens[$i]['code'] === T_ARRAY || $tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
// Skip arrays as they have their own indentation rules.
if ($tokens[$i]['code'] === T_OPEN_SHORT_ARRAY) {
Expand Down
47 changes: 47 additions & 0 deletions src/Standards/PEAR/Tests/Functions/FunctionDeclarationUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,50 @@ class ConstructorPropertyPromotionMultiLineAttributesIncorrectIndent
// Do something.
}
}

// PHP 8.1: new in initializers means that class instantiations with parameters can occur in a function declaration.
function usingNewInInitializersCallParamsIndented(
int $paramA,
string $paramB,
object $paramC = new SomeClass(
new InjectedDependencyA(),
new InjectedDependencyB
)
) {}

function usingNewInInitializersCallParamsNotIndented(
int $paramA,
string $paramB,
object $paramC = new SomeClass(
new InjectedDependencyA,
new InjectedDependencyB()
)
) {}

function usingNewInInitializersCallParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
int $paramA,
string $paramB,
object $paramC = new SomeClass(
new InjectedDependencyA(), new InjectedDependencyB()
)
) {}

class UsingNewInInitializers {
public function doSomething(
object $paramA,
stdClass $paramB = new stdClass(),
Exception $paramC = new Exception(
new ExceptionMessage(),
new ExceptionCode(),
),
) {
}

public function callParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
Exception $param = new Exception(
new ExceptionMessage(),
new ExceptionCode(),
),
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,50 @@ class ConstructorPropertyPromotionMultiLineAttributesIncorrectIndent
// Do something.
}
}

// PHP 8.1: new in initializers means that class instantiations with parameters can occur in a function declaration.
function usingNewInInitializersCallParamsIndented(
int $paramA,
string $paramB,
object $paramC = new SomeClass(
new InjectedDependencyA(),
new InjectedDependencyB
)
) {}

function usingNewInInitializersCallParamsNotIndented(
int $paramA,
string $paramB,
object $paramC = new SomeClass(
new InjectedDependencyA,
new InjectedDependencyB()
)
) {}

function usingNewInInitializersCallParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
int $paramA,
string $paramB,
object $paramC = new SomeClass(
new InjectedDependencyA(), new InjectedDependencyB()
)
) {}

class UsingNewInInitializers {
public function doSomething(
object $paramA,
stdClass $paramB = new stdClass(),
Exception $paramC = new Exception(
new ExceptionMessage(),
new ExceptionCode(),
),
) {
}

public function callParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
Exception $param = new Exception(
new ExceptionMessage(),
new ExceptionCode(),
),
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,50 @@ private string $private,
) {
}
}

// PHP 8.1: new in initializers means that class instantiations with parameters can occur in a function declaration.
function usingNewInInitializersCallParamsIndented(
int $paramA,
string $paramB,
object $paramC = new SomeClass(
new InjectedDependencyA(),
new InjectedDependencyB
)
) {}

function usingNewInInitializersCallParamsNotIndented(
int $paramA,
string $paramB,
object $paramC = new SomeClass(
new InjectedDependencyA,
new InjectedDependencyB()
)
) {}

function usingNewInInitializersCallParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
int $paramA,
string $paramB,
object $paramC = new SomeClass(
new InjectedDependencyA(), new InjectedDependencyB()
)
) {}

class UsingNewInInitializers {
public function doSomething(
object $paramA,
stdClass $paramB = new stdClass(),
Exception $paramC = new Exception(
new ExceptionMessage(),
new ExceptionCode(),
),
) {
}

public function callParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
Exception $param = new Exception(
new ExceptionMessage(),
new ExceptionCode(),
),
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,50 @@ class ConstructorPropertyPromotionMultiLineDocblockAndAttributeIncorrectIndent
) {
}
}

// PHP 8.1: new in initializers means that class instantiations with parameters can occur in a function declaration.
function usingNewInInitializersCallParamsIndented(
int $paramA,
string $paramB,
object $paramC = new SomeClass(
new InjectedDependencyA(),
new InjectedDependencyB
)
) {}

function usingNewInInitializersCallParamsNotIndented(
int $paramA,
string $paramB,
object $paramC = new SomeClass(
new InjectedDependencyA,
new InjectedDependencyB()
)
) {}

function usingNewInInitializersCallParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
int $paramA,
string $paramB,
object $paramC = new SomeClass(
new InjectedDependencyA(), new InjectedDependencyB()
)
) {}

class UsingNewInInitializers {
public function doSomething(
object $paramA,
stdClass $paramB = new stdClass(),
Exception $paramC = new Exception(
new ExceptionMessage(),
new ExceptionCode(),
),
) {
}

public function callParamsIncorrectlyIndentedShouldNotBeFlaggedNorFixed(
Exception $param = new Exception(
new ExceptionMessage(),
new ExceptionCode(),
),
) {
}
}

0 comments on commit 84e97dc

Please sign in to comment.