Macroable   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 59
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Support\Traits;
6
7
use Error;
8
use Exception;
9
use BadMethodCallException;
10
use Illuminate\Support\Traits\Macroable as BaseMacroable;
11
12
trait Macroable
13
{
14
    use BaseMacroable {
15
        BaseMacroable::__call as macroableCall;
16
        BaseMacroable::__callStatic as macroableCallStatic;
17
    }
18
19
    /**
20
     * Handle dynamic method calls into the model.
21
     *
22
     * @param string $method
23
     * @param array  $parameters
24
     *
25
     * @return mixed
26
     */
27
    public function __call($method, $parameters)
28
    {
29
        if (in_array($method, ['increment', 'decrement'])) {
30
            return $this->{$method}(...$parameters);
31
        }
32
33
        try {
34
            // Catch non-static calls for static methods
35
            if (method_exists(static::class, $method)) {
36
                return static::{$method}(...$parameters);
37
            }
38
39
            if ($resolver = (static::$relationResolvers[get_class($this)][$method] ?? null)) {
40
                return $resolver($this);
41
            }
42
43
            return $this->forwardCallTo($this->newQuery(), $method, $parameters);
0 ignored issues
show
Documentation Bug introduced by
The method newQuery does not exist on object<Rinvex\Support\Traits\Macroable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
Documentation Bug introduced by
The method forwardCallTo does not exist on object<Rinvex\Support\Traits\Macroable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
44
        } catch (Error | BadMethodCallException $e) {
45
            if ($method !== 'macroableCall') {
46
                return $this->macroableCall($method, $parameters);
0 ignored issues
show
Documentation Bug introduced by
The method macroableCall does not exist on object<Rinvex\Support\Traits\Macroable>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
47
            }
48
        }
49
    }
50
51
    /**
52
     * Handle dynamic static method calls into the method.
53
     *
54
     * @param string $method
55
     * @param array  $parameters
56
     *
57
     * @return mixed
58
     */
59
    public static function __callStatic($method, $parameters)
60
    {
61
        try {
62
            // Catch static calls for non-static methods
63
            return (new static())->{$method}(...$parameters);
64
        } catch (Exception $e) {
65
            if ($method !== 'macroableCallStatic') {
66
                return (new static())::macroableCallStatic($method, $parameters);
67
            }
68
        }
69
    }
70
}
71