Completed
Push — master ( 025a38...f7e4e4 )
by Lars
04:57
created

StaticArrayy::repeat()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arrayy;
6
7
/**
8
 * Class StaticArrayy
9
 *
10
 * INFO: "Method Parameter Information" via PhpStorm |
11
 * https://www.jetbrains.com/phpstorm/help/viewing-method-parameter-information.html
12
 *
13
 */
14
class StaticArrayy
15
{
16
  /**
17
   * A mapping of method names to the numbers of arguments it accepts. Each
18
   * should be two more than the equivalent Arrayy method. Necessary as
19
   * static methods place the optional $encoding as the last parameter.
20
   *
21
   * @var string[]
22
   */
23
  protected static $methodArgs;
24
25
  /**
26
   * Creates an instance of Arrayy and invokes the given method
27
   *
28
   * @param string  $name
29
   * @param mixed[] $arguments
30
   *
31
   * @return Arrayy
32
   */
33 5
  public static function __callStatic($name, $arguments)
34
  {
35 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...
36 1
      $arrayyClass = new \ReflectionClass(Arrayy::class);
37 1
      $methods = $arrayyClass->getMethods(\ReflectionMethod::IS_PUBLIC);
38
39 1
      foreach ($methods as $method) {
40 1
        $params = $method->getNumberOfParameters() + 2;
41 1
        static::$methodArgs[$method->name] = $params;
42
      }
43
    }
44
45 5
    if (!isset(static::$methodArgs[$name])) {
46 1
      throw new \BadMethodCallException($name . ' is not a valid method');
47
    }
48
49 4
    $numArgs = \count($arguments);
50 4
    $array = $numArgs ? $arguments[0] : '';
51
52 4
    if ($numArgs === static::$methodArgs[$name]) {
53 1
      $args = \array_slice($arguments, 1, -1);
54
    } else {
55 3
      $args = \array_slice($arguments, 1);
56
    }
57
58 4
    $arrayy = Arrayy::create($array);
59
60 4
    return \call_user_func_array(array($arrayy, $name), $args);
61
  }
62
63
  ////////////////////////////////////////////////////////////////////
64
  ///////////////////////////// GENERATE /////////////////////////////
65
  ////////////////////////////////////////////////////////////////////
66
67
  /**
68
   * Generate an array from a range.
69
   *
70
   * @param int      $base The base number
71
   * @param int|null $stop The stopping point
72
   * @param int      $step How many to increment of
73
   *
74
   * @return Arrayy
75
   */
76 4
  public static function range($base, $stop = null, $step = 1): Arrayy
77
  {
78 4
    if (!\is_int($base) || !\is_int($step)) {
79
      throw new \InvalidArgumentException('Passed value must be a int');
80
    }
81
82 4
    if (!\is_int($stop) && null !== $stop) {
83
      throw new \InvalidArgumentException('Passed value must be a int or nul');
84
    }
85
86 4
    if (null !== $stop) {
87 2
      $start = $base;
88
    } else {
89 2
      $start = 1;
90 2
      $stop = $base;
91
    }
92
93 4
    return Arrayy::create(\range($start, $stop, $step));
94
  }
95
96
  /**
97
   * Fill an array with $times times some $data.
98
   *
99
   * @param mixed $data
100
   * @param int   $times
101
   *
102
   * @return Arrayy
103
   */
104 3
  public static function repeat($data, $times): Arrayy
105
  {
106 3
    $times = (int)$times;
107
108 3
    if ($times === 0 || empty($data)) {
109 1
      return Arrayy::create();
110
    }
111
112 3
    return Arrayy::create(\array_fill(0, $times, $data));
113
  }
114
}
115