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

keradus / PHP-CS-Fixer / 24023665044

02 Apr 2026 09:33PM UTC coverage: 93.056% (+0.1%) from 92.938%
24023665044

push

github

web-flow
chore: add tests for `BracesPositionFixer` (#9522)

29548 of 31753 relevant lines covered (93.06%)

43.98 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
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
29
 */
30
final class FunctionsAnalyzer
31
{
32
    private const POSSIBLE_KINDS = [
33
        \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];
34

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

40
    /**
41
     * @return array<string, ArgumentAnalysis>
42
     */
43
    public function getFunctionArguments(Tokens $tokens, int $functionIndex): array
44
    {
45
        $argumentsStart = $tokens->getNextTokenOfKind($functionIndex, ['(']);
17✔
46
        $argumentsEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $argumentsStart);
17✔
47
        $argumentAnalyzer = new ArgumentsAnalyzer();
17✔
48
        $arguments = [];
17✔
49

50
        foreach ($argumentAnalyzer->getArguments($tokens, $argumentsStart, $argumentsEnd) as $start => $end) {
17✔
51
            $argumentInfo = $argumentAnalyzer->getArgumentInfo($tokens, $start, $end);
15✔
52
            $arguments[$argumentInfo->getName()] = $argumentInfo;
15✔
53
        }
54

55
        return $arguments;
17✔
56
    }
57

58
    public function getFunctionReturnType(Tokens $tokens, int $methodIndex): ?TypeAnalysis
59
    {
60
        $argumentsStart = $tokens->getNextTokenOfKind($methodIndex, ['(']);
9✔
61
        $argumentsEnd = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $argumentsStart);
9✔
62
        $typeColonIndex = $tokens->getNextMeaningfulToken($argumentsEnd);
9✔
63

64
        if (!$tokens[$typeColonIndex]->isGivenKind(CT::T_TYPE_COLON)) {
9✔
65
            return null;
3✔
66
        }
67

68
        $type = '';
6✔
69
        $typeStartIndex = $tokens->getNextMeaningfulToken($typeColonIndex);
6✔
70
        $typeEndIndex = $typeStartIndex;
6✔
71
        $functionBodyStart = $tokens->getNextTokenOfKind($typeColonIndex, ['{', ';', [\T_DOUBLE_ARROW]]);
6✔
72

73
        for ($i = $typeStartIndex; $i < $functionBodyStart; ++$i) {
6✔
74
            if ($tokens[$i]->isWhitespace() || $tokens[$i]->isComment()) {
6✔
75
                continue;
6✔
76
            }
77

78
            $type .= $tokens[$i]->getContent();
6✔
79
            $typeEndIndex = $i;
6✔
80
        }
81

82
        return new TypeAnalysis($type, $typeStartIndex, $typeEndIndex);
6✔
83
    }
84

85
    public function isTheSameClassCall(Tokens $tokens, int $index): bool
86
    {
87
        if (!$tokens->offsetExists($index)) {
10✔
88
            throw new \InvalidArgumentException(\sprintf('Token index %d does not exist.', $index));
1✔
89
        }
90

91
        $operatorIndex = $tokens->getPrevMeaningfulToken($index);
9✔
92

93
        if (null === $operatorIndex) {
9✔
94
            return false;
9✔
95
        }
96

97
        if (!$tokens[$operatorIndex]->isObjectOperator() && !$tokens[$operatorIndex]->isGivenKind(\T_DOUBLE_COLON)) {
9✔
98
            return false;
9✔
99
        }
100

101
        $referenceIndex = $tokens->getPrevMeaningfulToken($operatorIndex);
9✔
102

103
        if (null === $referenceIndex) {
9✔
104
            return false;
×
105
        }
106

107
        if (!$tokens[$referenceIndex]->equalsAny([[\T_VARIABLE, '$this'], [\T_STRING, 'self'], [\T_STATIC, 'static']], false)) {
9✔
108
            return false;
2✔
109
        }
110

111
        return $tokens[$tokens->getNextMeaningfulToken($index)]->equals('(');
7✔
112
    }
113

114
    /**
115
     * Important: risky because of the limited (file) scope of the tool.
116
     */
117
    public function isGlobalFunctionCall(Tokens $tokens, int $index): bool
