From a498419cc5e1e3bebc08c73003a57a999125f12f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Mon, 11 Nov 2019 20:08:01 +0000 Subject: [PATCH 1/2] Hotfix: parsing `@param` and `@var` PHPDoc tags with multiple spaces Split on string was done incorrectly, as it assumed always one white character, where we can have multiple spaces (in case variable names are aligned in PHPDocs). Fixed how we split the string, so we can get better results on reporting errors and fixing. --- .../Sniffs/Commenting/TagWithTypeSniff.php | 4 ++-- .../Sniffs/Functions/ParamSniff.php | 2 +- test/Sniffs/Commenting/TagWithTypeUnitTest.1.inc | 7 +++++++ test/Sniffs/Commenting/TagWithTypeUnitTest.1.inc.fixed | 7 +++++++ test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc | 5 +++++ test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc.fixed | 5 +++++ test/Sniffs/Commenting/TagWithTypeUnitTest.php | 1 + test/Sniffs/Functions/ParamUnitTest.inc | 5 +++++ test/Sniffs/Functions/ParamUnitTest.inc.fixed | 5 +++++ test/Sniffs/Functions/ParamUnitTest.php | 1 + test/Sniffs/Functions/ReturnTypeUnitTest.1.inc | 5 +++++ test/Sniffs/Functions/ReturnTypeUnitTest.1.inc.fixed | 5 +++++ test/Sniffs/Functions/ReturnTypeUnitTest.php | 1 + test/Sniffs/Functions/ThrowsUnitTest.inc | 8 ++++++++ test/Sniffs/Functions/ThrowsUnitTest.inc.fixed | 8 ++++++++ 15 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/WebimpressCodingStandard/Sniffs/Commenting/TagWithTypeSniff.php b/src/WebimpressCodingStandard/Sniffs/Commenting/TagWithTypeSniff.php index 882ecf48..14f4f956 100644 --- a/src/WebimpressCodingStandard/Sniffs/Commenting/TagWithTypeSniff.php +++ b/src/WebimpressCodingStandard/Sniffs/Commenting/TagWithTypeSniff.php @@ -190,7 +190,7 @@ private function processParamTag(File $phpcsFile, int $tagPtr) : bool { $tokens = $phpcsFile->getTokens(); - $split = preg_split('/\s/', $tokens[$tagPtr + 2]['content'], 3); + $split = preg_split('/\s+/', $tokens[$tagPtr + 2]['content'], 3); if (! isset($split[1])) { if ($this->isVariable($split[0])) { @@ -238,7 +238,7 @@ private function processVarTag(File $phpcsFile, int $tagPtr) : bool $condition = end($tokens[$tagPtr]['conditions']); $isMemberVar = isset(Tokens::$ooScopeTokens[$condition]); - $split = preg_split('/\s/', $tokens[$tagPtr + 2]['content'], 3); + $split = preg_split('/\s+/', $tokens[$tagPtr + 2]['content'], 3); if ($nested > 0 || ! $isMemberVar) { if (! isset($split[1])) { if ($this->isVariable($split[0])) { diff --git a/src/WebimpressCodingStandard/Sniffs/Functions/ParamSniff.php b/src/WebimpressCodingStandard/Sniffs/Functions/ParamSniff.php index 641e3cdd..b1bb195b 100644 --- a/src/WebimpressCodingStandard/Sniffs/Functions/ParamSniff.php +++ b/src/WebimpressCodingStandard/Sniffs/Functions/ParamSniff.php @@ -98,7 +98,7 @@ private function processParamDoc(File $phpcsFile, int $commentStart) : void continue; } - $split = preg_split('/\s/', $tokens[$tag + 2]['content'], 3); + $split = preg_split('/\s+/', $tokens[$tag + 2]['content'], 3); if (! isset($split[1]) || ! $this->isVariable($split[1])) { // Missing param type or it's not a variable continue; diff --git a/test/Sniffs/Commenting/TagWithTypeUnitTest.1.inc b/test/Sniffs/Commenting/TagWithTypeUnitTest.1.inc index 229c39b5..16766609 100644 --- a/test/Sniffs/Commenting/TagWithTypeUnitTest.1.inc +++ b/test/Sniffs/Commenting/TagWithTypeUnitTest.1.inc @@ -331,4 +331,11 @@ interface MyInterface extends MyOne, MyTwo * @param null|mixed[] $mixedArray */ public function method5($mixed, array $mixedArray = null); + + /** + * @param int $p1 Description + * @return iterable Description + * @throws Exception Description + */ + public function moreThanOneSpace(int $p1) : iterable; } diff --git a/test/Sniffs/Commenting/TagWithTypeUnitTest.1.inc.fixed b/test/Sniffs/Commenting/TagWithTypeUnitTest.1.inc.fixed index 6bd75aa9..70acb069 100644 --- a/test/Sniffs/Commenting/TagWithTypeUnitTest.1.inc.fixed +++ b/test/Sniffs/Commenting/TagWithTypeUnitTest.1.inc.fixed @@ -331,4 +331,11 @@ interface MyInterface extends MyOne, MyTwo * @param null|mixed[] $mixedArray */ public function method5($mixed, array $mixedArray = null); + + /** + * @param int $p1 Description + * @return iterable Description + * @throws Exception Description + */ + public function moreThanOneSpace(int $p1) : iterable; } diff --git a/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc b/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc index be933ebf..b38c8a6d 100644 --- a/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc +++ b/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc @@ -304,4 +304,9 @@ class VarTag extends VarTagParent { * @var \RuntimeException\Hello Description. */ public $a; + + /** + * @var int $moreThanOneSpace Some long description + */ + public $moreThanOneSpace; } diff --git a/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc.fixed b/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc.fixed index 3ed0e9b8..93a88063 100644 --- a/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc.fixed +++ b/test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc.fixed @@ -304,4 +304,9 @@ class VarTag extends VarTagParent { * @var Exception\Hello Description. */ public $a; + + /** + * @var int Some long description + */ + public $moreThanOneSpace; } diff --git a/test/Sniffs/Commenting/TagWithTypeUnitTest.php b/test/Sniffs/Commenting/TagWithTypeUnitTest.php index f4137c34..8ffa01ad 100644 --- a/test/Sniffs/Commenting/TagWithTypeUnitTest.php +++ b/test/Sniffs/Commenting/TagWithTypeUnitTest.php @@ -181,6 +181,7 @@ protected function getErrorList(string $testFile = '') : array 295 => 1, 299 => 1, 304 => 1, + 309 => 1, ]; } diff --git a/test/Sniffs/Functions/ParamUnitTest.inc b/test/Sniffs/Functions/ParamUnitTest.inc index 53b72de2..84321412 100644 --- a/test/Sniffs/Functions/ParamUnitTest.inc +++ b/test/Sniffs/Functions/ParamUnitTest.inc @@ -346,4 +346,9 @@ interface MyInterface extends MyOne, MyTwo ?iterable $i2, ?iterable $i3 ) : void; + + /** + * @param int $p1 + */ + public function moreThanOneSpace(int $p1) : void; } diff --git a/test/Sniffs/Functions/ParamUnitTest.inc.fixed b/test/Sniffs/Functions/ParamUnitTest.inc.fixed index f4469b58..fb287edb 100644 --- a/test/Sniffs/Functions/ParamUnitTest.inc.fixed +++ b/test/Sniffs/Functions/ParamUnitTest.inc.fixed @@ -346,4 +346,9 @@ interface MyInterface extends MyOne, MyTwo ?iterable $i2, ?iterable $i3 ) : void; + + /** + * + */ + public function moreThanOneSpace(int $p1) : void; } diff --git a/test/Sniffs/Functions/ParamUnitTest.php b/test/Sniffs/Functions/ParamUnitTest.php index 31e1dff8..3d292d84 100644 --- a/test/Sniffs/Functions/ParamUnitTest.php +++ b/test/Sniffs/Functions/ParamUnitTest.php @@ -103,6 +103,7 @@ protected function getErrorList(string $testFile = '') : array 334 => 1, 335 => 1, 336 => 1, + 351 => 1, ]; } diff --git a/test/Sniffs/Functions/ReturnTypeUnitTest.1.inc b/test/Sniffs/Functions/ReturnTypeUnitTest.1.inc index d7534504..9cb6f3e9 100644 --- a/test/Sniffs/Functions/ReturnTypeUnitTest.1.inc +++ b/test/Sniffs/Functions/ReturnTypeUnitTest.1.inc @@ -381,4 +381,9 @@ interface MyInterface extends MyOne, MyTwo * @return null|Foo|object Description */ public function typeSpecifiedWithDescriptionObject() : ?object; + + /** + * @return null|int + */ + public function moreThanOneSpace() : ?int; } diff --git a/test/Sniffs/Functions/ReturnTypeUnitTest.1.inc.fixed b/test/Sniffs/Functions/ReturnTypeUnitTest.1.inc.fixed index 69ce3320..33d21a1d 100644 --- a/test/Sniffs/Functions/ReturnTypeUnitTest.1.inc.fixed +++ b/test/Sniffs/Functions/ReturnTypeUnitTest.1.inc.fixed @@ -381,4 +381,9 @@ interface MyInterface extends MyOne, MyTwo * @return null|Foo Description */ public function typeSpecifiedWithDescriptionObject() : ?object; + + /** + * + */ + public function moreThanOneSpace() : ?int; } diff --git a/test/Sniffs/Functions/ReturnTypeUnitTest.php b/test/Sniffs/Functions/ReturnTypeUnitTest.php index 34876c04..67e6923d 100644 --- a/test/Sniffs/Functions/ReturnTypeUnitTest.php +++ b/test/Sniffs/Functions/ReturnTypeUnitTest.php @@ -80,6 +80,7 @@ protected function getErrorList(string $testFile = '') : array 371 => 1, 376 => 1, 381 => 1, + 386 => 1, ]; case 'ReturnTypeUnitTest.2.inc': return [ diff --git a/test/Sniffs/Functions/ThrowsUnitTest.inc b/test/Sniffs/Functions/ThrowsUnitTest.inc index 4a6204cd..aae98a68 100644 --- a/test/Sniffs/Functions/ThrowsUnitTest.inc +++ b/test/Sniffs/Functions/ThrowsUnitTest.inc @@ -310,4 +310,12 @@ class ThrowUnitTest throw new Exception(); }); } + + /** + * @throws Exception Description + */ + public function moreThanOneSpace() + { + throw new Exception(); + } } diff --git a/test/Sniffs/Functions/ThrowsUnitTest.inc.fixed b/test/Sniffs/Functions/ThrowsUnitTest.inc.fixed index b05e1a2f..96418ba1 100644 --- a/test/Sniffs/Functions/ThrowsUnitTest.inc.fixed +++ b/test/Sniffs/Functions/ThrowsUnitTest.inc.fixed @@ -310,4 +310,12 @@ class ThrowUnitTest throw new Exception(); }); } + + /** + * @throws Exception Description + */ + public function moreThanOneSpace() + { + throw new Exception(); + } } From e0b4882234abf1a621f1ba4c3845e80e21d4c1b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Bundyra?= Date: Mon, 11 Nov 2019 20:34:10 +0000 Subject: [PATCH 2/2] Adds CHANGELOG entry for #57 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3890c6d9..71cc9ff6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,10 @@ All notable changes to this project will be documented in this file, in reverse - `Commenting\MethodAnnotation` sniff - `final` methods. - `Commenting\PropertyAnnotation` sniff - properties defined with `var`. +- [#57](https://github.com/webimpress/coding-standard/pull/57) fixes parsing content of `@param` and `@var` tags with multiple spaces before variable name. Affects the following sniffs: + - `Commenting\TagWithType`, + - `Functions\Param`. + ## 1.0.5 - 2019-10-06 ### Added