Skip to content

Commit

Permalink
Merge pull request #24 from webimpress/hotfix/invalid-type-php-doc
Browse files Browse the repository at this point in the history
Fixes issue with invalid type in PHPDoc - fixer infinite loop
  • Loading branch information
michalbundyra committed May 18, 2019
2 parents 4c4599c + 6d864c9 commit e1c7e1b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use function implode;
use function in_array;
use function ltrim;
use function preg_match;
use function preg_match_all;
use function preg_quote;
use function preg_replace;
Expand Down Expand Up @@ -313,6 +314,10 @@ private function checkTag(File $phpcsFile, int $stackPtr) : void
private function getExpectedName(File $phpcsFile, string $class, int $stackPtr) : string
{
$suffix = strstr($class, '[');
if ($suffix && ! preg_match('/^(\[\])+$/', $suffix)) {
return $class;
}

$class = str_replace(['[', ']'], '', $class);

$imports = $this->getGlobalUses($phpcsFile);
Expand Down
23 changes: 12 additions & 11 deletions src/WebimpressCodingStandard/Sniffs/PHP/DisallowFqnSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use function implode;
use function in_array;
use function ltrim;
use function preg_match;
use function preg_match_all;
use function preg_quote;
use function preg_replace;
Expand Down Expand Up @@ -195,11 +196,7 @@ private function processTag(
$localToImport = [];
$newTypesArr = [];
foreach ($typesArr as $name) {
$suffix = strstr($name, '[');
$name = str_replace(['[', ']'], '', $name);

$newTypesArr[] = $this->getExpectedName($phpcsFile, $stackPtr + 2, $namespace, $name, $localToImport)
. $suffix;
$newTypesArr[] = $this->getExpectedName($phpcsFile, $stackPtr + 2, $namespace, $name, $localToImport);
}

$newTypes = implode('|', $newTypesArr);
Expand Down Expand Up @@ -244,30 +241,34 @@ private function getExpectedName(
return $name;
}

// Remove leading slash from the class name
$name = ltrim($name, '\\');
$suffix = strstr($name, '[');
if ($suffix && ! preg_match('/^(\[\])+$/', $suffix)) {
return $name;
}

$name = str_replace(['[', ']'], '', ltrim($name, '\\'));

if (stripos($name . '\\', $namespace . '\\') === 0) {
return substr($name, strlen($namespace) + 1);
return substr($name, strlen($namespace) + 1) . $suffix;
}

$alias = $this->getAliasFromName($name);
foreach ($this->imported['class'] ?? [] as $class) {
// If namespace or part of it is already imported
if (stripos($name . '\\', $class['fqn'] . '\\') === 0) {
return $class['name'];
return $class['name'] . $suffix;
}
}

// We can't suggest anything in that case
if (! $this->isValidClassName($phpcsFile, $stackPtr, $alias, $name)) {
return '\\' . $name;
return '\\' . $name . $suffix;
}

// We need to import it
$toImport += $this->import('class', $name, $alias);

return $alias;
return $alias . $suffix;
}

private function processString(
Expand Down
5 changes: 5 additions & 0 deletions test/Sniffs/PHP/CorrectClassNameCaseUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ class MyClass
public function trav(iterable $a)
{
}

/**
* @param \Bar[]\Foo[] $param
*/
abstract public function invalidTypeFormatIsNotChanged($param);
}
5 changes: 5 additions & 0 deletions test/Sniffs/PHP/CorrectClassNameCaseUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,9 @@ class MyClass
public function trav(iterable $a)
{
}

/**
* @param \Bar[]\Foo[] $param
*/
abstract public function invalidTypeFormatIsNotChanged($param);
}
5 changes: 5 additions & 0 deletions test/Sniffs/PHP/DisallowFqnUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,9 @@ class TheClass extends \ MyNamespace \ Hello \ ParentClass implements \ArrayAcce
}
};
}

/**
* @param \Bar[]\Foo[] $param
*/
abstract public function invalidTypeFormatIsNotChanged($param);
}
5 changes: 5 additions & 0 deletions test/Sniffs/PHP/DisallowFqnUnitTest.inc.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,9 @@ class TheClass extends ParentClass implements ArrayAccess, Countable
}
};
}

/**
* @param \Bar[]\Foo[] $param
*/
abstract public function invalidTypeFormatIsNotChanged($param);
}

0 comments on commit e1c7e1b

Please sign in to comment.