From 13ae2a034f4217bd50507cd860bc7e8371eeb732 Mon Sep 17 00:00:00 2001 From: Jaroslav Hanslik Date: Wed, 2 Aug 2017 00:42:42 +0200 Subject: [PATCH] Fixed notice --- .../Sniffs/Classes/UnusedPrivateElementsSniff.php | 12 ++++++++---- .../classWithPrivateMethodCalledOnSelfInstance.php | 5 +++++ .../classWithPrivateMethodCalledOnStaticInstance.php | 7 ++++++- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php b/SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php index c6cfc1240..c1f53c625 100644 --- a/SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php +++ b/SlevomatCodingStandard/Sniffs/Classes/UnusedPrivateElementsSniff.php @@ -157,11 +157,15 @@ public function process(\PHP_CodeSniffer\Files\File $phpcsFile, $classPointer) $newTokenPointer = TokenHelper::findPreviousEffective($phpcsFile, $tokenPointer - 1); if ($tokens[$newTokenPointer]['code'] === T_NEW) { $variableTokenPointer = TokenHelper::findPreviousLocal($phpcsFile, T_VARIABLE, $newTokenPointer - 1); - $scopeMethodPointer = TokenHelper::findPrevious($phpcsFile, T_FUNCTION, $variableTokenPointer - 1); - for ($i = $tokens[$scopeMethodPointer]['scope_opener']; $i < $tokens[$scopeMethodPointer]['scope_closer']; $i++) { - if ($tokens[$i]['content'] === $tokens[$variableTokenPointer]['content']) { - $findUsagesStartTokenPointer = $checkVariable($i); + if ($variableTokenPointer !== null) { + $scopeMethodPointer = TokenHelper::findPrevious($phpcsFile, T_FUNCTION, $variableTokenPointer - 1); + for ($i = $tokens[$scopeMethodPointer]['scope_opener']; $i < $tokens[$scopeMethodPointer]['scope_closer']; $i++) { + if ($tokens[$i]['content'] === $tokens[$variableTokenPointer]['content']) { + $findUsagesStartTokenPointer = $checkVariable($i); + } } + } else { + $findUsagesStartTokenPointer = $tokenPointer + 1; } } else { $doubleColonTokenPointer = TokenHelper::findNextEffective($phpcsFile, $tokenPointer + 1); diff --git a/tests/Sniffs/Classes/data/classWithPrivateMethodCalledOnSelfInstance.php b/tests/Sniffs/Classes/data/classWithPrivateMethodCalledOnSelfInstance.php index cad80d116..ec63d746f 100644 --- a/tests/Sniffs/Classes/data/classWithPrivateMethodCalledOnSelfInstance.php +++ b/tests/Sniffs/Classes/data/classWithPrivateMethodCalledOnSelfInstance.php @@ -15,4 +15,9 @@ private function setUp() { } + public static function foo() + { + return new self(); + } + } diff --git a/tests/Sniffs/Classes/data/classWithPrivateMethodCalledOnStaticInstance.php b/tests/Sniffs/Classes/data/classWithPrivateMethodCalledOnStaticInstance.php index cad80d116..743d825bb 100644 --- a/tests/Sniffs/Classes/data/classWithPrivateMethodCalledOnStaticInstance.php +++ b/tests/Sniffs/Classes/data/classWithPrivateMethodCalledOnStaticInstance.php @@ -5,7 +5,7 @@ class ClassWithPrivateMethodCalledOnSelfInstance public static function create() { - $self = new self(); + $self = new static(); $self->setUp(); return $self; @@ -15,4 +15,9 @@ private function setUp() { } + public static function foo() + { + return new static(); + } + }