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

keradus / PHP-CS-Fixer / 19958239208

05 Dec 2025 09:13AM UTC coverage: 93.181% (-1.0%) from 94.158%
19958239208

push

github

keradus
chore: .php-cs-fixer.dist.php - remove no longer needed rule, 'expectedDeprecation' annotation does not exist for long time

28928 of 31045 relevant lines covered (93.18%)

44.49 hits per line

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

98.92
/src/DocBlock/TypeExpression.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\DocBlock;
16

17
use PhpCsFixer\Preg;
18
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis;
19
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis;
20
use PhpCsFixer\Utils;
21

22
/**
23
 * @author Michael Vorisek <https://github.com/mvorisek>
24
 *
25
 * @internal
26
 *
27
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
28
 */
29
final class TypeExpression
30
{
31
    /**
32
     * Regex to match any PHP identifier.
33
     *
34
     * @internal
35
     */
36
    public const REGEX_IDENTIFIER = '(?:(?!(?<!\*)\d)[^\x00-\x2f\x3a-\x40\x5b-\x5e\x60\x7b-\x7f]++)';
37

38
    /**
39
     * Regex to match any PHPDoc type.
40
     *
41
     * @internal
42
     */
43
    public const REGEX_TYPES = '(?<types>(?x) # one or several types separated by `|` or `&`
44
'.self::REGEX_TYPE.'
45
        (?:
46
            \h*(?<glue>[|&])\h*
47
            (?&type)
48
        )*+
49
    )';
50

51
    /**
52
     * Based on:
53
     * - https://github.com/phpstan/phpdoc-parser/blob/1.26.0/doc/grammars/type.abnf fuzzing grammar
54
     * - and https://github.com/phpstan/phpdoc-parser/blob/1.26.0/src/Parser/PhpDocParser.php parser impl.
55
     */
56
    private const REGEX_TYPE = '(?<type>(?x) # single type
57
            (?:co(ntra)?variant\h+)?
58
            (?<nullable>\??\h*)
