From 9b0a1dd012978f7652963f6084fe25bd87d7f0f5 Mon Sep 17 00:00:00 2001 From: webimpress Date: Wed, 15 May 2019 08:17:40 +0100 Subject: [PATCH 1/2] ReturnType sniff - fixes false-positive error when function may return $this --- .../Sniffs/Functions/ReturnTypeSniff.php | 8 +- .../Sniffs/Functions/ReturnTypeUnitTest.2.inc | 3 +- .../Functions/ReturnTypeUnitTest.2.inc.fixed | 3 +- test/Sniffs/Functions/ReturnTypeUnitTest.inc | 16 ++++ test/Sniffs/Functions/ReturnTypeUnitTest.php | 79 +++++++++---------- 5 files changed, 61 insertions(+), 48 deletions(-) diff --git a/src/WebimpressCodingStandard/Sniffs/Functions/ReturnTypeSniff.php b/src/WebimpressCodingStandard/Sniffs/Functions/ReturnTypeSniff.php index 31fc4ddc..d2788991 100644 --- a/src/WebimpressCodingStandard/Sniffs/Functions/ReturnTypeSniff.php +++ b/src/WebimpressCodingStandard/Sniffs/Functions/ReturnTypeSniff.php @@ -57,7 +57,6 @@ use const T_OPEN_PARENTHESIS; use const T_OPEN_SHORT_ARRAY; use const T_OPEN_SQUARE_BRACKET; -use const T_PARENT; use const T_RETURN; use const T_SELF; use const T_SEMICOLON; @@ -547,14 +546,11 @@ private function processReturnStatements(File $phpcsFile, int $stackPtr) : void T_SELF, T_STATIC, T_STRING_CAST, - T_PARENT, ], true); if ($isThis && $tokens[$next]['code'] === T_VARIABLE - && (strtolower($tokens[$next]['content']) !== '$this' - || (($next = $phpcsFile->findNext(Tokens::$emptyTokens, $next + 1, null, true)) - && $tokens[$next]['code'] !== T_SEMICOLON)) + && strtolower($tokens[$next]['content']) !== '$this' ) { $isThis = false; } @@ -595,7 +591,7 @@ private function processReturnStatements(File $phpcsFile, int $stackPtr) : void if ($matches = array_udiff( preg_grep('/[^\]]$/', $this->returnDocTypes), ['null', 'array', 'iterable'], - function ($a, $b) { + static function (string $a, string $b) { return strcasecmp($a, $b); } )) { diff --git a/test/Sniffs/Functions/ReturnTypeUnitTest.2.inc b/test/Sniffs/Functions/ReturnTypeUnitTest.2.inc index cd5b76dd..ad8833a0 100644 --- a/test/Sniffs/Functions/ReturnTypeUnitTest.2.inc +++ b/test/Sniffs/Functions/ReturnTypeUnitTest.2.inc @@ -61,9 +61,10 @@ abstract class FunctionCommentReturn return self::CONST; return static::method(); return (string) $a; - return parent::method(); return true; return $a; + return parent::method(); + return $this->method(); return $this; } diff --git a/test/Sniffs/Functions/ReturnTypeUnitTest.2.inc.fixed b/test/Sniffs/Functions/ReturnTypeUnitTest.2.inc.fixed index 34526e1b..979fa07e 100644 --- a/test/Sniffs/Functions/ReturnTypeUnitTest.2.inc.fixed +++ b/test/Sniffs/Functions/ReturnTypeUnitTest.2.inc.fixed @@ -61,9 +61,10 @@ abstract class FunctionCommentReturn return self::CONST; return static::method(); return (string) $a; - return parent::method(); return true; return $a; + return parent::method(); + return $this->method(); return $this; } diff --git a/test/Sniffs/Functions/ReturnTypeUnitTest.inc b/test/Sniffs/Functions/ReturnTypeUnitTest.inc index e2bb78cf..7ad47c6a 100644 --- a/test/Sniffs/Functions/ReturnTypeUnitTest.inc +++ b/test/Sniffs/Functions/ReturnTypeUnitTest.inc @@ -238,4 +238,20 @@ abstract class FunctionCommentReturn * @return null[] */ abstract public function returnNullArray(); + + /** + * @return $this + */ + public function thisParent() : self + { + return parent::thisParent(); + } + + /** + * @return $this + */ + public function thisOther() : self + { + return $this->thisParent(); + } } diff --git a/test/Sniffs/Functions/ReturnTypeUnitTest.php b/test/Sniffs/Functions/ReturnTypeUnitTest.php index cd057e29..65960387 100644 --- a/test/Sniffs/Functions/ReturnTypeUnitTest.php +++ b/test/Sniffs/Functions/ReturnTypeUnitTest.php @@ -103,46 +103,45 @@ protected function getErrorList(string $testFile = '') : array 63 => 1, 64 => 1, 65 => 1, - 66 => 1, - 70 => 1, - 77 => 1, - 84 => 1, - 96 => 1, - 104 => 1, - 112 => 1, - 116 => 1, - 124 => 1, - 132 => 1, - 176 => 1, - 184 => 1, - 188 => 1, - 192 => 1, - 196 => 1, - 200 => 1, - 204 => 1, - 213 => 1, - 216 => 1, - 265 => 1, - 272 => 1, - 276 => 1, - 280 => 1, - 284 => 1, - 296 => 1, - 305 => 1, - 313 => 1, - 318 => 1, - 353 => 1, - 362 => 1, - 374 => 1, - 383 => 1, - 391 => 1, - 396 => 1, - 411 => 1, - 419 => 1, - 431 => 1, - 451 => 1, - 459 => 1, - 467 => 1, + 71 => 1, + 78 => 1, + 85 => 1, + 97 => 1, + 105 => 1, + 113 => 1, + 117 => 1, + 125 => 1, + 133 => 1, + 177 => 1, + 185 => 1, + 189 => 1, + 193 => 1, + 197 => 1, + 201 => 1, + 205 => 1, + 214 => 1, + 217 => 1, + 266 => 1, + 273 => 1, + 277 => 1, + 281 => 1, + 285 => 1, + 297 => 1, + 306 => 1, + 314 => 1, + 319 => 1, + 354 => 1, + 363 => 1, + 375 => 1, + 384 => 1, + 392 => 1, + 397 => 1, + 412 => 1, + 420 => 1, + 432 => 1, + 452 => 1, + 460 => 1, + 468 => 1, ]; case 'ReturnTypeUnitTest.3.inc': return [ From 79bd18057f8a3862ac1a701aba6fabc79e2a8376 Mon Sep 17 00:00:00 2001 From: webimpress Date: Wed, 15 May 2019 17:05:17 +0100 Subject: [PATCH 2/2] Adds CHANGELOG entry for #20 --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1d7fb5f..8b8c132c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,8 @@ All notable changes to this project will be documented in this file, in reverse - [#19](https://github.com/webimpress/coding-standard/pull/19) `Commenting\DocComment` - fixes issue with doc-block after colon (for example in switch statement) - empty line is no longer required before doc-block in that case +- [#20](https://github.com/webimpress/coding-standard/pull/20) `Functions\ReturnType` - fixes false-positive error when function may return `$this` + ## 1.0.2 - 2019-05-12 ### Added