AbstractGenerator::getLineGenerator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2014-04-24 
5
 */
6
7
namespace Net\Bazzline\Component\CodeGenerator;
8
9
/**
10
 * Class AbstractGenerator
11
 * @package Net\Bazzline\Component\Locator\LocatorGenerator\Generator
12
 */
13
abstract class AbstractGenerator extends AbstractBasicGenerator implements BlockGeneratorDependentInterface, LineGeneratorDependentInterface
14
{
15
    /** @var boolean */
16
    private $canBeGenerated;
17
18
    /** @var BlockGenerator */
19
    private $blockGenerator;
20
21
    /** @var LineGenerator */
22
    private $lineGenerator;
23
24
    /** @var array */
25
    private $properties = [];
26
27
    public function __construct()
28
    {
29
        $this->properties = [];
30
    }
31
32
    /**
33
     * @return $this
34
     */
35
    public function clear()
36
    {
37
        $this->properties = [];
38
        $this->resetContent();
39
    }
40
41
    /**
42
     * @return $this
43
     */
44
    public function __clone()
45
    {
46
        $this->clear();
47
    }
48
49
    /**
50
     * @param BlockGenerator $generator
51
     * @return $this
52
     */
53
    public function setBlockGenerator(BlockGenerator $generator)
54
    {
55
        $this->blockGenerator = $generator;
56
57
        return $this;
58
    }
59
60
    /**
61
     * @param LineGenerator $generator
62
     * @return $this
63
     */
64
    public function setLineGenerator(LineGenerator $generator)
65
    {
66
        $this->lineGenerator = $generator;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param Indention $indention
73
     * @return $this
74
     */
75
    final public function setIndention(Indention $indention)
76
    {
77
        parent::setIndention($indention);
78
        $this->blockGenerator->setIndention($indention);
79
        $this->lineGenerator->setIndention($indention);
80
81
        return $this;
82
    }
83
84
    /**
85
     * @return boolean
86
     */
87
    public function hasContent()
88
    {
89
        return (!empty($this->properties));
90
    }
91
92
    /**
93
     * @return $this
94
     */
95
    final protected function markAsCanBeGenerated()
96
    {
97
        $this->canBeGenerated = true;
98
99
        return $this;
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    final protected function canBeGenerated()
106
    {
107
        return ($this->canBeGenerated === true);
108
    }
109
110
    /**
111
     * @param string $name
112
     * @param mixed $value
113
     * @param bool $isStackable
114
     */
115
    final protected function addGeneratorProperty($name, $value, $isStackable = true)
116
    {
117
        $name = (string) $name;
118
119
        if ($isStackable) {
120 View Code Duplication
            if ((!isset($this->properties[$name]))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
121
                || (!is_array($this->properties[$name]))) {
122
                $this->properties[$name] = [];
123
            }
124
            $this->properties[$name][] = $value;
125 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
126
            if (!isset($this->properties[$name])) {
127
                $this->properties[$name] = null;
128
            }
129
            $this->properties[$name] = $value;
130
        }
131
132
        $this->markAsCanBeGenerated();
133
    }
134
135
    /**
136
     * @param string|AbstractGenerator[] $content
137
     * @param bool $isIndented
138
     * @throws InvalidArgumentException
139
     * @todo extend addContent to support int|string|array|LineGenerator|BlockGenerator
140
     */
141
    final protected function addContent($content, $isIndented = false)
142
    {
143
        if ($isIndented) {
144
            $this->getIndention()->increaseLevel();
145
        }
146
        if (!($content instanceof AbstractGenerator)) {
147
            if (is_array($content)) {
148
                $content = $this->getBlockGenerator($content);
0 ignored issues
show
Documentation introduced by
$content is of type array, but the function expects a null|string|object<Net\B...nerator\BlockGenerator>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
149
            } else {
150
                $content = $this->getLineGenerator($content);
151
            }
152
        }
153
        //@todo why? - take a look to BlockGenerator->generate(), strings are not supported
154
        $content = $content->generate();
155
        if ($isIndented) {
156
            $this->getIndention()->decreaseLevel();
157
        }
158
        $this->blockGenerator->add($content);
159
    }
160
161
    /**
162
     * @param GeneratorInterface $generator
163
     * @param bool $isIndented - needed?
164
     */
165
    final protected function addGeneratorAsContent(GeneratorInterface $generator, $isIndented = false)
166
    {
167
        $generator->setIndention($this->getIndention());
168
        $this->addContent(
169
            explode(
170
                PHP_EOL,
171
                $generator->generate()
172
            ),
173
            $isIndented
174
        );
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    final protected function generateStringFromContent()
181
    {
182
        return $this->blockGenerator->generate();
183
    }
184
185
    /**
186
     * @param string $name
187
     * @param mixed $default
188
     * @return null|string|array
189
     */
190
    final protected function getGeneratorProperty($name, $default = null)
191
    {
192
        return (isset($this->properties[$name])) ? $this->properties[$name] : $default;
193
    }
194
195
    /**
196
     * @param null|string|LineGenerator|BlockGenerator $content
197
     * @return BlockGenerator
198
     */
199 View Code Duplication
    final protected function getBlockGenerator($content = null)
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...
200
    {
201
        $block = clone $this->blockGenerator;
202
        $block->setIndention($this->getIndention());
203
204
        if (!is_null($content)) {
205
            $block->add($content);
206
        }
207
208
        return $block;
209
    }
210
211
    /**
212
     * @param null|string $content
213
     * @return LineGenerator
214
     */
215 View Code Duplication
    final protected function getLineGenerator($content = null)
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...
216
    {
217
        $line = clone $this->lineGenerator;
218
        $line->setIndention($this->getIndention());
219
220
        if (!is_null($content)) {
221
            $line->add($content);
222
        }
223
224
        return $line;
225
    }
226
227
    /**
228
     * @return array
229
     */
230
    final protected function getNotPrintableTypeHints()
231
    {
232
        return [
233
            'bool', 
234
            'boolean', 
235
            'int', 
236
            'integer', 
237
            'object', 
238
            'resource', 
239
            'string'
240
        ];
241
    }
242
243
    /**
244
     * @param array $array
245
     * @return mixed
246
     */
247
    final protected function getLastArrayKey(array $array)
248
    {
249
        end($array);
250
        $lastArrayKey = key($array);
251
        reset($array);
252
253
        return $lastArrayKey;
254
    }
255
256
    /**
257
     * @return $this
258
     */
259
    final protected function resetContent()
260
    {
261
        $this->blockGenerator = $this->getBlockGenerator();
262
263
        return $this;
264
    }
265
}
266