Completed
Pull Request — master (#23)
by Erin
03:28
created

Leo::setAssertion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Peridot\Leo;
3
4
use Peridot\Leo\Formatter\Formatter;
5
use Peridot\Leo\Formatter\FormatterInterface;
6
use Peridot\Leo\Responder\ExceptionResponder;
7
use Peridot\Leo\Responder\ResponderInterface;
8
9
/**
10
 * Class Leo. Singleton access to Leo and
11
 * all of its internals. A singleton is used as
12
 * opposed to static methods and properties because
13
 * of how php handles static closures.
14
 *
15
 * For instance:
16
 *
17
 * Leo::extend(callable $fn) will not allow binding
18
 * of variables inside $fn - i.e via the DynamicObjectTrait
19
 *
20
 * @package Peridot\Leo
21
 */
22
class Leo
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
23
{
24
    /**
25
     * @var \Peridot\Leo\Leo
26
     */
27
    private static $instance;
28
29
    /**
30
     * @var Formatter
31
     */
32
    protected $formatter;
33
34
    /**
35
     * @var ExceptionResponder
36
     */
37
    protected $responder;
38
39
    /**
40
     * @var Assertion
41
     */
42
    protected $assertion;
43
44
    /**
45
     * Private access Constructor
46
     */
47
    private function __construct()
48
    {
49
        $this->formatter = new Formatter();
50
        $this->responder = new ExceptionResponder($this->formatter);
51
        $this->assertion = new Assertion($this->responder);
52
53
        $this->assertion->extend(__DIR__ . '/Core/Definitions.php');
54
    }
55
56
    /**
57
     * Return the Leo Assertion.
58
     *
59
     * @return Assertion
60
     */
61
    public function getAssertion()
62
    {
63
        return $this->assertion;
64
    }
65
66
    /**
67
     * Set the Assertion used by Leo.
68
     *
69
     * @param $assertion
70
     * @return $this
71
     */
72
    public function setAssertion($assertion)
73
    {
74
        $this->assertion = $assertion;
75
        return $this;
76
    }
77
78
    /**
79
     * Return the FormatterInterface used by Leo.
80
     *
81
     * @return FormatterInterface
82
     */
83
    public function getFormatter()
84
    {
85
        return $this->formatter;
86
    }
87
88
    /**
89
     * Set the FormatterInterface used by Leo.
90
     *
91
     * @param $formatter
92
     * @return $this
93
     */
94
    public function setFormatter(FormatterInterface $formatter)
95
    {
96
        $this->formatter = $formatter;
0 ignored issues
show
Documentation Bug introduced by
$formatter is of type object<Peridot\Leo\Formatter\FormatterInterface>, but the property $formatter was declared to be of type object<Peridot\Leo\Formatter\Formatter>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
97
        return $this;
98
    }
99
100
    /**
101
     * Return the ResponderInterface being used by Leo.
102
     *
103
     * @return ResponderInterface
104
     */
105
    public function getResponder()
106
    {
107
        return $this->responder;
108
    }
109
110
    /**
111
     * Set the ResponderInterface used by Leo.
112
     *
113
     * @param $responder
114
     * @return $this
115
     */
116
    public function setResponder(ResponderInterface $responder)
117
    {
118
        $this->responder = $responder;
0 ignored issues
show
Documentation Bug introduced by
$responder is of type object<Peridot\Leo\Responder\ResponderInterface>, but the property $responder was declared to be of type object<Peridot\Leo\Responder\ExceptionResponder>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
119
        return $this;
120
    }
121
122
    /**
123
     * Singleton access to Leo. A singleton is used instead of a facade as
124
     * PHP has some hangups about binding scope from static methods. This should
125
     * be used to access all Assertion members.
126
     *
127
     * @code
128
     *
129
     * $assertion = Leo::instance()->getAssertion();
130
     * $assertion->extend(function($assertion)) {
131
     *     $assertion->addMethod('coolAssertion', function($expected, $message = "") {
132
     *         $this->flag('message', $message);
133
     *         return new CoolMatcher($expected);
134
     *     });
135
     * });
136
     *
137
     * @endcode
138
     *
139
     * @return Leo
140
     */
141
    public static function instance()
142
    {
143
        if (! self::$instance) {
144
            self::$instance = new Leo();
145
        }
146
        return self::$instance;
147
    }
148
149
    /**
150
     * Singleton access to Leo's assertion object.
151
     *
152
     * @return Assertion
153
     */
154
    public static function assertion()
155
    {
156
        return Leo::instance()->getAssertion();
157
    }
158
}
159