Test Setup Failed
Push — master ( ecdc34...0769f8 )
by Gabriel
02:46 queued 11s
created

HasParserTrait::getParser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router\Route\Traits;
4
5
use Nip\Router\Parsers\AbstractParser;
6
7
/**
8
 * Class HasParserTrait
9
 * @package Nip\Router\Route\Traits
10
 */
11
trait HasParserTrait
12
{
13
    protected $parser = null;
14
15
16
    /**
17
     * @return AbstractParser
18
     */
19 27
    public function getParser()
20
    {
21 27
        if ($this->parser === null) {
22 27
            $this->initParser();
23
        }
24
25 27
        return $this->parser;
26
    }
27
28
    /**
29
     * @param $class
30
     * @return $this
31
     */
32 27
    public function setParser($class)
33
    {
34 27
        $this->parser = $class;
35
36 27
        return $this;
37
    }
38
39 27
    public function initParser()
40
    {
41 27
        $class = $this->getParserClass();
42 27
        $parser = new $class;
43 27
        $this->setParser($parser);
44 27
    }
45
46
    /**
47
     * @return string
48
     */
49 27
    public function getParserClass()
50
    {
51 27
        return 'Nip\Router\Parsers\\' . inflector()->camelize($this->getType());
0 ignored issues
show
Bug introduced by
It seems like getType() 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...
52
    }
53
}
54