Skip to content

Commit

Permalink
RequireConstructorPropertyPromotionSniff: Fixed false positive
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Jul 29, 2021
1 parent 28351a2 commit 122a9bf
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
use function strtolower;
use function substr;
use function trim;
use const T_ATTRIBUTE_END;
use const T_BITWISE_AND;
use const T_CALLABLE;
use const T_CLOSE_CURLY_BRACKET;
use const T_COMMA;
use const T_ELLIPSIS;
use const T_ELSE;
Expand All @@ -31,6 +33,7 @@
use const T_FUNCTION;
use const T_IF;
use const T_OBJECT_OPERATOR;
use const T_OPEN_CURLY_BRACKET;
use const T_OPEN_PARENTHESIS;
use const T_SEMICOLON;
use const T_SWITCH;
Expand Down Expand Up @@ -138,6 +141,10 @@ public function process(File $phpcsFile, $functionPointer): void
continue;
}

if ($this->isPropertyWithAttribute($phpcsFile, $propertyPointer)) {
continue;
}

$propertyTypeHint = PropertyHelper::findTypeHint($phpcsFile, $propertyPointer);
$parameterTypeHint = FunctionHelper::getParametersTypeHints($phpcsFile, $functionPointer)[$parameterName];
if (!$this->areTypeHintEqual($parameterTypeHint, $propertyTypeHint)) {
Expand Down Expand Up @@ -322,6 +329,19 @@ private function isPropertyDocCommentUseful(File $phpcsFile, int $propertyPointe
return false;
}

private function isPropertyWithAttribute(File $phpcsFile, int $propertyPointer): bool
{
$tokens = $phpcsFile->getTokens();

$previousPointer = TokenHelper::findPrevious(
$phpcsFile,
[T_ATTRIBUTE_END, T_SEMICOLON, T_OPEN_CURLY_BRACKET, T_CLOSE_CURLY_BRACKET],
$propertyPointer - 1
);

return $tokens[$previousPointer]['code'] === T_ATTRIBUTE_END;
}

private function areTypeHintEqual(?TypeHint $parameterTypeHint, ?TypeHint $propertyTypeHint): bool
{
if ($parameterTypeHint === null && $propertyTypeHint === null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testErrors(): void
'enable' => true,
]);

self::assertSame(4, $report->getErrorCount());
self::assertSame(5, $report->getErrorCount());

self::assertSniffError(
$report,
Expand All @@ -43,7 +43,13 @@ public function testErrors(): void
);
self::assertSniffError(
$report,
29,
18,
RequireConstructorPropertyPromotionSniff::CODE_REQUIRED_CONSTRUCTOR_PROPERTY_PROMOTION,
'Required promotion of property $e.'
);
self::assertSniffError(
$report,
36,
RequireConstructorPropertyPromotionSniff::CODE_REQUIRED_CONSTRUCTOR_PROPERTY_PROMOTION,
'Required promotion of property $from.'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
class Whatever
{

public function __construct(public string $a, protected int|null $b = 0, private ?bool $c = null)
#[SomeAttribute]
private $d;

public function __construct(public string $a, protected int|null $b = 0, private ?bool $c = null, $d, private $e)
{
$this->d = $d;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ class Whatever

private ?bool $c = null;

public function __construct(string $a, int|null $b = 0, ?bool $c)
#[SomeAttribute]
private $d;

private $e;

public function __construct(string $a, int|null $b = 0, ?bool $c, $d, $e)
{
$this->a = $a;
$this->b = $b;
$this->c = $c;
$this->d = $d;
$this->e = $e;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ class Nothing

private $j;

public function __construct($a, $c, $d, $e, $f, $g, $h, string $i, string $j)
#[SomeAttribute]
private $k;

public function __construct($a, $c, $d, $e, $f, $g, $h, string $i, string $j, $k)
{
$phpVersion = phpversion();

Expand All @@ -97,6 +100,8 @@ public function __construct($a, $c, $d, $e, $f, $g, $h, string $i, string $j)
$this->i = $i;

$this->j = $j;

$this->k = $k;
}

}
Expand Down

0 comments on commit 122a9bf

Please sign in to comment.