59
            (?:
60
                (?<array_shape>
61
                    (?<array_shape_name>(?i)(?:array|list|object)(?-i))
62
                    (?<array_shape_start>\h*\{[\h\v*]*)
63
                    (?<array_shape_inners>
64
                        (?<array_shape_inner>
65
                            (?<array_shape_inner_key>(?:(?&constant)|(?&identifier)|(?&name))\h*\??\h*:\h*|)
66
                            (?<array_shape_inner_value>(?&types_inner))
67
                        )
68
                        (?:
69
                            \h*,[\h\v*]*
70
                            (?&array_shape_inner)
71
                        )*+
72
                        (?:\h*,|(?!(?&array_shape_unsealed_variadic)))
73
                    |)
74
                    (?<array_shape_unsealed> # unsealed array shape, e.g. `...`. `...<string>`
75
                        (?<array_shape_unsealed_variadic>\h*\.\.\.)
76
                        (?<array_shape_unsealed_type>
77
                            (?<array_shape_unsealed_type_start>\h*<\h*)
78
                            (?<array_shape_unsealed_type_a>(?&types_inner))
79
                            (?:
80
                                (?<array_shape_unsealed_type_comma>\h*,\h*)
81
                                (?<array_shape_unsealed_type_b>(?&array_shape_unsealed_type_a))
82
                            |)
83
                            \h*>
84
                        |)
85
                    |)
86
                    [\h\v*]*\}
87
                )
88
                |
89
                (?<callable> # callable syntax, e.g. `callable(string, int...): bool`, `\Closure<T>(T, int): T`
90
                    (?<callable_name>(?&name))
91
                    (?<callable_template>
92
                        (?<callable_template_start>\h*<\h*)
93
                        (?<callable_template_inners>
94
                            (?<callable_template_inner>
95
                                (?<callable_template_inner_name>
96
                                    (?&identifier)
97
                                )
98
                                (?<callable_template_inner_b> # template bound
99
                                    \h+(?i)(?<callable_template_inner_b_kw>of|as)(?-i)\h+
100
                                    (?<callable_template_inner_b_types>(?&types_inner))
101
                                |)
102
                                (?<callable_template_inner_d> # template default
103
                                    \h*=\h*
104
                                    (?<callable_template_inner_d_types>(?&types_inner))
105
                                |)
106
                            )
107
                            (?:
108
                                \h*,\h*
109
                                (?&callable_template_inner)
110
                            )*+
111
                        )
112
                        \h*>
113
                        (?=\h*\()
114
                    |)
115
                    (?<callable_start>\h*\(\h*)
116
                    (?<callable_arguments>
117
                        (?<callable_argument>
118
                            (?<callable_argument_type>(?&types_inner))
119
                            (?<callable_argument_is_reference>\h*&|)
120
                            (?<callable_argument_is_variadic>\h*\.\.\.|)
121
                            (?<callable_argument_name>\h*\$(?&identifier)|)
122
                            (?<callable_argument_is_optional>\h*=|)
123
                        )
124
                        (?:
125
                            \h*,\h*
126
                            (?&callable_argument)
127
                        )*+
128
                        (?:\h*,)?
129
                    |)
130
                    \h*\)
131
                    (?:
132
                        \h*\:\h*
133
                        (?<callable_return>(?&type))
134
                    )?
135
                )
136
                |
137
                (?<generic> # generic syntax, e.g.: `array<int, \Foo\Bar>`
138
                    (?<generic_name>(?&name))
139
                    (?<generic_start>\h*<[\h\v*]*)
140
                    (?<generic_types>
141
                        (?&types_inner)
142
                        (?:
143
                            \h*,[\h\v*]*
144
                            (?&types_inner)
145
                        )*+
146
                        (?:\h*,)?
147
                    )
148
                    [\h\v*]*>
149
                )
150
                |
151
                (?<class_constant> # class constants with optional wildcard, e.g.: `Foo::*`, `Foo::CONST_A`, `FOO::CONST_*`
152
                    (?<class_constant_name>(?&name))
153
                    ::\*?(?:(?&identifier)\*?)*
154
                )
155
                |
156
                (?<constant> # single constant value (case insensitive), e.g.: 1, -1.8E+6, `\'a\'`
157
                    (?i)
158
                    # all sorts of numbers: with or without sign, supports literal separator and several numeric systems,
159
                    # e.g.: 1, +1.1, 1., .1, -1, 123E+8, 123_456_789, 0x7Fb4, 0b0110, 0o777
160
                    [+-]?(?:
161
                        (?:0b[01]++(?:_[01]++)*+)
162
                        | (?:0o[0-7]++(?:_[0-7]++)*+)
163
                        | (?:0x[\da-f]++(?:_[\da-f]++)*+)
164
                        | (?:(?<constant_digits>\d++(?:_\d++)*+)|(?=\.\d))
165
                          (?:\.(?&constant_digits)|(?<=\d)\.)?+
166
                          (?:e[+-]?(?&constant_digits))?+
167
                    )
168
                    | \'(?:[^\'\\\]|\\\.)*+\'
169
                    | "(?:[^"\\\]|\\\.)*+"
170
                    (?-i)
171
                )
172
                |
173
                (?<this> # self reference, e.g.: $this, $self, @static
174
                    (?i)
175
                    [@$](?:this | self | static)
176
                    (?-i)
177
                )
178
                |
179
                (?<name> # full name, e.g.: `int`, `\DateTime`, `\Foo\Bar`, `positive-int`
180
                    \\\?+
181
                    (?<identifier>'.self::REGEX_IDENTIFIER.')
182
                    (?:[\\\\\-](?&identifier))*+
183
                )
184
                |
185
                (?<parenthesized> # parenthesized type, e.g.: `(int)`, `(int|\stdClass)`
186
                    (?<parenthesized_start>
187
                        \(\h*
188
                    )
189
                    (?:
190
                        (?<parenthesized_types>
191
                            (?&types_inner)
192
                        )
193
                        |
194
                        (?<conditional> # conditional type, e.g.: `$foo is \Throwable ? false : $foo`
195
                            (?<conditional_cond_left>
196
                                (?:\$(?&identifier))
197
                                |
198
                                (?<conditional_cond_left_types>(?&types_inner))
199
                            )
200
                            (?<conditional_cond_middle>
201
                                \h+(?i)is(?:\h+not)?(?-i)\h+
202
                            )
203
                            (?<conditional_cond_right_types>(?&types_inner))
204
                            (?<conditional_true_start>\h*\?\h*)
205
                            (?<conditional_true_types>(?&types_inner))
206
                            (?<conditional_false_start>\h*:\h*)
207
                            (?<conditional_false_types>(?&types_inner))
208
                        )
209
                    )
210
                    \h*\)
211
                )
