• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

keradus / PHP-CS-Fixer / 16999983712

15 Aug 2025 09:42PM UTC coverage: 94.75% (-0.09%) from 94.839%
16999983712

push

github

keradus
ci: more self-fixing checks on lowest/highest PHP

28263 of 29829 relevant lines covered (94.75%)

45.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.13
/src/Tokenizer/Analyzer/FunctionsAnalyzer.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer\Tokenizer\Analyzer;
16

17
use PhpCsFixer\Tokenizer\Analyzer\Analysis\ArgumentAnalysis;
18
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis;
19
use PhpCsFixer\Tokenizer\Analyzer\Analysis\TypeAnalysis;
20
use PhpCsFixer\Tokenizer\CT;
21
use PhpCsFixer\Tokenizer\FCT;
22
use PhpCsFixer\Tokenizer\Token;
23
use PhpCsFixer\Tokenizer\Tokens;
24

25
/**
26
 * @internal
27
 */
28
final class FunctionsAnalyzer
29
{
30
    private const POSSIBLE_KINDS = [
31
        \T_DOUBLE_COLON, \T_FUNCTION, CT::T_NAMESPACE_OPERATOR, \T_NEW, CT::T_RETURN_REF, \T_STRING, \T_OBJECT_OPERATOR, FCT::T_NULLSAFE_OBJECT_OPERATOR, FCT::T_ATTRIBUTE];
32

33
    /**
34
     * @var array{tokens: string, imports: list<NamespaceUseAnalysis>, declarations: list<int>}
35
     */
36
    private array $functionsAnalysis = ['tokens' => '', 'imports' => [], 'declarations' => []];
37

38
    /**
39
     * Important: risky because of the limited (file) scope of the tool.
40
     */
41
    public function isGlobalFunctionCall(Tokens $tokens, int $index): bool
42
    {
43
        if (!$tokens[$index]->isGivenKind(\T_STRING)) {
38✔
44
            return false;
38✔
45
        }
46

47
        $openParenthesisIndex = $tokens->getNextMeaningfulToken($index);
38✔
48

49
        if (!$tokens[$openParenthesisIndex]->equals('(')) {
38✔
50
            return false;
20✔
51
        }
52

53
        $previousIsNamespaceSeparator = false;
36✔
54
        $prevIndex = $tokens->getPrevMeaningfulToken($index);
36✔
55

56
        if ($tokens[$prevIndex]->isGivenKind(\T_NS_SEPARATOR)) {
36✔
57
            $previousIsNamespaceSeparator = true;
5✔
58
            $prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
5✔
59
        }
60

61
        if ($tokens[$prevIndex]->isGivenKind(self::POSSIBLE_KINDS)) {
36✔
62
            return false;
23✔
63
        }
64

65
        if ($tokens[$tokens->getNextMeaningfulToken($openParenthesisIndex)]->isGivenKind(CT::T_FIRST_CLASS_CALLABLE)) {
27✔
66
            return false;
1✔
67
        }
68

69
        if ($previousIsNamespaceSeparator) {
26✔
70
            return true;
1✔
71
        }
72

73
        $functionName = strtolower($tokens[$index]->getContent());
25✔
74

75
        if ('set' === $functionName) {
25✔
76
            if (!$tokens[$prevIndex]->equalsAny([[CT::T_PROPERTY_HOOK_BRACE_OPEN], ';', '}'])) {
2✔
77
                return true;
1✔
78
            }
79
            $closeParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesisIndex);
2✔
80
            $afterCloseParenthesisIndex = $tokens->getNextMeaningfulToken($closeParenthesisIndex);
2✔
81
            if ($tokens[$afterCloseParenthesisIndex]->equalsAny(['{', [\T_DOUBLE_ARROW]])) {
2✔
82
                return false;
1✔
83
            }
84
        }
85

86
        if ($tokens->isChanged() || $tokens->getCodeHash() !== $this->functionsAnalysis['tokens']) {
24✔
87
            $this->buildFunctionsAnalysis($tokens);
24✔
88
        }
89

90
        // figure out in which namespace we are
91
        $scopeStartIndex = 0;
24✔
92
        $scopeEndIndex = \count($tokens) - 1;
24✔
93
        $inGlobalNamespace = false;
24✔
94

95
        foreach ($tokens->getNamespaceDeclarations() as $declaration) {
24✔
96
            $scopeStartIndex = $declaration->getScopeStartIndex();
24✔
97
            $scopeEndIndex = $declaration->getScopeEndIndex();
24✔
98

99
            if ($index >= $scopeStartIndex && $index <= $scopeEndIndex) {
24✔
100
                $inGlobalNamespace = $declaration->isGlobalNamespace();
24✔
101

102
                break;
24✔
103
            }
104
        }
105

106
        // check if the call is to a function declared in the same namespace as the call is done,
107
        // if the call is already in the global namespace than declared functions are in the same
108
        // global namespace and don't need checking
109

110
        if (!$inGlobalNamespace) {
24✔
111
            foreach ($this->functionsAnalysis['declarations'] as $functionNameIndex) {
4✔
112
                if ($functionNameIndex < $scopeStartIndex || $functionNameIndex > $scopeEndIndex) {
3✔
113
                    continue;
1✔
114
                }
115

116
                if (strtolower($tokens[$functionNameIndex]->getContent()) === $functionName) {
2✔
117
                    return false;
2✔
118
                }
119
            }
120
        }
121

122
        foreach ($this->functionsAnalysis['imports'] as $functionUse) {
22✔
123
            if ($functionUse->getStartIndex() < $scopeStartIndex || $functionUse->getEndIndex() > $scopeEndIndex) {
3✔
124
                continue;
1✔
125
            }
126

127
            if ($functionName !== strtolower($functionUse->getShortName())) {
3✔
128
                continue;
2✔
129
            }
130

131
            // global import like `use function \str_repeat;`
132
            return $functionUse->getShortName() === ltrim($functionUse->getFullName(), '\\');
1✔
133
        }
134

135
        if (AttributeAnalyzer::isAttribute($tokens, $index)) {
21✔
136
            return false;
1✔
137
        }
138

139
        return true;
20✔
140
    }
