Skip to content

Commit

Permalink
Hotfix: parsing @param and @var PHPDoc tags with multiple spaces
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michalbundyra committed Nov 11, 2019
1 parent f97e8df commit a498419
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down Expand Up @@ -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])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions test/Sniffs/Commenting/TagWithTypeUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 7 additions & 0 deletions test/Sniffs/Commenting/TagWithTypeUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,9 @@ class VarTag extends VarTagParent {
* @var \RuntimeException\Hello Description.
*/
public $a;

/**
* @var int $moreThanOneSpace Some long description
*/
public $moreThanOneSpace;
}
5 changes: 5 additions & 0 deletions test/Sniffs/Commenting/TagWithTypeUnitTest.3.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,9 @@ class VarTag extends VarTagParent {
* @var Exception\Hello Description.
*/
public $a;

/**
* @var int Some long description
*/
public $moreThanOneSpace;
}
1 change: 1 addition & 0 deletions test/Sniffs/Commenting/TagWithTypeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ protected function getErrorList(string $testFile = '') : array
295 => 1,
299 => 1,
304 => 1,
309 => 1,
];
}

Expand Down
5 changes: 5 additions & 0 deletions test/Sniffs/Functions/ParamUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,9 @@ interface MyInterface extends MyOne, MyTwo
?iterable $i2,
?iterable $i3
) : void;

/**
* @param int $p1
*/
public function moreThanOneSpace(int $p1) : void;
}
5 changes: 5 additions & 0 deletions test/Sniffs/Functions/ParamUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,9 @@ interface MyInterface extends MyOne, MyTwo
?iterable $i2,
?iterable $i3
) : void;

/**
*
*/
public function moreThanOneSpace(int $p1) : void;
}
1 change: 1 addition & 0 deletions test/Sniffs/Functions/ParamUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ protected function getErrorList(string $testFile = '') : array
334 => 1,
335 => 1,
336 => 1,
351 => 1,
];
}

Expand Down
5 changes: 5 additions & 0 deletions test/Sniffs/Functions/ReturnTypeUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions test/Sniffs/Functions/ReturnTypeUnitTest.1.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,9 @@ interface MyInterface extends MyOne, MyTwo
* @return null|Foo Description
*/
public function typeSpecifiedWithDescriptionObject() : ?object;

/**
*
*/
public function moreThanOneSpace() : ?int;
}
1 change: 1 addition & 0 deletions test/Sniffs/Functions/ReturnTypeUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ protected function getErrorList(string $testFile = '') : array
371 => 1,
376 => 1,
381 => 1,
386 => 1,
];
case 'ReturnTypeUnitTest.2.inc':
return [
Expand Down
8 changes: 8 additions & 0 deletions test/Sniffs/Functions/ThrowsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,12 @@ class ThrowUnitTest
throw new Exception();
});
}

/**
* @throws Exception Description
*/
public function moreThanOneSpace()
{
throw new Exception();
}
}
8 changes: 8 additions & 0 deletions test/Sniffs/Functions/ThrowsUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,12 @@ class ThrowUnitTest
throw new Exception();
});
}

/**
* @throws Exception Description
*/
public function moreThanOneSpace()
{
throw new Exception();
}
}

0 comments on commit a498419

Please sign in to comment.