Skip to content

Null At last and handle space #101

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

Merged
merged 7 commits into from
Apr 1, 2020
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
282 changes: 211 additions & 71 deletions SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,112 @@

namespace SymfonyCustom\Sniffs\NamingConventions;

use PHP_CodeSniffer\Exceptions\DeepExitException;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Common;
use SymfonyCustom\Helpers\SniffHelper;

/**
* Throws errors if PHPDocs type hint are not valid.
*/
class ValidTypeHintSniff implements Sniff
{
private const TEXT = '[\\\\a-z0-9]';
private const OPENER = '\<|\[|\{|\(';
private const MIDDLE = '\,|\:|\=\>';
private const CLOSER = '\>|\]|\}|\)';
private const SEPARATOR = '\&|\|';

/*
/**
* <simple> is any non-array, non-generic, non-alternated type, eg `int` or `\Foo`
* <array> is array of <simple>, eg `int[]` or `\Foo[]`
* <generic> is generic collection type, like `array<string, int>`, `Collection<Item>` and more complex like `Collection<int, \null|SubCollection<string>>`
* <type> is <simple>, <array> or <generic> type, like `int`, `bool[]` or `Collection<ItemKey, ItemVal>`
* <generic> is generic collection type, like `array<string, int>`, `Collection<Item>` or more complex`
* <object> is array key => value type, like `array{type: string, name: string, value: mixed}`
* <type> is <simple>, <array>, <object>, <generic> type
* <types> is one or more types alternated via `|`, like `int|bool[]|Collection<ItemKey, ItemVal>`
*/
private const REGEX_TYPES = '
(?<types>
(?<type>
(?<array>
(?&simple)(\[\])*
)
|
(?<simple>
[@$?]?[\\\\\w]+
(?&notArray)(?:
\s*\[\s*\]
)+
)
|
(?<generic>
(?<genericName>(?&simple))
<
(?:(?<genericKey>(?&types)),\s*)?(?<genericValue>(?&types)|(?&generic))
>
(?<notArray>
(?<multiple>
\(\s*(?<mutipleContent>
(?&types)
)\s*\)
)
|
(?<generic>
(?<genericName>
(?&simple)
)
\s*<\s*
(?<genericContent>
(?:(?&types)\s*,\s*)*
(?&types)
)
\s*>
)
|
(?<object>
array\s*{\s*
(?<objectContent>
(?:
(?<objectKeyValue>
(?:\w+\s*\??:\s*)?
(?&types)
)
\s*,\s*
)*
(?&objectKeyValue)
)
\s*}
)
|
(?<simple>
\\\\?\w+(?:\\\\\w+)*
|
\$this
)
)
)
(?:
\|
(?:(?&simple)|(?&array)|(?&generic))
\s*[\|&]\s*(?&type)
)*
)
';

/**
* False if the type is not a reserved keyword and the check can't be case insensitive
**/
private const TYPES = [
'array' => true,
'bool' => true,
'callable' => true,
'false' => true,
'float' => true,
'int' => true,
'iterable' => true,
'mixed' => false,
'null' => true,
'number' => false,
'object' => true,
'resource' => false,
'self' => true,
'static' => true,
'string' => true,
'true' => true,
'void' => true,
'$this' => true,
];

private const ALIAS_TYPES = [
'boolean' => 'bool',
'integer' => 'int',
'double' => 'float',
'real' => 'float',
'callback' => 'callable',
];

/**
* @return array
*/
Expand All @@ -68,80 +126,166 @@ public function process(File $phpcsFile, $stackPtr): void
{
$tokens = $phpcsFile->getTokens();

if (in_array($tokens[$stackPtr]['content'], SniffHelper::TAGS_WITH_TYPE)) {
$matchingResult = preg_match(
'{^'.self::REGEX_TYPES.'(?:[ \t].*)?$}sx',
$tokens[$stackPtr + 2]['content'],
$matches
);
if (!in_array($tokens[$stackPtr]['content'], SniffHelper::TAGS_WITH_TYPE)) {
return;
}

$content = 1 === $matchingResult ? $matches['types'] : '';
$endOfContent = preg_replace('/'.preg_quote($content, '/').'/', '', $tokens[$stackPtr + 2]['content'], 1);
$matchingResult = preg_match(
'{^'.self::REGEX_TYPES.'(?:[\s\t].*)?$}six',
$tokens[$stackPtr + 2]['content'],
$matches
);

$content = 1 === $matchingResult ? $matches['types'] : '';
$endOfContent = substr($tokens[$stackPtr + 2]['content'], strlen($content));

try {
$suggestedType = $this->getValidTypes($content);
} catch (DeepExitException $exception) {
$phpcsFile->addError(
$exception->getMessage(),
$stackPtr + 2,
'Exception'
);

if ($content !== $suggestedType) {
$fix = $phpcsFile->addFixableError(
'For type-hinting in PHPDocs, use %s instead of %s',
$stackPtr + 2,
'Invalid',
[$suggestedType, $content]
);
return;
}

if ($content !== $suggestedType) {
$fix = $phpcsFile->addFixableError(
'For type-hinting in PHPDocs, use %s instead of %s',
$stackPtr + 2,
'Invalid',
[$suggestedType, $content]
);

if ($fix) {
$phpcsFile->fixer->replaceToken($stackPtr + 2, $suggestedType.$endOfContent);
}
if ($fix) {
$phpcsFile->fixer->replaceToken($stackPtr + 2, $suggestedType.$endOfContent);
}
}
}

/**
* @param string $content
*
* @return array
* @return string
*
* @throws DeepExitException
*/
private function getTypes(string $content): array
private function getValidTypes(string $content): string
{
$content = preg_replace('/\s/', '', $content);

$types = [];
$separators = [];
while ('' !== $content && false !== $content) {
preg_match('{^'.self::REGEX_TYPES.'$}x', $content, $matches);
preg_match('{^'.self::REGEX_TYPES.'$}ix', $content, $matches);

if (isset($matches['array']) && '' !== $matches['array']) {
$validType = $this->getValidTypes(substr($matches['array'], 0, -2)).'[]';
} elseif (isset($matches['multiple']) && '' !== $matches['multiple']) {
$validType = '('.$this->getValidTypes($matches['mutipleContent']).')';
} elseif (isset($matches['generic']) && '' !== $matches['generic']) {
$validType = $this->getValidGenericType($matches['genericName'], $matches['genericContent']);
} elseif (isset($matches['object']) && '' !== $matches['object']) {
$validType = $this->getValidObjectType($matches['objectContent']);
} else {
$validType = $this->getValidType($matches['type']);
}

$types[] = $matches['type'];
$types[] = $validType;

$separators[] = substr($content, strlen($matches['type']), 1);
$content = substr($content, strlen($matches['type']) + 1);
}

// Remove last separator since it's an empty string
array_pop($separators);

$uniqueSeparators = array_unique($separators);
switch (count($uniqueSeparators)) {
case 0:
return implode('', $types);
case 1:
return implode($uniqueSeparators[0], $this->orderTypes($types));
default:
throw new DeepExitException(
'Union and intersection types must be grouped with parenthesis when used in the same expression'
);
}
}

/**
* @param array $types
*
* @return array
*/
private function orderTypes(array $types): array
{
$types = array_unique($types);
usort($types, function ($type1, $type2) {
if ('null' === $type1) {
return 1;
}

if ('null' === $type2) {
return -1;
}

return 0;
});

return $types;
}

/**
* @param string $content
* @param string $genericName
* @param string $genericContent
*
* @return string
*
* @throws DeepExitException
*/
private function getValidTypes(string $content): string
private function getValidGenericType(string $genericName, string $genericContent): string
{
$types = $this->getTypes($content);
$validType = $this->getValidType($genericName).'<';

foreach ($types as $index => $type) {
$type = str_replace(' ', '', $type);
while ('' !== $genericContent && false !== $genericContent) {
preg_match('{^'.self::REGEX_TYPES.',?}ix', $genericContent, $matches);

preg_match('{^'.self::REGEX_TYPES.'$}x', $type, $matches);
if (isset($matches['generic'])) {
$validType = $this->getValidType($matches['genericName']).'<';
$validType .= $this->getValidTypes($matches['types']).', ';
$genericContent = substr($genericContent, strlen($matches['types']) + 1);
}

if ('' !== $matches['genericKey']) {
$validType .= $this->getValidTypes($matches['genericKey']).', ';
}
return preg_replace('/,\s$/', '>', $validType);
}

$validType .= $this->getValidTypes($matches['genericValue']).'>';
} else {
$validType = $this->getValidType($type);
/**
* @param string $objectContent
*
* @return string
*
* @throws DeepExitException
*/
private function getValidObjectType(string $objectContent): string
{
$validType = 'array{';

while ('' !== $objectContent && false !== $objectContent) {
$split = preg_split('/(\??:|,)/', $objectContent, 2, PREG_SPLIT_DELIM_CAPTURE);

if (isset($split[1]) && ',' !== $split[1]) {
$validType .= $split[0].$split[1].' ';
$objectContent = $split[2];
}

$types[$index] = $validType;
preg_match('{^'.self::REGEX_TYPES.',?}ix', $objectContent, $matches);

$validType .= $this->getValidTypes($matches['types']).', ';
$objectContent = substr($objectContent, strlen($matches['types']) + 1);
}

return implode('|', $types);
return preg_replace('/,\s$/', '}', $validType);
}

/**
Expand All @@ -151,20 +295,16 @@ private function getValidTypes(string $content): string
*/
private function getValidType(string $typeName): string
{
if ('[]' === substr($typeName, -2)) {
return $this->getValidType(substr($typeName, 0, -2)).'[]';
$lowerType = strtolower($typeName);
if (isset(self::TYPES[$lowerType])) {
return self::TYPES[$lowerType] ? $lowerType : $typeName;
}

$lowerType = strtolower($typeName);
switch ($lowerType) {
case 'bool':
case 'boolean':
return 'bool';
case 'int':
case 'integer':
return 'int';
// This can't be case insensitive since this is not reserved keyword
if (isset(self::ALIAS_TYPES[$typeName])) {
return self::ALIAS_TYPES[$typeName];
}

return Common::suggestType($typeName);
return $typeName;
}
}
Loading