-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathIsArrayNotEmptyFixer.php
113 lines (88 loc) · 3.38 KB
/
IsArrayNotEmptyFixer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao\EasyCodingStandard\Fixer;
use PhpCsFixer\AbstractFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;
final class IsArrayNotEmptyFixer extends AbstractFixer
{
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'When checking isset() and is_array() or is_array() and !empty(), the is_array() check should be last.',
[
new CodeSample(
<<<'EOT'
<?php
if (!empty($array) && is_array($array)) {
}
EOT,
),
new CodeSample(
<<<'EOT'
<?php
if (isset($array) && is_array($array)) {
}
EOT,
),
],
);
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_STRING);
}
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
for ($index = 1, $count = \count($tokens); $index < $count; ++$index) {
if (!$tokens[$index]->isGivenKind(T_STRING)) {
continue;
}
if ('is_array' !== $tokens[$index]->getContent() || !$tokens[$index + 1]->equals('(')) {
continue;
}
$isArrayStart = $index;
if ($tokens[$isArrayStart - 1]->isGivenKind(T_NS_SEPARATOR)) {
--$isArrayStart;
}
$prevMeaningful = $tokens->getPrevMeaningfulToken($isArrayStart);
if (!$tokens[$prevMeaningful]->isGivenKind(T_BOOLEAN_AND) && !$tokens[$prevMeaningful]->equals('(')) {
continue;
}
$isArrayArg = $index + 1;
$isArrayEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $isArrayArg);
$booleanAnd = $tokens->getNextMeaningfulToken($isArrayEnd);
if (!$tokens[$booleanAnd]->isGivenKind(T_BOOLEAN_AND)) {
continue;
}
if (!$empty = $tokens->getNextTokenOfKind($booleanAnd, [[T_ISSET], [T_EMPTY]])) {
continue;
}
$emptyStart = $empty;
if ($tokens[$emptyStart - 1]->equals('!')) {
--$emptyStart;
}
$emptyArg = $empty + 1;
$emptyEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $emptyArg);
$isArrayContent = $tokens->generatePartialCode($isArrayArg + 1, $isArrayEnd - 1);
$emptyContent = $tokens->generatePartialCode($emptyArg + 1, $emptyEnd - 1);
if ($isArrayContent !== $emptyContent) {
continue;
}
$isArrayCode = $tokens->generatePartialCode($isArrayStart, $isArrayEnd);
$emptyCode = $tokens->generatePartialCode($emptyStart, $emptyEnd);
$tokens->clearRange($isArrayStart, $emptyEnd);
$tokens->insertAt($emptyEnd, Tokens::fromCode($emptyCode.' && '.$isArrayCode));
$index = $emptyEnd + 1;
}
}
}