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

voku / Simple-PHP-Code-Parser / 24274065460

11 Apr 2026 03:53AM UTC coverage: 82.099% (-1.2%) from 83.27%
24274065460

push

github

web-flow
Merge pull request #78 from voku/copilot/update-php-dependencies

Update dependencies: php-parser v5, phpdoc-parser v2, reflection-docblock v6, type-resolver v2, phpunit v11

42 of 50 new or added lines in 9 files covered. (84.0%)

24 existing lines in 2 files now uncovered.

1541 of 1877 relevant lines covered (82.1%)

44.81 hits per line

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

85.89
/src/voku/SimplePhpParser/Model/PHPParameter.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace voku\SimplePhpParser\Model;
6

7
use PhpParser\Comment\Doc;
8
use PhpParser\Node\FunctionLike;
9
use PhpParser\Node\Param;
10
use ReflectionParameter;
11
use voku\SimplePhpParser\Parsers\Helper\DocFactoryProvider;
12
use voku\SimplePhpParser\Parsers\Helper\Utils;
13

14
class PHPParameter extends BasePHPElement
15
{
16
    /**
17
     * @var mixed|null
18
     */
19
    public $defaultValue;
20

21
    public ?string $phpDocRaw = null;
22

23
    public ?string $type = null;
24

25
    public ?string $typeFromDefaultValue = null;
26

27
    public ?string $typeFromPhpDoc = null;
28

29
    public ?string $typeFromPhpDocSimple = null;
30

31
    public ?string $typeFromPhpDocExtended = null;
32

33
    public ?string $typeFromPhpDocMaybeWithComment = null;
34

35
    public ?bool $is_vararg = null;
36

37
    public ?bool $is_passed_by_ref = null;
38

39
    public ?bool $is_inheritdoc = null;
40

41
    /**
42
     * PHP 8.0+ attributes on this parameter.
43
     *
44
     * @var PHPAttribute[]
45
     */
46
    public array $attributes = [];
47

48
    /**
49
     * @param Param        $parameter
50
     * @param FunctionLike $node
51
     * @param mixed|null   $classStr
52
     *
53
     * @return $this
54
     */
55
    public function readObjectFromPhpNode($parameter, $node = null, $classStr = null): self
56
    {
57
        $parameterVar = $parameter->var;
105✔
58
        if ($parameterVar instanceof \PhpParser\Node\Expr\Error) {
105✔
59
            $this->parseError[] = ($this->line ?? '?') . ':' . ($this->pos ?? '') . ' | may be at this position an expression is required';
×
60

61
            $this->name = \md5(\uniqid('error', true));
×
62

63
            return $this;
×
64
        }
65

66
        $this->name = \is_string($parameterVar->name) ? $parameterVar->name : '';
105✔
67

68
        if ($node) {
105✔
69
            $this->prepareNode($node);
105✔
70

71
            $docComment = $node->getDocComment();
105✔
72
            if ($docComment) {
105✔
73
                $docCommentText = $docComment->getText();
59✔
74

75
                if (\stripos($docCommentText, '@inheritdoc') !== false) {
59✔
76
                    $this->is_inheritdoc = true;
31✔
77
                }
78

79
                $this->readPhpDoc($docComment, $this->name);
59✔
80
            }
81
        }
82

83
        if ($parameter->type !== null) {
105✔
84
            if (!$this->type) {
81✔
85
                $typeStr = Utils::typeNodeToString($parameter->type);
26✔
86
                if ($typeStr !== null) {
26✔
87
                    $this->type = $typeStr;
26✔
88
                }
89
            }
90

91
            if ($parameter->type instanceof \PhpParser\Node\NullableType) {
81✔
92
                if ($this->type && $this->type !== 'null' && \strpos($this->type, 'null|') !== 0) {
16✔
93
                    $this->type = 'null|' . $this->type;
12✔
94
                } elseif (!$this->type) {
16✔
95
                    $this->type = 'null|mixed';
×
96
                }
97
            }
98
        }
99

100
        if ($parameter->default) {
105✔
101
            $defaultValue = Utils::getPhpParserValueFromNode($parameter->default, $classStr, $this->parserContainer);
52✔
102
            if ($defaultValue !== Utils::GET_PHP_PARSER_VALUE_FROM_NODE_HELPER) {
52✔
103
                $this->defaultValue = $defaultValue;
52✔
104

105
                $this->typeFromDefaultValue = Utils::normalizePhpType(\gettype($this->defaultValue));
52✔
106
            }
107
        }
108

109
        $this->is_vararg = $parameter->variadic;
105✔
110

111
        $this->is_passed_by_ref = $parameter->byRef;
105✔
112

113
        // Extract PHP 8.0+ attributes (only if not already populated by reflection)
114
        if (empty($this->attributes) && !empty($parameter->attrGroups)) {
105✔
115
            $this->attributes = Utils::extractAttributesFromAstNode($parameter->attrGroups);
4✔
116
        }
117

118
        return $this;
105✔
119
    }
120

121
    /**
122
     * @param ReflectionParameter $parameter
123
     *
124
     * @return $this
125
     */
126
    public function readObjectFromReflection($parameter): self
127
    {
128
        $this->name = $parameter->getName();
99✔
129

130
        $method = $parameter->getDeclaringFunction();
99✔
131
        if (!$this->line) {
99✔
132
            $lineTmp = $method->getStartLine();
99✔
133
            if ($lineTmp !== false) {
99✔
134
                $this->line = $lineTmp;
87✔
135
            }
136
        }
137

138
        $fileTmp = $method->getFileName();
99✔
139
        if ($fileTmp !== false) {
99✔
140
            $this->file = $fileTmp;
87✔
141
        }
142

143
        if ($parameter->isDefaultValueAvailable()) {
99✔
144
            try {
145
                $this->defaultValue = $parameter->getDefaultValue();
52✔
146
            } catch (\ReflectionException $e) {
×
147
                // nothing
148
            }
149
            if ($this->defaultValue !== null) {
52✔
150
                $this->typeFromDefaultValue = Utils::normalizePhpType(\gettype($this->defaultValue));
52✔
151
            }
152
        }
153

154
        $docComment = $method->getDocComment();
99✔
155
        if ($docComment) {
99✔
156
            if (\stripos($docComment, '@inheritdoc') !== false) {
51✔
157
                $this->is_inheritdoc = true;
31✔
158
            }
159

160
            $this->readPhpDoc($docComment, $this->name);
51✔
161
        }
162

163
        try {
164
            $type = $parameter->getType();
99✔
165
        } catch (\ReflectionException $e) {
×
166
            $type = null;
×
167
        }
168
        if ($type !== null) {
99✔
169
            if (\method_exists($type, 'getName')) {
99✔
170
                $this->type = Utils::normalizePhpType($type->getName(), true);
79✔
171
            } else {
172
                $this->type = Utils::normalizePhpType($type . '', true);
36✔
173
            }
174
            if ($this->type && \class_exists($this->type, true)) {
99✔
175
                $this->type = '\\' . \ltrim($this->type, '\\');
55✔
176
            }
177

178
            try {
179
                $constNameTmp = $parameter->getDefaultValueConstantName();
99✔
180
                if ($constNameTmp && \defined($constNameTmp)) {
52✔
181
                    $defaultTmp = \constant($constNameTmp);
12✔
182
                    if ($defaultTmp === null) {
12✔
183
                        if ($this->type && $this->type !== 'null' && \strpos($this->type, 'null|') !== 0) {
×
184
                            $this->type = 'null|' . $this->type;
×
185
                        } elseif (!$this->type) {
×
186
                            $this->type = 'null|mixed';
52✔
187
                        }
188
                    }
189
                }
190
            } catch (\ReflectionException $e) {
99✔
191
                if ($type->allowsNull()) {
99✔
192
                    if ($this->type && $this->type !== 'null' && \strpos($this->type, 'null|') !== 0) {
15✔
193
                        $this->type = 'null|' . $this->type;
15✔
194
                    } elseif (!$this->type) {
×
195
                        $this->type = 'null|mixed';
×
196
                    }
197
                }
198
            }
199
        }
200

201
        $this->is_vararg = $parameter->isVariadic();
99✔
202

203
        $this->is_passed_by_ref = $parameter->isPassedByReference();
99✔
204

205
        // Extract PHP 8.0+ attributes
206
        $this->attributes = Utils::extractAttributesFromReflection($parameter);
99✔
207

208
        return $this;
99✔
209
    }
210

211
    /**
212
     * @return string|null
213
     */
214
    public function getType(): ?string
215
    {
216
        if ($this->typeFromPhpDocExtended) {
×
217
            return $this->typeFromPhpDocExtended;
×
218
        }
219

220
        if ($this->type) {
×
221
            return $this->type;
×
222
        }
223

224
        if ($this->typeFromPhpDocSimple) {
×
225
            return $this->typeFromPhpDocSimple;
×
226
        }
227

228
        return null;
×
229
    }
230

231
    /**
232
     * @param Doc|string $doc
233
     */
234
    private function readPhpDoc($doc, string $parameterName): void
235
    {
236
        if ($doc instanceof Doc) {
63✔
237
            $docComment = $doc->getText();
59✔
238
        } else {
239
            $docComment = $doc;
51✔
240
        }
241
        if ($docComment === '') {
63✔
242
            return;
×
243
        }
244

245
        try {
246
            $phpDoc = DocFactoryProvider::getDocFactory()->create($docComment);
63✔
247

248
            $parsedParamTags = $phpDoc->getTagsByName('param');
63✔
249

250
            if (!empty($parsedParamTags)) {
63✔
251
                foreach ($parsedParamTags as $parsedParamTag) {
59✔
252
                    if ($parsedParamTag instanceof \phpDocumentor\Reflection\DocBlock\Tags\Param) {
59✔
253
                        // check only the current "param"-tag
254
                        if (\strtoupper($parameterName) !== \strtoupper((string) $parsedParamTag->getVariableName())) {
59✔
255
                            continue;
52✔
256
                        }
257

258
                        $type = $parsedParamTag->getType();
59✔
259

260
                        $this->typeFromPhpDoc = Utils::normalizePhpType($type . '');
59✔
261

262
                        $typeFromPhpDocMaybeWithCommentTmp = \trim((string) $parsedParamTag);
59✔
263
                        if (
264
                            $typeFromPhpDocMaybeWithCommentTmp
59✔
265
                            &&
266
                            \strpos($typeFromPhpDocMaybeWithCommentTmp, '$') !== 0
59✔
267
                        ) {
268
                            $this->typeFromPhpDocMaybeWithComment = $typeFromPhpDocMaybeWithCommentTmp;
59✔
269
                        }
270

271
                        $typeTmp = Utils::parseDocTypeObject($type);
59✔
272
                        if ($typeTmp !== '') {
59✔
273
                            $this->typeFromPhpDocSimple = $typeTmp;
59✔
274
                        }
275
                    }
276

277
                    $parsedParamTagParam = (string) $parsedParamTag;
59✔
278
                    $spitedData = Utils::splitTypeAndVariable($parsedParamTag);
59✔
279
                    $variableName = $spitedData['variableName'];
59✔
280

281
                    // check only the current "param"-tag
282
                    if ($variableName && \strtoupper($parameterName) === \strtoupper($variableName)) {
59✔
283
                        $this->phpDocRaw = $parsedParamTagParam;
59✔
284
                        $this->typeFromPhpDocExtended = Utils::modernPhpdoc($parsedParamTagParam);
59✔
285
                    }
286

287
                    break;
59✔
288
                }
289
            }
290

291
            $parsedParamTags = $phpDoc->getTagsByName('psalm-param')
63✔
292
                               + $phpDoc->getTagsByName('phpstan-param');
63✔
293

294
            if (!empty($parsedParamTags)) {
63✔
295
                foreach ($parsedParamTags as $parsedParamTag) {
63✔
296
                    if (!$parsedParamTag instanceof \phpDocumentor\Reflection\DocBlock\Tags\Generic) {
32✔
297
                        continue;
×
298
                    }
299

300
                    $spitedData = Utils::splitTypeAndVariable($parsedParamTag);
32✔
301
                    $parsedParamTagStr = $spitedData['parsedParamTagStr'];
32✔
302
                    $variableName = $spitedData['variableName'];
32✔
303

304
                    // check only the current "param"-tag
305
                    if (!$variableName || \strtoupper($parameterName) !== \strtoupper($variableName)) {
32✔
306
                        continue;
28✔
307
                    }
308

309
                    $this->typeFromPhpDocExtended = Utils::modernPhpdoc($parsedParamTagStr);
32✔
310
                }
311
            }
312
        } catch (\Exception $e) {
12✔
313
            $this->addParseError($e);
12✔
314
        }
315

316
        try {
317
            $this->readPhpDocByTokens($docComment, $parameterName);
63✔
318
        } catch (\Exception $e) {
12✔
319
            $this->addParseError($e);
12✔
320
        }
321

322
        $this->reportBrokenParamTagWithoutType($docComment, $parameterName);
63✔
323
    }
324

325
    /**
326
     * @throws \PHPStan\PhpDocParser\Parser\ParserException
327
     */
328
    private function readPhpDocByTokens(string $docComment, string $parameterName): void
329
    {
330
        $tokens = Utils::modernPhpdocTokens($docComment);
63✔
331

332
        $paramContent = null;
63✔
333
        foreach ($tokens->getTokens() as $token) {
63✔
334
            $content = $token[0];
63✔
335

336
            if ($content === '@param' || $content === '@psalm-param' || $content === '@phpstan-param') {
63✔
337
                // reset
338
                $paramContent = '';
59✔
339

340
                continue;
59✔
341
            }
342

343
            // We can stop if we found the param variable e.g. `@param array{foo:int} $param`.
344
            if ($content === '$' . $parameterName) {
63✔
345
                break;
59✔
346
            }
347

348
            if ($paramContent !== null) {
63✔
349
                $paramContent .= $content;
59✔
350
            }
351
        }
352

353
        $paramContent = $paramContent ? \trim($paramContent) : null;
63✔
354
        if ($paramContent) {
63✔
355
            if (!$this->phpDocRaw) {
59✔
356
                $this->phpDocRaw = $paramContent . ' ' . '$' . $parameterName;
32✔
357
            }
358
            $this->typeFromPhpDocExtended = Utils::modernPhpdoc($paramContent);
59✔
359
        }
360
    }
361

362
    private function reportBrokenParamTagWithoutType(string $docComment, string $parameterName): void
363
    {
364
        if ($this->line === null) {
63✔
NEW
365
            return;
×
366
        }
367

368
        if (!\preg_match('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/u', $parameterName)) {
63✔
NEW
369
            return;
×
370
        }
371

372
        if (
373
            !\preg_match(
63✔
374
                '#@(param|psalm-param|phpstan-param)[ \t]+\$' . $parameterName . '(?=[ \t\r\n\*]|$)#u',
63✔
375
                $docComment
63✔
376
            )
63✔
377
        ) {
378
            return;
63✔
379
        }
380

381
        try {
382
            // Re-parse the malformed tag payload to preserve the original parser
383
            // error message even though the docblock library now falls back to mixed.
384
            Utils::modernPhpdoc('$' . $parameterName);
36✔
385
        } catch (\Exception $e) {
36✔
386
            $this->addParseError($e);
36✔
387
        }
388
    }
389

390
    private function addParseError(\Exception $e): void
391
    {
392
        $tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
44✔
393
        $this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
44✔
394
    }
395
}
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