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

HasGeneratorTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 38.1 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 34.62%

Importance

Changes 0
Metric Value
dl 24
loc 63
ccs 9
cts 26
cp 0.3462
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A assemble() 12 12 2
A assembleFull() 12 12 2
A getDefaultRoute() 0 18 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Nip\Router\Router\Traits;
4
5
use Nip\Router\Router;
6
7
/**
8
 * Trait HasGeneratorTrait
9
 * @package Nip\Router\Router\Traits
10
 */
11
trait HasGeneratorTrait
12
{
13
14
    /**
15
     * @param $name
16
     * @param boolean $params
17
     * @return string|null
18
     */
19 1 View Code Duplication
    public function assemble($name, $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...
20
    {
21 1
        $route = $this->getDefaultRoute($name, $params);
0 ignored issues
show
Bug introduced by
It seems like $params defined by parameter $params on line 19 can also be of type boolean; however, Nip\Router\Router\Traits...rait::getDefaultRoute() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
22
23 1
        if ($route) {
24 1
            $route->setRequest($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...
25 1
            return $route->assemble($params);
26
        }
27
28
        trigger_error("Route \"$name\" not connected", E_USER_ERROR);
29
        return null;
30
    }
31
32
    /**
33
     * @param $name
34
     * @param boolean $params
35
     * @return string
36
     */
37 View Code Duplication
    public function assembleFull($name, $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...
38
    {
39
        $route = $this->getDefaultRoute($name, $params);
0 ignored issues
show
Bug introduced by
It seems like $params defined by parameter $params on line 37 can also be of type boolean; however, Nip\Router\Router\Traits...rait::getDefaultRoute() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
40
        if ($route) {
41
            $route->setRequest($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...
42
            return $route->assembleFull($params);
43
        }
44
45
        trigger_error("Route \"$name\" not connected", E_USER_ERROR);
46
47
        return null;
48
    }
49
50
    /**
51
     * @param $name
52
     * @param array $params
53
     * @return null|Route\Route
54
     */
55 1
    public function getDefaultRoute($name, &$params = [])
56
    {
57 1
        $route = $this->getRoute($name);
0 ignored issues
show
Bug introduced by
It seems like getRoute() 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...
58 1
        if (!$route) {
59
            $parts = explode(".", $name);
60
            $count = count($parts);
61
            if ($count <= 3) {
62
                if (in_array(reset($parts), app('mvc.modules')->getNames())) {
63
                    $module = array_shift($parts);
64
                    $params['controller'] = isset($parts[0]) ? $parts[0] : null;
65
                    $params['action'] = isset($parts[1]) ? $parts[1] : null;
66
                    $route = $this->getRoute($module . '.default');
0 ignored issues
show
Bug introduced by
It seems like getRoute() 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...
67
                }
68
            }
69
        }
70
71 1
        return $route;
72
    }
73
}