Test Setup Failed
Push — master ( fc6567...89d4a6 )
by Gabriel
07:58
created

HasRequestTrait::populateRequest()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0061

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 0
dl 0
loc 21
ccs 15
cts 16
cp 0.9375
crap 5.0061
rs 9.2728
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router\Route\Traits;
4
5
/**
6
 * Trait HasRequestTrait
7
 * @package Nip\Router\Route\Traits
8
 */
9
trait HasRequestTrait
10
{
11
    protected $request = null;
12
13 2
    public function populateRequest()
14
    {
15 2
        $params = $this->getParams();
0 ignored issues
show
Bug introduced by
It seems like getParams() 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...
16 2
        foreach ($params as $param => $value) {
17
            switch ($param) {
18 1
                case 'module':
19 1
                    $this->getRequest()->setModuleName($value);
20 1
                    break;
21 1
                case 'controller':
22 1
                    $this->getRequest()->setControllerName($value);
23 1
                    break;
24 1
                case 'action':
25 1
                    $this->getRequest()->setActionName($value);
26 1
                    break;
27
                default:
28
                    $this->getRequest()->attributes->set($param, $value);
29 1
                    break;
30
            }
31
        }
32 2
        $this->getRequest()->attributes->add($this->getMatches());
0 ignored issues
show
Bug introduced by
It seems like getMatches() 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...
33 2
    }
34
35
    /**
36
     * @return \Nip\Request
37
     */
38 2
    public function getRequest()
39
    {
40 2
        return $this->request;
41
    }
42
43
    /**
44
     * @param \Nip\Request $request
45
     */
46 3
    public function setRequest($request)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
47
    {
48 3
        $this->request = $request;
49
    }
50
}