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

voku / Simple-PHP-Code-Parser / 24289924886

11 Apr 2026 07:31PM UTC coverage: 84.527% (+1.8%) from 82.757%
24289924886

push

github

web-flow
Merge pull request #83 from voku/copilot/fix-failing-phpdoc-test

Fix CI pipeline: phpunit.xml validation warning, php-parser v4 test skip, and comprehensive type-analysis regression coverage

272 of 295 new or added lines in 7 files covered. (92.2%)

5 existing lines in 2 files now uncovered.

1759 of 2081 relevant lines covered (84.53%)

113.31 hits per line

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

86.98
/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;
294✔
58
        if ($parameterVar instanceof \PhpParser\Node\Expr\Error) {
294✔
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 : '';
294✔
67

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

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

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

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

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

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

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

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

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

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

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

118
        return $this;
294✔
119
    }
120

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

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

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

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

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

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

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

178
            try {
179
                $constNameTmp = $parameter->getDefaultValueConstantName();
214✔
180
                if ($constNameTmp && \defined($constNameTmp)) {
112✔
181
                    $defaultTmp = \constant($constNameTmp);
24✔
182
                    if ($defaultTmp === null) {
24✔
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';
112✔
187
                        }
188
                    }
189
                }
190
            } catch (\ReflectionException $e) {
206✔
191
                if ($type->allowsNull()) {
206✔
192
                    if ($this->type && $this->type !== 'null' && \strpos($this->type, 'null|') !== 0) {
30✔
193
                        $this->type = 'null|' . $this->type;
30✔
194
                    } elseif (!$this->type) {
×
195
                        $this->type = 'null|mixed';
×
196
                    }
197
                }
198
            }
199
        }
200

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

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

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

208
        return $this;
214✔
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) {
166✔
237
            $docComment = $doc->getText();
158✔
238
        } else {
239
            $docComment = $doc;
110✔
240
        }
241
        if ($docComment === '') {
166✔
242
            return;
×
243
        }
244

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

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

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

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

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

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

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

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

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

287
                    break;
150✔
288
                }
289
            }
290

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

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

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

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

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

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

322
        $this->reportBrokenParamTagWithoutType($docComment, $parameterName);
166✔
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);
166✔
331

332
        // Track standard (@param) and extended (@phpstan-param / @psalm-param) content separately
333
        // so that the more specific phpstan/psalm annotation always wins regardless of tag order.
334
        // We scan the ENTIRE docblock to find all occurrences of both tag types for this parameter.
335
        $paramContent = null;
166✔
336
        $extendedParamContent = null;
166✔
337
        $currentTarget = null; // 'standard' | 'extended'
166✔
338
        $currentContent = '';
166✔
339

340
        foreach ($tokens->getTokens() as $token) {
166✔
341
            $content = $token[0];
166✔
342

343
            if ($content === '@param') {
166✔
344
                $currentTarget = 'standard';
150✔
345
                $currentContent = '';
150✔
346
                continue;
150✔
347
            }
348

349
            if ($content === '@psalm-param' || $content === '@phpstan-param') {
166✔
350
                $currentTarget = 'extended';
88✔
351
                $currentContent = '';
88✔
352
                continue;
88✔
353
            }
354

355
            if ($currentTarget !== null) {
166✔
356
                // Check if we hit the target parameter variable e.g. `$param`.
357
                if ($content === '$' . $parameterName) {
158✔
358
                    if ($currentTarget === 'standard') {
158✔
359
                        $paramContent = \trim($currentContent);
150✔
360
                    } else {
361
                        $extendedParamContent = \trim($currentContent);
88✔
362
                    }
363
                    $currentTarget = null;
158✔
364
                    $currentContent = '';
158✔
365
                    continue;
158✔
366
                }
367

368
                // Check if we hit a different parameter variable — discard this tag.
369
                if (\strlen($content) > 1 && $content[0] === '$') {
158✔
370
                    $currentTarget = null;
112✔
371
                    $currentContent = '';
112✔
372
                    continue;
112✔
373
                }
374

375
                $currentContent .= $content;
158✔
376
            }
377
        }
378

379
        // Prefer @phpstan-param / @psalm-param over plain @param regardless of tag order.
380
        $bestContent = null;
166✔
381
        if ($extendedParamContent !== null && $extendedParamContent !== '') {
166✔
382
            $bestContent = $extendedParamContent;
80✔
383
        } elseif ($paramContent !== null && $paramContent !== '') {
142✔
384
            $bestContent = $paramContent;
134✔
385
        }
386

387
        if ($bestContent) {
166✔
388
            if (!$this->phpDocRaw) {
158✔
389
                $this->phpDocRaw = $bestContent . ' ' . '$' . $parameterName;
64✔
390
            }
391
            try {
392
                $this->typeFromPhpDocExtended = Utils::modernPhpdoc($bestContent);
158✔
393
            } catch (\PHPStan\PhpDocParser\Parser\ParserException $e) {
32✔
394
                $recoveredType = Utils::recoverBrokenPhpdocType($bestContent);
32✔
395
                if ($recoveredType !== null) {
32✔
396
                    $normalizedRecoveredType = Utils::normalizePhpType($recoveredType);
32✔
397
                    $this->typeFromPhpDoc = $this->typeFromPhpDoc ?? $normalizedRecoveredType;
32✔
398
                    $this->typeFromPhpDocSimple = $this->typeFromPhpDocSimple ?? $normalizedRecoveredType;
32✔
399
                    $this->typeFromPhpDocExtended = $recoveredType;
32✔
400
                }
401

402
                $this->addParseError($e);
32✔
403
            }
404
        }
405
    }
406

407
    private function reportBrokenParamTagWithoutType(string $docComment, string $parameterName): void
408
    {
409
        if ($this->line === null) {
166✔
410
            return;
×
411
        }
412

413
        if (!\preg_match('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/u', $parameterName)) {
166✔
414
            return;
×
415
        }
416

417
        if (
418
            !\preg_match(
166✔
419
                '#@(param|psalm-param|phpstan-param)[ \t]+\$' . $parameterName . '(?=[ \t\r\n\*]|$)#u',
166✔
420
                $docComment
166✔
421
            )
166✔
422
        ) {
423
            return;
166✔
424
        }
425

426
        try {
427
            // Re-parse the malformed tag payload to preserve the original parser
428
            // error message even though the docblock library now falls back to mixed.
429
            Utils::modernPhpdoc('$' . $parameterName);
72✔
430
        } catch (\Exception $e) {
72✔
431
            $this->addParseError($e);
72✔
432
        }
433
    }
434

435
    private function addParseError(\Exception $e): void
436
    {
437
        $tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
96✔
438
        $this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
96✔
439
    }
440
}
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