Completed
Push — master ( 6c3b78...8b46fe )
by John
04:40
created

AbstractBuilder::setScale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Graze\CiffRenderer\Field\Builder;
4
5
use Graze\CiffRenderer\Field\Builder\BuilderInterface;
6
use Graze\CiffRenderer\Field\Renderer\RendererInterface;
7
use Graze\CiffRenderer\Field\Parser\ParserInterface;
8
use Graze\CiffRenderer\Field\Parser\ParserManager;
9
use Intervention\Image\ImageManager;
10
use \SimpleXMLElement;
11
12
abstract class AbstractBuilder implements BuilderInterface
13
{
14
    /**
15
     * @var ParserManager
16
     */
17
    protected $parserManager;
18
19
    /**
20
     * @var SimpleXMLElement
21
     */
22
    protected $xmlField;
23
24
    /**
25
     * @var ImageManager
26
     */
27
    protected $imageManager;
28
29
    /**
30
     * @var SimpleXMLElement
31
     */
32
    protected $xmlHeader;
33
34
    /**
35
     * @var callable
36
     */
37
    protected $fontResolver;
38
39
    /**
40
     * @var callable
41
     */
42
    protected $graphicResolver;
43
44
    /**
45
     * @var string
46
     */
47
    protected $tracerColour;
48
49
    /**
50
     * @return ParserInterface
51
     */
52
    abstract protected function instantiateParser();
53
54
    /**
55
     * @param ParserInterface $parser
56
     *
57
     * @return ParserInterface
58
     */
59 6
    protected function buildParser(ParserInterface $parser)
60
    {
61 6
        $parser->setXmlField($this->getXmlField());
62 6
        $parser->setParserManager($this->getParserManager());
63
64 6
        return $parser;
65
    }
66
67
    /**
68
     * @param ParserInterface $parser
69
     *
70
     * @return RendererInterface
71
     */
72
    abstract protected function instantiateRenderer(ParserInterface $parser);
73
74
    /**
75
     * @param RendererInterface $renderer
76
     * @param ParserInterface $parser
77
     *
78
     * @return RendererInterface
79
     */
80 6
    protected function buildRenderer(RendererInterface $renderer, ParserInterface $parser)
81
    {
82 6
        $renderer->setParser($parser);
83 6
        $renderer->setImageManager($this->getImageManager());
84 6
        $renderer->setScale($this->getScale());
85 6
        $renderer->setTracerColour($this->getTracerColour());
86
87 6
        return $renderer;
88
    }
89
90
    /**
91
     * @return RendererInterface
92
     */
93 6
    public function build()
94
    {
95 6
        $parser = $this->instantiateParser();
96
97 6
        $parser = $this->buildParser($parser);
98
99
        // store it in the manager - other parsers may depend on it
100 6
        $this->getParserManager()->addParser($parser);
101
102 6
        $renderer = $this->instantiateRenderer($parser);
103
104 6
        return $this->buildRenderer($renderer, $parser);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->buildRenderer($renderer, $parser); (Graze\CiffRenderer\Field...derer\RendererInterface) is incompatible with the return type declared by the interface Graze\CiffRenderer\Field...BuilderInterface::build of type Graze\CiffRenderer\Field\Builder\RendererInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
105
    }
106
107
    /**
108
     * @param ParserManager $parserManager
109
     */
110 6
    public function setParserManager(ParserManager $parserManager)
111
    {
112 6
        $this->parserManager = $parserManager;
113 6
    }
114
115
    /**
116
     * @return ParserManager
117
     */
118 6
    protected function getParserManager()
119
    {
120 6
        return $this->parserManager;
121
    }
122
123
    /**
124
     * @param SimpleXMLElement $xmlField
125
     */
126 6
    public function setXmlField(SimpleXMLElement $xmlField)
127
    {
128 6
        $this->xmlField = $xmlField;
129 6
    }
130
131
    /**
132
     * @return SimpleXMLElement
133
     */
134 6
    protected function getXmlField()
135
    {
136 6
        return $this->xmlField;
137
    }
138
139
    /**
140
     * @param ImageManager $imageManager
141
     */
142 6
    public function setImageManager(ImageManager $imageManager)
143
    {
144 6
        $this->imageManager = $imageManager;
145 6
    }
146
147
    /**
148
     * @return ImageManager
149
     */
150 6
    protected function getImageManager()
151
    {
152 6
        return $this->imageManager;
153
    }
154
155
    /**
156
     * @param float $scale
157
     */
158 6
    public function setScale($scale)
159
    {
160 6
        $this->scale = $scale;
0 ignored issues
show
Bug introduced by
The property scale does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
161 6
    }
162
163
    /**
164
     * @return float
165
     */
166 6
    protected function getScale()
167
    {
168 6
        return $this->scale;
169
    }
170
171
    /**
172
     * @param string $tracerColour
173
     */
174 6
    public function setTracerColour($tracerColour)
175
    {
176 6
        $this->tracerColour = $tracerColour;
177 6
    }
178
179
    /**
180
     * @return string
181
     */
182 6
    protected function getTracerColour()
183
    {
184 6
        return $this->tracerColour;
185
    }
186
187
    /**
188
     * @param SimpleXMLElement $xmlHeader
189
     */
190 1
    public function setXmlHeader(SimpleXMLElement $xmlHeader)
191
    {
192 1
        $this->xmlHeader = $xmlHeader;
193 1
    }
194
195
    /**
196
     * @return SimpleXMLElement
197
     */
198 1
    protected function getXmlHeader()
199
    {
200 1
        return $this->xmlHeader;
201
    }
202
203
    /**
204
     * @param callable $fontResolver
205
     */
206 4
    public function setFontResolver(callable $fontResolver)
207
    {
208 4
        $this->fontResolver = $fontResolver;
209 4
    }
210
211
    /**
212
     * @return callable
213
     */
214 4
    protected function getFontResolver()
215
    {
216 4
        return $this->fontResolver;
217
    }
218
219
    /**
220
     * @param callable $graphicResolver
221
     */
222 1
    public function setGraphicResolver(callable $graphicResolver)
223
    {
224 1
        $this->graphicResolver = $graphicResolver;
225 1
    }
226
227
    /**
228
     * @return callable
229
     */
230 1
    protected function getGraphicResolver()
231
    {
232 1
        return $this->graphicResolver;
233
    }
234
}
235