212
            )
213
            (?<array> # array, e.g.: `string[]`, `array<int, string>[][]`
214
                (\h*\[\h*\])*
215
            )
216
            (?:(?=1)0
217
                (?<types_inner>(?>
218
                    (?&type)
219
                    (?:
220
                        \h*[|&]\h*
221
                        (?&type)
222
                    )*+
223
                ))
224
            |)
225
        )';
226

227
    private const ALIASES = [
228
        'boolean' => 'bool',
229
        'callback' => 'callable',
230
        'double' => 'float',
231
        'false' => 'bool',
232
        'integer' => 'int',
233
        'list' => 'array',
234
        'real' => 'float',
235
        'true' => 'bool',
236
    ];
237

238
    private string $value;
239

240
    private bool $isCompositeType;
241

242
    /** @var null|'&'|'|' */
243
    private ?string $typesGlue = null;
244

245
    /** @var list<array{start_index: int, expression: self}> */
246
    private array $innerTypeExpressions = [];
247

248
    private ?NamespaceAnalysis $namespace;
249

250
    /** @var list<NamespaceUseAnalysis> */
251
    private array $namespaceUses;
252

253
    /**
254
     * @param list<NamespaceUseAnalysis> $namespaceUses
255
     */
256
    public function __construct(string $value, ?NamespaceAnalysis $namespace, array $namespaceUses)
257
    {
258
        $this->value = $value;
357✔
259
        $this->namespace = $namespace;
357✔
260
        $this->namespaceUses = $namespaceUses;
357✔
261

262
        $this->parse();
357✔
263
    }
264

265
    public function toString(): string
266
    {
267
        return $this->value;
228✔
268
    }
269

270
    /**
271
     * @return list<string>
272
     */
273
    public function getTypes(): array
274
    {
275
        if ($this->isCompositeType) {
221✔
276
            return array_map(
174✔
277
                static fn (array $type) => $type['expression']->toString(),
174✔
278
                $this->innerTypeExpressions,
174✔
279
            );
174✔
280
        }
281

282
        return [$this->value];
179✔
283
    }
284

285
    /**
286
     * Determines if type expression is a composite type (union or intersection).
287
     */
288
    public function isCompositeType(): bool
289
    {
290
        return $this->isCompositeType;
155✔
291
    }
292

293
    public function isUnionType(): bool
294
    {
295
        return $this->isCompositeType && '|' === $this->typesGlue;
26✔
296
    }
297

298
    public function isIntersectionType(): bool
299
    {
300
        return $this->isCompositeType && '&' === $this->typesGlue;
5✔
301
    }
302

303
    /**
304
     * @return null|'&'|'|'
305
     */
306
    public function getTypesGlue(): ?string
307
    {
308
        return $this->typesGlue;
54✔
309
    }
310

311
    /**
312
     * @param \Closure(self): self $callback
313
     */
314
    public function mapTypes(\Closure $callback): self
315
    {
316
        $value = $this->value;
54✔
317
        $startIndexOffset = 0;
54✔
318

319
        foreach ($this->innerTypeExpressions as [
54✔
320
            'start_index' => $startIndexOrig,
54✔
321
            'expression' => $inner,
54✔
322
        ]) {
54✔
323
            $innerValueOrig = $inner->value;
53✔
324

325
            $inner = $inner->mapTypes($callback);
53✔
326

327
            if ($inner->value !== $innerValueOrig) {
53✔
328
                $value = substr_replace(
48✔
329
                    $value,
48✔
330
                    $inner->value,
48✔
331
                    $startIndexOrig + $startIndexOffset,
48✔
332
                    \strlen($innerValueOrig)
48✔
333
                );
48✔
334

335
                $startIndexOffset += \strlen($inner->value) - \strlen($innerValueOrig);
48✔
336
            }
337
        }
338

339
        $type = $value === $this->value
54✔
340
            ? $this
54✔
341
            : $this->inner($value);
48✔
342

343
        return $callback($type);
54✔
344
    }
345

346
    /**
347
     * @param \Closure(self): void $callback
348
     */
349
    public function walkTypes(\Closure $callback): void