118
    {
119
        if (!$tokens[$index]->isGivenKind(\T_STRING)) {
38✔
120
            return false;
38✔
121
        }
122

123
        $openParenthesisIndex = $tokens->getNextMeaningfulToken($index);
38✔
124

125
        if (!$tokens[$openParenthesisIndex]->equals('(')) {
38✔
126
            return false;
20✔
127
        }
128

129
        $previousIsNamespaceSeparator = false;
36✔
130
        $prevIndex = $tokens->getPrevMeaningfulToken($index);
36✔
131

132
        if ($tokens[$prevIndex]->isGivenKind(\T_NS_SEPARATOR)) {
36✔
133
            $previousIsNamespaceSeparator = true;
5✔
134
            $prevIndex = $tokens->getPrevMeaningfulToken($prevIndex);
5✔
135
        }
136

137
        if ($tokens[$prevIndex]->isGivenKind(self::POSSIBLE_KINDS)) {
36✔
138
            return false;
23✔
139
        }
140

141
        if ($tokens[$tokens->getNextMeaningfulToken($openParenthesisIndex)]->isGivenKind(CT::T_FIRST_CLASS_CALLABLE)) {
27✔
142
            return false;
1✔
143
        }
144

145
        if ($previousIsNamespaceSeparator) {
26✔
146
            return true;
1✔
147
        }
148

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

151
        if ('set' === $functionName) {
25✔
152
            if (!$tokens[$prevIndex]->equalsAny([[CT::T_PROPERTY_HOOK_BRACE_OPEN], ';', '}'])) {
2✔
153
                return true;
1✔
154
            }
155
            $closeParenthesisIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesisIndex);
2✔
156
            $afterCloseParenthesisIndex = $tokens->getNextMeaningfulToken($closeParenthesisIndex);
2✔
157
            if ($tokens[$afterCloseParenthesisIndex]->equalsAny(['{', [\T_DOUBLE_ARROW]])) {
2✔
158
                return false;
1✔
159
            }
160
        }
161

162
        if ($tokens->isChanged() || $tokens->getCodeHash() !== $this->functionsAnalysis['tokens']) {
24✔
163
            $this->buildFunctionsAnalysis($tokens);
24✔
164
        }
165

166
        // figure out in which namespace we are
167
        $scopeStartIndex = 0;
24✔
168
        $scopeEndIndex = \count($tokens) - 1;
24✔
169
        $inGlobalNamespace = false;
24✔
170

171
        foreach ($tokens->getNamespaceDeclarations() as $declaration) {
24✔
172
            $scopeStartIndex = $declaration->getScopeStartIndex();
24✔
173
            $scopeEndIndex = $declaration->getScopeEndIndex();
24✔
174

175
            if ($index >= $scopeStartIndex && $index <= $scopeEndIndex) {
24✔
176
                $inGlobalNamespace = $declaration->isGlobalNamespace();
24✔
177

178
                break;
24✔
179
            }
180
        }
181

182
        // check if the call is to a function declared in the same namespace as the call is done,
183
        // if the call is already in the global namespace than declared functions are in the same
184
        // global namespace and don't need checking
185

186
        if (!$inGlobalNamespace) {
24✔
187
            foreach ($this->functionsAnalysis['declarations'] as $functionNameIndex) {
4✔
188
                if ($functionNameIndex < $scopeStartIndex || $functionNameIndex > $scopeEndIndex) {
3✔
189
                    continue;
1✔
190
                }
191

192
                if (strtolower($tokens[$functionNameIndex]->getContent()) === $functionName) {
2✔
193
                    return false;
2✔
194
                }
195
            }
196
        }
197

198
        foreach ($this->functionsAnalysis['imports'] as $functionUse) {
22✔
199
            if ($functionUse->getStartIndex() < $scopeStartIndex || $functionUse->getEndIndex() > $scopeEndIndex) {
3✔
200
                continue;
1✔
201
            }
202

203
            if ($functionName !== strtolower($functionUse->getShortName())) {
3✔
204
                continue;
2✔
205
            }
206

207
            // global import like `use function \str_repeat;`
208
            return $functionUse->getShortName() === ltrim($functionUse->getFullName(), '\\');
1✔
209
        }
210

211
        if (AttributeAnalyzer::isAttribute($tokens, $index)) {
21✔
212
            return false;
1✔
213
        }
214

215
        return true;
20✔
216
    }
217

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

226
        // find declarations
227

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

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

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

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

243
                    continue;
4✔
244
                }
245

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

250
                $i = $tokens->getNextMeaningfulToken($i);
8✔
251

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

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

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

264
        // find imported functions
265

266
        $namespaceUsesAnalyzer = new NamespaceUsesAnalyzer();
24✔
267

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

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