diff --git a/SymfonyCustom/Sniffs/NamingConventions/ValidScalarTypeNameSniff.php b/SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php similarity index 81% rename from SymfonyCustom/Sniffs/NamingConventions/ValidScalarTypeNameSniff.php rename to SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php index 338f999..f004020 100644 --- a/SymfonyCustom/Sniffs/NamingConventions/ValidScalarTypeNameSniff.php +++ b/SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php @@ -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 @@ -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 @@ -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 @@ -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 .= ' '; } } @@ -102,7 +104,7 @@ private function getValidTypeName(string $typeName): string $validType .= $this->suggestType($parts[$partsNumber]); } - return $validType; + return trim($validType); } /** diff --git a/SymfonyCustom/Tests/NamingConventions/ValidScalarTypeNameUnitTest.inc b/SymfonyCustom/Tests/NamingConventions/ValidTypeHintUnitTest.inc similarity index 85% rename from SymfonyCustom/Tests/NamingConventions/ValidScalarTypeNameUnitTest.inc rename to SymfonyCustom/Tests/NamingConventions/ValidTypeHintUnitTest.inc index ee000ab..c7ddaf2 100644 --- a/SymfonyCustom/Tests/NamingConventions/ValidScalarTypeNameUnitTest.inc +++ b/SymfonyCustom/Tests/NamingConventions/ValidTypeHintUnitTest.inc @@ -46,3 +46,13 @@ echo ( float ) $a; /** @method array|integer[]|array truc */ /** @method array| 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|int[]|array truc */ /** @method array|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 1, 47 => 1, 48 => 1, + 54 => 1, + 56 => 1, ]; }