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

voku / Simple-PHP-Code-Parser / 24286329431

11 Apr 2026 04:09PM UTC coverage: 82.898% (+0.01%) from 82.886%
24286329431

Pull #83

github

web-flow
Merge e95fe6f21 into 90e1e60d3
Pull Request #83: Fix CI pipeline: phpunit.xml validation warning, php-parser v4 test skip, and comprehensive type-analysis regression coverage

178 of 221 new or added lines in 7 files covered. (80.54%)

33 existing lines in 4 files now uncovered.

1682 of 2029 relevant lines covered (82.9%)

36.9 hits per line

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

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

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

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

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

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

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

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

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

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

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

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

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

118
        return $this;
95✔
119
    }
120

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

287
                    break;
46✔
288
                }
289
            }
290

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

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

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

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

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

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

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

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

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

340
                continue;
46✔
341
            }
342

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

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

353
        $paramContent = $paramContent ? \trim($paramContent) : null;
49✔
354
        if ($paramContent) {
49✔
355
            if (!$this->phpDocRaw) {
46✔
356
                $this->phpDocRaw = $paramContent . ' ' . '$' . $parameterName;
24✔
357
            }
358
            try {
359
                $this->typeFromPhpDocExtended = Utils::modernPhpdoc($paramContent);
46✔
360
            } catch (\PHPStan\PhpDocParser\Parser\ParserException $e) {
12✔
361
                $recoveredType = Utils::recoverBrokenPhpdocType($paramContent);
12✔
362
                if ($recoveredType !== null) {
12✔
363
                    $normalizedRecoveredType = Utils::normalizePhpType($recoveredType);
12✔
364
                    $this->typeFromPhpDoc = $this->typeFromPhpDoc ?? $normalizedRecoveredType;
12✔
365
                    $this->typeFromPhpDocSimple = $this->typeFromPhpDocSimple ?? $normalizedRecoveredType;
12✔
366
                    $this->typeFromPhpDocExtended = $recoveredType;
12✔
367
                }
368

369
                $this->addParseError($e);
12✔
370
            }
371
        }
372
    }
373

374
    private function reportBrokenParamTagWithoutType(string $docComment, string $parameterName): void
375
    {
376
        if ($this->line === null) {
49✔
377
            return;
×
378
        }
379

380
        if (!\preg_match('/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/u', $parameterName)) {
49✔
381
            return;
×
382
        }
383

384
        if (
385
            !\preg_match(
49✔
386
                '#@(param|psalm-param|phpstan-param)[ \t]+\$' . $parameterName . '(?=[ \t\r\n\*]|$)#u',
49✔
387
                $docComment
49✔
388
            )
49✔
389
        ) {
390
            return;
49✔
391
        }
392

393
        try {
394
            // Re-parse the malformed tag payload to preserve the original parser
395
            // error message even though the docblock library now falls back to mixed.
396
            Utils::modernPhpdoc('$' . $parameterName);
27✔
397
        } catch (\Exception $e) {
27✔
398
            $this->addParseError($e);
27✔
399
        }
400
    }
401

402
    private function addParseError(\Exception $e): void
403
    {
404
        $tmpErrorMessage = $this->name . ':' . ($this->line ?? '?') . ' | ' . \print_r($e->getMessage(), true);
36✔
405
        $this->parseError[\md5($tmpErrorMessage)] = $tmpErrorMessage;
36✔
406
    }
407
}
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