Completed
Push — master ( e08a43...bd87cf )
by Lars
02:30
created

StaticArrayy::__callStatic()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 10
nop 2
dl 0
loc 29
ccs 16
cts 16
cp 1
crap 6
rs 8.8337
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
class StaticArrayy
14
{
15
    /**
16
     * A mapping of method names to the numbers of arguments it accepts. Each
17
     * should be two more than the equivalent Arrayy method. Necessary as
18
     * static methods place the optional $encoding as the last parameter.
19
     *
20
     * @var int[]|string[]
21
     */
22
    protected static $methodArgs;
23
24
    /**
25
     * Creates an instance of Arrayy and invokes the given method
26
     *
27
     * @param string  $name
28
     * @param mixed[] $arguments
29
     *
30
     * @return Arrayy
31
     */
32 5
    public static function __callStatic(string $name, $arguments)
33
    {
34 5
        if (!static::$methodArgs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::$methodArgs of type array<integer|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...
35 1
            $arrayyClass = new \ReflectionClass(Arrayy::class);
36 1
            $methods = $arrayyClass->getMethods(\ReflectionMethod::IS_PUBLIC);
37
38 1
            foreach ($methods as $method) {
39 1
                $params = $method->getNumberOfParameters() + 2;
40 1
                static::$methodArgs[$method->name] = $params;
41
            }
42
        }
43
44 5
        if (!isset(static::$methodArgs[$name])) {
45 1
            throw new \BadMethodCallException($name . ' is not a valid method');
46
        }
47
48 4
        $numArgs = \count($arguments);
49 4
        $array = $numArgs ? $arguments[0] : '';
50
51 4
        if ($numArgs === static::$methodArgs[$name]) {
52 1
            $args = \array_slice($arguments, 1, -1);
53
        } else {
54 3
            $args = \array_slice($arguments, 1);
55
        }
56
57 4
        $arrayy = Arrayy::create($array);
58
59 4
        return \call_user_func_array([$arrayy, $name], $args);
60
    }
61
62
    ////////////////////////////////////////////////////////////////////
63
    ///////////////////////////// GENERATE /////////////////////////////
64
    ////////////////////////////////////////////////////////////////////
65
66
    /**
67
     * Generate an array from a range.
68
     *
69
     * @param int      $base The base number
70
     * @param int|null $stop The stopping point
71
     * @param int      $step How many to increment of
72
     *
73
     * @return Arrayy
74
     */
75 4
    public static function range(int $base, int $stop = null, int $step = 1): Arrayy
76
    {
77 4
        if ($stop !== null) {
78 2
            $start = $base;
79
        } else {
80 2
            $start = 1;
81 2
            $stop = $base;
82
        }
83
84 4
        return Arrayy::create(\range($start, $stop, $step));
85
    }
86
87
    /**
88
     * Fill an array with $times times some $data.
89
     *
90
     * @param mixed $data
91
     * @param int   $times
92
     *
93
     * @return Arrayy
94
     */
95 3
    public static function repeat($data, int $times): Arrayy
96
    {
97 3
        if ($times === 0 || empty($data)) {
98 1
            return Arrayy::create();
99
        }
100
101 3
        return Arrayy::create(\array_fill(0, $times, $data));
102
    }
103
}
104