350
    {
351
        $this->mapTypes(static function (self $type) use ($callback) {
1✔
352
            $valueOrig = $type->value;
1✔
353
            $callback($type);
1✔
354
            \assert($type->value === $valueOrig);
1✔
355

356
            return $type;
1✔
357
        });
1✔
358
    }
359

360
    /**
361
     * @param \Closure(self, self): (-1|0|1) $compareCallback
362
     */
363
    public function sortTypes(\Closure $compareCallback): self
364
    {
365
        return $this->mapTypes(function (self $type) use ($compareCallback): self {
52✔
366
            if ($type->isCompositeType) {
52✔
367
                $innerTypeExpressions = Utils::stableSort(
51✔
368
                    $type->innerTypeExpressions,
51✔
369
                    static fn (array $v): self => $v['expression'],
51✔
370
                    $compareCallback,
51✔
371
                );
51✔
372

373
                if ($innerTypeExpressions !== $type->innerTypeExpressions) {
51✔
374
                    $value = implode(
51✔
375
                        $type->getTypesGlue(),
51✔
376
                        array_map(static fn (array $v): string => $v['expression']->toString(), $innerTypeExpressions)
51✔
377
                    );
51✔
378

379
                    return $this->inner($value);
51✔
380
                }
381
            }
382

383
            return $type;
52✔
384
        });
52✔
385
    }
386

387
    public function getCommonType(): ?string
388
    {
389
        $mainType = null;
60✔
390

391
        foreach ($this->getTypes() as $type) {
60✔
392
            if ('null' === $type) {
60✔
393
                continue;
2✔
394
            }
395

396
            if (str_starts_with($type, '?')) {
60✔
397
                $type = substr($type, 1);
3✔
398
            }
399

400
            if (Preg::match('/\[\h*\]$/', $type)) {
60✔
401
                $type = 'array';
13✔
402
            } elseif (Preg::match('/^(.+?)\h*[<{(]/', $type, $matches)) {
47✔
403
                $type = $matches[1];
17✔
404
            }
405

406
            if (isset(self::ALIASES[$type])) {
60✔
407
                $type = self::ALIASES[$type];
4✔
408
            }
409

410
            if (null === $mainType || $type === $mainType) {
60✔
411
                $mainType = $type;
60✔
412

413
                continue;
60✔
414
            }
415

416
            $mainType = $this->getParentType($type, $mainType);
17✔
417

418
            if (null === $mainType) {
17✔
419
                return null;
6✔
420
            }
421
        }
422

423
        return $mainType;
54✔
424
    }
425

426
    public function allowsNull(): bool
427
    {
428
        foreach ($this->getTypes() as $type) {
10✔
429
            if (\in_array($type, ['null', 'mixed'], true) || str_starts_with($type, '?')) {
10✔
430
                return true;
7✔
431
            }
432
        }
433

434
        return false;
3✔
435
    }
436

437
    private function parse(): void
