Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-detected private typed properties in PHP 7.4 #875

Merged
merged 1 commit into from
Jan 28, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use const T_MOD_EQUAL;
use const T_MUL_EQUAL;
use const T_NEW;
use const T_NULLABLE;
use const T_OBJECT_OPERATOR;
use const T_OPEN_PARENTHESIS;
use const T_OR_EQUAL;
Expand Down Expand Up @@ -622,6 +623,16 @@ private function findVisibilityModifierTokenPointer(File $phpcsFile, array $toke
return $visibilityModifiedTokenPointer;
}

if (in_array($visibilityModifiedToken['code'], [T_SELF, T_STRING], true)) {
$mightBeNullableTokenPointer = TokenHelper::findPreviousEffective($phpcsFile, $visibilityModifiedTokenPointer - 1);
$mightBeNullableToken = $tokens[$mightBeNullableTokenPointer];
kukulich marked this conversation as resolved.
Show resolved Hide resolved
if ($mightBeNullableToken['code'] === T_NULLABLE) {
$visibilityModifiedTokenPointer = $mightBeNullableTokenPointer;
}

return $this->findVisibilityModifierTokenPointer($phpcsFile, $tokens, $visibilityModifiedTokenPointer);
}

if (in_array($visibilityModifiedToken['code'], [T_ABSTRACT, T_STATIC], true)) {
return $this->findVisibilityModifierTokenPointer($phpcsFile, $tokens, $visibilityModifiedTokenPointer);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Sniffs/Classes/UnusedPrivateElementsSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,14 @@ public function testClassWithWriteOnlyProperties(): void
}
}

public function testClassWithTypedProperties(): void
{
$resultFile = self::checkFile(__DIR__ . '/data/classWithTypedProperties.php');

self::assertNoSniffError($resultFile, 6);
self::assertSniffError($resultFile, 7, UnusedPrivateElementsSniff::CODE_WRITE_ONLY_PROPERTY);
self::assertSniffError($resultFile, 8, UnusedPrivateElementsSniff::CODE_WRITE_ONLY_PROPERTY);
self::assertSniffError($resultFile, 9, UnusedPrivateElementsSniff::CODE_WRITE_ONLY_PROPERTY);
}

}
24 changes: 24 additions & 0 deletions tests/Sniffs/Classes/data/classWithTypedProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php // lint >= 7.4

class ClassWithTypedProperties
{

private self $self;
private ?self $empty;
private ClassWithTypedProperties $static;
private int $int;

public function __construct()
{
$this->self = $this;
$this->empty = null;
$this->static = $this;
$this->int = 0;
}

public function getSelf(): self
{
return $this->self;
}

}