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

dg / dresscode / 41044778467

30 Oct 2026 10:08AM UTC coverage: 97.234% (-0.3%) from 97.495%
41044778467

push

github

dg
upgrade: a decision only the data of the target version turn on belongs to the jump, the project as it is today resolved without narrowing, so that the plan no longer fixes it in the bridge, where nothing runs it

837123ff-7353-45e9-8cd7-ba68b373e93e

5 of 5 new or added lines in 1 file covered. (100.0%)

160 existing lines in 33 files now uncovered.

27315 of 28092 relevant lines covered (97.23%)

0.97 hits per line

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

96.46
/src/Analyses/Types.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the DressCode, a coding style and upgrade tool for PHP (https://dresscode.run)
5
 * Copyright (c) 2026 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace DressCode\Analyses;
9

10
use DressCode\Tristate;
11
use PhpParser\Node as ParserNode;
12
use PhpParser\Node\Expr;
13
use PHPStan\Analyser\Scope;
14
use PHPStan\Node\{InstantiationCallableNode, MethodCallableNode, StaticMethodCallableNode};
15
use PHPStan\Php\PhpVersion;
16
use PHPStan\Reflection\{
17
        ClassConstantReflection, ClassReflection, ExtendedMethodReflection, ExtendedParameterReflection, ExtendedPropertyReflection,
18
};
19
use PHPStan\Reflection\Php\PhpPropertyReflection;
20
use PHPStan\TrinaryLogic;
21
use PHPStan\Type\Constant\{ConstantIntegerType, ConstantStringType};
22
use PHPStan\Type\{Type, TypeCombinator};
23
use PhpSyntax\Analyses\NameResolver;
24
use PhpSyntax\{Node, Printer, Token, Visibility};
25
use PhpSyntax\Nodes\{ClassLikeNode, ExpressionNode, FileNode, IdentifierNode, MemberNode, NameNode, ParameterNode};
26
use PhpSyntax\Nodes\Expression\{
27
        ClassConstantFetchNode, MethodCallNode, NewNode, ParenthesizedNode, PropertyFetchNode, StaticMethodCallNode, StaticPropertyFetchNode,
28
};
29
use PhpSyntax\Nodes\Member\{ClassConstNode, EnumCaseNode, MethodNode, PropertyNode};
30
use function count, strlen;
31

32

33
/**
34
 * The types of the code, from the PHPStan of the project: what an expression is, which member a call or an
35
 * access reaches, and what the declaration of that member says about it. The answers are those of the text of the
36
 * pass at its first question; a node inserted after it has none, and a rule asking for one asks with `?->`.
37
 * A rule asks questions; the answers in the words of PHPStan stay inside.
38
 */
39
final class Types implements PassAnalysis
40
{
41
        /** why PHPStan gave some nodes of the file no types, null where it gave all of them their types */
42
        public private(set) ?string $failure = null;
43

44
        /** @var \SplObjectStorage<ExpressionNode, array{Expr, Scope}> */
45
        private \SplObjectStorage $expressions;
46

47
        /** @var \SplObjectStorage<MethodNode, array{ParserNode\Stmt\ClassMethod, Scope}> */
48
        private \SplObjectStorage $methods;
49

50
        /** @var ?\Closure(): array<ParserNode\Stmt>  the text parsed by PHPStan once asked, until the scopes are resolved from it */
51
        private ?\Closure $parse;
52

53
        /**
54
         * @var array<string, list<ExpressionNode|MethodNode>>  "start:end" => the nodes the text stood at when the analysis
55
         * was made, the innermost first
56
         */
57
        private array $spans = [];
58

59
        /** @var \SplObjectStorage<MemberNode, null>  the member declarations the analysis was made with */
60
        private \SplObjectStorage $members;
61

62
        private ?NameResolver $names = null;
63

64
        private readonly PhpStan $phpstan;
65

66
        private readonly Declarations $declarations;
67

68

69
        /**
70
         * The scopes wait for the first question that needs them, most of a file asking about classes alone; the nodes are
71
         * found by where their text stood now, so that what the pass changes meanwhile moves nothing.
72
         * @internal
73
         */
74
        public function __construct(
75
                private readonly FileNode $file,
1 ✔
76
                private readonly string $path,
1 ✔
77
                PhpStan $phpstan,
78
        ) {
79
                $this->expressions = new \SplObjectStorage;
1 ✔
80
                $this->methods = new \SplObjectStorage;
1 ✔
81
                $this->members = new \SplObjectStorage;
1 ✔
82
                $code = Printer::print($file);
1 ✔
83
                $ast = null;
1 ✔
84
                $this->parse = function () use ($phpstan, $code, &$ast): array {
1 ✔
85
                        return $ast ??= $phpstan->parse($code);
1 ✔
86
                };
87
                $this->phpstan = $phpstan->deriveFor($path, $code, $this->parse);
1 ✔
88
                $this->declarations = $this->phpstan->getDeclarations();
1 ✔
89
                $this->collectSpans($file);
1 ✔
90
        }
1 ✔
91

92

93
        /**
94
         * Records where the text of the node and of everything under it stands, as `FileNode::findNode()` does.
95
         * @return ?array{int, int}  its offsets, the end exclusive; null for a node without tokens
96
         */
97
        private function collectSpans(Node $node): ?array
98
        {
99
                $start = $end = null;
1 ✔
100
                foreach ($node->getChildren() as $child) {
1 ✔
101
                        if ($child instanceof Token) {
1 ✔
102
                                $offset = $child->getCurrentOffset();
1 ✔
103
                                $range = [$offset, $offset + strlen($child->text)];
1 ✔
104
                        } else {
105
                                $range = $this->collectSpans($child);
1 ✔
106
                        }
107

108
                        if ($range !== null) {
1 ✔
109
                                $start ??= $range[0];
1 ✔
110
                                $end = $range[1];
1 ✔
111
                        }
112
                }
113

114
                if ($start === null || $end === null) {
1 ✔
115
                        return null;
1 ✔
116
                } elseif ($node instanceof ExpressionNode || $node instanceof MethodNode) {
1 ✔
117
                        $this->spans["$start:$end"][] = $node;
1 ✔
118
                }
119

120
                if ($node instanceof MemberNode) {
1 ✔
121
                        $this->members[$node] = null;
1 ✔
122
                }
123

124
                return [$start, $end];
1 ✔
125
        }
126

127

128
        private function resolveScopes(): void
129
        {
130
                if ($this->parse === null) {
1 ✔
131
                        return;
1 ✔
132
                }
133

134
                $ast = ($this->parse)();
1 ✔
135
                $this->parse = null;
1 ✔
136
                $circular = []; // class => whether its ancestors run in a circle, which leaves the nodes inside it without types
1 ✔
137
                try {
138
                        $this->phpstan->resolveScopes($this->path, $ast, function (ParserNode $node, Scope $scope) use (&$circular): void {
1 ✔
139
                                $class = $scope->getClassReflection();
1 ✔
140
                                if ($class !== null && ($circular[$class->getName()] ??= PhpStan::hasCircularAncestors($class))) {
1 ✔
141
                                        return;
×
142
                                } elseif ($node instanceof Expr) {
1 ✔
143
                                        $expression = $this->findSpan($node, ExpressionNode::class);
1 ✔
144
                                        if ($expression !== null && !isset($this->expressions[$expression])) {
1 ✔
145
                                                $this->expressions[$expression] = [$node, $scope];
1 ✔
146
                                        }
147
                                } elseif ($node instanceof ParserNode\Stmt\ClassMethod) {
1 ✔
148
                                        $method = $this->findSpan($node, MethodNode::class);
1 ✔
149
                                        if ($method !== null && !isset($this->methods[$method])) {
1 ✔
150
                                                $this->methods[$method] = [$node, $scope];
1 ✔
151
                                        }
152
                                }
153
                        });
1 ✔
154
                } catch (TypesUnavailableException $e) {
1 ✔
155
                        $this->failure = $e->getMessage();
1 ✔
156
                }
157
        }
1 ✔
158

159

160
        /**
161
         * The outermost node of the class the text of the node of PHPStan stood at.
162
         * @template T of ExpressionNode|MethodNode
163
         * @param  class-string<T>  $class
164
         * @return ?T
165
         */
166
        private function findSpan(ParserNode $node, string $class): ?Node
167
        {
168
                $nodes = $this->spans[$node->getStartFilePos() . ':' . ($node->getEndFilePos() + 1)] ?? [];
1 ✔
169
                for ($i = count($nodes) - 1; $i >= 0; $i--) {
1 ✔
170
                        if ($nodes[$i] instanceof $class) {
1 ✔
171
                                return $nodes[$i];
1 ✔
172
                        }
173
                }
174

175
                return null;
1 ✔
176
        }
177

178

179
        /**
180
         * The parentheses around an expression are not a node of PHPStan, so they are answered by the expression inside.
181
         * @return ?array{Expr, Scope}
182
         */
183
        private function findExpression(ExpressionNode $node): ?array
184
        {
185
                $this->resolveScopes();
1 ✔
186
                while ($node instanceof ParenthesizedNode) {
1 ✔
187
                        $node = $node->expression;
1 ✔
188
                }
189

190
                return $this->expressions[$node] ?? null;
1 ✔
191
        }
192

193

194
        /** @return ?array{ParserNode\Stmt\ClassMethod, Scope} */
195
        private function findDeclaration(MethodNode $node): ?array
196
        {
197
                $this->resolveScopes();
1 ✔
198
                return $this->methods[$node] ?? null;
1 ✔
199
        }
200

201

202
        /**
203
         * The type of the expression as PHPStan sees it, with `$at` as it sees it where that node stands; null for an
204
         * expression the analysis was made without.
205
         */
206
        private function getType(ExpressionNode $expression, ?ExpressionNode $at = null): ?Type
207
        {
208
                $found = $this->findExpression($expression);
1 ✔
209
                $scope = $at === null ? $found[1] ?? null : $this->findExpression($at)[1] ?? null;
1 ✔
210
                return $found === null || $scope === null ? null : $scope->getType($found[0]);
1 ✔
211
        }
212

213

214
        /**
215
         * Whether the expression is of the type, written in the syntax of PHPDoc as PHPStan reads it (`int|string`,
216
         * `list<int>`, `non-empty-string`), its classes fully qualified; maybe where it may or may not be, and for an
217
         * expression the analysis was made without. With `$at` the expression is asked about where that node stands, which
218
         * a part of the variable of `isset()` needs: PHPStan takes it there as not null, since `isset()` would not fail on it.
219
         */
220
        public function isOfType(ExpressionNode $expression, string $type, ?ExpressionNode $at = null): Tristate
221
        {
222
                $actual = $this->getType($expression, $at);
1 ✔
223
                if ($actual === null) {
1 ✔
224
                        return Tristate::Maybe;
1 ✔
225
                }
226

227
                $answer = $this->phpstan->resolveType($type)->isSuperTypeOf($actual);
1 ✔
228
                return match (true) {
229
                        $answer->yes() => Tristate::Yes,
1 ✔
230
                        $answer->no() => Tristate::No,
1 ✔
231
                        default => Tristate::Maybe,
1 ✔
232
                };
233
        }
234

235

236
        /**
237
         * Whether the loose and the strict comparison of the first expression with each of the others answer alike: yes
238
         * where they are all integers, all booleans or all enum cases; no where the first and another are scalars or null
239
         * of types with no value in common, which `===` never finds equal while `==` may (`1 == '1'`); maybe otherwise,
240
         * two strings among it, `'1' == '01'` comparing them as numbers, and for an expression the analysis was made
241
         * without.
242
         * @param list<ExpressionNode> $expressions
243
         */
244
        public function isComparedAlike(array $expressions): Tristate
245
        {
246
                if (array_any(
1 ✔
247
                        ['int', 'bool', \UnitEnum::class],
1 ✔
248
                        fn(string $type) => array_all(
1 ✔
249
                                $expressions,
250
                                fn(ExpressionNode $expression) => $this->isOfType($expression, $type) === Tristate::Yes,
1 ✔
251
                        ),
1 ✔
252
                )) {
253
                        return Tristate::Yes;
1 ✔
254
                }
255

256
                $first = $expressions === [] ? null : $this->getType($expressions[0]);
1 ✔
257
                if ($first === null || !$first->isScalar()->or($first->isNull())->yes()) {
1 ✔
258
                        return Tristate::Maybe;
×
259
                }
260

261
                foreach (array_slice($expressions, 1) as $expression) {
1 ✔
262
                        $type = $this->getType($expression);
1 ✔
263
                        if (
264
                                $type !== null
1 ✔
265
                                && $type->isScalar()->or($type->isNull())->yes()
1 ✔
266
                                && $first->isSuperTypeOf($type)->no()
1 ✔
267
                                && !$first->looseCompare($type, new PhpVersion(80000))->isFalse()->yes()
1 ✔
268
                        ) {
269
                                return Tristate::No;
1 ✔
270
                        }
271
                }
272

273
                return Tristate::Maybe;
1 ✔
274
        }
275

276

277
        /**
278
         * The classes the expression may be an instance of, fully qualified: the candidates and not the whole type, those
279
         * of a union and of an intersection alike; none both for a value that is no object and for one of a type unknown.
280
         * @return list<string>
281
         */
282
        public function findClasses(ExpressionNode $expression): array
283
        {
284
                return $this->getType($expression)?->getObjectClassNames() ?? [];
1 ✔
285
        }
286

287

288
        /**
289
         * The member the node reaches: the constant of a class constant access, the method of a call, the property of
290
         * an access, the constructor of an instantiation, decided by the class that declares it and of the kind it is
291
         * declared (`parent::recalc()` reaches a method, `$order->make()` a static one, `parent::__construct()` the
292
         * constructor). Null for a node that reaches no known member, or whose name is an expression.
293
         */
294
        public function findMember(ExpressionNode $node): ?Member
295
        {
296
                $receiver = $this->findReceiver($node);
1 ✔
297
                if ($receiver === null) {
1 ✔
298
                        return null;
1 ✔
299
                }
300

301
                [$kind, $type, $name, $scope] = $receiver;
1 ✔
302
                $reflection = match ($kind) {
1 ✔
303
                        MemberKind::Constant => $scope->getConstantReflection($type, $name),
1 ✔
304
                        MemberKind::Method, MemberKind::StaticMethod, MemberKind::Constructor => $scope->getMethodReflection($type, $name),
1 ✔
305
                        MemberKind::Property => $scope->getInstancePropertyReflection($type, $name),
1 ✔
306
                        MemberKind::StaticProperty => $scope->getStaticPropertyReflection($type, $name),
1 ✔
307
                };
308
                if ($reflection === null) {
1 ✔
309
                        return null;
1 ✔
310
                } elseif ($kind === MemberKind::Method || $kind === MemberKind::StaticMethod) {
1 ✔
311
                        $kind = match (true) {
1 ✔
312
                                strcasecmp($name, '__construct') === 0 => MemberKind::Constructor,
1 ✔
313
                                $reflection->isStatic() => MemberKind::StaticMethod,
1 ✔
314
                                default => MemberKind::Method,
1 ✔
315
                        };
316
                }
317

318
                // a property reflection does not say its name, which the declaration cannot spell differently anyway
319
                return new Member(
1 ✔
320
                        $kind,
321
                        $reflection instanceof ExtendedPropertyReflection ? $name : $reflection->getName(),
1 ✔
322
                        $reflection->getDeclaringClass()->getName(),
1 ✔
323
                );
324
        }
325

326

327
        /**
328
         * The member access the node makes, decided by the type of its receiver and not by the class that declares the
329
         * member: a class constant access, a method call, a property access, or an instantiation of a named class. There
330
         * is one for a member no class declares any more, and where a child overrides the member the classes are those of
331
         * the receiver. A property is declared only where the access sees it, PHP calling `__get` and `__set` for one it
332
         * does not. Null for a name that is an expression and for a receiver that is no object.
333
         */
334
        public function findMemberAccess(ExpressionNode $node): ?MemberAccess
335
        {
336
                $receiver = $this->findReceiver($node);
1 ✔
337
                if ($receiver === null) {
1 ✔
338
                        return null;
1 ✔
339
                }
340

341
                [$kind, $type, $name, $scope] = $receiver;
1 ✔
342
                $classes = $type->getObjectClassNames();
1 ✔
343
                return new MemberAccess(
1 ✔
344
                        $kind,
345
                        $name,
346
                        $classes,
347
                        array_any($classes, fn(string $class) => $this->declaresMember($class, $kind, $name, $scope)),
1 ✔
348
                        $scope->getClassReflection()?->getName(),
1 ✔
349
                );
350
        }
351

352

353
        /**
354
         * The instantiation, or a call of `parent::__construct()`, as the access of the constructor it runs, whose class is
355
         * the one that declares it: `new Child` of a child that declares none runs the constructor of its parent, a child
356
         * that declares one its own. Null for any other node.
357
         */
358
        public function findConstructorAccess(ExpressionNode $node): ?MemberAccess
359
        {
360
                $access = $node instanceof NewNode
1 ✔
361
                        || ($node instanceof StaticMethodCallNode && $node->name instanceof IdentifierNode && $node->name->equals('__construct'))
1 ✔
362
                        ? $this->findMemberAccess($node)
1 ✔
363
                        : null;
1 ✔
364
                if ($access === null) {
1 ✔
365
                        return null;
1 ✔
366
                }
367

368
                $declaring = $this->findMember($node)?->declaringClass;
1 ✔
369
                return new MemberAccess(
1 ✔
370
                        MemberKind::Constructor,
1 ✔
371
                        '__construct',
1 ✔
372
                        $declaring === null ? $access->classes : [$declaring],
1 ✔
373
                        $access->declared,
1 ✔
374
                        $access->scopeClass,
1 ✔
375
                );
376
        }
377

378

379
        /**
380
         * The method a value in the shape of a callable names, `[$object, 'name']`, `[Order::class, 'name']` or
381
         * `'Acme\Order::name'`, as an access of the class of the object or of the one named. Whether a class declares the
382
         * method is not asked, a method the library removed being named as well as any. Null for a value of another shape.
383
         */
384
        public function findCallableMethodAccess(ExpressionNode $callable): ?MemberAccess
385
        {
386
                $type = $this->getType($callable);
1 ✔
387
                $strings = $type?->getConstantStrings() ?? [];
1 ✔
388
                $arrays = $type?->getConstantArrays() ?? [];
1 ✔
389
                $pair = count($arrays) === 1
1 ✔
390
                        && array_map(fn(ConstantIntegerType|ConstantStringType $key) => $key->getValue(), $arrays[0]->getKeyTypes()) === [0, 1]
1 ✔
391
                        ? $arrays[0]->getValueTypes()
1 ✔
392
                        : null;
1 ✔
393
                $names = $pair === null ? [] : $pair[1]->getConstantStrings();
1 ✔
394
                if (count($strings) === 1 && str_contains($strings[0]->getValue(), '::')) {
1 ✔
395
                        [$class, $name] = explode('::', $strings[0]->getValue(), 2);
1 ✔
396
                        $classes = [ltrim($class, '\\')];
1 ✔
397
                        $named = true;
1 ✔
398
                } elseif ($pair !== null && count($names) === 1) {
1 ✔
399
                        $name = $names[0]->getValue();
1 ✔
400
                        $classes = $pair[0]->getObjectClassNames();
1 ✔
401
                        $named = $classes === [];
1 ✔
402
                        $classes = $named
1 ✔
403
                                ? array_map(fn(ConstantStringType $class) => ltrim($class->getValue(), '\\'), $pair[0]->getConstantStrings())
1 ✔
404
                                : $classes;
1 ✔
405
                } else {
406
                        return null;
×
407
                }
408

409
                if ($classes === [] || !preg_match('~^[a-z_\x80-\xff][\w\x80-\xff]*$~Di', $name)) {
1 ✔
410
                        return null;
×
411
                }
412

413
                // a class named by its name calls a method that is not static too, where a container makes the object
414
                $kind = $named && array_all($classes, fn(string $class) => $this->declarations->isStaticMethod($class, $name) === Tristate::Yes)
1 ✔
415
                        ? MemberKind::StaticMethod
1 ✔
416
                        : MemberKind::Method;
1 ✔
417
                return new MemberAccess(
1 ✔
418
                        $kind,
419
                        $name,
420
                        $classes,
421
                        array_any($classes, fn(string $class) => $this->declarations->hasMember($class, $kind, $name)),
1 ✔
422
                        $this->findScopeClass($callable),
1 ✔
423
                );
424
        }
425

426

427
        /** The class whose code the expression stands in; null outside a class and for an expression the analysis was made without. */
428
        public function findScopeClass(ExpressionNode $node): ?string
429
        {
430
                return ($this->findExpression($node)[1] ?? null)?->getClassReflection()?->getName();
1 ✔
431
        }
432

433

434
        /**
435
         * The parameters of the method the access calls, as the class of the receiver declares them; null for an access
436
         * that is no call, for a method no class of the receiver declares, and where two of them declare it differently.
437
         * @return ?list<Parameter>
438
         */
439
        public function findParameters(MemberAccess $access): ?array
440
        {
441
                if (!in_array($access->kind, [MemberKind::Method, MemberKind::StaticMethod, MemberKind::Constructor], true)) {
1 ✔
442
                        return null;
1 ✔
443
                }
444

445
                $found = null;
1 ✔
446
                foreach ($access->classes as $class) {
1 ✔
447
                        if (!$this->declarations->hasMember($class, $access->kind, $access->name)) {
1 ✔
448
                                continue;
1 ✔
449
                        }
450

451
                        // the default does not decide how an argument binds
452
                        $parameters = $this->declarations->findMethodParameters($class, $access->name);
1 ✔
453
                        $describe = fn(Parameter $parameter) => [
1 ✔
454
                                $parameter->name,
1 ✔
455
                                $parameter->type,
1 ✔
456
                                $parameter->optional,
1 ✔
457
                                $parameter->variadic,
1 ✔
458
                                $parameter->byReference,
1 ✔
459
                        ];
460
                        if (
461
                                $parameters === null
1 ✔
462
                                || ($found !== null && array_map($describe, $found) !== array_map($describe, $parameters))
1 ✔
463
                        ) {
464
                                return null;
1 ✔
465
                        }
466

467
                        $found = $parameters;
1 ✔
468
                }
469

470
                return $found;
1 ✔
471
        }
472

473

474
        /**
475
         * The methods the class takes from a trait that a parent or an interface declares with parameters named otherwise,
476
         * which the trait, declared without the class, cannot be checked against itself.
477
         * @return list<array{string, string, list<string>, string, list<string>}>  per method its name, the trait, the names
478
         *   of its parameters, the ancestor and the names of its parameters
479
         * @internal
480
         */
481
        public function findTraitParameterNames(string $class): array
482
        {
483
                return $this->declarations->findTraitParameterNames($class);
1 ✔
484
        }
485

486

487
        /**
488
         * The parameters of the method as the class declares it, itself or through an ancestor, the constructor among them;
489
         * null for a method the class does not have, for a class nothing declares and for a method of several variants.
490
         * @return ?list<Parameter>
491
         */
492
        public function findMethodParameters(string $class, string $method): ?array
493
        {
494
                return $this->declarations->findMethodParameters($class, $method);
1 ✔
495
        }
496

497

498
        /**
499
         * The parameters of the function the project, its packages or PHP declare, given its fully qualified name in any
500
         * letter case without a leading backslash; null for a name nothing declares and for a function of several variants.
501
         * @return ?list<Parameter>
502
         */
503
        public function findFunctionParameters(string $function): ?array
504
        {
505
                return $this->declarations->findFunctionParameters($function);
1 ✔
506
        }
507

508

509
        /**
510
         * Whether a child of the class may declare the method again, which an instance of the child then runs instead: no
511
         * for a private or final method and for a final class, an enum and an anonymous class among them; maybe for a method
512
         * the class does not have and for a class nothing declares.
513
         */
514
        public function isOverridable(string $class, string $method): Tristate
515
        {
516
                return $this->declarations->isOverridable($class, $method);
1 ✔
517
        }
518

519

520
        /**
521
         * Whether the method reads its arguments by `func_get_args()`, `func_get_arg()` or `func_num_args()`, so that an
522
         * argument written as the default may still change what it does; true for a method nothing declares.
523
         */
524
        public function countsArguments(string $class, string $method): bool
525
        {
526
                return $this->declarations->countsArguments($class, $method);
1 ✔
527
        }
528

529

530
        /** The class the method declaration belongs to; null for a declaration the analysis was made without. */
531
        public function findDeclaringClass(MethodNode $declaration): ?string
532
        {
533
                return $this->findDeclaredClass($declaration)?->getName();
1 ✔
534
        }
535

536

537
        /**
538
         * The member of the same name a parent class or an interface declares, which the declaration overrides: a method
539
         * by a method, a constant by a constant or an enum case, a property by a property or a promoted parameter; a
540
         * private member of an ancestor is hidden, not overridden, and the next ancestor is asked. Null where the
541
         * declaration overrides nothing, for one of several constants or properties, for a constant, an enum case or a
542
         * property declared outside the constructor of an anonymous class, and for a declaration the analysis was made
543
         * without.
544
         */
545
        public function findOverridden(MethodNode|ClassConstNode|EnumCaseNode|PropertyNode|ParameterNode $declaration): ?Member
546
        {
547
                [$kind, $name] = match (true) {
1 ✔
548
                        $declaration instanceof MethodNode => [MemberKind::Method, $declaration->name->text],
1 ✔
549
                        $declaration instanceof EnumCaseNode => [MemberKind::Constant, $declaration->name->text],
1 ✔
550
                        $declaration instanceof ClassConstNode => [
1 ✔
551
                                MemberKind::Constant,
1 ✔
552
                                $declaration->items->count() === 1 ? $declaration->items->getItems()[0]->name->text : null,
1 ✔
553
                        ],
554
                        $declaration instanceof PropertyNode => [
1 ✔
555
                                MemberKind::Property,
1 ✔
556
                                $declaration->items->count() === 1 ? $declaration->items->getItems()[0]->plainName : null,
1 ✔
557
                        ],
558
                        default => [MemberKind::Property, $declaration->promoted ? $declaration->variable->plainName : null],
1 ✔
559
                };
560
                $class = $name === null ? null : $this->findDeclaredClass($declaration);
1 ✔
561
                $member = $class === null ? null : self::findOverriddenMember($class, $kind, $name);
1 ✔
562
                return match (true) {
563
                        $member instanceof ExtendedMethodReflection => new Member(
1 ✔
564
                                $member->isStatic() ? MemberKind::StaticMethod : MemberKind::Method,
1 ✔
565
                                $member->getName(),
1 ✔
566
                                $member->getDeclaringClass()->getName(),
1 ✔
567
                        ),
568
                        $member instanceof PhpPropertyReflection => new Member(
1 ✔
569
                                $member->isStatic() ? MemberKind::StaticProperty : MemberKind::Property,
1 ✔
570
                                $member->getName(),
1 ✔
571
                                $member->getDeclaringClass()->getName(),
1 ✔
572
                        ),
573
                        $member !== null => new Member(MemberKind::Constant, $member->getName(), $member->getDeclaringClass()->getName()),
1 ✔
574
                        default => null,
1 ✔
575
                };
576
        }
577

578

579
        /**
580
         * The property of the same name a trait the class uses declares, which PHP composes with the declaration, a
581
         * property or a promoted parameter. Null where no trait declares one, for one of several properties, and for a
582
         * declaration the analysis was made without.
583
         */
584
        public function findTraitProperty(PropertyNode|ParameterNode $declaration): ?Member
585
        {
586
                $name = match (true) {
1 ✔
587
                        $declaration instanceof PropertyNode => $declaration->items->count() === 1
1 ✔
588
                                ? $declaration->items->getItems()[0]->plainName
1 ✔
589
                                : null,
1 ✔
590
                        default => $declaration->promoted ? $declaration->variable->plainName : null,
1 ✔
591
                };
592
                $class = $name === null ? null : $this->findDeclaredClass($declaration);
1 ✔
593
                if ($name === null || $class === null) {
1 ✔
594
                        return null;
×
595
                }
596

597
                foreach ($class->getTraits() as $trait) {
1 ✔
598
                        if ($trait->hasNativeProperty($name)) {
1 ✔
599
                                $property = $trait->getNativeProperty($name);
1 ✔
600
                                return new Member(
1 ✔
601
                                        $property->isStatic() ? MemberKind::StaticProperty : MemberKind::Property,
1 ✔
602
                                        $property->getName(),
1 ✔
603
                                        $trait->getName(),
1 ✔
604
                                );
605
                        }
606
                }
607

608
                return null;
1 ✔
609
        }
610

611

612
        /**
613
         * Whether the parent class declares the method the way the declaration does, so that a caller sees no difference
614
         * between the two: the same visibility and staticness, the same parameters by name, order, default, reference,
615
         * variadic and native type, and the same native return type. False for a declaration the analysis was made without,
616
         * for one no parent class declares, and for one whose parent declaration is private or abstract.
617
         */
618
        public function matchesParentSignature(MethodNode $declaration): bool
619
        {
620
                $class = $this->findDeclaredClass($declaration);
1 ✔
621
                if ($class === null) {
1 ✔
622
                        return false;
×
623
                }
624

625
                $name = $declaration->name->text;
1 ✔
626
                $parent = $class->getParentClass();
1 ✔
627
                if ($parent === null || !$class->hasNativeMethod($name) || !$parent->hasNativeMethod($name)) {
1 ✔
628
                        return false;
1 ✔
629
                }
630

631
                $own = $class->getNativeMethod($name);
1 ✔
632
                $inherited = $parent->getNativeMethod($name);
1 ✔
633
                $abstract = $inherited->isAbstract();
1 ✔
634
                if (
635
                        $inherited->isPrivate()
1 ✔
636
                        || ($abstract instanceof TrinaryLogic ? !$abstract->no() : $abstract)
1 ✔
637
                        || $own->isPublic() !== $inherited->isPublic()
1 ✔
638
                        || $own->isPrivate() !== $inherited->isPrivate()
1 ✔
639
                        || $own->isStatic() !== $inherited->isStatic()
1 ✔
640
                        || count($own->getVariants()) !== 1
1 ✔
641
                        || count($inherited->getVariants()) !== 1
1 ✔
642
                ) {
643
                        return false;
1 ✔
644
                }
645

646
                $ownVariant = $own->getOnlyVariant();
1 ✔
647
                $inheritedVariant = $inherited->getOnlyVariant();
1 ✔
648
                $ownParameters = $ownVariant->getParameters();
1 ✔
649
                $inheritedParameters = $inheritedVariant->getParameters();
1 ✔
650
                if (
651
                        count($ownParameters) !== count($inheritedParameters)
1 ✔
652
                        || !$ownVariant->getNativeReturnType()->equals($inheritedVariant->getNativeReturnType())
1 ✔
653
                ) {
654
                        return false;
1 ✔
655
                }
656

657
                foreach ($ownParameters as $i => $parameter) {
1 ✔
658
                        $other = $inheritedParameters[$i];
1 ✔
659
                        $default = $parameter->getDefaultValue();
1 ✔
660
                        $otherDefault = $other->getDefaultValue();
1 ✔
661
                        if (
662
                                $parameter->getName() !== $other->getName()
1 ✔
663
                                || $parameter->isOptional() !== $other->isOptional()
1 ✔
664
                                || $parameter->isVariadic() !== $other->isVariadic()
1 ✔
665
                                || !$parameter->passedByReference()->equals($other->passedByReference())
1 ✔
666
                                || !$parameter->getNativeType()->equals($other->getNativeType())
1 ✔
667
                                || ($default === null) !== ($otherDefault === null)
1 ✔
668
                                || ($default !== null && $otherDefault !== null && !$default->equals($otherDefault))
1 ✔
669
                        ) {
670
                                return false;
1 ✔
671
                        }
672
                }
673

674
                return true;
1 ✔
675
        }
676

677

678
        /**
679
         * The method the declaration overrides as the parent class or an interface declares it natively, the first of
680
         * them that declares it and not privately, with where the declaration departs from it; null where it overrides
681
         * nothing, either has more than one variant, or the analysis was made without the declaration.
682
         */
683
        public function findOverriddenSignature(MethodNode $declaration): ?OverriddenSignature
684
        {
685
                $class = $this->findDeclaredClass($declaration);
1 ✔
686
                if ($class === null) {
1 ✔
687
                        return null;
×
688
                }
689

690
                $name = $declaration->name->text;
1 ✔
691
                $inherited = $class->hasNativeMethod($name) ? self::findOverriddenMember($class, MemberKind::Method, $name) : null;
1 ✔
692
                if (!$inherited instanceof ExtendedMethodReflection || count($inherited->getVariants()) !== 1) {
1 ✔
693
                        return null;
1 ✔
694
                }
695

696
                $found = $this->findDeclaration($declaration); // the scopes are asked only for a declaration that overrides something
1 ✔
697
                if ($found === null) {
1 ✔
698
                        return null;
×
699
                }
700

701
                // the declaration is read from the text of the pass, the reflection of its class is the file on the disk
702
                [$method, $scope] = $found;
1 ✔
703
                $ownReturnType = $scope->getFunctionType($method->returnType, isNullable: false, isVariadic: false);
1 ✔
704
                $ownTypes = array_map(fn(ParserNode\Param $param) => $scope->getFunctionType(
1 ✔
705
                        $param->type,
1 ✔
706
                        $param->default instanceof Expr\ConstFetch && $param->default->name->toLowerString() === 'null',
1 ✔
707
                        isVariadic: false,
1 ✔
708
                ), $method->params);
1 ✔
709
                $variant = $inherited->getOnlyVariant();
1 ✔
710
                $returnType = $variant->getNativeReturnType();
1 ✔
711
                return new OverriddenSignature(
1 ✔
712
                        $inherited->getDeclaringClass()->getName(),
1 ✔
713
                        $inherited->isFinal()->yes(),
1 ✔
714
                        $inherited->isStatic(),
1 ✔
715
                        $inherited->isPublic() ? Visibility::Public : Visibility::Protected,
1 ✔
716
                        Declarations::renderNativeType($returnType),
1 ✔
717
                        Declarations::renderNativeType($returnType) !== null
1 ✔
718
                        && (Declarations::renderNativeType($ownReturnType) === null || !$returnType->isSuperTypeOf($ownReturnType)->yes()),
1 ✔
719
                        array_map(Declarations::toParameter(...), $variant->getParameters()),
1 ✔
720
                        array_keys(array_filter(
1 ✔
721
                                $variant->getParameters(),
1 ✔
722
                                fn(ExtendedParameterReflection $parameter, int $i) => isset($ownTypes[$i])
1 ✔
723
                                        && !$ownTypes[$i]->isSuperTypeOf($parameter->getNativeType())->yes(),
1 ✔
724
                                ARRAY_FILTER_USE_BOTH,
1 ✔
725
                        )),
726
                );
727
        }
728

729

730
        /**
731
         * The class the member declaration, or the constructor of the promoted parameter, belongs to, found by its name,
732
         * which needs no scopes; only an anonymous class has none, and is asked of them for a method. Null for a
733
         * declaration the analysis was made without.
734
         */
735
        private function findDeclaredClass(MemberNode|ParameterNode $declaration): ?ClassReflection
736
        {
737
                $member = $declaration instanceof ParameterNode ? $declaration->findAncestor(MethodNode::class) : $declaration;
1 ✔
738
                if ($member === null || !isset($this->members[$member])) {
1 ✔
739
                        return null;
×
740
                }
741

742
                $owner = $member->findAncestor(ClassLikeNode::class);
1 ✔
743
                $className = $owner === null ? null : ($this->names ??= new NameResolver($this->file))->getDeclaredName($owner);
1 ✔
744
                return match (true) {
1 ✔
745
                        $className !== null => $this->phpstan->findClass($className),
1 ✔
746
                        $member instanceof MethodNode => ($this->findDeclaration($member)[1] ?? null)?->getClassReflection(),
1 ✔
747
                        default => null,
1 ✔
748
                };
749
        }
750

751

752
        /**
753
         * The member of the kind and name the parent class or the first interface declares natively and not privately,
754
         * which a member of the class overrides; a private member of an ancestor is hidden, not overridden, and a member
755
         * only an annotation declares is none PHP overrides.
756
         */
757
        private static function findOverriddenMember(
758
                ClassReflection $class,
759
                MemberKind $kind,
760
                string $name,
761
        ): ClassConstantReflection|ExtendedMethodReflection|PhpPropertyReflection|null
762
        {
763
                $parent = $class->getParentClass();
1 ✔
764
                foreach ([...($parent === null ? [] : [$parent]), ...$class->getInterfaces()] as $ancestor) {
1 ✔
765
                        $member = Declarations::findNativeMember($ancestor, $kind, $name);
1 ✔
766
                        if ($member !== null && !$member->isPrivate()) {
1 ✔
767
                                return $member;
1 ✔
768
                        }
769
                }
770

771
                return null;
1 ✔
772
        }
773

774

775
        /**
776
         * What the declaration of the member says about its deprecation; null for a member that is not deprecated, for
777
         * one its class does not have, and for one a package declares that the project supports in an older version than
778
         * the installed one, which need not deprecate it nor have its replacement.
779
         */
780
        public function findDeprecation(Member $member): ?Deprecation
781
        {
782
                return $this->declarations->findDeprecation($member);
1 ✔
783
        }
784

785

786
        /**
787
         * Whether the member is abstract, which a class that is not abstract must implement: a method or a property of an
788
         * interface is, a constant never; maybe for a member its class does not have.
789
         */
790
        public function isAbstract(Member $member): Tristate
791
        {
792
                return $this->declarations->isAbstract($member);
1 ✔
793
        }
794

795

796
        /**
797
         * Whether the member of that name, which the class declaring the member has, can be written in its place without
798
         * touching anything else: of the same kind and staticness, and for a method one that takes every call of the
799
         * member the same way, the parameters of the member in the same order under the same names, each taking what
800
         * the one of the member takes, and none required that the member does not have; at least as visible as the
801
         * member, a private member of an ancestor being seen nowhere; for a property also at least as writable, and of the same
802
         * native type.
803
         */
804
        public function canReplace(Member $member, string $name): bool
805
        {
806
                return $this->declarations->canReplace($member, $name);
1 ✔
807
        }
808

809

810
        /**
811
         * Whether a public static method of another class can be called in place of the static method of the member: it takes
812
         * every call of the member the same way, as `canReplace()` asks, the names of the parameters counting only where the
813
         * call names its arguments; not one of a package installed in a newer version than the project supports.
814
         */
815
        public function canReplaceByStaticMethod(Member $member, string $class, string $method, bool $named): bool
816
        {
817
                return $this->declarations->canReplaceByStaticMethod($member, $class, $method, $named);
1 ✔
818
        }
819

820

821
        /**
822
         * The declared spelling of a class, interface or enum, with `$traits` also of a trait, the project, its packages or
823
         * PHP declare, given its fully qualified name in any letter case without a leading backslash; null for a name
824
         * nothing declares.
825
         */
826
        public function findClassName(string $name, bool $traits = false): ?string
827
        {
828
                return $this->declarations->findClassName($name, $traits);
1 ✔
829
        }
830

831

832
        /**
833
         * Whether the file may name the class as one it reaches: not where only the tests of the project declare it and the
834
         * file is not one of them, the autoload of the tests being no part of the code, and not where a `.phpt` other than
835
         * the file declares it, a script no autoload loads.
836
         */
837
        public function canNameClass(string $name): bool
838
        {
839
                $file = $this->phpstan->findClass($name)?->getFileName();
1 ✔
840
                return match (true) {
1 ✔
841
                        $file === null => true,
×
842
                        str_ends_with($file, '.phpt') => $this->phpstan->isSameFile($file, $this->path),
1 ✔
843
                        default => !$this->phpstan->isTestFile($file) || $this->phpstan->isTestFile($this->path),
1 ✔
844
                };
845
        }
846

847

848
        /**
849
         * The declared spelling of a function the project, its packages or PHP declare, given its fully qualified name in
850
         * any letter case without a leading backslash; null for a name nothing declares.
851
         */
852
        public function findFunctionName(string $name): ?string
853
        {
854
                return $this->declarations->findFunctionName($name);
1 ✔
855
        }
856

857

858
        /**
859
         * Whether the class is the ancestor, extends it, implements it or uses it as a trait, both fully qualified, in any
860
         * letter case; maybe where either is a class nothing declares, or one whose ancestors run in a circle, which PHP
861
         * refuses to load, and where an ancestor of the class is one nothing declares, so that its hierarchy is not seen whole.
862
         */
863
        public function isSubtype(string $class, string $ancestor): Tristate
864
        {
865
                return $this->declarations->isSubtype($class, $ancestor);
1 ✔
866
        }
867

868

869
        /**
870
         * Whether the class only stands for the other one: it extends or implements it and declares nothing of its own, as
871
         * the declaration of an old name kept for a `class_alias()` does.
872
         */
873
        public function isAlias(string $class, string $of): bool
874
        {
875
                return $this->declarations->isAlias($class, $of);
1 ✔
876
        }
877

878

879
        /** Whether PHP reads the class as an attribute, `#[\Attribute]` standing on it; maybe for a class nothing declares. */
880
        public function isAttributeClass(string $class): Tristate
881
        {
882
                return $this->declarations->isAttributeClass($class);
1 ✔
883
        }
884

885

886
        /** Whether the class is an interface; maybe for a class nothing declares. */
887
        public function isInterface(string $class): Tristate
888
        {
889
                return $this->declarations->isInterface($class);
1 ✔
890
        }
891

892

893
        /** Whether the class is declared final; maybe for a class nothing declares. */
894
        public function isFinalClass(string $class): Tristate
895
        {
896
                return $this->declarations->isFinalClass($class);
1 ✔
897
        }
898

899

900
        /**
901
         * Whether an instance of the class takes a property it does not declare without the deprecation of PHP 8.2: through
902
         * `#[AllowDynamicProperties]`, `stdClass`, a magic method or its version of PHP; maybe for a class nothing declares.
903
         */
904
        public function allowsDynamicProperties(string $class): Tristate
905
        {
906
                return $this->declarations->allowsDynamicProperties($class);
1 ✔
907
        }
908

909

910
        /**
911
         * The abstract methods a class or an enum that is not abstract itself inherits from its parents, interfaces and
912
         * traits and implements nowhere, each as the member of the ancestor or the trait declaring it; none for an
913
         * abstract class, an interface, a trait and a class nothing declares.
914
         * @return list<Member>
915
         */
916
        public function findUnimplementedMethods(string $class): array
917
        {
918
                return $this->declarations->findUnimplementedMethods($class);
1 ✔
919
        }
920

921

922
        /**
923
         * Whether the class has the member of that kind, itself or through an ancestor; a magic one it does not have, and
924
         * a class nothing declares has none: what the maps of members ask is whether the code still reaches a declaration,
925
         * a member a library removed together with its class being as undeclared as one removed from the class alone.
926
         */
927
        public function hasMember(string $class, MemberKind $kind, string $name): bool
928
        {
929
                return $this->declarations->hasMember($class, $kind, $name);
1 ✔
930
        }
931

932

933
        /** Whether the class still has the member in any form, declared or magic as PHPStan sees it; a constructor where it has the class. */
934
        public function knowsMember(string $class, MemberKind $kind, string $name): bool
935
        {
936
                $reflection = $this->phpstan->findClass($class);
1 ✔
937
                return $reflection !== null && match ($kind) {
1 ✔
938
                        MemberKind::Constant => $reflection->hasConstant($name),
1 ✔
939
                        MemberKind::Method, MemberKind::StaticMethod => $reflection->hasMethod($name),
1 ✔
940
                        MemberKind::Property => $reflection->hasInstanceProperty($name),
1 ✔
941
                        MemberKind::StaticProperty => $reflection->hasStaticProperty($name),
1 ✔
942
                        MemberKind::Constructor => true,
1 ✔
943
                };
944
        }
945

946

947
        /** Whether the project, its packages or PHP declare the global or namespaced constant, given its fully qualified name. */
948
        public function knowsConstant(string $name): bool
949
        {
UNCOV
950
                return $this->phpstan->hasConstant($name);
×
951
        }
952

953

954
        /**
955
         * Whether the property the access reaches holds a plain value its reads and writes go straight to: declared,
956
         * neither readonly, virtual nor hooked, and one no child can hook, being private or final or of a final class.
957
         * No for a property reached through `__get` and `__set` and for a readonly, virtual or hooked one; maybe where
958
         * a child may hook it, and for an access the types cannot tell or the analysis was made without.
959
         */
960
        public function isPlainProperty(ExpressionNode $access): Tristate
961
        {
962
                $member = $this->findMember($access);
1 ✔
963
                if ($member === null) {
1 ✔
964
                        // a property no class declares is magic where every class of the receiver answers through __get or __set
965
                        $classes = $this->findMemberAccess($access)->classes ?? [];
1 ✔
966
                        return $classes !== [] && array_all($classes, fn(string $name) => ($class = $this->phpstan->findClass($name)) !== null
1 ✔
967
                                && ($class->hasNativeMethod('__get') || $class->hasNativeMethod('__set')))
1 ✔
968
                                ? Tristate::No
1 ✔
969
                                : Tristate::Maybe;
1 ✔
970
                }
971

972
                $class = $member->kind === MemberKind::Property || $member->kind === MemberKind::StaticProperty
1 ✔
973
                        ? $this->phpstan->findClass($member->declaringClass)
1 ✔
UNCOV
974
                        : null;
×
975
                if ($class === null) {
1 ✔
UNCOV
976
                        return Tristate::Maybe;
×
977
                } elseif (!$class->hasNativeProperty($member->name)) {
1 ✔
UNCOV
978
                        return Tristate::No;
×
979
                }
980

981
                $property = $class->getNativeProperty($member->name);
1 ✔
982
                return match (true) {
983
                        $property->isReadOnly(), $property->isVirtual()->yes(), $property->hasHook('get'), $property->hasHook('set') => Tristate::No,
1 ✔
984
                        $property->isPrivate(), $property->isFinal()->yes(), $class->isFinalByKeyword() => Tristate::Yes,
1 ✔
985
                        default => Tristate::Maybe,
1 ✔
986
                };
987
        }
988

989

990
        /**
991
         * Whether the class has the method, in any letter case of either, and it is static; no for a class that has no such
992
         * method, maybe for a class nothing declares and for one whose hierarchy is not seen whole and has no such method.
993
         */
994
        public function isStaticMethod(string $class, string $method): Tristate
995
        {
996
                return $this->declarations->isStaticMethod($class, $method);
1 ✔
997
        }
998

999

1000
        /**
1001
         * What the declaration of the class says about its deprecation; null for one that is not deprecated or that nothing
1002
         * declares, and for one of a package the project supports in an older version than the installed one. The replacement
1003
         * is the class its description names, `use Acme\Mail\SmtpTransport`, in its declared spelling: a bare name is looked
1004
         * up beside the deprecated class before it is taken as written, a qualified one the other way round, and there is
1005
         * none where neither exists.
1006
         */
1007
        public function findClassDeprecation(string $class): ?Deprecation
1008
        {
1009
                return $this->declarations->findClassDeprecation($class);
1 ✔
1010
        }
1011

1012

1013
        /**
1014
         * What the declaration of the function, fully qualified in any letter case, says about its deprecation, as
1015
         * `findClassDeprecation()` says it of a class; the replacement is the function its description names as a call,
1016
         * `use format()`, fully qualified in its declared spelling.
1017
         */
1018
        public function findFunctionDeprecation(string $function): ?Deprecation
1019
        {
1020
                return $this->declarations->findFunctionDeprecation($function);
1 ✔
1021
        }
1022

1023

1024
        /**
1025
         * What the syntax of a member access says and the types add: the kind, the type of the receiver, the name as
1026
         * written and the scope; null for a node that is no access, whose name is an expression or whose receiver is no object.
1027
         * @return ?array{MemberKind, Type, string, Scope}
1028
         */
1029
        private function findReceiver(ExpressionNode $node): ?array
1030
        {
1031
                $found = $this->findExpression($node);
1 ✔
1032
                if ($found === null) {
1 ✔
1033
                        return null;
1 ✔
1034
                }
1035

1036
                [$parserNode, $scope] = $found;
1 ✔
1037
                if (
1038
                        $parserNode instanceof MethodCallableNode
1 ✔
1039
                        || $parserNode instanceof StaticMethodCallableNode
1 ✔
1040
                        || $parserNode instanceof InstantiationCallableNode
1 ✔
1041
                ) {
1042
                        $parserNode = $parserNode->getOriginalNode(); // a first-class callable comes as a node of PHPStan's own
1 ✔
1043
                }
1044

1045
                [$kind, $type, $name] = match (true) {
1 ✔
1046
                        $node instanceof ClassConstantFetchNode && $node->name instanceof IdentifierNode && $parserNode instanceof Expr\ClassConstFetch
1 ✔
1047
                                => [MemberKind::Constant, self::getClassType($parserNode->class, $scope), $node->name->text],
1 ✔
1048
                        $node instanceof StaticMethodCallNode && $node->name instanceof IdentifierNode && $parserNode instanceof Expr\StaticCall
1 ✔
1049
                                => [MemberKind::StaticMethod, self::getClassType($parserNode->class, $scope), $node->name->text],
1 ✔
1050
                        $node instanceof MethodCallNode
1 ✔
1051
                                && $node->name instanceof IdentifierNode
1 ✔
1052
                                && ($parserNode instanceof Expr\MethodCall || $parserNode instanceof Expr\NullsafeMethodCall)
1 ✔
1053
                                => [MemberKind::Method, $scope->getType($parserNode->var), $node->name->text],
1 ✔
1054
                        $node instanceof StaticPropertyFetchNode && $node->plainName !== null && $parserNode instanceof Expr\StaticPropertyFetch
1 ✔
1055
                                => [MemberKind::StaticProperty, self::getClassType($parserNode->class, $scope), $node->plainName],
1 ✔
1056
                        $node instanceof PropertyFetchNode
1 ✔
1057
                                && $node->name instanceof IdentifierNode
1 ✔
1058
                                && ($parserNode instanceof Expr\PropertyFetch || $parserNode instanceof Expr\NullsafePropertyFetch)
1 ✔
1059
                                => [MemberKind::Property, $scope->getType($parserNode->var), $node->name->text],
1 ✔
1060
                        $node instanceof NewNode
1 ✔
1061
                                && $node->class instanceof NameNode
1 ✔
1062
                                && $parserNode instanceof Expr\New_
1 ✔
1063
                                && $parserNode->class instanceof ParserNode\Name
1 ✔
1064
                                => [MemberKind::Constructor, $scope->resolveTypeByName($parserNode->class), '__construct'],
1 ✔
1065
                        default => [null, null, null],
1 ✔
1066
                };
1067
                // a receiver that may be null is the class it may be: where it is null, the call fails before and after alike
1068
                $type = $type === null ? null : TypeCombinator::removeNull($type);
1 ✔
1069
                return $kind === null || $type === null || $name === null || $type->getObjectClassNames() === []
1 ✔
1070
                        ? null // no class the member could be declared by: mixed, a scalar, an unknown variable
1 ✔
1071
                        : [$kind, $type, $name, $scope];
1 ✔
1072
        }
1073

1074

1075
        private static function getClassType(ParserNode\Name|Expr $class, Scope $scope): Type
1076
        {
1077
                return $class instanceof ParserNode\Name ? $scope->resolveTypeByName($class) : $scope->getType($class);
1 ✔
1078
        }
1079

1080

1081
        /** Whether the class declares the member, a property only where the scope sees it. */
1082
        private function declaresMember(string $class, MemberKind $kind, string $name, Scope $scope): bool
1083
        {
1084
                if ($kind !== MemberKind::Property && $kind !== MemberKind::StaticProperty) {
1 ✔
1085
                        return $this->declarations->hasMember($class, $kind, $name);
1 ✔
1086
                }
1087

1088
                $reflection = $this->phpstan->findClass($class);
1 ✔
1089
                return $reflection !== null
1 ✔
1090
                        && $reflection->hasNativeProperty($name)
1 ✔
1091
                        && $scope->canReadProperty($reflection->getNativeProperty($name));
1 ✔
1092
        }
1093
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc