Completed
Push — master ( f8d6fe...92a23c )
by Lars
02:21
created

src/StaticArrayy.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Arrayy;
4
5
/**
6
 * Class StaticArrayy
7
 *
8
 * INFO: "Method Parameter Information" via PhpStorm |
9
 * https://www.jetbrains.com/phpstorm/help/viewing-method-parameter-information.html
10
 *
11
 */
12
class StaticArrayy
13
{
14
  /**
15
   * A mapping of method names to the numbers of arguments it accepts. Each
16
   * should be two more than the equivalent Arrayy method. Necessary as
17
   * static methods place the optional $encoding as the last parameter.
18
   *
19
   * @var string[]
20
   */
21
  protected static $methodArgs = null;
22
23
  /**
24
   * Creates an instance of Arrayy and invokes the given method
25
   *
26
   * @param string  $name
27
   * @param mixed[] $arguments
28
   *
29
   * @return Arrayy
30
   */
31 5
  public static function __callStatic($name, $arguments)
32
  {
33 5
    if (!static::$methodArgs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::$methodArgs of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
34 1
      $arrayyClass = new \ReflectionClass('Arrayy\Arrayy');
35 1
      $methods = $arrayyClass->getMethods(\ReflectionMethod::IS_PUBLIC);
36
37 1
      foreach ($methods as $method) {
38 1
        $params = $method->getNumberOfParameters() + 2;
39 1
        static::$methodArgs[$method->name] = $params;
40 1
      }
41 1
    }
42
43 5
    if (!isset(static::$methodArgs[$name])) {
44 1
      throw new \BadMethodCallException($name . ' is not a valid method');
45
    }
46
47 4
    $numArgs = count($arguments);
48 4
    $array = $numArgs ? $arguments[0] : '';
49
50 4
    if ($numArgs === static::$methodArgs[$name]) {
51 1
      $args = \array_slice($arguments, 1, -1);
52 1
    } else {
53 3
      $args = \array_slice($arguments, 1);
54
    }
55
56 4
    $arrayy = Arrayy::create($array);
57
58 4
    return call_user_func_array(array($arrayy, $name), $args);
59
  }
60
61
  ////////////////////////////////////////////////////////////////////
62
  ///////////////////////////// GENERATE /////////////////////////////
63
  ////////////////////////////////////////////////////////////////////
64
65
  /**
66
   * Generate an array from a range.
67
   *
68
   * @param int      $base The base number
69
   * @param int|null $stop The stopping point
70
   * @param int      $step How many to increment of
71
   *
72
   * @return Arrayy
73
   */
74 4
  public static function range($base, $stop = null, $step = 1)
75
  {
76 4
    if (!is_int($base) || !is_int($step)) {
77
      throw new \InvalidArgumentException('Passed value must be a int');
78
    }
79
80 4
    if (!is_int($stop) && null !== $stop) {
81
      throw new \InvalidArgumentException('Passed value must be a int or nul');
82
    }
83
84 4
    if (null !== $stop) {
85 2
      $start = $base;
86 2
    } else {
87 2
      $start = 1;
88 2
      $stop = $base;
89
    }
90
91 4
    return Arrayy::create(\range($start, $stop, $step));
92
  }
93
94
  /**
95
   * Fill an array with $times times some $data.
96
   *
97
   * @param mixed $data
98
   * @param int   $times
99
   *
100
   * @return Arrayy
101
   */
102 3
  public static function repeat($data, $times)
103
  {
104 3
    $times = (int)$times;
105
106 3
    if ($times === 0 || empty($data)) {
107 1
      return Arrayy::create();
108
    }
109
110 3
    return Arrayy::create(\array_fill(0, $times, $data));
111
  }
112
}
113