Test Setup Failed
Push — master ( b9b7e5...f50e96 )
by Gabriel
02:03 queued 10s
created

HasMatcherTrait::populateRequest()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 19
loc 19
rs 9.3222
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router\Legacy\Router\Traits;
4
5
use Nip\Request;
6
use Nip\Router\Route\Route;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
/**
10
 * Trait HasMatcherTrait
11
 * @package Nip\Router\Legacy\Router\Traits
12
 */
13
trait HasMatcherTrait
14
{
15
    /**
16
     * @param Request|ServerRequestInterface $request
17
     * @return array
18
     * @deprecated Use matchRequest($request)
19
     */
20
    public function route($request)
21
    {
22
        $return = $this->matchRequest($request);
0 ignored issues
show
Bug introduced by
It seems like matchRequest() 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...
23
        if ($return['_route']) {
24
            $this->populateRequest($request, $return);
0 ignored issues
show
Compatibility introduced by
$request of type object<Psr\Http\Message\ServerRequestInterface> is not a sub-type of object<Nip\Request>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ServerRequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
25
        }
26
        return $return;
27
    }
28
29
    /**
30
     * @param Request $request
31
     * @param $params
32
     */
33 View Code Duplication
    protected function populateRequest($request, $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
    {
35
        foreach ($params as $param => $value) {
36
            switch ($param) {
37
                case 'module':
38
                    $request->setModuleName($value);
39
                    break;
40
                case 'controller':
41
                    $request->setControllerName($value);
42
                    break;
43
                case 'action':
44
                    $request->setActionName($value);
45
                    break;
46
                default:
47
                    $request->attributes->set($param, $value);
48
                    break;
49
            }
50
        }
51
    }
52
}
53