Failed Conditions
Pull Request — master (#3)
by
unknown
12:22 queued 09:43
created

RouteAction   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 0
dl 0
loc 75
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 21 6
A getActionMethod() 0 4 1
B getFunctionArgs() 0 27 6
1
<?php
2
3
namespace Jasny\Controller;
4
5
use Psr\Http\Message\ResponseInterface;
6
7
/**
8
 * Execute controller on given route
9
 */
10
trait RouteAction
11
{
12
    /**
13
     * Run the controller
14
     *
15
     * @return ResponseInterface
16
     */
17 8
    public function run() {
18 8
        $request = $this->getRequest();
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...
19 8
        if (!$request) {
20 1
            throw new \RuntimeException("Request object is not set for controller");            
21
        }
22
23 7
        $route = $request->getAttribute('route');
24 7
        $method = $this->getActionMethod(isset($route->action) ? $route->action : null);
25
        
26 7
        if (!method_exists($this, $method)) {
27 2
            throw new \RuntimeException("No method $method in conrtoller to route to");
28
        }
29
30 5
        $args = isset($route->args) ? 
31 1
            $route->args :
32 5
            $this->getFunctionArgs($route, new \ReflectionMethod($this, $method));
33
34 3
        $response = call_user_func_array([$this, $method], $args);
35
36 3
        return $response ?: $this->getResponse();        
0 ignored issues
show
Bug introduced by
It seems like getResponse() 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...
37
    }
38
39
    /**
40
     * Get the method name of the action
41
     * 
42
     * @param string $action
43
     * @return string
44
     */
45 7
    protected function getActionMethod($action)
46
    {
47 7
        return \Jasny\camelcase($action) . 'Action';
48
    }
49
50
    /**
51
     * Get the arguments for a function from a route using reflection
52
     * 
53
     * @param object $route
54
     * @param \ReflectionFunctionAbstract $refl
55
     * @return array
56
     */
57 4
    protected function getFunctionArgs($route, \ReflectionFunctionAbstract $refl)
58
    {
59 4
        $args = [];
60 4
        $params = $refl->getParameters();
61
62 4
        foreach ($params as $param) {
63 4
            $key = $param->name;
64
65 4
            if (property_exists($route, $key)) {
66 2
                $value = $route->{$key};
67
            } else {
68 3
                if (!$param->isOptional()) {
69 2
                    $fn = $refl instanceof \ReflectionMethod
70 2
                        ? $refl->getDeclaringClass()->getName() . ':' . $refl->getName()
0 ignored issues
show
introduced by
Consider using $refl->class. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
Bug introduced by
Consider using $refl->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
71 2
                        : $refl->getName();
72
73 2
                    throw new \RuntimeException("Missing argument '$key' for $fn()");
74
                }
75
                
76 1
                $value = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null;
77
            }
78
79 2
            $args[$key] = $value;
80
        }
81
        
82 2
        return $args;
83
    }
84
}
85
86