Completed
Push — master ( 63fea9...ea37f6 )
by Gabriel
08:41
created

DispatcherAwareTrait::callMCA()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
1
<?php
2
3
namespace Nip\Controllers\Traits;
4
5
use Nip\Container\Container;
6
use Nip\Controllers\Controller;
7
use Nip\Dispatcher\Dispatcher;
8
use Nip\Request;
9
10
/**
11
 * Trait DispatcherAwareTrait
12
 * @package Nip\Controllers\Traits
13
 */
14
trait DispatcherAwareTrait
15
{
16
    /**
17
     * @return mixed
18
     * @throws \Exception
19
     */
20 2
    public function call()
21
    {
22 2
        $arguments = func_get_args();
23 2
        if (count($arguments) >= 3) {
24
            return $this->callMCA(...$arguments);
0 ignored issues
show
Documentation introduced by
$arguments is of type array, but the function expects a boolean.

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...
25
        }
26
27 2
        if (count($arguments) == 2 && is_array($arguments[1])) {
28 1
            if (is_string($arguments[0])) {
29 1
                return $this->{$arguments[0]}(...$arguments[1]);
30
            }
31
        }
32
33 1
        if (count($arguments) == 1) {
34 1
            return $this->{$arguments[0]}();
35
        }
36
37
        throw new \Exception("Controller call method invoked with invalid parameters");
38
    }
39
40
    /**
41
     * @param bool $action
42
     * @param bool $controller
43
     * @param bool $module
44
     * @param array $params
45
     * @return mixed
46
     */
47
    protected function callMCA($action = false, $controller = false, $module = false, $params = [])
48
    {
49
        /** @var Request $newRequest */
50
        $newRequest = $this->getRequest()->duplicateWithParams($action, $controller, $module, $params);
0 ignored issues
show
Bug introduced by
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
51
52
        /** @noinspection PhpUnhandledExceptionInspection */
53
        return $this->getDispatcher()->callFromRequest($newRequest, $params);
0 ignored issues
show
Unused Code introduced by
The call to Dispatcher::callFromRequest() has too many arguments starting with $params.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
54
    }
55
56
    /**
57
     * @param bool $action
58
     * @param bool $controller
59
     * @param bool $module
60
     * @param array $params
61
     */
62
    protected function forward($action = false, $controller = false, $module = false, $params = [])
63
    {
64
        $this->getDispatcher()->forward($action, $controller, $module, $params);
65
    }
66
67
    /**
68
     * @return Dispatcher
69
     */
70
    protected function getDispatcher()
71
    {
72
        if (function_exists('app')) {
73
            return app('dispatcher');
74
        }
75
76
        return Container::getInstance()->get('dispatcher');
77
    }
78
}
79