Skip to content

Commit

Permalink
Merge pull request #359 from cakephp/object-arrays
Browse files Browse the repository at this point in the history
Add option to skip legacy generics
  • Loading branch information
othercorey authored May 21, 2022
2 parents 3f42885 + 9f8feb4 commit 4bca0b8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions CakePHP/Sniffs/Commenting/TypeHintSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class TypeHintSniff implements Sniff
*/
public bool $convertArraysToGenerics = true;

/**
* Keeps types in the form: \ClassName|Type[].
*
* @var bool
*/
public bool $ignoreLegacyGenerics = false;

/**
* @var array<string>
*/
Expand Down Expand Up @@ -102,6 +109,10 @@ public function process(File $phpcsFile, $stackPtr)
continue;
}

if ($this->ignoreLegacyGenerics && $this->isLegacyGenericType($types)) {
continue;
}

$originalTypeHint = $this->renderUnionTypes($types);
$sortedTypeHint = $this->getSortedTypeHint($types);
if ($sortedTypeHint === $originalTypeHint) {
Expand Down Expand Up @@ -152,6 +163,21 @@ public function process(File $phpcsFile, $stackPtr)
}
}

/**
* @param array $types node types
* @return bool
*/
protected function isLegacyGenericType(array $types): bool
{
if (count($types) != 2) {
return false;
}

return $types[0] instanceof IdentifierTypeNode &&
$types[1] instanceof ArrayTypeNode &&
$types[0]->name[0] === '\\';
}

/**
* @param array $types node types
* @return string
Expand Down

0 comments on commit 4bca0b8

Please sign in to comment.