Skip to content
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
55 changes: 40 additions & 15 deletions CakePHP/Sniffs/Classes/ReturnTypeHintSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,40 +41,67 @@ public function process(File $phpcsFile, $stackPtr)
$closeParenthesisIndex = $tokens[$openParenthesisIndex]['parenthesis_closer'];

$colonIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $closeParenthesisIndex + 1, null, true);
if (!$colonIndex) {

if (!$this->isChainingMethod($phpcsFile, $stackPtr)) {
if ($colonIndex && $tokens[$colonIndex]['code'] === T_COLON) {
$this->assertNotThisOrStatic($phpcsFile, $stackPtr);
}

return;
}

$startIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $colonIndex + 1, $colonIndex + 3, true);
if (!$startIndex) {
// We skip for interface methods
if (empty($tokens[$stackPtr]['scope_opener']) || empty($tokens[$stackPtr]['scope_closer'])) {
return;
}

if (!$this->isChainingMethod($phpcsFile, $stackPtr)) {
$this->assertNotThisOrStatic($phpcsFile, $stackPtr);
// No colon means no return type hint - add static
if (!$colonIndex || $tokens[$colonIndex]['code'] !== T_COLON) {
$fix = $phpcsFile->addFixableError(
'Chaining methods (@return $this) should have "static" return type.',
$closeParenthesisIndex,
'MissingStatic',
);
if (!$fix) {
return;
}

$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->addContent($closeParenthesisIndex, ': static');
$phpcsFile->fixer->endChangeset();

return;
}

// We skip for interface methods
if (empty($tokens[$stackPtr]['scope_opener']) || empty($tokens[$stackPtr]['scope_closer'])) {
$startIndex = $phpcsFile->findNext(Tokens::$emptyTokens, $colonIndex + 1, $colonIndex + 3, true);
if (!$startIndex) {
return;
}

$returnTokenCode = $tokens[$startIndex]['code'];
if ($returnTokenCode === T_STATIC) {
return;
}

if ($returnTokenCode !== T_SELF) {
// Then we can only warn, but not auto-fix
$phpcsFile->addError(
'Chaining methods (@return $this) should not have any return-type-hint.',
$fix = $phpcsFile->addFixableError(
'Chaining methods (@return $this) should have "static" return type.',
$startIndex,
'InvalidSelf',
);
if (!$fix) {
return;
}

$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($startIndex, 'static');
$phpcsFile->fixer->endChangeset();

return;
}

$fix = $phpcsFile->addFixableError(
'Chaining methods (@return $this) should not have any return-type-hint (Remove "self").',
'Chaining methods (@return $this) should have "static" return type instead of "self".',
$startIndex,
'InvalidSelf',
);
Expand All @@ -83,9 +110,7 @@ public function process(File $phpcsFile, $stackPtr)
}

$phpcsFile->fixer->beginChangeset();
for ($i = $colonIndex; $i <= $startIndex; $i++) {
$phpcsFile->fixer->replaceToken($i, '');
}
$phpcsFile->fixer->replaceToken($startIndex, 'static');
$phpcsFile->fixer->endChangeset();
}

Expand Down Expand Up @@ -173,7 +198,7 @@ protected function assertNotThisOrStatic(File $phpCsFile, int $stackPointer): vo
}

$phpCsFile->addError(
'Class name repeated, expected `self` or `$this`.',
'Class name repeated, expected `static` or `$this`.',
$classNameIndex,
'InvalidClass',
);
Expand Down
11 changes: 9 additions & 2 deletions CakePHP/Tests/Classes/ReturnTypeHintUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ class Foo
/**
* @return $this
*/
public function correct()
public function correct(): static
{
}

/**
* @return $this
*/
public function incorrect(): Foo
public function incorrect()
{
}

/**
* @return $this
*/
public function incorrectClass(): Foo
{
}

Expand Down
13 changes: 10 additions & 3 deletions CakePHP/Tests/Classes/ReturnTypeHintUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@ class Foo
/**
* @return $this
*/
public function correct()
public function correct(): static
{
}

/**
* @return $this
*/
public function incorrect(): Foo
public function incorrect(): static
{
}

/**
* @return $this
*/
public function incorrectSelf()
public function incorrectClass(): static
{
}

/**
* @return $this
*/
public function incorrectSelf(): static
{
}
}
1 change: 1 addition & 0 deletions CakePHP/Tests/Classes/ReturnTypeHintUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function getErrorList()
return [
16 => 1,
23 => 1,
30 => 1,
];
}

Expand Down
Loading