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

keradus / PHP-CS-Fixer / 17252691116

26 Aug 2025 11:09PM UTC coverage: 94.743% (-0.01%) from 94.755%
17252691116

push

github

keradus
chore: apply phpdoc_tag_no_named_arguments

28313 of 29884 relevant lines covered (94.74%)

45.64 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
     * Important: risky because of the limited (file) scope of the tool.
42
     */
43
    public function isGlobalFunctionCall(Tokens $tokens, int $index): bool
44
    {
45
        if (!$tokens[$index]->isGivenKind(\T_STRING)) {
38✔
46
            return false;
38✔
47
        }
48

49
        $openParenthesisIndex = $tokens->getNextMeaningfulToken($index);
38✔
50

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

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

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

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

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

71
        if ($previousIsNamespaceSeparator) {
26✔
72
            return true;
1✔
73
        }
74

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

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

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

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

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

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

104
                break;
24✔
105
            }
106
        }
107

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

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

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

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

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

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

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

141
        return true;
20✔
142
    }
143

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

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

159
        return $arguments;
17✔
160
    }
161

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

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

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

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

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

186
        return new TypeAnalysis($type, $typeStartIndex, $typeEndIndex);
6✔
187
    }
188

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

195
        $operatorIndex = $tokens->getPrevMeaningfulToken($index);
9✔
196

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

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

205
        $referenceIndex = $tokens->getPrevMeaningfulToken($operatorIndex);
9✔
206

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

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

215
        return $tokens[$tokens->getNextMeaningfulToken($index)]->equals('(');
7✔
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