Completed
Pull Request — master (#13)
by
unknown
02:21
created

WriterAbstract::indenting()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 6
nop 2
crap 4
1
<?php
2
3
namespace Flagbit\Plantuml\TokenReflection;
4
5
use TokenReflection\IReflectionClass;
6
7
abstract class WriterAbstract
8
{
9
    /**
10
     * @var string
11
     */
12
    private $indent = '    ';
13
14
    /**
15
     * @var string
16
     */
17
    private $linebreak = "\n";
18
19
    /**
20
     * @param string $indent
21
     */
22 10
    protected function setIndent($indent = '    ')
23
    {
24 10
        $this->indent = (string) $indent;
25 10
    }
26
27
    /**
28
     * @param string $string
29
     * @return string
30
     */
31 10
    public function formatLine($string)
32
    {
33 10
        return $this->indent . $string . $this->linebreak;
34
    }
35
36
    /**
37
     * @param integer $times How much indentation needed to be repeated
38
     * @param bool $addingTabs Whether appending tab character after each indentation
39
     * @return string
40
     */
41 1
    public function indenting($times = 1, $addingTabs = false)
42
    {
43 1
        $tabChar = $addingTabs ? '\t' : '';
44
45 1
        $indent = '';
46 1
        for ($i=0; $i < $times; $i++) {
47 1
            $tab = $i==0 ? '' : $tabChar;
48 1
            $indent .= $this->indent . $tab;
49 1
        }
50 1
        return $indent;
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 3
    protected function expandNamespaceAlias(IReflectionClass $declaringClass, $aliasedClassName)
59
    {
60 3
        $aliasedClassName = trim($aliasedClassName);
61 3
        foreach ($declaringClass->getNamespaceAliases() as $namespaceAlias) {
62
            if (1 === preg_match('/\\\\' . preg_quote($aliasedClassName) . '$/', $namespaceAlias)) {
63
                $aliasedClassName = $namespaceAlias;
64
                break;
65
            }
66 3
        }
67 3
        return $aliasedClassName;
68
    }
69
70
    /**
71
     * @param string $className
72
     * @return string
73
     */
74 10
    protected function formatClassName($className)
75
    {
76 10
        $className = str_replace('\\', '.', trim($className));
77 10
        if ('.' === $className[0]) {
78
            $className = substr($className, 1);
79
        }
80 10
        return $className;
81
    }
82
83
    /**
84
     * @param mixed $value
85
     * @return string
86
     */
87 4
    protected function formatValue($value, $_arrayDepth = 0)
88
    {
89 4
        if (is_null($value)) {
90
            $value = 'null';
91
        }
92 4
        else if (is_array($value)) {
93 1
            $formattedValues = array();
94 1
            $_arrayDepth++;
95 1
            foreach ($value as $key => $currentValue) {
96
                // recursively formatting array values
97 1
                $formattedValues[] = $this->formatValue($key) . ' => ' . $this->formatValue($currentValue, $_arrayDepth);
98 1
            }
99 1
            $value = count($formattedValues) > 0
100 1
                ? "[\n{$this->indenting($_arrayDepth+1, true)}" .implode(",\n{$this->indenting($_arrayDepth+1, true)}", $formattedValues) . "\n{$this->indenting($_arrayDepth, true)}]"
101 1
                : '[' .implode(', ', $formattedValues) . ']';
102 1
        }
103 4
        else if (is_numeric($value)) {
104
            // nothing to do here
105 3
        }
106 4
        else if (is_bool($value)) {
107 3
            $value = $value ? 'true' : 'false';
108 3
        }
109
        else {
110
            // make sure we receive two backslashes in the output as
111
            // plantuml needs them escaped as well
112 3
            $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...
113 3
                "\n" => '\\\\n',
114 3
                "\r" => '\\\\r',
115 3
                "\t" => '\\\\t',
116 3
                "\l" => '\\\\l',
117 3
            ));
118 3
            $value = '"' . $value . '"';
119
        }
120 4
        return $value;
121
    }
122
}
123