438
    {
439
        $seenGlues = null;
357✔
440
        $innerValues = [];
357✔
441

442
        $index = 0;
357✔
443
        while (true) {
357✔
444
            Preg::match(
357✔
445
                '{\G'.self::REGEX_TYPE.'(?<glue_raw>\h*(?<glue>[|&])\h*(?!$)|$)}',
357✔
446
                $this->value,
357✔
447
                $matches,
357✔
448
                \PREG_OFFSET_CAPTURE,
357✔
449
                $index
357✔
450
            );
357✔
451

452
            if ([] === $matches) {
357✔
453
                throw new \Exception('Unable to parse phpdoc type '.var_export($this->value, true));
61✔
454
            }
455

456
            if (null === $seenGlues) {
298✔
457
                if (($matches['glue'][0] ?? '') === '') {
298✔
458
                    break;
296✔
459
                }
460

461
                $seenGlues = ['|' => false, '&' => false];
244✔
462
            }
463

464
            if (($matches['glue'][0] ?? '') !== '') {
244✔
465
                \assert(isset($seenGlues[$matches['glue'][0]]));
244✔
466
                $seenGlues[$matches['glue'][0]] = true;
244✔
467
            }
468

469
            $innerValues[] = [
244✔
470
                'start_index' => $index,
244✔
471
                'value' => $matches['type'][0],
244✔
472
                'next_glue' => $matches['glue'][0] ?? null,
244✔
473
                'next_glue_raw' => $matches['glue_raw'][0] ?? null,
244✔
474
            ];
244✔
475

476
            $consumedValueLength = \strlen($matches[0][0]);
244✔
477
            $index += $consumedValueLength;
244✔
478

479
            if (\strlen($this->value) <= $index) {
244✔
480
                \assert(\strlen($this->value) === $index);
242✔
481

482
                $seenGlues = array_filter($seenGlues);
242✔
483
                \assert([] !== $seenGlues);
242✔
484

485
                $this->isCompositeType = true;
242✔
486
                $this->typesGlue = array_key_first($seenGlues);
242✔
487

488
                if (1 === \count($seenGlues)) {
242✔
489
                    foreach ($innerValues as $innerValue) {
242✔
490
                        $this->innerTypeExpressions[] = [
242✔
491
                            'start_index' => $innerValue['start_index'],
242✔
492
                            'expression' => $this->inner($innerValue['value']),
242✔
493
                        ];
242✔
494
                    }
495
                } else {
496
                    for ($i = 0; $i < \count($innerValues); ++$i) {
4✔
497
                        $innerStartIndex = $innerValues[$i]['start_index'];
4✔
498
                        $innerValue = '';
4✔
499
                        while (true) {
4✔
500
                            $innerValue .= $innerValues[$i]['value'];
4✔
501

502
                            if (($innerValues[$i]['next_glue'] ?? $this->typesGlue) === $this->typesGlue) {
4✔
503
                                break;
4✔
504
                            }
505

506
                            $innerValue .= $innerValues[$i]['next_glue_raw'];
4✔
507

508
                            ++$i;
4✔
509
                        }
510

511
                        $this->innerTypeExpressions[] = [
4✔
512
                            'start_index' => $innerStartIndex,
4✔
513
                            'expression' => $this->inner($innerValue),
4✔
514
                        ];
4✔
515
                    }
516
                }
517

518
                return;
242✔
519
            }
520
        }
521

522
        $this->isCompositeType = false;
296✔
523

524
        if ('' !== $matches['nullable'][0]) {
296✔
525
            $this->innerTypeExpressions[] = [
17✔
526
                'start_index' => \strlen($matches['nullable'][0]),
17✔
527
                'expression' => $this->inner(substr($matches['type'][0], \strlen($matches['nullable'][0]))),
17✔
528
            ];
17✔
529
        } elseif ('' !== $matches['array'][0]) {
296✔
530
            $this->innerTypeExpressions[] = [
27✔
531
                'start_index' => 0,
27✔
532
                'expression' => $this->inner(substr($matches['type'][0], 0, -\strlen($matches['array'][0]))),
27✔
533
            ];
27✔
534
        } elseif ('' !== ($matches['generic'][0] ?? '') && 0 === $matches['generic'][1]) {
296✔
535
            $this->innerTypeExpressions[] = [
55✔
536
                'start_index' => 0,
55✔
537
                'expression' => $this->inner($matches['generic_name'][0]),
55✔
538
            ];
55✔
539

540
            $this->parseCommaSeparatedInnerTypes(
55✔
541
                \strlen($matches['generic_name'][0]) + \strlen($matches['generic_start'][0]),
55✔
542
                $matches['generic_types'][0]
55✔
543
            );
55✔
544
        } elseif ('' !== ($matches['callable'][0] ?? '') && 0 === $matches['callable'][1]) {
296✔
545
            $this->innerTypeExpressions[] = [
63✔
546
                'start_index' => 0,
63✔
547
                'expression' => $this->inner($matches['callable_name'][0]),
63✔
548
            ];
63✔
549

550
            $this->parseCallableTemplateInnerTypes(
63✔
551
                \strlen($matches['callable_name'][0])
63✔
552
                    + \strlen($matches['callable_template_start'][0]),
63✔
553
                $matches['callable_template_inners'][0]
63✔
554
            );
63✔
555

556
            $this->parseCallableArgumentTypes(
63✔
557
                \strlen($matches['callable_name'][0])
63✔
558
                    + \strlen($matches['callable_template'][0])
63✔
559
                    + \strlen($matches['callable_start'][0]),
63✔
560
                $matches['callable_arguments'][0]
63✔
561
            );
63✔
562

563
            if ('' !== ($matches['callable_return'][0] ?? '')) {
63✔
564
                $this->innerTypeExpressions[] = [
47✔
565
                    'start_index' => \strlen($this->value) - \strlen($matches['callable_return'][0]),
47✔
566
                    'expression' => $this->inner($matches['callable_return'][0]),
47✔
567
                ];
47✔
568
            }
569
        } elseif ('' !== ($matches['array_shape'][0] ?? '') && 0 === $matches['array_shape'][1]) {
296✔
570
            $this->innerTypeExpressions[] = [
47✔
571
                'start_index' => 0,
47✔
572
                'expression' => $this->inner($matches['array_shape_name'][0]),
47✔
573
            ];
47✔
574

575
            $nextIndex = \strlen($matches['array_shape_name'][0]) + \strlen($matches['array_shape_start'][0]);
47✔
576

577
            $this->parseArrayShapeInnerTypes(
47✔
578
                $nextIndex,
47✔
579
                $matches['array_shape_inners'][0]
47✔
580
            );
47✔
581

582
            if ('' !== ($matches['array_shape_unsealed_type'][0] ?? '')) {
47✔
583
                $nextIndex += \strlen($matches['array_shape_inners'][0])
8✔
584
                    + \strlen($matches['array_shape_unsealed_variadic'][0])
8✔
585
                    + \strlen($matches['array_shape_unsealed_type_start'][0]);
8✔
586

587
                $this->innerTypeExpressions[] = [
8✔
588
                    'start_index' => $nextIndex,
8✔
589
                    'expression' => $this->inner($matches['array_shape_unsealed_type_a'][0]),
8✔
590
                ];
8✔
591

592
                if ('' !== ($matches['array_shape_unsealed_type_b'][0] ?? '')) {
8✔
593
                    $nextIndex += \strlen($matches['array_shape_unsealed_type_a'][0])
2✔
594
                        + \strlen($matches['array_shape_unsealed_type_comma'][0]);
2✔
595

596
                    $this->innerTypeExpressions[] = [
2✔
597
                        'start_index' => $nextIndex,
2✔
598
                        'expression' => $this->inner($matches['array_shape_unsealed_type_b'][0]),
2✔
599
                    ];
2✔
600
                }
601
            }
602
        } elseif ('' !== ($matches['parenthesized'][0] ?? '') && 0 === $matches['parenthesized'][1]) {
296✔
603
            $index = \strlen($matches['parenthesized_start'][0]);
26✔
604

605
            if ('' !== ($matches['conditional'][0] ?? '')) {
26✔
606
                if ('' !== ($matches['conditional_cond_left_types'][0] ?? '')) {
7✔
607
                    $this->innerTypeExpressions[] = [
2✔
608
                        'start_index' => $index,
2✔
609
                        'expression' => $this->inner($matches['conditional_cond_left_types'][0]),
2✔
610
                    ];
2✔
611
                }
612

613
                $index += \strlen($matches['conditional_cond_left'][0]) + \strlen($matches['conditional_cond_middle'][0]);
7✔
614

615
                $this->innerTypeExpressions[] = [
7✔
616
                    'start_index' => $index,
7✔
617
                    'expression' => $this->inner($matches['conditional_cond_right_types'][0]),
7✔
618
                ];
7✔
619

620
                $index += \strlen($matches['conditional_cond_right_types'][0]) + \strlen($matches['conditional_true_start'][0]);
7✔
621

622
                $this->innerTypeExpressions[] = [
7✔
623
                    'start_index' => $index,
7✔
624
                    'expression' => $this->inner($matches['conditional_true_types'][0]),
7✔
625
                ];
7✔
626

627
                $index += \strlen($matches['conditional_true_types'][0]) + \strlen($matches['conditional_false_start'][0]);
7✔
628

629
                $this->innerTypeExpressions[] = [
7✔
630
                    'start_index' => $index,
7✔
631
                    'expression' => $this->inner($matches['conditional_false_types'][0]),
7✔
632
                ];
7✔
633
            } else {
634
                $this->innerTypeExpressions[] = [
24✔
635
                    'start_index' => $index,
24✔
636
                    'expression' => $this->inner($matches['parenthesized_types'][0]),
24✔
637
                ];
24✔
638
            }
639
        } elseif ('' !== $matches['class_constant'][0]) {
296✔
640
            $this->innerTypeExpressions[] = [
8✔
641
                'start_index' => 0,
8✔
642
                'expression' => $this->inner($matches['class_constant_name'][0]),
8✔
643
            ];
8✔
644
        }
645
    }
