Skip to content

Commit

Permalink
Fixed ArrayHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed May 14, 2023
1 parent 29b2d1c commit cc04334
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 56 deletions.
21 changes: 10 additions & 11 deletions SlevomatCodingStandard/Helpers/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,24 @@ 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);

$arrayKeyValueStartPointer = $arrayKeyValueEndPointer + 1;
$i = $arrayKeyValueEndPointer;
}

$keyValues[] = new ArrayKeyValue(
$phpcsFile,
$arrayKeyValueStartPointer,
self::getValueEndPointer($phpcsFile, $arrayKeyValueEndPointer, $indentation)
);
$keyValues[] = new ArrayKeyValue($phpcsFile, $arrayKeyValueStartPointer, $arrayKeyValueEndPointer);

return $keyValues;
}
Expand Down Expand Up @@ -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']) {
Expand Down
13 changes: 0 additions & 13 deletions SlevomatCodingStandard/Helpers/ArrayKeyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
75 changes: 43 additions & 32 deletions tests/Sniffs/Arrays/data/disallowPartiallyKeyedNoErrors.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,47 @@
<?php // lint >= 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 protected]'],
'__tpe' => 'ContactEmailUpdate',
],
]];

0 comments on commit cc04334

Please sign in to comment.