Completed
Push — master ( 6ece6a...9c4912 )
by Marcus
02:53
created

StringValue::initializeFromOldFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace LesserPhp\Compiler\Value;
3
4
/**
5
 * lesserphp
6
 * https://www.maswaba.de/lesserphp
7
 *
8
 * LESS CSS compiler, adapted from http://lesscss.org
9
 *
10
 * Copyright 2013, Leaf Corcoran <[email protected]>
11
 * Copyright 2016, Marcus Schwarz <[email protected]>
12
 * Copyright 2017, Stefan Pöhner <[email protected]>
13
 * Licensed under MIT or GPLv3, see LICENSE
14
 *
15
 * @package LesserPhp
16
 */
17
18
class StringValue extends AbstractValue
19
{
20
    /**
21
     * @var string
22
     */
23
    private $delimiter;
24
25
    /**
26
     * @var array|string
27
     */
28
    private $content;
29
30
    /**
31
     * @inheritdoc
32
     */
33 27
    public function getCompiled()
34
    {
35 27
        $content = $this->content;
36 27
        foreach ($content as &$part) {
0 ignored issues
show
Bug introduced by
The expression $content of type array|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
37 27
            if (is_array($part)) {
38 27
                $part = $this->compiler->compileValue($part);
39
            }
40
        }
41
42 27
        return $this->delimiter . implode($content) . $this->delimiter;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48 27
    public function initializeFromOldFormat(array $value)
49
    {
50 27
        $this->delimiter = $value[1];
51 27
        $this->content   = $value[2];
52 27
    }
53
}
54