646

647
    private function parseCommaSeparatedInnerTypes(int $startIndex, string $value): void
648
    {
649
        $index = 0;
55✔
650
        while (\strlen($value) !== $index) {
55✔
651
            Preg::match(
55✔
652
                '{\G'.self::REGEX_TYPES.'(?:\h*,[\h\v*]*|$)}',
55✔
653
                $value,
55✔
654
                $matches,
55✔
655
                0,
55✔
656
                $index
55✔
657
            );
55✔
658

659
            $this->innerTypeExpressions[] = [
55✔
660
                'start_index' => $startIndex + $index,
55✔
661
                'expression' => $this->inner($matches['types']),
55✔
662
            ];
55✔
663

664
            $index += \strlen($matches[0]);
55✔
665
        }
666
    }
667

668
    private function parseCallableTemplateInnerTypes(int $startIndex, string $value): void
669
    {
670
        $index = 0;
63✔
671
        while (\strlen($value) !== $index) {
63✔
672
            Preg::match(
11✔
673
                '{\G(?:(?=1)0'.self::REGEX_TYPES.'|(?<_callable_template_inner>(?&callable_template_inner))(?:\h*,\h*|$))}',
11✔
674
                $value,
11✔
675
                $prematches,
11✔
676
                0,
11✔
677
                $index
11✔
678
            );
11✔
679
            $consumedValue = $prematches['_callable_template_inner'];
11✔
680
            $consumedValueLength = \strlen($consumedValue);
11✔
681
            $consumedCommaLength = \strlen($prematches[0]) - $consumedValueLength;
11✔
682

683
            $addedPrefix = 'Closure<';
11✔
684
            Preg::match(
11✔
685
                '{^'.self::REGEX_TYPES.'$}',
11✔
686
                $addedPrefix.$consumedValue.'>(): void',
11✔
687
                $matches,
11✔
688
                \PREG_OFFSET_CAPTURE
11✔
689
            );
11✔
690

691
            if ('' !== $matches['callable_template_inner_b'][0]) {
11✔
692
                $this->innerTypeExpressions[] = [
5✔
693
                    'start_index' => $startIndex + $index + $matches['callable_template_inner_b_types'][1]
5✔
694
                        - \strlen($addedPrefix),
5✔
695
                    'expression' => $this->inner($matches['callable_template_inner_b_types'][0]),
5✔
696
                ];
5✔
697
            }
698

699
            if ('' !== $matches['callable_template_inner_d'][0]) {
11✔
700
                $this->innerTypeExpressions[] = [
4✔
701
                    'start_index' => $startIndex + $index + $matches['callable_template_inner_d_types'][1]
4✔
702
                        - \strlen($addedPrefix),
4✔
703
                    'expression' => $this->inner($matches['callable_template_inner_d_types'][0]),
4✔
704
                ];
4✔
705
            }
706

707
            $index += $consumedValueLength + $consumedCommaLength;
11✔
708
        }
709
    }
