Completed
Push — scrutinizer-quality ( c3b7e2 )
by Erin
02:15
created

MatcherTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 82
rs 10
wmc 6
lcom 2
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isNegated() 0 4 1
A invert() 0 6 1
A getTemplate() 0 8 2
A setTemplate() 0 6 1
A setAssertion() 0 6 1
1
<?php
2
3
namespace Peridot\Leo\Matcher;
4
5
use Peridot\Leo\Assertion;
6
use Peridot\Leo\Matcher\Template\TemplateInterface;
7
8
/**
9
 * MatcherTrait provides some common implementation details for matchers.
10
 *
11
 * @package Peridot\Leo\Matcher
12
 */
13
trait MatcherTrait
14
{
15
    /**
16
     * Returns whether or not the matcher is negated. A negated matcher negates
17
     * the results of a match.
18
     *
19
     * @return bool
20
     */
21
    public function isNegated()
22
    {
23
        return $this->negated;
24
    }
25
26
    /**
27
     * Inverts a matcher. If a matcher is not negated, it will become negated. If a matcher
28
     * is negated, it will no longer be negated.
29
     *
30
     * @return $this
31
     */
32
    public function invert()
33
    {
34
        $this->negated = !$this->negated;
35
36
        return $this;
37
    }
38
39
    /**
40
     * Return the TemplateInterface being used by the matcher.
41
     *
42
     * @return TemplateInterface
43
     */
44
    public function getTemplate()
45
    {
46
        if (!isset($this->template)) {
47
            return $this->getDefaultTemplate();
0 ignored issues
show
Bug introduced by
It seems like getDefaultTemplate() 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...
48
        }
49
50
        return $this->template;
51
    }
52
53
    /**
54
     * Set the TemplateInterface to use for formatting match results.
55
     *
56
     * @param  TemplateInterface $template
57
     * @return $this
58
     */
59
    public function setTemplate(TemplateInterface $template)
60
    {
61
        $this->template = $template;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Set the Assertion bound to the matcher. Useful for checking
68
     * flags from within a matcher.
69
     *
70
     * @param  Assertion $assertion
71
     * @return mixed
72
     */
73
    public function setAssertion(Assertion $assertion)
74
    {
75
        $this->assertion = $assertion;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @var bool
82
     */
83
    protected $negated = false;
84
85
    /**
86
     * @var TemplateInterface
87
     */
88
    protected $template;
89
90
    /**
91
     * @var Assertion
92
     */
93
    protected $assertion;
94
}
95