Completed
Push — master ( fa64d8...e093dd )
by Abdelrahman
02:31 queued 01:25
created

Macroable   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 49
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
            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...
35
        } catch (Error | BadMethodCallException $e) {
36
            if ($method !== 'macroableCall') {
37
                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...
38
            }
39
        }
40
    }
41
42
    /**
43
     * Handle dynamic static method calls into the method.
44
     *
45
     * @param string $method
46
     * @param array  $parameters
47
     *
48
     * @return mixed
49
     */
50
    public static function __callStatic($method, $parameters)
51
    {
52
        try {
53
            return (new static())->{$method}(...$parameters);
54
        } catch (Exception $e) {
55
            if ($method !== 'macroableCallStatic') {
56
                return (new static())::macroableCallStatic($method, $parameters);
57
            }
58
        }
59
    }
60
}
61