-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathrules.php
717 lines (641 loc) · 15.1 KB
/
rules.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
<?php
/**
* @noinspection ALL
* @linter disable
*/
/**
* @comment Report ternary expressions that can be simplified.
* @before $x ? $x : $y
* @after $x ?: $y
*/
function ternarySimplify() {
/**
* @maybe Could replace the ternary with just $cond
* @type bool $cond
*/
$cond ? true : false;
/**
* @maybe Could rewrite as `(bool)$cond`
* @type !bool $cond
*/
$cond ? true : false;
/**
* @maybe Could rewrite as `$x ?: $y`
* @fix $x ?: $y
* @pure $x
*/
$x ? $x : $y;
/**
* @maybe Could rewrite as `$x ?? $y`
* @fix $x ?? $y
*/
isset($x) ? $x : $y;
/**
* @maybe Could rewrite as `$x[$i] ?? $y`
* @pure $i
*/
any_indexing: {
$x[$i] !== null ? $x[$i] : $y;
null !== $x[$i] ? $x[$i] : $y;
$x[$i] === null ? $y : $x[$i];
null === $x[$i] ? $y : $x[$i];
}
/**
* @maybe Could rewrite as `$x[$i] ?? $y`
* @pure $i
*/
any_array_key_exists: {
array_key_exists($i, $x) ? $x[$i] : $y;
!array_key_exists($i, $x) ? $y : $x[$i];
}
}
/**
* @comment Report potential operation precedence issues.
* @before $x & $mask == 0; // == has higher precedence than &
* @after ($x & $mask) == 0
*/
function precedence() {
// Note: we report `$x & $mask != $y` as a precedence issue even
// if it can be caught with `typecheckOp` that checks both operand
// types (bool is not a good operand for bitwise operation).
//
// Reporting `invalid types, expected number found bool` is
// not that helpful, because the root of the problem is precedence.
// Invalid types are a result of that.
/** @warning == has higher precedence than & */
any_eq_bitand: {
$_ == $_ & $_;
$_ & $_ == $_;
}
/** @warning != has higher precedence than & */
any_neq_bitand: {
$_ != $_ & $_;
$_ & $_ != $_;
}
/** @warning === has higher precedence than & */
any_eq3_bitand: {
$_ === $_ & $_;
$_ & $_ === $_;
}
/** @warning !== has higher precedence than & */
any_neq3_bitand: {
$_ !== $_ & $_;
$_ & $_ !== $_;
}
/** @warning == has higher precedence than | */
any_eq_bitor: {
$_ == $_ | $_;
$_ | $_ == $_;
}
/** @warning != has higher precedence than | */
any_neq_bitor: {
$_ != $_ | $_;
$_ | $_ != $_;
}
/** @warning === has higher precedence than | */
any_eq3_bitor: {
$_ === $_ | $_;
$_ | $_ === $_;
}
/** @warning !== has higher precedence than | */
any_neq3_bitor: {
$_ !== $_ | $_;
$_ | $_ !== $_;
}
/** @warning === has higher precedence than ?? */
$_ === $_ ?? $_;
/** @warning !== has higher precedence than ?? */
$_ !== $_ ?? $_;
/** @warning == has higher precedence than ?? */
$_ == $_ ?? $_;
/** @warning != has higher precedence than ?? */
$_ != $_ ?? $_;
/** @warning > has higher precedence than ?? */
$_ > $_ ?? $_;
/** @warning >= has higher precedence than ?? */
$_ >= $_ ?? $_;
/** @warning < has higher precedence than ?? */
$_ < $_ ?? $_;
/** @warning <= has higher precedence than ?? */
$_ <= $_ ?? $_;
}
/**
* @comment Report assignments that can be simplified.
* @before $x = $x + $y;
* @after $x += $y;
*/
function assignOp() {
/**
* @maybe Could rewrite as `$x += $y`
* @fix $x += $y
* @pure $x
*/
$x = $x + $y;
/**
* @maybe Could rewrite as `$x -= $y`
* @fix $x -= $y
* @pure $x
*/
$x = $x - $y;
/**
* @maybe Could rewrite as `$x *= $y`
* @fix $x *= $y
* @pure $x
*/
$x = $x * $y;
/**
* @maybe Could rewrite as `$x /= $y`
* @fix $x /= $y
* @pure $x
*/
$x = $x / $y;
/**
* @maybe Could rewrite as `$x %= $y`
* @fix $x %= $y
* @pure $x
*/
$x = $x % $y;
/**
* @maybe Could rewrite as `$x &= $y`
* @fix $x &= $y
* @pure $x
*/
$x = $x & $y;
/**
* @maybe Could rewrite as `$x |= $y`
* @fix $x |= $y
* @pure $x
*/
$x = $x | $y;
/**
* @maybe Could rewrite as `$x ^= $y`
* @fix $x ^= $y
* @pure $x
*/
$x = $x ^ $y;
/**
* @maybe Could rewrite as `$x <<= $y`
* @fix $x <<= $y
* @pure $x
*/
$x = $x << $y;
/**
* @maybe Could rewrite as `$x >>= $y`
* @fix $x >>= $y
* @pure $x
*/
$x = $x >> $y;
/**
* @maybe Could rewrite as `$x .= $y`
* @fix $x .= $y
* @pure $x
*/
$x = $x . $y;
/**
* @maybe Could rewrite as `$x ??= $y`
* @fix $x ??= $y
* @pure $x
*/
$x = $x ?? $y;
}
/**
* @comment Report potential off-by-one mistakes.
* @before $a[count($a)]
* @after $a[count($a)-1]
*/
function offBy1() {
/**
* @warning Probably intended to use count-1 as an index
* @fix $a[count($a) - 1]
* @strict-syntax
*/
$a[count($a)];
/**
* @warning Probably intended to use sizeof-1 as an index
* @fix $a[sizeof($a) - 1]
* @strict-syntax
*/
$a[sizeof($a)];
}
/**
* @extends
*/
function argsOrder() {
/**
* @warning Potentially incorrect haystack and needle arguments order
*/
any_haystack_needle: {
strpos(${"char"}, ${"*"});
stripos(${"char"}, ${"*"});
strrpos(${"char"}, ${"*"});
substr_count(${"str"}, ${"*"});
}
/**
* @warning Potentially incorrect replacement and subject arguments order
*/
preg_replace($_, $_, ${"str"}, ${"*"});
/**
* @warning Potentially incorrect replace and string arguments order
*/
any_str_replace: {
str_replace($_, $_, ${"char"}, ${"*"});
str_replace($_, $_, "", ${"*"});
}
/**
* @warning Potentially incorrect delimiter and string arguments order
*/
explode($_, ${"char"}, ${"*"});
}
/**
* @comment Report suspicious usage of bitwise operations.
* @before if ($isURL & $verify) { ... } // Bitwise AND on two bool looks suspicious,
* @after if ($isURL && $verify) { ... }
*/
function bitwiseOps() {
/**
* @warning Used & bitwise operator over bool operands, perhaps && is intended?
* @fix $x && $y
* @type bool $x
* @type bool $y
*/
$x & $y;
/**
* @warning Used | bitwise operator over bool operands, perhaps || is intended?
* @fix $x || $y
* @type bool $x
* @type bool $y
*/
$x | $y;
}
/**
* @comment Report call expressions that can be simplified.
* @before in_array($k, array_keys($this->data))
* @after array_key_exists($k, $this->data)
*/
function callSimplify() {
/**
* @maybe Could simplify to array_key_exists($key, $a)
* @fix array_key_exists($key, $a)
*/
in_array($key, array_keys($a));
/**
* @maybe Could simplify to $x[$y]
*/
substr($x, $y, 1);
/**
* @maybe Could simplify to $a[] = $v
*/
array_push($a, $v);
}
/**
* @comment Report not-strict-enough comparisons.
* @before in_array("what", $s)
* @after in_array("what", $s, true)
*/
function strictCmp() {
/**
* @warning Non-strict comparison (use ===)
*/
any_equal: {
$_ == true;
true == $_;
$_ == false;
false == $_;
$_ == null;
null == $_;
}
/**
* @warning Non-strict string comparison (use ===)
* @type string $x
* @type string $y
*/
$x == $y;
/**
* @warning Non-strict comparison (use !==)
*/
any_not_equal: {
$_ != true;
true != $_;
$_ != false;
false != $_;
$_ != null;
null != $_;
}
/**
* @warning Non-strict string comparison (use !==)
* @type string $x
* @type string $y
*/
$x != $y;
/**
* @warning 3rd argument of in_array must be true when comparing strings
* @type string $b
*/
in_array($b, $_);
/**
* @warning 3rd argument of array_search must be true when comparing strings
* @type string $b
*/
array_search($b, $_);
}
/**
* @comment Report the use of curly braces for indexing.
* @before $x{0}
* @after $x[0]
*/
function indexingSyntax() {
/**
* @warning a{i} indexing is deprecated since PHP 7.4, use a[i] instead
* @fix $x[$y]
*/
$x{$y};
}
/**
* @comment Report using an integer for `$needle` argument of `str*` functions.
* @before strpos("hello", 10)
* @after strpos("hello", chr(10))
*/
function intNeedle() {
/**
* @warning Since PHP 7.3, passing the int parameter needle to string search functions has been deprecated, cast it explicitly to string or wrap it in a chr() function call
* @type int $x
*/
any: {
strpos($_, $x);
strrpos($_, $x);
stripos($_, $x);
strripos($_, $x);
strstr($_, $x);
strchr($_, $x);
strrchr($_, $x);
stristr($_, $x);
}
}
/**
* @extends
*/
function langDeprecated() {
/**
* @warning Since PHP 7.3, the definition of case insensitive constants has been deprecated
*/
define($_, $_, true);
/**
* @warning Define defaults to a case sensitive constant, the third argument can be removed
* @fix define($x, $y)
*/
define($x, $y, false);
}
/**
* @extends
*/
function strangeCast() {
/**
* @warning Concatenation with empty string, possible type cast, use explicit cast to string instead of concatenate with empty string
*/
any_string_cast: {
$x . "";
"" . $x;
$x . '';
'' . $x;
}
/**
* @warning Addition with zero, possible type cast, use an explicit cast to int or float instead of zero addition
*/
any_number_cast: {
0 + $x;
0.0 + $x;
}
}
/**
* @comment Report string emptyness checking using `strlen(...)`.
* @before if (strlen($string)) { ... }
* @after if ($string !== "") { ... }
*/
function emptyStringCheck() {
/**
* @warning Use '$x !== ""' instead
*/
any_not_equal: {
if (strlen($x)) { $_; }
if (mb_strlen($x)) { $_; }
if ($x || strlen($x)) { $_; }
}
/**
* @warning Use '$x === ""' instead
*/
any_equal: {
if (!strlen($x)) { $_; }
if (!mb_strlen($x)) { $_; }
}
}
/**
* @comment Report the use of assignment in the `return` statement.
* @before return $a = 100;
* @after return $a;
* @disabled
*/
function returnAssign() {
/**
* @warning Don't use assignment in the return statement
*/
any: {
return $_ = $_;
return $_ += $_;
return $_ -= $_;
return $_ *= $_;
return $_ /= $_;
return $_ %= $_;
return $_ &= $_;
return $_ |= $_;
return $_ ^= $_;
return $_ <<= $_;
return $_ >>= $_;
return $_ .= $_;
return $_ ??= $_;
}
}
/**
* @comment Report comparisons `count(...)` which are always `false` or `true`.
* @before if (count($arr) >= 0) { ... }
* @after if (count($arr) != 0) { ... }
*/
function countUse() {
/**
* @warning Count of elements is always greater than or equal to zero, this expression is always true
*/
any_greater_or_equal: {
count($arr) >= 0;
0 <= count($arr);
}
/**
* @warning Count of elements is always greater than or equal to zero, this expression is always false
*/
any_less: {
count($arr) < 0;
0 > count($arr);
}
/**
* @warning Count of elements is always greater than or equal to zero, use count($arr) == 0 instead
* @fix count($arr) == 0
*/
count($arr) <= 0;
/**
* @warning Count of elements is always greater than or equal to zero, use 0 == count($arr) instead
* @fix 0 == count($arr)
*/
0 >= count($arr);
}
/**
* @comment Report the repetition of unary (`!` or `~`) operators in a row.
* @before echo !!$a;
* @after echo (bool) $a;
*/
function unaryRepeat() {
/**
* @warning Several negations in a row does not make sense, if you want to cast a value to a boolean type, use an explicit cast
*/
any_repeat_negation: {
!!$a;
!!!$a;
!!!!$a;
}
/**
* @warning Several bitwise not (~) in a row does not make sense
* @fix $a
*/
~~~~$a;
/**
* @warning Several bitwise not (~) in a row does not make sense
* @fix ~$a
*/
~~~$a;
/**
* @warning Several bitwise not (~) in a row does not make sense
* @fix $a
*/
~~$a;
}
/**
* @comment Report potentially erroneous `for` loops.
* @before for ($i = 0; $i < 100; $i--) { ... }
* @after for ($i = 0; $i < 100; $i++) { ... }
*/
function forLoop() {
/**
* @warning Potentially infinite 'for' loop, because $i decreases and is always less than initial value $start and, accordingly, $length
*/
any_for_loop_decrease: {
for ($i = $start; $i < $length; $i--) { ${"*"};}
for ($i = $start; $i <= $length; $i--) { ${"*"};}
for ($i = $start; $i < $length; $i -= $subtraction) { ${"*"};}
for ($i = $start; $i <= $length; $i -= $subtraction) { ${"*"};}
}
/**
* @warning Potentially infinite 'for' loop, because $i increases and is always greater than initial value $start and, accordingly, $length
*/
any_for_loop_increase: {
for ($i = $start; $i > $length; $i++) { ${"*"};}
for ($i = $start; $i >= $length; $i++) { ${"*"};}
for ($i = $start; $i > $length; $i += $addition) { ${"*"};}
for ($i = $start; $i >= $length; $i += $addition) { ${"*"};}
}
}
/**
* @comment Report when use to always null object.
* @before if ($obj == null && $obj->method()) { ... }
* @after if ($obj != null && $obj->method()) { ... }
*/
function alwaysNull() {
/**
* @warning '$obj' is always 'null', maybe you meant '$obj != null && ...' or '$obj == null || ...'
* @location $obj
*/
any_equal: {
if ($obj == null && $obj->$method(${"*"})) { ${"*"}; }
if ($obj == null && $obj->$prop) { ${"*"}; }
}
/**
* @warning '$obj' is always 'null', maybe you meant '$obj == null || ...' or '$obj != null && ...'
* @location $obj
*/
any_not_equal: {
if ($obj != null || $obj->$method(${"*"})) { ${"*"}; }
if ($obj != null || $obj->$prop) { ${"*"}; }
}
}
/**
* @comment Report self-assignment of variables.
* @before $x = $x;
* @after $x = $y;
*/
function selfAssign() {
/**
* @warning Assignment to $x itself does not make sense
*/
${'x:var'} = ${'x:var'};
}
/**
* @comment Report using `@`.
* @before @f();
* @after f();
* @disabled
*/
function errorSilence() {
/**
* @warning Don't use @, silencing errors is bad practice
*/
@$_;
}
/**
* @comment Report when use unparenthesized expression containing both `.` and binary operator.
* @before "id: " . $id - 10
* @after "id: " . ($id - 10)
*/
function concatenationPrecedence() {
/**
* @warning Unparenthesized expression containing both '.' and binary operator may produce unexpected results
*/
any_concat: {
$_ . $_ - $_;
$_ . $_ + $_;
$_ . $_ << $_;
$_ . $_ >> $_;
}
}
/**
* @comment Report using `exit` or `die` functions.
* @before exit(1);
* @after // no exit
*/
function useExitOrDie() {
/**
* @warning Don't use the 'exit' function
*/
exit($_);
/**
* @warning Don't use the 'die' function
*/
die($_);
}
/**
* @comment Report using `eval` function.
* @before eval("2 + 2");
* @after // no eval
*/
function useEval() {
/**
* @warning Don't use the 'eval' function
*/
eval($_);
}
/**
* @comment Report using `sleep` function.
* @before sleep(10);
* @after // no sleep
*/
function useSleep() {
/**
* @warning Don't use the 'sleep' function
*/
sleep($_);
}