Skip to content

Commit ca389e2

Browse files
committed
Refactored tools & custom fixer classes
2 parents 19cdd3c + 4595ba1 commit ca389e2

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

src/Fixer/AlignedMethodChainFixer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ private function findNextChain(int $search): ?int
8686

8787
private function isStartOfMultilineChain(int $idx): bool
8888
{
89-
$next = ($this->tokens[$idx + 2]->getContent() === '(') ? $this->findClosing($idx + 2) + 1 : $idx + 2;
89+
$next = $this->tokens[$idx + 2]->getContent() === '(' ? $this->findClosing($idx + 2) + 1 : $idx + 2;
9090
if ($this->tokens[$next]->isWhitespace() && $this->tokens[$next + 1]->isGivenKind(T_OBJECT_OPERATOR)) {
9191
return true;
9292
}
9393

94-
return $this->tokens[$next]->isGivenKind(T_OBJECT_OPERATOR) ? $this->isStartOfMultilineChain($next) : false;
94+
return $this->tokens[$next]->isGivenKind(T_OBJECT_OPERATOR) && $this->isStartOfMultilineChain($next);
9595
}
9696

9797
private function indentationLength(int $idx): int

src/Fixer/ArrayContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private function isMultipleAssign(int $firstArrow, int $lineEnd): bool
7878
$last = $this->tokens->getPrevTokenOfKind($lineEnd, [[T_DOUBLE_ARROW], [CT::T_ARRAY_SQUARE_BRACE_CLOSE]]);
7979
if ($this->tokens[$last]->isGivenKind(CT::T_ARRAY_SQUARE_BRACE_CLOSE)) {
8080
$continueFrom = $this->tokens->findBlockStart(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $last);
81-
return $continueFrom < $firstArrow ? true : $this->isMultipleAssign($firstArrow, $continueFrom);
81+
return $continueFrom < $firstArrow || $this->isMultipleAssign($firstArrow, $continueFrom);
8282
}
8383

8484
return $firstArrow !== $last;

src/Fixer/NoTrailingCommaInMultilineArrayFixer.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
use PhpCsFixer\Fixer\FixerInterface;
1515
use PhpCsFixer\FixerDefinition\FixerDefinition;
1616
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
17-
use PhpCsFixer\Tokenizer\CT;
1817
use PhpCsFixer\Tokenizer\Tokens;
19-
use PhpCsFixer\Tokenizer\TokensAnalyzer;
18+
use PhpCsFixer\Tokenizer\CT;
2019
use SplFileInfo;
2120

2221

@@ -54,13 +53,36 @@ public function isCandidate(Tokens $tokens): bool
5453

5554
public function fix(SplFileInfo $file, Tokens $tokens): void
5655
{
57-
$tokensAnalyzer = new TokensAnalyzer($tokens);
58-
5956
for ($idx = $tokens->count() - 1; $idx >= 0; --$idx) {
60-
if ($tokensAnalyzer->isArray($idx) && $tokensAnalyzer->isArrayMultiLine($idx)) {
61-
$this->fixArray($tokens, $idx);
57+
if (!$this->isMultilineArray($tokens, $idx)) { continue; }
58+
$this->fixArray($tokens, $idx);
59+
}
60+
}
61+
62+
private function isMultilineArray(Tokens $tokens, int $idx): bool
63+
{
64+
$isArray = $tokens[$idx]->isGivenKind([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN]);
65+
if (!$isArray) { return false; }
66+
67+
$openBrace = $tokens[$idx]->isGivenKind(T_ARRAY) ? $tokens->getNextMeaningfulToken($idx) : $idx;
68+
$blockType = Tokens::detectBlockType($tokens[$openBrace]);
69+
if (!$blockType || !$blockType['isStart']) { return false; }
70+
71+
$endIndex = $tokens->findBlockEnd($blockType['type'], $openBrace);
72+
for ($index = $openBrace + 1; $index < $endIndex; ++$index) {
73+
$token = $tokens[$index];
74+
$blockType = Tokens::detectBlockType($token);
75+
76+
if ($blockType && $blockType['isStart']) {
77+
$index = $tokens->findBlockEnd($blockType['type'], $index);
78+
continue;
6279
}
80+
81+
$isLineBreak = $token->isWhitespace() && strpos($token->getContent(), "\n") !== false;
82+
if ($isLineBreak) { return true; }
6383
}
84+
85+
return false;
6486
}
6587

6688
private function fixArray(Tokens $tokens, int $idx): void

src/Tools/FixerTokens.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ public static function dump(Tokens $tokens, ?string $dumpFile = null): void
5151
];
5252
}
5353

54-
self::json($data, $dumpFile ?: dirname(dirname(__DIR__)) . '/temp/tokens-dump.json');
54+
self::json($data, $dumpFile ?: dirname(__DIR__, 2) . '/temp/tokens-dump.json');
5555
}
5656
}

src/Tools/SnifferTokens.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use PHP_CodeSniffer\Config;
1717
use PHP_CodeSniffer\Exceptions;
1818

19-
require_once dirname(dirname(__DIR__)) . '/vendor/squizlabs/php_codesniffer/autoload.php';
19+
require_once dirname(__DIR__, 2) . '/vendor/squizlabs/php_codesniffer/autoload.php';
2020
if (!defined('PHP_CODESNIFFER_CBF')) {
2121
define('PHP_CODESNIFFER_CBF', false);
2222
}
@@ -35,7 +35,7 @@ final class SnifferTokens
3535
*/
3636
public static function runner(?string $configFile = null): Runner
3737
{
38-
$configFile = $configFile ?: dirname(dirname(__DIR__)) . '/phpcs.xml.dist';
38+
$configFile = $configFile ?: dirname(__DIR__, 2) . '/phpcs.xml.dist';
3939
$runner = new Runner();
4040
$runner->config = new Config(['-q', '--standard=' . $configFile]);
4141
$runner->init();
@@ -84,6 +84,6 @@ public static function dump(Files\File $tokens, ?string $tokensFile = null): voi
8484
$token = ['idx' => $id] + $token;
8585
}
8686

87-
self::json($tokens, $tokensFile ?: dirname(dirname(__DIR__)) . '/temp/tokens-dump.json');
87+
self::json($tokens, $tokensFile ?: dirname(__DIR__, 2) . '/temp/tokens-dump.json');
8888
}
8989
}

0 commit comments

Comments
 (0)