Issues (994)

src/IMEI/imei.php (5 issues)

1
<?php
2
3
namespace IMEI;
4
5
class imei
6
{
7
  public function is_luhn($n)
8
  {
9
    $str = '';
10
    foreach (str_split(strrev((string) $n)) as $i => $d) {
11
      $str .= 0 !== $i % 2 ? $d * 2 : $d;
12
    }
13
14
    return 0 === array_sum(str_split($str)) % 10;
0 ignored issues
show
It seems like str_split($str) can also be of type true; however, parameter $array of array_sum() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

14
    return 0 === array_sum(/** @scrutinizer ignore-type */ str_split($str)) % 10;
Loading history...
15
  }
16
17
  public function is_imei2($n)
18
  {
19
    return $this->is_luhn($n) && 15 == strlen($n);
20
  }
21
22
  public function is_imei($imei)
23
  {
24
    // Should be 15 digits
25
    if (15 != strlen($imei) || !ctype_digit($imei)) {
26
      return false;
27
    }
28
    // Get digits
29
    $digits = str_split($imei);
30
    // Remove last digit, and store it
31
    $imei_last = array_pop($digits);
0 ignored issues
show
It seems like $digits can also be of type true; however, parameter $array of array_pop() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
    $imei_last = array_pop(/** @scrutinizer ignore-type */ $digits);
Loading history...
32
    // Create log
33
    $log = [];
34
    // Loop through digits
35
    foreach ($digits as $key => $n) {
36
      // If key is odd, then count is even
37
      if ($key & 1) {
38
        // Get double digits
39
        $double = str_split($n * 2);
40
        // Sum double digits
41
        $n = array_sum($double);
0 ignored issues
show
It seems like $double can also be of type true; however, parameter $array of array_sum() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        $n = array_sum(/** @scrutinizer ignore-type */ $double);
Loading history...
42
      }
43
      // Append log
44
      $log[] = $n;
45
    }
46
    // Sum log & multiply by 9
47
    $sum = array_sum($log) * 9;
48
    // Compare the last digit with $imei_last
49
    return substr($sum, -1) == $imei_last;
50
  }
51
52
  /**
53
   * Generates IMEI code valid and random
54
   * Generates 14 aleatory digits. These 14, multiplies the multiples of 2 by 2 and sum the result
55
   * The result Must be divisible by 10,
56
   * Then get the diference and genaretes the last digit.
57
   *
58
   * @return int $imei
59
   */
60
  public static function rand_imei()
61
  {
62
    $code = self::intRandom(14);
63
    $position = 0;
64
    $total = 0;
65
    while ($position < 14) {
66
      if (0 == $position % 2) {
67
        $prod = 1;
68
      } else {
69
        $prod = 2;
70
      }
71
      $actualNum = $prod * $code[$position];
72
      if ($actualNum > 9) {
73
        $strNum = strval($actualNum);
74
        $total += $strNum[0] + $strNum[1];
75
      } else {
76
        $total += $actualNum;
77
      }
78
      ++$position;
79
    }
80
    $last = 10 - ($total % 10);
81
    if (10 == $last) {
82
      $imei = $code . 0;
83
    } else {
84
      $imei = $code . $last;
85
    }
86
87
    if (strlen($imei) < 15) {
88
      $digits = 15 - strlen($imei);
89
      $imei .= rand(pow(10, $digits - 1), pow(10, $digits) - 1);
90
    } else {
91
      $imei = substr($imei, 0, 15);
92
    }
93
94
    return $imei;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $imei returns the type string which is incompatible with the documented return type integer.
Loading history...
95
  }
96
97
  /**
98
   * @param int $size
99
   *
100
   * @return $int
0 ignored issues
show
Documentation Bug introduced by
The doc comment $int at position 0 could not be parsed: Unknown type name '$int' at position 0 in $int.
Loading history...
101
   */
102
  public static function intRandom($size)
103
  {
104
    $validCharacters = utf8_decode('0123456789');
105
    $validCharNumber = strlen($validCharacters);
106
    $int = '';
107
    while (strlen($int) < $size) {
108
      $index = mt_rand(0, $validCharNumber - 1);
109
      $int .= $validCharacters[$index];
110
    }
111
112
    return $int;
113
  }
114
}
115