Skip to content

Commit

Permalink
Detect useless property annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
asispts committed Nov 3, 2023
1 parent 7510051 commit 8869408
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ptscs/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@
<rule ref="SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure" />
<rule ref="SlevomatCodingStandard.Commenting.DisallowOneLinePropertyDocComment" />

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


<!--++++++++++++++++++++++++++++++++++++
Code analysis
Expand Down
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'),
],
];
}
}
20 changes: 20 additions & 0 deletions tests/Sniffs/Slevomat/TypeHints/UselessDocblockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace Ptscs\Tests\Sniffs\Slevomat\TypeHints;

use Iterator;
use Ptscs\Tests\SniffTestCase;

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

public static function provideTestData(): Iterator
{
yield [];
}
}
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;
}
}
28 changes: 28 additions & 0 deletions tests/Sniffs/Slevomat/TypeHints/_data/UselessDocblock.php.fixed
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;
}
}
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 8869408

Please sign in to comment.