GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — appveyor ( 2cdcbe...623601 )
by Eric
68:47 queued 03:54
created

Formatter::renderLines()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Ivory Google Map package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source declaration.
10
 */
11
12
namespace Ivory\GoogleMap\Helper\Formatter;
13
14
use Ivory\GoogleMap\Utility\VariableAwareInterface;
15
16
/**
17
 * @author GeLo <[email protected]>
18
 */
19
class Formatter
20
{
21
    /**
22
     * @var bool
23
     */
24
    private $debug;
25
26
    /**
27
     * @var int
28
     */
29
    private $indentationStep;
30
31
    /**
32
     * @param bool $debug
33
     * @param int  $indentationStep
34
     */
35
    public function __construct($debug = false, $indentationStep = 4)
36
    {
37
        $this->setDebug($debug);
38
        $this->setIndentationStep($indentationStep);
39
    }
40
41
    /**
42
     * @return bool
43
     */
44
    public function isDebug()
45
    {
46
        return $this->debug;
47
    }
48
49
    /**
50
     * @param bool $debug
51
     */
52
    public function setDebug($debug)
53
    {
54
        $this->debug = $debug;
55
    }
56
57
    /**
58
     * @return int
59
     */
60
    public function getIndentationStep()
61
    {
62
        return $this->indentationStep;
63
    }
64
65
    /**
66
     * @param int $indentationStep
67
     */
68
    public function setIndentationStep($indentationStep)
69
    {
70
        $this->indentationStep = $indentationStep;
71
    }
72
73
    /**
74
     * @param string|null       $name
75
     * @param string|false|null $namespace
76
     *
77
     * @return string
78
     */
79
    public function renderClass($name = null, $namespace = null)
80
    {
81
        if ($namespace === null) {
82
            $namespace = $this->renderProperty('google', 'maps');
83
        }
84
85
        if (empty($namespace)) {
86
            return $name;
87
        }
88
89
        return $this->renderProperty($namespace, $name);
90
    }
91
92
    /**
93
     * @param string            $class
94
     * @param string            $value
95
     * @param string|false|null $namespace
96
     *
97
     * @return string
98
     */
99
    public function renderConstant($class, $value, $namespace = null)
100
    {
101
        return $this->renderClass($this->renderProperty($class, strtoupper($value)), $namespace);
102
    }
103
104
    /**
105
     * @param string            $class
106
     * @param string[]          $arguments
107
     * @param string|false|null $namespace
108
     * @param bool              $semicolon
109
     * @param bool              $newLine
110
     *
111
     * @return string
112
     */
113
    public function renderObject(
114
        $class,
115
        array $arguments = [],
116
        $namespace = null,
117
        $semicolon = false,
118
        $newLine = false
119
    ) {
120
        return $this->renderCall(
121
            'new '.$this->renderClass($class, $namespace),
122
            $arguments,
123
            $semicolon,
124
            $newLine
125
        );
126
    }
127
128
    /**
129
     * @param string      $object
130
     * @param string|null $property
131
     *
132
     * @return string
133
     */
134
    public function renderProperty($object, $property = null)
135
    {
136
        if (!empty($property)) {
137
            $property = '.'.$property;
138
        }
139
140
        return $object.$property;
141
    }
142
143
    /**
144
     * @param VariableAwareInterface $object
145
     * @param string                 $method
146
     * @param string[]               $arguments
147
     * @param bool                   $semicolon
148
     * @param bool                   $newLine
149
     *
150
     * @return string
151
     */
152
    public function renderObjectCall(
153
        VariableAwareInterface $object,
154
        $method,
155
        array $arguments = [],
156
        $semicolon = false,
157
        $newLine = false
158
    ) {
159
        return $this->renderCall(
160
            $this->renderProperty($object->getVariable(), $method),
161
            $arguments,
162
            $semicolon,
163
            $newLine
164
        );
165
    }
166
167
    /**
168
     * @param string|null $method
169
     * @param string[]    $arguments
170
     * @param bool        $semicolon
171
     * @param bool        $newLine
172
     *
173
     * @return string
174
     */
175
    public function renderCall($method, array $arguments = [], $semicolon = false, $newLine = false)
176
    {
177
        return $this->renderCode(
178
            $method.$this->renderArguments($arguments),
179
            $semicolon,
180
            $newLine
181
        );
182
    }
183
184
    /**
185
     * @param string|null $code
186
     * @param string[]    $arguments
187
     * @param string|null $name
188
     * @param bool        $semicolon
189
     * @param bool        $newLine
190
     *
191
     * @return string
192
     */
193
    public function renderClosure(
194
        $code = null,
195
        array $arguments = [],
196
        $name = null,
197
        $semicolon = false,
198
        $newLine = false
199
    ) {
200
        $separator = $this->renderSeparator();
201
202
        if ($name !== null) {
203
            $name = ' '.$name;
204
        }
205
206
        return $this->renderCode($this->renderLines([
207
            'function'.$name.$separator.$this->renderArguments($arguments).$separator.'{',
208
            $this->renderIndentation($code),
209
            '}',
210
        ], !empty($code), $newLine && !$semicolon), $semicolon, $newLine && $semicolon);
211
    }
212
213
    /**
214
     * @param VariableAwareInterface $object
215
     * @param string                 $declaration
216
     * @param bool                   $semicolon
217
     * @param bool                   $newLine
218
     *
219
     * @return string
220
     */
221
    public function renderObjectAssignment(
222
        VariableAwareInterface $object,
223
        $declaration,
224
        $semicolon = false,
225
        $newLine = false
226
    ) {
227
        return $this->renderAssignment($object->getVariable(), $declaration, $semicolon, $newLine);
228
    }
229
230
    /**
231
     * @param VariableAwareInterface      $root
232
     * @param string                      $declaration
233
     * @param string|null                 $propertyPath
234
     * @param VariableAwareInterface|null $object
235
     * @param bool                        $semicolon
236
     * @param bool                        $newLine
237
     *
238
     * @return string
239
     */
240
    public function renderContainerAssignment(
241
        VariableAwareInterface $root,
242
        $declaration,
243
        $propertyPath = null,
244
        VariableAwareInterface $object = null,
245
        $semicolon = true,
246
        $newLine = true
247
    ) {
248
        return $this->renderAssignment(
249
            $this->renderContainerVariable($root, $propertyPath, $object),
250
            $declaration,
251
            $semicolon,
252
            $newLine
253
        );
254
    }
255
256
    /**
257
     * @param VariableAwareInterface      $root
258
     * @param string|null                 $propertyPath
259
     * @param VariableAwareInterface|null $object
260
     *
261
     * @return string
262
     */
263
    public function renderContainerVariable(
264
        VariableAwareInterface $root,
265
        $propertyPath = null,
266
        VariableAwareInterface $object = null
267
    ) {
268
        $variable = $root->getVariable().'_container';
269
270
        if ($propertyPath !== null) {
271
            $variable = $this->renderProperty($variable, $propertyPath);
272
        }
273
274
        if ($object !== null) {
275
            $variable = $this->renderProperty($variable, $object->getVariable());
276
        }
277
278
        return $variable;
279
    }
280
281
    /**
282
     * @param string $variable
283
     * @param string $declaration
284
     * @param bool   $semicolon
285
     * @param bool   $newLine
286
     *
287
     * @return string
288
     */
289
    public function renderAssignment($variable, $declaration, $semicolon = false, $newLine = false)
290
    {
291
        $separator = $this->renderSeparator();
292
293
        return $this->renderCode($variable.$separator.'='.$separator.$declaration, $semicolon, $newLine);
294
    }
295
296
    /**
297
     * @param string      $statement
298
     * @param string      $code
299
     * @param string|null $condition
300
     * @param string|null $next
301
     * @param bool        $newLine
302
     *
303
     * @return string
304
     */
305
    public function renderStatement($statement, $code, $condition = null, $next = null, $newLine = true)
306
    {
307
        $separator = $this->renderSeparator();
308
        $statement .= $separator;
309
310
        if (!empty($condition)) {
311
            $statement .= $this->renderArguments([$condition]).$separator;
312
        }
313
314
        if (!empty($next)) {
315
            $next = $separator.$next;
316
        }
317
318
        return $this->renderLines([
319
            $statement.'{',
320
            $this->renderIndentation($code),
321
            '}'.$next,
322
        ], true, $newLine);
323
    }
324
325
    /**
326
     * @param string $code
327
     * @param bool   $semicolon
328
     * @param bool   $newLine
329
     *
330
     * @return string
331
     */
332
    public function renderCode($code, $semicolon = true, $newLine = true)
333
    {
334
        if ($semicolon) {
335
            $code .= ';';
336
        }
337
338
        return $this->renderLine($code, $newLine);
339
    }
340
341
    /**
342
     * @param string|null $code
343
     *
344
     * @return string
345
     */
346
    public function renderIndentation($code = null)
347
    {
348
        if ($this->debug && !empty($code)) {
349
            $indentation = str_repeat(' ', $this->indentationStep);
350
            $code = $indentation.str_replace("\n", "\n".$indentation, $code);
351
        }
352
353
        return (string) $code;
354
    }
355
356
    /**
357
     * @param string[] $codes
358
     * @param bool     $newLine
359
     * @param bool     $eolLine
360
     *
361
     * @return string
362
     */
363
    public function renderLines(array $codes, $newLine = true, $eolLine = true)
364
    {
365
        $result = '';
366
        $count = count($codes);
367
368
        for ($index = 0; $index < $count; ++$index) {
369
            $result .= $this->renderLine($codes[$index], $newLine && $index !== $count - 1);
370
        }
371
372
        return $this->renderLine($result, $eolLine);
373
    }
374
375
    /**
376
     * @param string|null $code
377
     * @param bool        $newLine
378
     *
379
     * @return string
380
     */
381
    public function renderLine($code = null, $newLine = true)
382
    {
383
        if ($newLine && !empty($code) && $this->debug) {
384
            $code .= "\n";
385
        }
386
387
        return (string) $code;
388
    }
389
390
    /**
391
     * @param string $argument
392
     *
393
     * @return string
394
     */
395
    public function renderEscape($argument)
396
    {
397
        return json_encode($argument, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
398
    }
399
400
    /**
401
     * @return string
402
     */
403
    public function renderSeparator()
404
    {
405
        return $this->debug ? ' ' : '';
406
    }
407
408
    /**
409
     * @param string[] $arguments
410
     *
411
     * @return string
412
     */
413
    private function renderArguments(array $arguments)
414
    {
415
        return '('.implode(','.$this->renderSeparator(), $arguments).')';
416
    }
417
}
418