710

711
    private function parseCallableArgumentTypes(int $startIndex, string $value): void
712
    {
713
        $index = 0;
63✔
714
        while (\strlen($value) !== $index) {
63✔
715
            Preg::match(
48✔
716
                '{\G(?:(?=1)0'.self::REGEX_TYPES.'|(?<_callable_argument>(?&callable_argument))(?:\h*,\h*|$))}',
48✔
717
                $value,
48✔
718
                $prematches,
48✔
719
                0,
48✔
720
                $index
48✔
721
            );
48✔
722
            $consumedValue = $prematches['_callable_argument'];
48✔
723
            $consumedValueLength = \strlen($consumedValue);
48✔
724
            $consumedCommaLength = \strlen($prematches[0]) - $consumedValueLength;
48✔
725

726
            $addedPrefix = 'Closure(';
48✔
727
            Preg::match(
48✔
728
                '{^'.self::REGEX_TYPES.'$}',
48✔
729
                $addedPrefix.$consumedValue.'): void',
48✔
730
                $matches,
48✔
731
                \PREG_OFFSET_CAPTURE
48✔
732
            );
48✔
733

734
            $this->innerTypeExpressions[] = [
48✔
735
                'start_index' => $startIndex + $index,
48✔
736
                'expression' => $this->inner($matches['callable_argument_type'][0]),
48✔
737
            ];
48✔
738

739
            $index += $consumedValueLength + $consumedCommaLength;
48✔
740
        }
741
    }
