Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEAR/Squiz/[MultiLine]FunctionDeclaration: allow for PHP 8.1 new in initializers #3787

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
),
) {
}
}