Skip to content

Commit

Permalink
Detect useless return annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
asispts committed Nov 3, 2023
1 parent 8869408 commit 18d60a4
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 13 deletions.
18 changes: 7 additions & 11 deletions ptscs/Sniffs/PSR12/FileHeaderSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ final class FileHeaderSniff implements Sniff
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
* @return int[]
*/
public function register()
public function register(): array
{
return [\T_OPEN_TAG];
}
Expand All @@ -33,9 +33,8 @@ public function register()
* @param int $stackPtr The position of the current
* token in the stack.
*
* @return int|null
*/
public function process(File $phpcsFile, $stackPtr)
public function process(File $phpcsFile, $stackPtr): ?int
{
$tokens = $phpcsFile->getTokens();

Expand All @@ -49,7 +48,7 @@ public function process(File $phpcsFile, $stackPtr)
$headerLines = $this->getHeaderLines($phpcsFile, $openTag);
if (empty($headerLines) === true && $openTag === $stackPtr) {
// No content in the file.
return;
return null;
}

$possibleHeaders[$openTag] = $headerLines;
Expand Down Expand Up @@ -123,7 +122,7 @@ public function process(File $phpcsFile, $stackPtr)
*
* @return array
*/
public function getHeaderLines(File $phpcsFile, $stackPtr)
public function getHeaderLines(File $phpcsFile, $stackPtr): array
{
$tokens = $phpcsFile->getTokens();

Expand Down Expand Up @@ -276,12 +275,9 @@ public function getHeaderLines(File $phpcsFile, $stackPtr)
* Check the spacing and grouping of the statements inside each header block.
*
* @param File $phpcsFile The file being scanned.
* @param array $headerLines Header information, as sourced
* from getHeaderLines().
*
* @return int|null
* @param array $headerLines Header information, as sourced from getHeaderLines().
*/
public function processHeaderLines(File $phpcsFile, $headerLines)
public function processHeaderLines(File $phpcsFile, $headerLines): void
{
$tokens = $phpcsFile->getTokens();

Expand Down
1 change: 1 addition & 0 deletions ptscs/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@

<!-- Useless typehint -->
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint.UselessAnnotation" />
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation" />


<!--++++++++++++++++++++++++++++++++++++
Expand Down
23 changes: 23 additions & 0 deletions tests/Sniffs/Slevomat/TypeHints/ReturnTypeHintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace Ptscs\Tests\Sniffs\Slevomat\TypeHints;

use Iterator;
use Ptscs\Tests\SniffTestCase;
use Ptscs\Tests\Utils\ErrorData;

final class ReturnTypeHintTest extends SniffTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->appendExclude('Squiz.Classes.ClassFileName.NoMatch');
}

public static function provideTestData(): Iterator
{
yield [
[ new ErrorData(8, 'SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation')],
];
}
}
5 changes: 4 additions & 1 deletion tests/Sniffs/Slevomat/TypeHints/UselessDocblockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Iterator;
use Ptscs\Tests\SniffTestCase;
use Ptscs\Tests\Utils\ErrorData;

final class UselessDocblockTest extends SniffTestCase
{
Expand All @@ -15,6 +16,8 @@ protected function setUp(): void

public static function provideTestData(): Iterator
{
yield [];
yield [
[ new ErrorData(22, 'SlevomatCodingStandard.TypeHints.ReturnTypeHint.UselessAnnotation')],
];
}
}
18 changes: 18 additions & 0 deletions tests/Sniffs/Slevomat/TypeHints/_data/ReturnTypeHint.php.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace App;

final class Foobar
{
/**
*/
public function useless(): string
{
return $this->value;
}

public function missing()
{
return 'missing';
}
}
19 changes: 19 additions & 0 deletions tests/Sniffs/Slevomat/TypeHints/_data/ReturnTypeHint.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace App;

final class Foobar
{
/**
* @return string
*/
public function useless(): string
{
return $this->value;
}

public function missing()
{
return 'missing';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ final class Foobar
}

/**
* @return string
*/
public function getValue(): string
{
Expand Down

0 comments on commit 18d60a4

Please sign in to comment.