-
Notifications
You must be signed in to change notification settings - Fork 672
/
Copy pathArithmeticOpAnalyzer.php
1416 lines (1270 loc) · 57.7 KB
/
ArithmeticOpAnalyzer.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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Psalm\Internal\Analyzer\Statements\Expression\BinaryOp;
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Codebase;
use Psalm\Config;
use Psalm\Context;
use Psalm\Internal\Analyzer\FunctionLikeAnalyzer;
use Psalm\Internal\Analyzer\Statements\Expression\Assignment\ArrayAssignmentAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Analyzer\TraitAnalyzer;
use Psalm\Internal\Provider\NodeDataProvider;
use Psalm\Internal\Type\TypeCombiner;
use Psalm\Issue\FalseOperand;
use Psalm\Issue\InvalidOperand;
use Psalm\Issue\MixedOperand;
use Psalm\Issue\NullOperand;
use Psalm\Issue\PossiblyFalseOperand;
use Psalm\Issue\PossiblyInvalidOperand;
use Psalm\Issue\PossiblyNullOperand;
use Psalm\Issue\StringIncrement;
use Psalm\IssueBuffer;
use Psalm\StatementsSource;
use Psalm\Type;
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TIntRange;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TList;
use Psalm\Type\Atomic\TLiteralFloat;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TMixed;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TNumeric;
use Psalm\Type\Atomic\TNumericString;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Atomic\TTemplateParam;
use Psalm\Type\Union;
use function array_diff_key;
use function array_values;
use function count;
use function get_class;
use function is_int;
use function is_numeric;
use function max;
use function min;
use function preg_match;
use function strtolower;
/**
* @internal
*/
final class ArithmeticOpAnalyzer
{
public static function analyze(
?StatementsSource $statements_source,
NodeDataProvider $nodes,
PhpParser\Node\Expr $left,
PhpParser\Node\Expr $right,
PhpParser\Node $parent,
?Union &$result_type = null,
?Context $context = null
): void {
$codebase = $statements_source ? $statements_source->getCodebase() : null;
$left_type = $nodes->getType($left);
$right_type = $nodes->getType($right);
$config = Config::getInstance();
if ($left_type && $left_type->isNever()) {
$left_type = $right_type;
} elseif ($right_type && $right_type->isNever()) {
$right_type = $left_type;
}
if ($left_type && $right_type) {
if ($left_type->isNull()) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new NullOperand(
'Left operand cannot be null',
new CodeLocation($statements_source, $left),
),
$statements_source->getSuppressedIssues(),
);
}
$result_type = Type::getMixed();
return;
}
if ($left_type->isNullable() && !$left_type->ignore_nullable_issues) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new PossiblyNullOperand(
'Left operand cannot be nullable, got ' . $left_type,
new CodeLocation($statements_source, $left),
),
$statements_source->getSuppressedIssues(),
);
}
}
if ($right_type->isNull()) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new NullOperand(
'Right operand cannot be null',
new CodeLocation($statements_source, $right),
),
$statements_source->getSuppressedIssues(),
);
}
$result_type = Type::getMixed();
return;
}
if ($right_type->isNullable() && !$right_type->ignore_nullable_issues) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new PossiblyNullOperand(
'Right operand cannot be nullable, got ' . $right_type,
new CodeLocation($statements_source, $right),
),
$statements_source->getSuppressedIssues(),
);
}
}
if ($left_type->isFalse()) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new FalseOperand(
'Left operand cannot be false',
new CodeLocation($statements_source, $left),
),
$statements_source->getSuppressedIssues(),
);
}
return;
}
if ($left_type->isFalsable() && !$left_type->ignore_falsable_issues) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new PossiblyFalseOperand(
'Left operand cannot be falsable, got ' . $left_type,
new CodeLocation($statements_source, $left),
),
$statements_source->getSuppressedIssues(),
);
}
}
if ($right_type->isFalse()) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new FalseOperand(
'Right operand cannot be false',
new CodeLocation($statements_source, $right),
),
$statements_source->getSuppressedIssues(),
);
}
return;
}
if ($right_type->isFalsable() && !$right_type->ignore_falsable_issues) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new PossiblyFalseOperand(
'Right operand cannot be falsable, got ' . $right_type,
new CodeLocation($statements_source, $right),
),
$statements_source->getSuppressedIssues(),
);
}
}
$invalid_left_messages = [];
$invalid_right_messages = [];
$has_valid_left_operand = false;
$has_valid_right_operand = false;
$has_string_increment = false;
foreach ($left_type->getAtomicTypes() as $left_type_part) {
foreach ($right_type->getAtomicTypes() as $right_type_part) {
$candidate_result_type = self::analyzeOperands(
$statements_source,
$codebase,
$config,
$context,
$left,
$right,
$parent,
$left_type_part,
$right_type_part,
$invalid_left_messages,
$invalid_right_messages,
$has_valid_left_operand,
$has_valid_right_operand,
$has_string_increment,
$result_type,
);
if ($candidate_result_type) {
$result_type = $candidate_result_type;
return;
}
}
}
if ($invalid_left_messages && $statements_source) {
$first_left_message = $invalid_left_messages[0];
if ($has_valid_left_operand) {
IssueBuffer::maybeAdd(
new PossiblyInvalidOperand(
$first_left_message,
new CodeLocation($statements_source, $left),
),
$statements_source->getSuppressedIssues(),
);
} else {
IssueBuffer::maybeAdd(
new InvalidOperand(
$first_left_message,
new CodeLocation($statements_source, $left),
),
$statements_source->getSuppressedIssues(),
);
}
}
if ($invalid_right_messages && $statements_source) {
$first_right_message = $invalid_right_messages[0];
if ($has_valid_right_operand) {
IssueBuffer::maybeAdd(
new PossiblyInvalidOperand(
$first_right_message,
new CodeLocation($statements_source, $right),
),
$statements_source->getSuppressedIssues(),
);
} else {
IssueBuffer::maybeAdd(
new InvalidOperand(
$first_right_message,
new CodeLocation($statements_source, $right),
),
$statements_source->getSuppressedIssues(),
);
}
}
if ($has_string_increment && $statements_source) {
IssueBuffer::maybeAdd(
new StringIncrement(
'Possibly unintended string increment',
new CodeLocation($statements_source, $left),
),
$statements_source->getSuppressedIssues(),
);
}
}
}
/**
* @param int|float $result
*/
private static function getNumericalType($result): Union
{
if (is_int($result)) {
return Type::getInt(false, $result);
}
return Type::getFloat($result);
}
/**
* @param string[] $invalid_left_messages
* @param string[] $invalid_right_messages
* @psalm-suppress ComplexMethod Unavoidably complex method.
*/
private static function analyzeOperands(
?StatementsSource $statements_source,
?Codebase $codebase,
Config $config,
?Context $context,
PhpParser\Node\Expr $left,
PhpParser\Node\Expr $right,
PhpParser\Node $parent,
Atomic $left_type_part,
Atomic $right_type_part,
array &$invalid_left_messages,
array &$invalid_right_messages,
bool &$has_valid_left_operand,
bool &$has_valid_right_operand,
bool &$has_string_increment,
?Union &$result_type = null
): ?Union {
if (($left_type_part instanceof TLiteralInt || $left_type_part instanceof TLiteralFloat)
&& ($right_type_part instanceof TLiteralInt || $right_type_part instanceof TLiteralFloat)
&& (
//we don't try to do arithmetics on variables in loops
$context === null
|| $context->inside_loop === false
|| (!$left instanceof PhpParser\Node\Expr\Variable && !$right instanceof PhpParser\Node\Expr\Variable)
)
) {
// get_class is fine here because both classes are final.
if ($statements_source !== null
&& $config->strict_binary_operands
&& get_class($left_type_part) !== get_class($right_type_part)
) {
IssueBuffer::maybeAdd(
new InvalidOperand(
'Cannot process numeric types together in strict operands mode, '.
'please cast explicitly',
new CodeLocation($statements_source, $parent),
),
$statements_source->getSuppressedIssues(),
);
}
// time for some arithmetic!
$calculated_type = self::arithmeticOperation(
$parent,
$left_type_part->value,
$right_type_part->value,
true,
);
if ($calculated_type) {
$result_type = Type::combineUnionTypes(
$calculated_type,
$result_type,
);
$has_valid_left_operand = true;
$has_valid_right_operand = true;
return null;
}
}
if ($left_type_part instanceof TNull || $right_type_part instanceof TNull) {
// null case is handled above
return null;
}
if ($left_type_part instanceof TFalse || $right_type_part instanceof TFalse) {
// null case is handled above
return null;
}
if ($left_type_part instanceof TString
&& $right_type_part instanceof TInt
&& (
$parent instanceof PhpParser\Node\Expr\PostInc ||
$parent instanceof PhpParser\Node\Expr\PreInc
)
) {
if ($left_type_part instanceof TNumericString ||
($left_type_part instanceof TLiteralString && is_numeric($left_type_part->value))
) {
$new_result_type = new Union(
[new TFloat(), new TInt()],
['from_calculation' => true],
);
} else {
$new_result_type = Type::getNonEmptyString();
$has_string_increment = true;
}
$result_type = Type::combineUnionTypes($new_result_type, $result_type);
$has_valid_left_operand = true;
$has_valid_right_operand = true;
return null;
}
if ($left_type_part instanceof TTemplateParam
&& $right_type_part instanceof TTemplateParam
) {
$combined_type = Type::combineUnionTypes(
$left_type_part->as,
$right_type_part->as,
);
$combined_atomic_types = array_values($combined_type->getAtomicTypes());
if (count($combined_atomic_types) <= 2) {
$left_type_part = $combined_atomic_types[0];
$right_type_part = $combined_atomic_types[1] ?? $combined_atomic_types[0];
}
}
if ($left_type_part instanceof TMixed || $right_type_part instanceof TMixed) {
if ($statements_source && $codebase && $context) {
if (!$context->collect_initializations
&& !$context->collect_mutations
&& $statements_source->getFilePath() === $statements_source->getRootFilePath()
&& (!(($source = $statements_source->getSource())
instanceof FunctionLikeAnalyzer)
|| !$source->getSource() instanceof TraitAnalyzer)
) {
$codebase->analyzer->incrementMixedCount($statements_source->getFilePath());
}
}
if ($left_type_part instanceof TMixed) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new MixedOperand(
'Left operand cannot be mixed',
new CodeLocation($statements_source, $left),
),
$statements_source->getSuppressedIssues(),
);
}
} else {
if ($statements_source) {
IssueBuffer::maybeAdd(
new MixedOperand(
'Right operand cannot be mixed',
new CodeLocation($statements_source, $right),
),
$statements_source->getSuppressedIssues(),
);
}
}
if ($left_type_part instanceof TMixed
&& $left_type_part->from_loop_isset
&& $parent instanceof PhpParser\Node\Expr\AssignOp\Plus
&& !$right_type_part instanceof TMixed
) {
$result_type = Type::combineUnionTypes(new Union([$right_type_part]), $result_type);
return null;
}
$from_loop_isset = (!($left_type_part instanceof TMixed) || $left_type_part->from_loop_isset)
&& (!($right_type_part instanceof TMixed) || $right_type_part->from_loop_isset);
$result_type = Type::getMixed($from_loop_isset);
return $result_type;
}
if ($left_type_part instanceof TTemplateParam || $right_type_part instanceof TTemplateParam) {
if ($left_type_part instanceof TTemplateParam
&& !$left_type_part->as->isInt()
&& !$left_type_part->as->isFloat()
) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new MixedOperand(
'Left operand cannot be a non-numeric template',
new CodeLocation($statements_source, $left),
),
$statements_source->getSuppressedIssues(),
);
}
} elseif ($right_type_part instanceof TTemplateParam
&& !$right_type_part->as->isInt()
&& !$right_type_part->as->isFloat()
) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new MixedOperand(
'Right operand cannot be a non-numeric template',
new CodeLocation($statements_source, $right),
),
$statements_source->getSuppressedIssues(),
);
}
}
return null;
}
if ($statements_source && $codebase && $context) {
if (!$context->collect_initializations
&& !$context->collect_mutations
&& $statements_source->getFilePath() === $statements_source->getRootFilePath()
&& (!(($parent_source = $statements_source->getSource())
instanceof FunctionLikeAnalyzer)
|| !$parent_source->getSource() instanceof TraitAnalyzer)
) {
$codebase->analyzer->incrementNonMixedCount($statements_source->getFilePath());
}
}
if ($left_type_part instanceof TArray
|| $right_type_part instanceof TArray
|| $left_type_part instanceof TKeyedArray
|| $right_type_part instanceof TKeyedArray
|| $left_type_part instanceof TList
|| $right_type_part instanceof TList
) {
if ($left_type_part instanceof TList) {
$left_type_part = $left_type_part->getKeyedArray();
}
if ($right_type_part instanceof TList) {
$right_type_part = $right_type_part->getKeyedArray();
}
if ((!$right_type_part instanceof TArray
&& !$right_type_part instanceof TKeyedArray)
|| (!$left_type_part instanceof TArray
&& !$left_type_part instanceof TKeyedArray)
) {
if (!$left_type_part instanceof TArray
&& !$left_type_part instanceof TKeyedArray
) {
$invalid_left_messages[] = 'Cannot add an array to a non-array ' . $left_type_part;
} else {
$invalid_right_messages[] = 'Cannot add an array to a non-array ' . $right_type_part;
}
if ($left_type_part instanceof TArray
|| $left_type_part instanceof TKeyedArray
) {
$has_valid_left_operand = true;
} elseif ($right_type_part instanceof TArray
|| $right_type_part instanceof TKeyedArray
) {
$has_valid_right_operand = true;
}
return null;
}
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Plus) {
$has_valid_right_operand = true;
$has_valid_left_operand = true;
if ($left_type_part instanceof TKeyedArray
&& $right_type_part instanceof TKeyedArray
) {
$definitely_existing_mixed_right_properties = array_diff_key(
$right_type_part->properties,
$left_type_part->properties,
);
$properties = $left_type_part->properties;
foreach ($right_type_part->properties as $key => $type) {
if (!isset($properties[$key])) {
$properties[$key] = $type;
} elseif ($properties[$key]->possibly_undefined) {
$properties[$key] = Type::combineUnionTypes(
$properties[$key],
$type,
$codebase,
false,
true,
500,
$type->possibly_undefined,
);
}
}
if ($left_type_part->fallback_params !== null) {
foreach ($definitely_existing_mixed_right_properties as $key => $type) {
$properties[$key] = Type::combineUnionTypes(Type::getMixed(), $type);
}
}
if ($left_type_part->fallback_params === null
&& $right_type_part->fallback_params === null
) {
$fallback_params = null;
} elseif ($left_type_part->fallback_params !== null
&& $right_type_part->fallback_params !== null
) {
$fallback_params = [
Type::combineUnionTypes(
$left_type_part->fallback_params[0],
$right_type_part->fallback_params[0],
),
Type::combineUnionTypes(
$left_type_part->fallback_params[1],
$right_type_part->fallback_params[1],
),
];
} else {
$fallback_params = $left_type_part->fallback_params ?: $right_type_part->fallback_params;
}
$new_keyed_array = new TKeyedArray(
$properties,
null,
$fallback_params,
);
$result_type_member = new Union([$new_keyed_array]);
} else {
$result_type_member = TypeCombiner::combine(
[$left_type_part, $right_type_part],
$codebase,
true,
);
}
$result_type = Type::combineUnionTypes($result_type_member, $result_type, $codebase, true);
if ($left instanceof PhpParser\Node\Expr\ArrayDimFetch
&& $context
&& $statements_source instanceof StatementsAnalyzer
) {
ArrayAssignmentAnalyzer::updateArrayType(
$statements_source,
$left,
$right,
$result_type,
$context,
);
}
return null;
}
}
if (($left_type_part instanceof TNamedObject && strtolower($left_type_part->value) === 'gmp')
|| ($right_type_part instanceof TNamedObject && strtolower($right_type_part->value) === 'gmp')
) {
if ((($left_type_part instanceof TNamedObject
&& strtolower($left_type_part->value) === 'gmp')
&& (($right_type_part instanceof TNamedObject
&& strtolower($right_type_part->value) === 'gmp')
|| ($right_type_part->isNumericType() || $right_type_part instanceof TMixed)))
|| (($right_type_part instanceof TNamedObject
&& strtolower($right_type_part->value) === 'gmp')
&& (($left_type_part instanceof TNamedObject
&& strtolower($left_type_part->value) === 'gmp')
|| ($left_type_part->isNumericType() || $left_type_part instanceof TMixed)))
) {
$result_type = Type::combineUnionTypes(
new Union([new TNamedObject('GMP')]),
$result_type,
);
} else {
if ($statements_source) {
IssueBuffer::maybeAdd(
new InvalidOperand(
'Cannot add GMP to non-numeric type',
new CodeLocation($statements_source, $parent),
),
$statements_source->getSuppressedIssues(),
);
}
}
return null;
}
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Plus
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\Minus
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\Mul
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\Div
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\Mod
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\Pow
) {
$non_decimal_type = null;
if ($left_type_part instanceof TNamedObject
&& strtolower($left_type_part->value) === "decimal\\decimal"
) {
$non_decimal_type = $right_type_part;
} elseif ($right_type_part instanceof TNamedObject
&& strtolower($right_type_part->value) === "decimal\\decimal"
) {
$non_decimal_type = $left_type_part;
}
if ($non_decimal_type !== null) {
if ($non_decimal_type instanceof TInt
|| $non_decimal_type instanceof TNumericString
|| $non_decimal_type instanceof TNamedObject
&& strtolower($non_decimal_type->value) === "decimal\\decimal"
) {
$result_type = Type::combineUnionTypes(
new Union([new TNamedObject("Decimal\\Decimal")]),
$result_type,
);
} else {
if ($statements_source) {
IssueBuffer::maybeAdd(
new InvalidOperand(
"Cannot add Decimal\\Decimal to {$non_decimal_type->getId()}",
new CodeLocation($statements_source, $parent),
),
$statements_source->getSuppressedIssues(),
);
}
}
return null;
}
}
if ($left_type_part instanceof TLiteralString) {
if (preg_match('/^\-?\d+$/', $left_type_part->value)) {
$left_type_part = new TLiteralInt((int) $left_type_part->value);
} elseif (preg_match('/^\-?\d?\.\d+$/', $left_type_part->value)) {
$left_type_part = new TLiteralFloat((float) $left_type_part->value);
}
}
if ($right_type_part instanceof TLiteralString) {
if (preg_match('/^\-?\d+$/', $right_type_part->value)) {
$right_type_part = new TLiteralInt((int) $right_type_part->value);
} elseif (preg_match('/^\-?\d?\.\d+$/', $right_type_part->value)) {
$right_type_part = new TLiteralFloat((float) $right_type_part->value);
}
}
if ($left_type_part->isNumericType() || $right_type_part->isNumericType()) {
if (($left_type_part instanceof TNumeric || $right_type_part instanceof TNumeric)
&& ($left_type_part->isNumericType() && $right_type_part->isNumericType())
) {
if ($config->strict_binary_operands) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new InvalidOperand(
'Cannot process different numeric types together in strict binary operands mode, '.
'please cast explicitly',
new CodeLocation($statements_source, $parent),
),
$statements_source->getSuppressedIssues(),
);
}
}
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$new_result_type = Type::getInt();
} else {
$new_result_type = new Union([new TFloat(), new TInt()]);
}
$result_type = Type::combineUnionTypes($new_result_type, $result_type);
$has_valid_right_operand = true;
$has_valid_left_operand = true;
return null;
}
if ($left_type_part instanceof TIntRange && $right_type_part instanceof TIntRange) {
self::analyzeOperandsBetweenIntRange($parent, $result_type, $left_type_part, $right_type_part);
return null;
}
if (($left_type_part instanceof TIntRange && $right_type_part instanceof TInt) ||
($left_type_part instanceof TInt && $right_type_part instanceof TIntRange)
) {
self::analyzeOperandsBetweenIntRangeAndInt(
$parent,
$result_type,
$left_type_part,
$right_type_part,
);
return null;
}
if ($left_type_part instanceof TInt && $right_type_part instanceof TInt) {
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Div) {
$result_type = new Union([new TInt(), new TFloat()]);
} else {
$left_is_positive = ($left_type_part instanceof TLiteralInt && $left_type_part->value > 0)
|| ($left_type_part instanceof TIntRange && $left_type_part->isPositive());
$right_is_positive = ($right_type_part instanceof TLiteralInt && $right_type_part->value > 0)
|| ($right_type_part instanceof TIntRange && $right_type_part->isPositive());
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Minus) {
$always_positive = false;
} elseif ($left_is_positive && $right_is_positive) {
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\ShiftLeft
|| $parent instanceof PhpParser\Node\Expr\BinaryOp\ShiftRight
) {
$always_positive = false;
} else {
$always_positive = true;
}
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Plus
&& ($left_type_part instanceof TLiteralInt && $left_type_part->value === 0)
&& $right_is_positive
) {
$always_positive = true;
} elseif ($parent instanceof PhpParser\Node\Expr\BinaryOp\Plus
&& ($right_type_part instanceof TLiteralInt && $right_type_part->value === 0)
&& $left_is_positive
) {
$always_positive = true;
} else {
$always_positive = false;
}
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
if ($right_type_part instanceof TLiteralInt) {
$literal_value_max = $right_type_part->value - 1;
if ($always_positive) {
$result_type = Type::getIntRange(0, $literal_value_max);
} else {
$result_type = Type::getIntRange(-$literal_value_max, $literal_value_max);
}
} else {
if ($always_positive) {
$result_type = Type::getListKey();
} else {
$result_type = Type::getInt();
}
}
} else {
$result_type = Type::combineUnionTypes(
$always_positive ? Type::getIntRange(1, null) : Type::getInt(true),
$result_type,
);
}
}
$has_valid_right_operand = true;
$has_valid_left_operand = true;
return null;
}
if ($left_type_part instanceof TFloat && $right_type_part instanceof TFloat) {
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$result_type = Type::getInt();
} else {
$result_type = Type::combineUnionTypes(Type::getFloat(), $result_type);
}
$has_valid_right_operand = true;
$has_valid_left_operand = true;
return null;
}
if (($left_type_part instanceof TFloat && $right_type_part instanceof TInt)
|| ($left_type_part instanceof TInt && $right_type_part instanceof TFloat)
) {
if ($config->strict_binary_operands) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new InvalidOperand(
'Cannot process ints and floats in strict binary operands mode, '.
'please cast explicitly',
new CodeLocation($statements_source, $parent),
),
$statements_source->getSuppressedIssues(),
);
}
}
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$result_type = Type::getInt();
} else {
$result_type = Type::combineUnionTypes(Type::getFloat(), $result_type);
}
$has_valid_right_operand = true;
$has_valid_left_operand = true;
return null;
}
if ($left_type_part->isNumericType() && $right_type_part->isNumericType()) {
if ($config->strict_binary_operands) {
if ($statements_source) {
IssueBuffer::maybeAdd(
new InvalidOperand(
'Cannot process numeric types together in strict operands mode, '.
'please cast explicitly',
new CodeLocation($statements_source, $parent),
),
$statements_source->getSuppressedIssues(),
);
}
}
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
$result_type = Type::getInt();
} else {
$result_type = new Union([new TInt, new TFloat]);
}
$has_valid_right_operand = true;
$has_valid_left_operand = true;
return null;
}
if (!$left_type_part->isNumericType()) {
$invalid_left_messages[] = 'Cannot perform a numeric operation with a non-numeric type '
. $left_type_part;
$has_valid_right_operand = true;
} else {
$invalid_right_messages[] = 'Cannot perform a numeric operation with a non-numeric type '
. $right_type_part;
$has_valid_left_operand = true;
}
} else {
$invalid_left_messages[] =
'Cannot perform a numeric operation with non-numeric types ' . $left_type_part
. ' and ' . $right_type_part;
}
return null;
}
/**
* @param float|int $operand1
* @param float|int $operand2
*/
public static function arithmeticOperation(
PhpParser\Node $operation,
$operand1,
$operand2,
bool $allow_float_result
): ?Union {
if ($operation instanceof PhpParser\Node\Expr\BinaryOp\Plus) {
$result = $operand1 + $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\Minus) {
$result = $operand1 - $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
if ($operand2 === 0) {
return Type::getNever();
}
$result = $operand1 % $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\Mul) {
$result = $operand1 * $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\Pow) {
$result = $operand1 ** $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\BitwiseOr) {
$result = $operand1 | $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd) {
$result = $operand1 & $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\BitwiseXor) {
$result = $operand1 ^ $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\ShiftLeft) {
$result = $operand1 << $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\ShiftRight) {
$result = $operand1 >> $operand2;
} elseif ($operation instanceof PhpParser\Node\Expr\BinaryOp\Div) {
if ($operand2 === 0 || $operand2 === 0.0) {
return Type::getNever();
}
$result = $operand1 / $operand2;
} else {
return null;
}
$calculated_type = self::getNumericalType($result);
if (!$allow_float_result && $calculated_type->isFloat()) {
return null;
}
return $calculated_type;
}
private static function analyzeOperandsBetweenIntRange(
PhpParser\Node $parent,
?Union &$result_type,
TIntRange $left_type_part,
TIntRange $right_type_part
): void {
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Div) {
//can't assume an int range will stay int after division
$result_type = Type::combineUnionTypes(
new Union([new TInt(), new TFloat()]),
$result_type,
);
return;
}
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\Mod) {
self::analyzeModBetweenIntRange($result_type, $left_type_part, $right_type_part);
return;
}
if ($parent instanceof PhpParser\Node\Expr\BinaryOp\BitwiseAnd ||