Test Setup Failed
Push — master ( 7450ca...e4bb3e )
by Gabriel
05:50 queued 15s
created

HasRequestTrait::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 2
cp 0
crap 2
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
    public function populateRequest()
14
    {
15
        $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
        foreach ($params as $param => $value) {
17
            switch ($param) {
18
                case 'module':
19
                    $this->getRequest()->setModuleName($value);
20
                    break;
21
                case 'controller':
22
                    $this->getRequest()->setControllerName($value);
23
                    break;
24
                case 'action':
25
                    $this->getRequest()->setActionName($value);
26
                    break;
27
                default:
28
                    $this->getRequest()->attributes->set($param, $value);
29
                    break;
30
            }
31
        }
32
        $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
    }
34
35
    /**
36
     * @return \Nip\Request
37
     */
38
    public function getRequest()
39
    {
40
        return $this->request;
41
    }
42
43
    /**
44
     * @param \Nip\Request $request
45
     */
46
    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
        $this->request = $request;
49
    }
50
}
51