Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue with invalid type in PHPDoc - fixer infinite loop #24

Merged
merged 1 commit into from
May 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}