141

142
    /**
143
     * @return array<string, ArgumentAnalysis>
144
     */
145
    public function getFunctionArguments(Tokens $tokens, int $functionIndex): array
146
    {
147
        $argumentsStart = $tokens->getNextTokenOfKind($functionIndex, ['(']);
17✔
148
        $argumentsEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $argumentsStart);
17✔
149
        $argumentAnalyzer = new ArgumentsAnalyzer();
17✔
150
        $arguments = [];
17✔
151

152
        foreach ($argumentAnalyzer->getArguments($tokens, $argumentsStart, $argumentsEnd) as $start => $end) {
17✔
153
            $argumentInfo = $argumentAnalyzer->getArgumentInfo($tokens, $start, $end);
15✔
154
            $arguments[$argumentInfo->getName()] = $argumentInfo;
15✔
155
        }
156

157
        return $arguments;
17✔
158
    }
159

160
    public function getFunctionReturnType(Tokens $tokens, int $methodIndex): ?TypeAnalysis
161
    {
162
        $argumentsStart = $tokens->getNextTokenOfKind($methodIndex, ['(']);
9✔
163
        $argumentsEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $argumentsStart);
9✔
164
        $typeColonIndex = $tokens->getNextMeaningfulToken($argumentsEnd);
9✔
165

166
        if (!$tokens[$typeColonIndex]->isGivenKind(CT::T_TYPE_COLON)) {
9✔
167
            return null;
3✔
168
        }
169

170
        $type = '';
6✔
171
        $typeStartIndex = $tokens->getNextMeaningfulToken($typeColonIndex);
6✔
172
        $typeEndIndex = $typeStartIndex;
6✔
173
        $functionBodyStart = $tokens->getNextTokenOfKind($typeColonIndex, ['{', ';', [\T_DOUBLE_ARROW]]);
6✔
174

