Completed
Push — master ( 08e4d5...a0c1d4 )
by Antonio Carlos
06:14 queued 04:40
created

Macroable   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 29.63 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 79.31%

Importance

Changes 0
Metric Value
dl 32
loc 108
ccs 23
cts 29
cp 0.7931
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A macro() 0 4 1
A mixin() 0 13 4
A hasMacro() 0 4 1
A __callStatic() 16 16 3
A __call() 16 16 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace IlluminateAgnostic\Arr\Support\Traits;
4
5
use BadMethodCallException;
6
use Closure;
7
use ReflectionClass;
8
use ReflectionMethod;
9
10
trait Macroable
11
{
12
    /**
13
     * The registered string macros.
14
     *
15
     * @var array
16
     */
17
    protected static $macros = [];
18
19
    /**
20
     * Register a custom macro.
21
     *
22
     * @param  string $name
23
     * @param  object|callable  $macro
24
     *
25
     * @return void
26
     */
27 9
    public static function macro($name, $macro)
28
    {
29 9
        static::$macros[$name] = $macro;
30 9
    }
31
32
    /**
33
     * Mix another object into the class.
34
     *
35
     * @param  object  $mixin
36
     * @param  bool  $replace
37
     * @return void
38
     *
39
     * @throws \ReflectionException
40
     */
41 2
    public static function mixin($mixin, $replace = true)
42
    {
43 2
        $methods = (new ReflectionClass($mixin))->getMethods(
44 2
            ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
45
        );
46
47 2
        foreach ($methods as $method) {
48 2
            if ($replace || ! static::hasMacro($method->name)) {
49 2
                $method->setAccessible(true);
50 2
                static::macro($method->name, $method->invoke($mixin));
0 ignored issues
show
Documentation introduced by
$method->invoke($mixin) is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
            }
52
        }
53 2
    }
54
55
    /**
56
     * Checks if macro is registered.
57
     *
58
     * @param  string  $name
59
     * @return bool
60
     */
61 9
    public static function hasMacro($name)
62
    {
63 9
        return isset(static::$macros[$name]);
64
    }
65
66
    /**
67
     * Dynamically handle calls to the class.
68
     *
69
     * @param  string  $method
70
     * @param  array  $parameters
71
     * @return mixed
72
     *
73
     * @throws \BadMethodCallException
74
     */
75 2 View Code Duplication
    public static function __callStatic($method, $parameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77 2
        if (! static::hasMacro($method)) {
78
            throw new BadMethodCallException(sprintf(
79
                'Method %s::%s does not exist.', static::class, $method
80
            ));
81
        }
82
83 2
        $macro = static::$macros[$method];
84
85 2
        if ($macro instanceof Closure) {
86 2
            return call_user_func_array(Closure::bind($macro, null, static::class), $parameters);
87
        }
88
89
        return $macro(...$parameters);
90
    }
91
92
    /**
93
     * Dynamically handle calls to the class.
94
     *
95
     * @param  string  $method
96
     * @param  array  $parameters
97
     * @return mixed
98
     *
99
     * @throws \BadMethodCallException
100
     */
101 8 View Code Duplication
    public function __call($method, $parameters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103 8
        if (! static::hasMacro($method)) {
104
            throw new BadMethodCallException(sprintf(
105
                'Method %s::%s does not exist.', static::class, $method
106
            ));
107
        }
108
109 8
        $macro = static::$macros[$method];
110
111 8
        if ($macro instanceof Closure) {
112 8
            return call_user_func_array($macro->bindTo($this, static::class), $parameters);
113
        }
114
115
        return $macro(...$parameters);
116
    }
117
}
118