From af8154f8a28cb291f8b0808d30960ba31393c98b Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Thu, 29 Jul 2021 20:23:44 +0200 Subject: [PATCH] Improve support of array shape --- .../NamingConventions/ValidTypeHintSniff.php | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php b/SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php index d132c1b..8511bd6 100644 --- a/SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php +++ b/SymfonyCustom/Sniffs/NamingConventions/ValidTypeHintSniff.php @@ -112,14 +112,14 @@ private function getValidTypes(string $content): string while ('' !== $content && false !== $content) { preg_match('{^'.SniffHelper::REGEX_TYPES.'$}ix', $content, $matches); - if (isset($matches['array']) && '' !== $matches['array']) { - $validType = $this->getValidTypes(mb_substr($matches['array'], 0, -2)).'[]'; - } elseif (isset($matches['multiple']) && '' !== $matches['multiple']) { + if (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']); + } elseif (isset($matches['generic']) && '' !== $matches['generic']) { + $validType = $this->getValidGenericType($matches['genericName'], $matches['genericContent']); + } elseif (isset($matches['array']) && '' !== $matches['array']) { + $validType = $this->getValidTypes(mb_substr($matches['array'], 0, -2)).'[]'; } else { $validType = $this->getValidType($matches['type']); } @@ -203,11 +203,13 @@ private function getValidObjectType(string $objectContent): string $validType = 'array{'; while ('' !== $objectContent && false !== $objectContent) { - $split = preg_split('/(\??:|,)/', $objectContent, 2, PREG_SPLIT_DELIM_CAPTURE); + if (0 !== mb_strpos($objectContent, 'array{') && 0 !== mb_strpos($objectContent, 'array<')) { + $split = preg_split('/(\??:|,)/', $objectContent, 2, PREG_SPLIT_DELIM_CAPTURE); - if (isset($split[1]) && ',' !== $split[1]) { - $validType .= $split[0].$split[1].' '; - $objectContent = $split[2]; + if (isset($split[1]) && ',' !== $split[1]) { + $validType .= $split[0].$split[1].' '; + $objectContent = $split[2]; + } } preg_match('{^'.SniffHelper::REGEX_TYPES.',?}ix', $objectContent, $matches);