Skip to content

Commit

Permalink
Merge branch 'hotfix/24'
Browse files Browse the repository at this point in the history
Close #24
  • Loading branch information
michalbundyra committed May 18, 2019
2 parents 4c4599c + 744469f commit 5c8d28d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ All notable changes to this project will be documented in this file, in reverse

- [#23](https://github.com/webimpress/coding-standard/pull/23) `Functions\ReturnType` - fixes recognising yoda comparison in return statement and type of returned value

- [#24](https://github.com/webimpress/coding-standard/pull/24) fixes error in fixer when recognised type was invalid. Affects the following sniffs:
- `PHP\CorrectClassNameCase`
- `PHP\DisallowFqn`

## 1.0.2 - 2019-05-12

### Added
Expand Down
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 5c8d28d

Please sign in to comment.