742

743
    private function parseArrayShapeInnerTypes(int $startIndex, string $value): void
744
    {
745
        $index = 0;
47✔
746
        while (\strlen($value) !== $index) {
47✔
747
            Preg::match(
42✔
748
                '{\G(?:(?=1)0'.self::REGEX_TYPES.'|(?<_array_shape_inner>(?&array_shape_inner))(?:\h*,[\h\v*]*|$))}',
42✔
749
                $value,
42✔
750
                $prematches,
42✔
751
                0,
42✔
752
                $index
42✔
753
            );
42✔
754
            $consumedValue = $prematches['_array_shape_inner'];
42✔
755
            $consumedValueLength = \strlen($consumedValue);
42✔
756
            $consumedCommaLength = \strlen($prematches[0]) - $consumedValueLength;
42✔
757

758
            $addedPrefix = 'array{';
42✔
759
            Preg::match(
42✔
760
                '{^'.self::REGEX_TYPES.'$}',
42✔
761
                $addedPrefix.$consumedValue.'}',
42✔
762
                $matches,
42✔
763
                \PREG_OFFSET_CAPTURE
42✔
764
            );
42✔
765

766
            $this->innerTypeExpressions[] = [
42✔
767
                'start_index' => $startIndex + $index + $matches['array_shape_inner_value'][1]
42✔
768
                    - \strlen($addedPrefix),
42✔
769
                'expression' => $this->inner($matches['array_shape_inner_value'][0]),
42✔
770
            ];
42✔
771

772
            $index += $consumedValueLength + $consumedCommaLength;
42✔
773
        }
774
    }
775

776
    private function inner(string $value): self
777
    {
778
        return new self($value, $this->namespace, $this->namespaceUses);
274✔
779
    }
780

781
    private function getParentType(string $type1, string $type2): ?string
782
    {
783
        $types = [
17✔
784
            $this->normalize($type1),
17✔
785
            $this->normalize($type2),
17✔
786
        ];
17✔
787
        natcasesort($types);
17✔
788
        $types = implode('|', $types);
17✔
789

790
        $parents = [
17✔
791
            'array|Traversable' => 'iterable',
17✔
792
            'array|iterable' => 'iterable',
17✔
793
            'iterable|Traversable' => 'iterable',
17✔
794
            'self|static' => 'self',
17✔
795
        ];
17✔
796

797
        return $parents[$types] ?? null;
17✔
798
    }
799

800
    private function normalize(string $type): string
801
    {
802
        if (isset(self::ALIASES[$type])) {
17✔
803
            return self::ALIASES[$type];
×
804
        }
805

806
        if (\in_array($type, [
17✔
807
            'array',
17✔
808
            'bool',
17✔
809
            'callable',
17✔
810
            'false',
17✔
811
            'float',
17✔
812
            'int',
17✔
813
            'iterable',
17✔
814
            'mixed',
17✔
815
            'never',
17✔
816
            'null',
17✔
817
            'object',
17✔
818
            'resource',
17✔
819
            'string',
17✔
820
            'true',
17✔
821
            'void',
17✔
822
        ], true)) {
17✔
823
            return $type;
16✔
824
        }
825

826
        if (Preg::match('/\[\]$/', $type)) {
12✔
827
            return 'array';
×
828
        }
829

830
        if (Preg::match('/^(.+?)</', $type, $matches)) {
12✔
831
            return $matches[1];
×
832
        }
833

834
        if (str_starts_with($type, '\\')) {
12✔
835
            return substr($type, 1);
1✔
836
        }
837

838
        foreach ($this->namespaceUses as $namespaceUse) {
11✔
839
            if ($namespaceUse->getShortName() === $type) {
6✔
840
                return $namespaceUse->getFullName();
6✔
841
            }
842
        }
843

844
        if (null === $this->namespace || $this->namespace->isGlobalNamespace()) {
5✔
845
            return $type;
5✔
846
        }
847

848
        return "{$this->namespace->getFullName()}\\{$type}";
×
849
    }
850
}
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