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

keradus / PHP-CS-Fixer / 15295226534

28 May 2025 08:23AM UTC coverage: 94.849% (-0.01%) from 94.859%
15295226534

push

github

keradus
DX: introduce `FCT` class for tokens not present in the lowest supported PHP version (#8706)

Co-authored-by: Dariusz Rumiński <dariusz.ruminski@gmail.com>

186 of 192 new or added lines in 52 files covered. (96.88%)

307 existing lines in 29 files now uncovered.

28099 of 29625 relevant lines covered (94.85%)

45.33 hits per line

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

99.12
/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)) {
37✔
44
            return false;
37✔
45
        }
46

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

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

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

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

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

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

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

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

75
        if ('set' === $functionName) {
24✔
76
            $closeParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesisIndex);
1✔
77
            $afterCloseParenthesisIndex = $tokens->getNextMeaningfulToken($closeParenthesisIndex);
1✔
78
            if ($tokens[$afterCloseParenthesisIndex]->equalsAny(['{', [T_DOUBLE_ARROW]])) {
1✔
79
                return false;
1✔
80
            }
81
        }
82

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

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

92
        foreach ($tokens->getNamespaceDeclarations() as $declaration) {
24✔
93
            $scopeStartIndex = $declaration->getScopeStartIndex();
24✔
94
            $scopeEndIndex = $declaration->getScopeEndIndex();
24✔
95

96
            if ($index >= $scopeStartIndex && $index <= $scopeEndIndex) {
24✔
97
                $inGlobalNamespace = $declaration->isGlobalNamespace();
24✔
98

99
                break;
24✔
100
            }
101
        }
102

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

107
        if (!$inGlobalNamespace) {
24✔
108
            /** @var int $functionNameIndex */
109
            foreach ($this->functionsAnalysis['declarations'] as $functionNameIndex) {
4✔
110
                if ($functionNameIndex < $scopeStartIndex || $functionNameIndex > $scopeEndIndex) {
3✔
111
                    continue;
1✔
112
                }
113

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

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

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

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

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

138
        return true;
20✔
139
    }
140

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

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

156
        return $arguments;
16✔
157
    }
158

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

223
        // find declarations
224

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

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

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

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

240
                    continue;
4✔
241
                }
242

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

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

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

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

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

261
        // find imported functions
262

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

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

268
            foreach ($declarations as $declaration) {
3✔
269
                if ($declaration->isFunction()) {
3✔
270
                    $this->functionsAnalysis['imports'][] = $declaration;
3✔
271
                }
272
            }
273
        }
274
    }
275
}
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