Skip to content

🐛 Avoid space bug #97

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 1 commit into from
Dec 26, 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 @@ -10,14 +10,15 @@
use SymfonyCustom\Helpers\SniffHelper;

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

/**
* @return array
Expand All @@ -38,9 +39,10 @@ public function process(File $phpcsFile, $stackPtr): void
if (in_array($tokens[$stackPtr]['content'], SniffHelper::TAGS_WITH_TYPE)) {
preg_match(
'`^((?:'
.'['.self::OPENER.self::MIDDLE.']\s*'
.'|(?:['.self::TYPING.self::CLOSER.']\s+)(?=[\|'.self::OPENER.self::MIDDLE.self::CLOSER.'])'
.'|['.self::TYPING.self::CLOSER.']'
.'['.self::OPENER.self::MIDDLE.self::SEPARATOR.']\s*'
.'|(?:['.self::TEXT.self::CLOSER.']\s+)'
.'(?=[\|'.self::OPENER.self::MIDDLE.self::CLOSER.self::SEPARATOR.'])'
.'|['.self::TEXT.self::CLOSER.']'
.')+)(.*)?`i',
$tokens[($stackPtr + 2)]['content'],
$match
Expand Down Expand Up @@ -82,7 +84,7 @@ private function getValidTypeName(string $typeName): string
{
$typeNameWithoutSpace = str_replace(' ', '', $typeName);
$parts = preg_split(
'/([\|'.self::OPENER.self::MIDDLE.self::CLOSER.'])/',
'/([\|'.self::OPENER.self::MIDDLE.self::CLOSER.self::SEPARATOR.'])/',
$typeNameWithoutSpace,
-1,
PREG_SPLIT_DELIM_CAPTURE
Expand All @@ -93,7 +95,7 @@ private function getValidTypeName(string $typeName): string
for ($i = 0; $i < $partsNumber; $i += 2) {
$validType .= $this->suggestType($parts[$i]).$parts[$i + 1];

if (in_array($parts[$i + 1], [',', ':'])) {
if (preg_match('/['.self::MIDDLE.']/', $parts[$i + 1])) {
$validType .= ' ';
}
}
Expand All @@ -102,7 +104,7 @@ private function getValidTypeName(string $typeName): string
$validType .= $this->suggestType($parts[$partsNumber]);
}

return $validType;
return trim($validType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,13 @@ echo ( float ) $a;
/** @method array<integer,boolean>|integer[]|array<integer> truc */
/** @method array<integer, boolean>| integer[] |string|(integer|boolean)[] truc */
/** @method array{scheme:string,host:string} truc */

/**
* Avoid weird bug
*
* @method array{
* @method array{scheme:integer,host:
* @method array<
* @method array<integer,
* @method array|
*/
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,13 @@ echo ( float ) $a;
/** @method array<int, bool>|int[]|array<int> truc */
/** @method array<int, bool>|int[]|string|(int|bool)[] truc */
/** @method array{scheme: string, host: string} truc */

/**
* Avoid weird bug
*
* @method array{
* @method array{scheme: int, host:
* @method array<
* @method array<int,
* @method array|
*/
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @group SymfonyCustom
*/
class ValidScalarTypeNameUnitTest extends AbstractSniffUnitTest
class ValidTypeHintUnitTest extends AbstractSniffUnitTest
{
/**
* @return array
Expand All @@ -38,6 +38,8 @@ protected function getErrorList(): array
46 => 1,
47 => 1,
48 => 1,
54 => 1,
56 => 1,
];
}

Expand Down