From cc04334ed0ce5a251389112fbd2dbe1dbc931ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20Hansl=C3=ADk?= Date: Sun, 14 May 2023 21:57:49 +0200 Subject: [PATCH] Fixed ArrayHelper --- .../Helpers/ArrayHelper.php | 21 +++--- .../Helpers/ArrayKeyValue.php | 13 ---- .../data/disallowPartiallyKeyedNoErrors.php | 75 +++++++++++-------- 3 files changed, 53 insertions(+), 56 deletions(-) diff --git a/SlevomatCodingStandard/Helpers/ArrayHelper.php b/SlevomatCodingStandard/Helpers/ArrayHelper.php index 3e10da169..8bf354b48 100644 --- a/SlevomatCodingStandard/Helpers/ArrayHelper.php +++ b/SlevomatCodingStandard/Helpers/ArrayHelper.php @@ -62,17 +62,16 @@ public static function parse(File $phpcsFile, int $arrayPointer): array $nextEffectivePointer = TokenHelper::findNextEffective($phpcsFile, $i + 1); if ($nextEffectivePointer === $arrayCloserPointer) { - $arrayKeyValueEndPointer = $i; + $arrayKeyValueEndPointer = self::getValueEndPointer($phpcsFile, $i, $arrayCloserPointer, $indentation); break; } if ($token['code'] !== T_COMMA || !ScopeHelper::isInSameScope($phpcsFile, $arrayOpenerPointer, $i)) { + $arrayKeyValueEndPointer = $i; continue; } - $arrayKeyValueEndPointer = $tokens[$nextEffectivePointer]['line'] === $tokens[$i]['line'] - ? $nextEffectivePointer - 1 - : self::getValueEndPointer($phpcsFile, $i, $indentation); + $arrayKeyValueEndPointer = self::getValueEndPointer($phpcsFile, $i, $arrayCloserPointer, $indentation); $keyValues[] = new ArrayKeyValue($phpcsFile, $arrayKeyValueStartPointer, $arrayKeyValueEndPointer); @@ -80,11 +79,7 @@ public static function parse(File $phpcsFile, int $arrayPointer): array $i = $arrayKeyValueEndPointer; } - $keyValues[] = new ArrayKeyValue( - $phpcsFile, - $arrayKeyValueStartPointer, - self::getValueEndPointer($phpcsFile, $arrayKeyValueEndPointer, $indentation) - ); + $keyValues[] = new ArrayKeyValue($phpcsFile, $arrayKeyValueStartPointer, $arrayKeyValueEndPointer); return $keyValues; } @@ -200,11 +195,15 @@ public static function openClosePointers(array $token): array return [(int) $pointerOpener, (int) $pointerCloser]; } - private static function getValueEndPointer(File $phpcsFile, int $endPointer, string $indentation): int + private static function getValueEndPointer(File $phpcsFile, int $endPointer, int $arrayCloserPointer, string $indentation): int { $tokens = $phpcsFile->getTokens(); - $nextEffectivePointer = TokenHelper::findNextEffective($phpcsFile, $endPointer + 1); + $nextEffectivePointer = TokenHelper::findNextEffective($phpcsFile, $endPointer + 1, $arrayCloserPointer + 1); + + if ($tokens[$nextEffectivePointer]['line'] === $tokens[$endPointer]['line']) { + return $nextEffectivePointer - 1; + } for ($i = $endPointer + 1; $i < $nextEffectivePointer; $i++) { if ($tokens[$i]['line'] === $tokens[$endPointer]['line']) { diff --git a/SlevomatCodingStandard/Helpers/ArrayKeyValue.php b/SlevomatCodingStandard/Helpers/ArrayKeyValue.php index b43d4d987..3588426df 100644 --- a/SlevomatCodingStandard/Helpers/ArrayKeyValue.php +++ b/SlevomatCodingStandard/Helpers/ArrayKeyValue.php @@ -3,7 +3,6 @@ namespace SlevomatCodingStandard\Helpers; use PHP_CodeSniffer\Files\File; -use function array_key_exists; use function in_array; use function ltrim; use function rtrim; @@ -146,18 +145,6 @@ private function addValues(File $phpcsFile): void continue; } - if (array_key_exists('scope_closer', $token) && $token['scope_closer'] > $i) { - $key .= TokenHelper::getContent($phpcsFile, $i, $token['scope_closer']); - $i = $token['scope_closer']; - continue; - } - - if (array_key_exists('parenthesis_closer', $token)) { - $key .= TokenHelper::getContent($phpcsFile, $i, $token['parenthesis_closer']); - $i = $token['parenthesis_closer']; - continue; - } - if ($firstNonWhitespace === null && $token['code'] !== T_WHITESPACE) { $firstNonWhitespace = $i; } diff --git a/tests/Sniffs/Arrays/data/disallowPartiallyKeyedNoErrors.php b/tests/Sniffs/Arrays/data/disallowPartiallyKeyedNoErrors.php index 09d8cb296..afd3ab9ff 100644 --- a/tests/Sniffs/Arrays/data/disallowPartiallyKeyedNoErrors.php +++ b/tests/Sniffs/Arrays/data/disallowPartiallyKeyedNoErrors.php @@ -1,36 +1,47 @@ = 7.4 -[]; +// []; +// +// array(); +// +// ['foo', 'bar', 'baz']; +// +// array('foo', 'bar', 'baz'); +// +// $a = [ +// 0 => 'zero', +// 'foo' => 'foo', +// 'bar' => 'bar', +// 'baz' => 'baz' +// ]; +// +// array( +// 0 => 'zero', +// 'foo' => 'foo', +// ...$a, +// 'bar' => 'bar', +// 'baz' => 'baz', +// ...$a +// ); +// +// [ +// 'bail', +// 'array', +// 'required', +// static function (array $value): array { +// foreach ($value as $x => $z) { +// $x + $z; +// } +// }, +// ]; -array(); +['newsletter' => [], 'campaign' => [], 'other' => []]; -['foo', 'bar', 'baz']; - -array('foo', 'bar', 'baz'); - -$a = [ - 0 => 'zero', - 'foo' => 'foo', - 'bar' => 'bar', - 'baz' => 'baz' -]; - -array( - 0 => 'zero', - 'foo' => 'foo', - ...$a, - 'bar' => 'bar', - 'baz' => 'baz', - ...$a -); - -[ - 'bail', - 'array', - 'required', - static function (array $value): array { - foreach ($value as $x => $z) { - $x + $z; - } - }, -]; +$data = [ + 'contactId' => 'string', + 'updates' => [ + [ + 'update' => [['__tpe' => 'Personal'], 'email@domain.tld'], + '__tpe' => 'ContactEmailUpdate', + ], + ]];