Completed
Pull Request — master (#11)
by
unknown
05:00
created

WriterAbstract::setIndent()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Flagbit\Plantuml\TokenReflection;
4
5
use TokenReflection\IReflectionClass;
6
7
abstract class WriterAbstract
8
{
9
10
    /**
11
     * @param Flagbit\Plantuml\TokenReflection\WriterOptions $writerOptions
12
     */
13 13
    public function __construct(WriterOptions $writerOptions=null)
14
    {
15 13
        if (!is_null($writerOptions)) {
16 13
            $this->writerOptions = $writerOptions;
0 ignored issues
show
Documentation Bug introduced by
It seems like $writerOptions of type object<Flagbit\Plantuml\...flection\WriterOptions> is incompatible with the declared type object<Flagbit\Plantuml\...flection\WriterOptions> of property $writerOptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
17 13
        } else {
18 13
            $this->writerOptions = new WriterOptions();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Flagbit\Plantuml\To...lection\WriterOptions() of type object<Flagbit\Plantuml\...flection\WriterOptions> is incompatible with the declared type object<Flagbit\Plantuml\...flection\WriterOptions> of property $writerOptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
19
        }
20 13
    }
21
    /**
22
     * @var string
23
     */
24
    private $indent = '    ';
25
26
    /**
27
     * @var string
28
     */
29
    private $linebreak = "\n";
30
31
    /**
32
     * @var  Flagbit\Plantuml\TokenReflection\WriterOptions
33
     */
34
    protected $writerOptions = null;
35
36
    /**
37
     * @param string $indent
38
     */
39 13
    protected function setIndent($indent = '    ')
40
    {
41 13
        $this->indent = (string) $indent;
42 13
    }
43
44
    /**
45
     * @param string $string
46
     * @return string
47
     */
48 13
    public function formatLine($string)
49
    {
50 13
        return $this->indent . $string . $this->linebreak;
51
    }
52
53
    /**
54
     * @param \TokenReflection\IReflectionClass $declaringClass The class using the namespace aliases
55
     * @param string $aliasedClassName The class name used in the declaring class
56
     * @return string
57
     */
58 6
    protected function expandNamespaceAlias(IReflectionClass $declaringClass, $aliasedClassName)
59
    {
60 6
        $aliasedClassName = trim($aliasedClassName);
61 6
        foreach ($declaringClass->getNamespaceAliases() as $namespaceAlias) {
62
            if (1 === preg_match('/\\\\' . preg_quote($aliasedClassName) . '$/', $namespaceAlias)) {
63
                $aliasedClassName = $namespaceAlias;
64
                break;
65
            }
66 6
        }
67 6
        return $aliasedClassName;
68
    }
69
70
    /**
71
     * @param string $className
72
     * @return string
73
     */
74 13
    protected function formatClassName($className)
75
    {
76 13
        $className = str_replace('\\', '.', trim($className));
77 13
        if ('.' === $className[0]) {
78
            $className = substr($className, 1);
79
        }
80 13
        return $className;
81
    }
82
83
    /**
84
     * @param mixed $value
85
     * @return string
86
     */
87 6
    protected function formatValue($value)
88
    {
89 6
        if (is_null($value)) {
90
            $value = 'null';
91
        }
92 6
        else if (is_array($value)) {
93 1
            $formattedValues = array();
94 1
            foreach ($value as $currentValue) {
95 1
                $formattedValues[] = $this->formatValue($currentValue);
96 1
            }
97 1
            $value = '[' .implode(', ', $formattedValues) . ']';
98 1
        }
99 6
        else if (is_numeric($value)) {
100
            // nothing to do here
101 4
        }
102 6
        else if (is_bool($value)) {
103 4
            $value = $value ? 'true' : 'false';
104 4
        }
105
        else {
106
            // make sure we receive two backslashes in the output as
107
            // plantuml needs them escaped as well
108 5
            $value = strtr($value, array(
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type object; however, strtr() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
109 5
                "\n" => '\\\\n',
110 5
                "\r" => '\\\\r',
111 5
                "\t" => '\\\\t',
112 5
            ));
113 5
            $value = '"' . $value . '"';
114
        }
115 6
        return $value;
116
    }
117
}
118