175
        for ($i = $typeStartIndex; $i < $functionBodyStart; ++$i) {
6✔
176
            if ($tokens[$i]->isWhitespace() || $tokens[$i]->isComment()) {
6✔
177
                continue;
6✔
178
            }
179

180
            $type .= $tokens[$i]->getContent();
6✔
181
            $typeEndIndex = $i;
6✔
182
        }
183

184
        return new TypeAnalysis($type, $typeStartIndex, $typeEndIndex);
6✔
185
    }
186

187
    public function isTheSameClassCall(Tokens $tokens, int $index): bool
188
    {
189
        if (!$tokens->offsetExists($index)) {
10✔
190
            throw new \InvalidArgumentException(\sprintf('Token index %d does not exist.', $index));
1✔
191
        }
192

193
        $operatorIndex = $tokens->getPrevMeaningfulToken($index);
9✔
194

195
        if (null === $operatorIndex) {
9✔
196
            return false;
9✔
197
        }
198

199
        if (!$tokens[$operatorIndex]->isObjectOperator() && !$tokens[$operatorIndex]->isGivenKind(\T_DOUBLE_COLON)) {
9✔
200
            return false;
9✔
201
        }
202

203
        $referenceIndex = $tokens->getPrevMeaningfulToken($operatorIndex);
9✔
204

205
        if (null === $referenceIndex) {
9✔
206
            return false;
×
207
        }
208

209
        if (!$tokens[$referenceIndex]->equalsAny([[\T_VARIABLE, '$this'], [\T_STRING, 'self'], [\T_STATIC, 'static']], false)) {
9✔
210
            return false;
2✔
211
        }
212

213
        return $tokens[$tokens->getNextMeaningfulToken($index)]->equals('(');
7✔
214
    }
215

216
    private function buildFunctionsAnalysis(Tokens $tokens): void
217
    {
218
        $this->functionsAnalysis = [
24✔
219
            'tokens' => $tokens->getCodeHash(),
24✔
220
            'imports' => [],
24✔
221
            'declarations' => [],
24✔
222
        ];
24✔
223

224
        // find declarations
225

226
        if ($tokens->isTokenKindFound(\T_FUNCTION)) {
24✔
227
            $end = \count($tokens);
12✔
228

229
            for ($i = 0; $i < $end; ++$i) {
12✔
230
                // skip classy, we are looking for functions not methods
231
                if ($tokens[$i]->isGivenKind(Token::getClassyTokenKinds())) {
12✔
232
                    $i = $tokens->getNextTokenOfKind($i, ['(', '{']);
4✔
233

234
                    if ($tokens[$i]->equals('(')) { // anonymous class
4✔
235
                        $i = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $i);
1✔
236
                        $i = $tokens->getNextTokenOfKind($i, ['{']);
1✔
237
                    }
238

239
                    $i = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $i);
4✔
240

241
                    continue;
4✔
242
                }
243

244
                if (!$tokens[$i]->isGivenKind(\T_FUNCTION)) {
12✔
245
                    continue;
12✔
246
                }
247

248
                $i = $tokens->getNextMeaningfulToken($i);
8✔
249

250
                if ($tokens[$i]->isGivenKind(CT::T_RETURN_REF)) {
8✔
251
                    $i = $tokens->getNextMeaningfulToken($i);
1✔
252
                }
253

254
                if (!$tokens[$i]->isGivenKind(\T_STRING)) {
8✔
255
                    continue;
1✔
256
                }
257

258
                $this->functionsAnalysis['declarations'][] = $i;
8✔
259
            }
260
        }
261

262
        // find imported functions
263

264
        $namespaceUsesAnalyzer = new NamespaceUsesAnalyzer();
24✔
265

266
        if ($tokens->isTokenKindFound(CT::T_FUNCTION_IMPORT)) {
24✔
267
            $declarations = $namespaceUsesAnalyzer->getDeclarationsFromTokens($tokens);
3✔
268

269
            foreach ($declarations as $declaration) {
3✔
270
                if ($declaration->isFunction()) {
3✔
271
                    $this->functionsAnalysis['imports'][] = $declaration;
3✔
272
                }
273
            }
274
        }
275
    }
276
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc