/
AtomicMethodCallAnalyzer.php
856 lines (748 loc) · 30.8 KB
/
AtomicMethodCallAnalyzer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
<?php
namespace Psalm\Internal\Analyzer\Statements\Expression\Call\Method;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Codebase;
use Psalm\Context;
use Psalm\Internal\Analyzer\ClassLikeAnalyzer;
use Psalm\Internal\Analyzer\ClassLikeNameOptions;
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
use Psalm\Internal\Analyzer\MethodAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Call\ArgumentsAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Call\ClassTemplateParamCollector;
use Psalm\Internal\Analyzer\Statements\Expression\CallAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Analyzer\TraitAnalyzer;
use Psalm\Internal\Codebase\InternalCallMapHandler;
use Psalm\Internal\Codebase\VariableUseGraph;
use Psalm\Internal\MethodIdentifier;
use Psalm\Internal\Type\TemplateResult;
use Psalm\Internal\Type\TypeExpander;
use Psalm\Issue\MixedMethodCall;
use Psalm\IssueBuffer;
use Psalm\StatementsSource;
use Psalm\Storage\ClassLikeStorage;
use Psalm\Type;
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\TEmpty;
use Psalm\Type\Atomic\TEmptyMixed;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TGenericObject;
use Psalm\Type\Atomic\TMixed;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNonEmptyMixed;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TObject;
use Psalm\Type\Atomic\TObjectWithProperties;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Union;
use function array_keys;
use function array_merge;
use function array_search;
use function array_shift;
use function array_values;
use function count;
use function get_class;
use function reset;
use function strtolower;
/**
* This is a bunch of complex logic to handle the potential for missing methods,
* use of intersection types and/or mixins, together with handling for fallback magic
* methods.
*
* The happy path (i.e 99% of method calls) is handled in ExistingAtomicMethodCallAnalyzer
*/
class AtomicMethodCallAnalyzer extends CallAnalyzer
{
/**
* @param TNamedObject|TTemplateParam|null $static_type
*
* @psalm-suppress ComplexMethod it's really complex, but unavoidably so
*/
public static function analyze(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\MethodCall $stmt,
Codebase $codebase,
Context $context,
Union $lhs_type,
Atomic $lhs_type_part,
?Atomic $static_type,
bool $is_intersection,
?string $lhs_var_id,
AtomicMethodCallAnalysisResult $result,
?TemplateResult $inferred_template_result = null
): void {
if ($lhs_type_part instanceof TTemplateParam
&& !$lhs_type_part->as->isMixed()
) {
$extra_types = $lhs_type_part->extra_types;
$lhs_type_part = array_values(
$lhs_type_part->as->getAtomicTypes()
)[0];
$lhs_type_part->from_docblock = true;
if ($lhs_type_part instanceof TNamedObject) {
$lhs_type_part->extra_types = $extra_types;
} elseif ($lhs_type_part instanceof TObject && $extra_types) {
$lhs_type_part = array_shift($extra_types);
if ($extra_types) {
$lhs_type_part->extra_types = $extra_types;
}
}
$result->has_mixed_method_call = true;
}
$source = $statements_analyzer->getSource();
if (!$lhs_type_part instanceof TNamedObject) {
self::handleInvalidClass(
$statements_analyzer,
$codebase,
$stmt,
$lhs_type,
$lhs_type_part,
$lhs_var_id,
$context,
$is_intersection,
$result
);
return;
}
if (!$context->collect_initializations
&& !$context->collect_mutations
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
&& (!(($parent_source = $statements_analyzer->getSource())
instanceof FunctionLikeAnalyzer)
|| !$parent_source->getSource() instanceof TraitAnalyzer)
) {
$codebase->analyzer->incrementNonMixedCount($statements_analyzer->getFilePath());
}
$result->has_valid_method_call_type = true;
$fq_class_name = $lhs_type_part->value;
$is_mock = ExpressionAnalyzer::isMock($fq_class_name);
$result->has_mock = $result->has_mock || $is_mock;
if ($fq_class_name === 'static') {
$fq_class_name = (string) $context->self;
}
if ($is_mock ||
$context->isPhantomClass($fq_class_name)
) {
$result->return_type = Type::getMixed();
ArgumentsAnalyzer::analyze(
$statements_analyzer,
$stmt->getArgs(),
null,
null,
true,
$context
);
return;
}
if ($lhs_var_id === '$this') {
$does_class_exist = true;
} else {
$does_class_exist = ClassLikeAnalyzer::checkFullyQualifiedClassLikeName(
$statements_analyzer,
$fq_class_name,
new CodeLocation($source, $stmt->var),
$context->self,
$context->calling_method_id,
$statements_analyzer->getSuppressedIssues(),
new ClassLikeNameOptions(true, false, true, true, $lhs_type_part->from_docblock)
);
}
if (!$does_class_exist) {
return;
}
$class_storage = $codebase->classlike_storage_provider->get($fq_class_name);
$result->check_visibility = $result->check_visibility && !$class_storage->override_method_visibility;
$intersection_types = $lhs_type_part->getIntersectionTypes();
if (!$stmt->name instanceof PhpParser\Node\Identifier) {
if (!$context->ignore_variable_method) {
$codebase->analyzer->addMixedMemberName(
strtolower($fq_class_name) . '::',
$context->calling_method_id ?: $statements_analyzer->getFileName()
);
}
ArgumentsAnalyzer::analyze(
$statements_analyzer,
$stmt->getArgs(),
null,
null,
true,
$context
);
$result->return_type = Type::getMixed();
return;
}
$method_name_lc = strtolower($stmt->name->name);
$method_id = new MethodIdentifier($fq_class_name, $method_name_lc);
$args = $stmt->isFirstClassCallable() ? [] : $stmt->getArgs();
$naive_method_id = $method_id;
// this tells us whether or not we can stay on the happy path
$naive_method_exists = $codebase->methods->methodExists(
$method_id,
$context->calling_method_id,
$codebase->collect_locations
? new CodeLocation($source, $stmt->name)
: null,
!$context->collect_initializations
&& !$context->collect_mutations
? $statements_analyzer
: null,
$statements_analyzer->getFilePath(),
false,
$context->insideUse()
);
$fake_method_exists = false;
if (!$naive_method_exists) {
// if the method doesn't exist we check for any method existence providers
if ($codebase->methods->existence_provider->has($fq_class_name)) {
$method_exists = $codebase->methods->existence_provider->doesMethodExist(
$fq_class_name,
$method_id->method_name,
$source,
null
);
if ($method_exists) {
$fake_method_exists = true;
}
}
$naive_method_exists = false;
// @mixin attributes are an absolute pain! Lots of complexity here,
// as they can redefine the called class, method id etc.
if ($class_storage->templatedMixins
&& $lhs_type_part instanceof TGenericObject
&& $class_storage->template_types
) {
[$lhs_type_part, $class_storage, $naive_method_exists, $method_id, $fq_class_name]
= self::handleTemplatedMixins(
$class_storage,
$lhs_type_part,
$method_name_lc,
$codebase,
$context,
$method_id,
$source,
$stmt,
$statements_analyzer,
$fq_class_name
);
} elseif ($class_storage->mixin_declaring_fqcln
&& $class_storage->namedMixins
) {
[$lhs_type_part, $class_storage, $naive_method_exists, $method_id, $fq_class_name]
= self::handleRegularMixins(
$class_storage,
$lhs_type_part,
$method_name_lc,
$codebase,
$context,
$method_id,
$source,
$stmt,
$statements_analyzer,
$fq_class_name,
$lhs_var_id
);
}
}
$all_intersection_return_type = null;
$all_intersection_existent_method_ids = [];
// insersection types are also fun, they also complicate matters
if ($intersection_types) {
[$all_intersection_return_type, $all_intersection_existent_method_ids]
= self::getIntersectionReturnType(
$statements_analyzer,
$stmt,
$codebase,
$context,
$lhs_type,
$lhs_type_part,
$lhs_var_id,
$result,
$intersection_types
);
}
if (($fake_method_exists
&& $codebase->methods->methodExists(new MethodIdentifier($fq_class_name, '__call')))
|| !$naive_method_exists
|| !MethodAnalyzer::isMethodVisible(
$method_id,
$context,
$statements_analyzer->getSource()
)
) {
$interface_has_method = false;
if ($class_storage->abstract && $class_storage->class_implements) {
foreach ($class_storage->class_implements as $interface_fqcln_lc => $_) {
$interface_storage = $codebase->classlike_storage_provider->get($interface_fqcln_lc);
if (isset($interface_storage->methods[$method_name_lc])) {
$interface_has_method = true;
$fq_class_name = $interface_storage->name;
$method_id = new MethodIdentifier(
$fq_class_name,
$method_name_lc
);
break;
}
}
}
if (!$interface_has_method
&& $codebase->methods->methodExists(
new MethodIdentifier($fq_class_name, '__call'),
$context->calling_method_id,
$codebase->collect_locations
? new CodeLocation($source, $stmt->name)
: null,
!$context->collect_initializations
&& !$context->collect_mutations
? $statements_analyzer
: null,
$statements_analyzer->getFilePath(),
true,
$context->insideUse()
)
) {
$new_call_context = MissingMethodCallHandler::handleMagicMethod(
$statements_analyzer,
$codebase,
$stmt,
$method_id,
$class_storage,
$context,
$codebase->config,
$all_intersection_return_type,
$result,
$lhs_type_part
);
if ($new_call_context) {
if ($method_id === $new_call_context->method_id) {
return;
}
$method_id = $new_call_context->method_id;
$args = $new_call_context->args;
} else {
return;
}
}
}
$intersection_method_id = $intersection_types
? '(' . $lhs_type_part . ')' . '::' . $stmt->name->name
: null;
$cased_method_id = $fq_class_name . '::' . $stmt->name->name;
if ($lhs_var_id === '$this'
&& $context->self
&& $fq_class_name !== $context->self
&& $codebase->methods->methodExists(
new MethodIdentifier($context->self, $method_name_lc)
)
) {
$method_id = new MethodIdentifier($context->self, $method_name_lc);
$cased_method_id = $context->self . '::' . $stmt->name->name;
$fq_class_name = $context->self;
}
$source_method_id = $source instanceof FunctionLikeAnalyzer
? $source->getId()
: null;
$corrected_method_exists = ($naive_method_exists && $method_id === $naive_method_id)
|| ($method_id !== $naive_method_id
&& $codebase->methods->methodExists(
$method_id,
$context->calling_method_id,
$codebase->collect_locations && $method_id !== $source_method_id
? new CodeLocation($source, $stmt->name)
: null
));
if (!$corrected_method_exists
|| ($codebase->config->use_phpdoc_method_without_magic_or_parent
&& isset($class_storage->pseudo_methods[$method_name_lc]))
) {
MissingMethodCallHandler::handleMissingOrMagicMethod(
$statements_analyzer,
$codebase,
$stmt,
$method_id,
$codebase->interfaceExists($fq_class_name),
$context,
$codebase->config,
$all_intersection_return_type,
$all_intersection_existent_method_ids,
$intersection_method_id,
$cased_method_id,
$result,
$lhs_type_part
);
return;
}
$old_node_data = $statements_analyzer->node_data;
$return_type_candidate = ExistingAtomicMethodCallAnalyzer::analyze(
$statements_analyzer,
$stmt,
$stmt->name,
$args,
$codebase,
$context,
$lhs_type_part,
$static_type,
$lhs_var_id,
$method_id,
$result,
$inferred_template_result
);
$statements_analyzer->node_data = $old_node_data;
$declaring_method_id = $codebase->methods->getDeclaringMethodId($method_id);
$in_call_map = InternalCallMapHandler::inCallMap((string) ($declaring_method_id ?? $method_id));
if (!$in_call_map) {
if ($result->check_visibility) {
$name_code_location = new CodeLocation($statements_analyzer, $stmt->name);
MethodVisibilityAnalyzer::analyze(
$method_id,
$context,
$statements_analyzer->getSource(),
$name_code_location,
$statements_analyzer->getSuppressedIssues()
);
}
}
self::updateResultReturnType(
$result,
$return_type_candidate,
$all_intersection_return_type,
$codebase
);
}
/**
* @param TNamedObject|TTemplateParam $lhs_type_part
* @param array<string, Atomic> $intersection_types
*
* @return array{?Union, array<string>}
*/
private static function getIntersectionReturnType(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\MethodCall $stmt,
Codebase $codebase,
Context $context,
Union $lhs_type,
Atomic $lhs_type_part,
?string $lhs_var_id,
AtomicMethodCallAnalysisResult $result,
array $intersection_types
): array {
$all_intersection_return_type = null;
$all_intersection_existent_method_ids = [];
foreach ($intersection_types as $intersection_type) {
$intersection_result = clone $result;
/** @var ?Union $intersection_result->return_type */
$intersection_result->return_type = null;
self::analyze(
$statements_analyzer,
$stmt,
$codebase,
$context,
$lhs_type,
$intersection_type,
$lhs_type_part,
true,
$lhs_var_id,
$intersection_result
);
$result->returns_by_ref = $intersection_result->returns_by_ref;
$result->has_mock = $intersection_result->has_mock;
$result->has_valid_method_call_type = $intersection_result->has_valid_method_call_type;
$result->has_mixed_method_call = $intersection_result->has_mixed_method_call;
$result->invalid_method_call_types = $intersection_result->invalid_method_call_types;
$result->check_visibility = $intersection_result->check_visibility;
$result->too_many_arguments = $intersection_result->too_many_arguments;
$all_intersection_existent_method_ids = array_merge(
$all_intersection_existent_method_ids,
$intersection_result->existent_method_ids
);
if ($intersection_result->return_type) {
if (!$all_intersection_return_type || $all_intersection_return_type->isMixed()) {
$all_intersection_return_type = $intersection_result->return_type;
} else {
$all_intersection_return_type = Type::intersectUnionTypes(
$all_intersection_return_type,
$intersection_result->return_type,
$codebase
) ?? Type::getMixed();
}
}
}
return [$all_intersection_return_type, $all_intersection_existent_method_ids];
}
private static function updateResultReturnType(
AtomicMethodCallAnalysisResult $result,
Union $return_type_candidate,
?Union $all_intersection_return_type,
Codebase $codebase
): void {
if ($all_intersection_return_type) {
$return_type_candidate = Type::intersectUnionTypes(
$all_intersection_return_type,
$return_type_candidate,
$codebase
) ?? Type::getMixed();
}
$result->return_type = Type::combineUnionTypes($return_type_candidate, $result->return_type);
}
private static function handleInvalidClass(
StatementsAnalyzer $statements_analyzer,
Codebase $codebase,
PhpParser\Node\Expr\MethodCall $stmt,
Union $lhs_type,
Atomic $lhs_type_part,
?string $lhs_var_id,
Context $context,
bool $is_intersection,
AtomicMethodCallAnalysisResult $result
): void {
switch (get_class($lhs_type_part)) {
case TNull::class:
case TFalse::class:
// handled above
return;
case TTemplateParam::class:
case TEmptyMixed::class:
case TEmpty::class:
case TMixed::class:
case TNonEmptyMixed::class:
case TObject::class:
case TObjectWithProperties::class:
if (!$context->collect_initializations
&& !$context->collect_mutations
&& $statements_analyzer->getFilePath() === $statements_analyzer->getRootFilePath()
&& (!(($parent_source = $statements_analyzer->getSource())
instanceof FunctionLikeAnalyzer)
|| !$parent_source->getSource() instanceof TraitAnalyzer)
) {
$codebase->analyzer->incrementMixedCount($statements_analyzer->getFilePath());
}
$result->has_mixed_method_call = true;
if ($lhs_type_part instanceof TObjectWithProperties
&& $stmt->name instanceof PhpParser\Node\Identifier
&& isset($lhs_type_part->methods[$stmt->name->name])
) {
$result->existent_method_ids[] = $lhs_type_part->methods[$stmt->name->name];
} elseif (!$is_intersection) {
if ($stmt->name instanceof PhpParser\Node\Identifier) {
$codebase->analyzer->addMixedMemberName(
strtolower($stmt->name->name),
$context->calling_method_id ?: $statements_analyzer->getFileName()
);
}
if ($context->check_methods) {
$message = 'Cannot determine the type of the object'
. ' on the left hand side of this expression';
if ($lhs_var_id) {
$message = 'Cannot determine the type of ' . $lhs_var_id;
if ($stmt->name instanceof PhpParser\Node\Identifier) {
$message .= ' when calling method ' . $stmt->name->name;
}
}
$origin_locations = [];
if ($statements_analyzer->data_flow_graph instanceof VariableUseGraph) {
foreach ($lhs_type->parent_nodes as $parent_node) {
$origin_locations = array_merge(
$origin_locations,
$statements_analyzer->data_flow_graph->getOriginLocations($parent_node)
);
}
}
$origin_location = count($origin_locations) === 1 ? reset($origin_locations) : null;
$name_code_location = new CodeLocation($statements_analyzer, $stmt->name);
if ($origin_location && $origin_location->getHash() === $name_code_location->getHash()) {
$origin_location = null;
}
IssueBuffer::maybeAdd(
new MixedMethodCall(
$message,
$name_code_location,
$origin_location
),
$statements_analyzer->getSuppressedIssues()
);
}
}
if (ArgumentsAnalyzer::analyze(
$statements_analyzer,
$stmt->getArgs(),
null,
null,
true,
$context
) === false) {
return;
}
$result->return_type = Type::getMixed();
return;
default:
$result->invalid_method_call_types[] = (string)$lhs_type_part;
return;
}
}
/**
* @param lowercase-string $method_name_lc
* @return array{TNamedObject, ClassLikeStorage, bool, MethodIdentifier, string}
*/
private static function handleTemplatedMixins(
ClassLikeStorage $class_storage,
TNamedObject $lhs_type_part,
string $method_name_lc,
Codebase $codebase,
Context $context,
MethodIdentifier $method_id,
StatementsSource $source,
PhpParser\Node\Expr\MethodCall $stmt,
StatementsAnalyzer $statements_analyzer,
string $fq_class_name
): array {
$naive_method_exists = false;
if ($class_storage->templatedMixins
&& $lhs_type_part instanceof TGenericObject
&& $class_storage->template_types
) {
$template_type_keys = array_keys($class_storage->template_types);
foreach ($class_storage->templatedMixins as $mixin) {
$param_position = array_search(
$mixin->param_name,
$template_type_keys
);
if ($param_position !== false
&& isset($lhs_type_part->type_params[$param_position])
) {
$current_type_param = $lhs_type_part->type_params[$param_position];
if ($current_type_param->isSingle()) {
$lhs_type_part_new = array_values(
$current_type_param->getAtomicTypes()
)[0];
if ($lhs_type_part_new instanceof TNamedObject) {
$new_method_id = new MethodIdentifier(
$lhs_type_part_new->value,
$method_name_lc
);
$mixin_class_storage = $codebase->classlike_storage_provider->get(
$lhs_type_part_new->value
);
if ($codebase->methods->methodExists(
$new_method_id,
$context->calling_method_id,
$codebase->collect_locations
? new CodeLocation($source, $stmt->name)
: null,
!$context->collect_initializations
&& !$context->collect_mutations
? $statements_analyzer
: null,
$statements_analyzer->getFilePath(),
true,
$context->insideUse()
)) {
$lhs_type_part = clone $lhs_type_part_new;
$class_storage = $mixin_class_storage;
$naive_method_exists = true;
$method_id = $new_method_id;
} elseif (isset($mixin_class_storage->pseudo_methods[$method_name_lc])) {
$lhs_type_part = clone $lhs_type_part_new;
$class_storage = $mixin_class_storage;
$method_id = $new_method_id;
}
}
}
}
}
}
return [
$lhs_type_part,
$class_storage,
$naive_method_exists,
$method_id,
$fq_class_name
];
}
/**
* @param lowercase-string $method_name_lc
* @return array{TNamedObject, ClassLikeStorage, bool, MethodIdentifier, string}
*/
private static function handleRegularMixins(
ClassLikeStorage $class_storage,
TNamedObject $lhs_type_part,
string $method_name_lc,
Codebase $codebase,
Context $context,
MethodIdentifier $method_id,
StatementsSource $source,
PhpParser\Node\Expr\MethodCall $stmt,
StatementsAnalyzer $statements_analyzer,
string $fq_class_name,
?string $lhs_var_id
): array {
$naive_method_exists = false;
foreach ($class_storage->namedMixins as $mixin) {
if (!$class_storage->mixin_declaring_fqcln) {
continue;
}
$new_method_id = new MethodIdentifier(
$mixin->value,
$method_name_lc
);
if ($codebase->methods->methodExists(
$new_method_id,
$context->calling_method_id,
$codebase->collect_locations
? new CodeLocation($source, $stmt->name)
: null,
!$context->collect_initializations
&& !$context->collect_mutations
? $statements_analyzer
: null,
$statements_analyzer->getFilePath(),
true,
$context->insideUse()
)) {
$mixin_declaring_class_storage = $codebase->classlike_storage_provider->get(
$class_storage->mixin_declaring_fqcln
);
$mixin_class_template_params = ClassTemplateParamCollector::collect(
$codebase,
$mixin_declaring_class_storage,
$codebase->classlike_storage_provider->get($fq_class_name),
null,
$lhs_type_part,
$lhs_var_id === '$this'
);
$lhs_type_part = clone $mixin;
$lhs_type_part->replaceTemplateTypesWithArgTypes(
new TemplateResult([], $mixin_class_template_params ?: []),
$codebase
);
$lhs_type_expanded = TypeExpander::expandUnion(
$codebase,
new Union([$lhs_type_part]),
$mixin_declaring_class_storage->name,
$fq_class_name,
$class_storage->parent_class,
true,
false,
$class_storage->final
);
$new_lhs_type_part = $lhs_type_expanded->getSingleAtomic();
if ($new_lhs_type_part instanceof TNamedObject) {
$lhs_type_part = $new_lhs_type_part;
}
$mixin_class_storage = $codebase->classlike_storage_provider->get($mixin->value);
$fq_class_name = $mixin_class_storage->name;
$mixin_class_storage->mixin_declaring_fqcln = $class_storage->mixin_declaring_fqcln;
$class_storage = $mixin_class_storage;
$naive_method_exists = true;
$method_id = $new_method_id;
}
}
return [
$lhs_type_part,
$class_storage,
$naive_method_exists,
$method_id,
$fq_class_name
];
}
}