Skip to content

Commit

Permalink
Remove useless property, parameter, and return annotations (#16)
Browse files Browse the repository at this point in the history
* Detect useless property annotation

* Detect useless return annotation

* Detect useless parameter annotation
  • Loading branch information
asispts committed Nov 3, 2023
1 parent 7510051 commit e9ce7c5
Show file tree
Hide file tree
Showing 14 changed files with 326 additions and 11 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
5 changes: 5 additions & 0 deletions ptscs/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@
<rule ref="SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure" />
<rule ref="SlevomatCodingStandard.Commenting.DisallowOneLinePropertyDocComment" />

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


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

namespace Ptscs\Tests\Sniffs\Slevomat\TypeHints;

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

final class ParameterTypeHintTest 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.ParameterTypeHint.UselessAnnotation'),
],
];
}
}
25 changes: 25 additions & 0 deletions tests/Sniffs/Slevomat/TypeHints/PropertyTypeHintTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

namespace Ptscs\Tests\Sniffs\Slevomat\TypeHints;

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

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

public static function provideTestData(): Iterator
{
yield [
[
new ErrorData(10, 'SlevomatCodingStandard.TypeHints.PropertyTypeHint.UselessAnnotation'),
],
];
}
}
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')],
];
}
}
26 changes: 26 additions & 0 deletions tests/Sniffs/Slevomat/TypeHints/UselessDocblockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace Ptscs\Tests\Sniffs\Slevomat\TypeHints;

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

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

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

namespace App;

final class Foobar
{
/**
*/
public function check(string $useless, array $list, $missing): void
{
}
}
13 changes: 13 additions & 0 deletions tests/Sniffs/Slevomat/TypeHints/_data/ParameterTypeHint.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace App;

final class Foobar
{
/**
* @param string $useless
*/
public function check(string $useless, array $list, $missing): void
{
}
}
48 changes: 48 additions & 0 deletions tests/Sniffs/Slevomat/TypeHints/_data/PropertyTypeHint.php.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

namespace App;

use Symfony\Component\Validator\Constraints as Assert;

final class Foobar
{
private string $useless;

/**
* @Assert\NotBlank
*/
private $missing;

/**
* Description
*
*/
private string $value1;

private ?int $value2 = null;

private string|int|float $value3;

/**
* @var string[]
*/
private array $value4;

/**
* @var string The hashed password
*/
private string $password;

private $inferMissing;

/**
* @var int
*/
private $inferUseless;

public function __construct(string $inferMissing, int $inferUseless)
{
$this->inferMissing = $inferMissing;
$this->inferUseless = $inferUseless;
}
}
51 changes: 51 additions & 0 deletions tests/Sniffs/Slevomat/TypeHints/_data/PropertyTypeHint.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

namespace App;

use Symfony\Component\Validator\Constraints as Assert;

final class Foobar
{
/**
* @var string
*/
private string $useless;

/**
* @Assert\NotBlank
*/
private $missing;

/**
* Description
*
*/
private string $value1;

private ?int $value2 = null;

private string|int|float $value3;

/**
* @var string[]
*/
private array $value4;

/**
* @var string The hashed password
*/
private string $password;

private $inferMissing;

/**
* @var int
*/
private $inferUseless;

public function __construct(string $inferMissing, int $inferUseless)
{
$this->inferMissing = $inferMissing;
$this->inferUseless = $inferUseless;
}
}
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';
}
}
26 changes: 26 additions & 0 deletions tests/Sniffs/Slevomat/TypeHints/_data/UselessDocblock.php.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);

namespace App;

final class Foobar
{
/**
* @var string
*/
private $value;

/**
* Class constructor.
*/
public function __construct(string $value)
{
$this->value = $value;
}

/**
*/
public function getValue(): string
{
return $this->value;
}
}
28 changes: 28 additions & 0 deletions tests/Sniffs/Slevomat/TypeHints/_data/UselessDocblock.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace App;

final class Foobar
{
/**
* @var string
*/
private $value;

/**
* Class constructor.
* @param string $value
*/
public function __construct(string $value)
{
$this->value = $value;
}

/**
* @return string
*/
public function getValue(): string
{
return $this->value;
}
}

0 comments on commit e9ce7c5

Please sign in to comment.