WriterAbstract   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 83.33%

Importance

Changes 8
Bugs 3 Features 3
Metric Value
wmc 14
c 8
b 3
f 3
lcom 1
cbo 0
dl 0
loc 94
ccs 35
cts 42
cp 0.8333
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setIndent() 0 4 1
A formatLine() 0 4 1
A expandNamespaceAlias() 0 11 3
A formatClassName() 0 8 2
C formatValue() 0 30 7
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 \TokenReflection\IReflectionClass $declaringClass The class using the namespace aliases
38
     * @param string $aliasedClassName The class name used in the declaring class
39
     * @return string
40
     */
41 3
    protected function expandNamespaceAlias(IReflectionClass $declaringClass, $aliasedClassName)
42
    {
43 3
        $aliasedClassName = trim($aliasedClassName);
44 3
        foreach ($declaringClass->getNamespaceAliases() as $namespaceAlias) {
45
            if (1 === preg_match('/\\\\' . preg_quote($aliasedClassName) . '$/', $namespaceAlias)) {
46
                $aliasedClassName = $namespaceAlias;
47
                break;
48
            }
49 3
        }
50 3
        return $aliasedClassName;
51
    }
52
53
    /**
54
     * @param string $className
55
     * @return string
56
     */
57 10
    protected function formatClassName($className)
58
    {
59 10
        $className = str_replace('\\', '.', trim($className));
60 10
        if ('.' === $className[0]) {
61
            $className = substr($className, 1);
62
        }
63 10
        return $className;
64
    }
65
66
    /**
67
     * @param mixed $value
68
     * @return string
69
     */
70 4
    protected function formatValue($value)
71
    {
72 4
        if (is_null($value)) {
73
            $value = 'null';
74
        }
75 4
        else if (is_array($value)) {
76 1
            $formattedValues = array();
77 1
            foreach ($value as $currentValue) {
78 1
                $formattedValues[] = $this->formatValue($currentValue);
79 1
            }
80 1
            $value = '[' .implode(', ', $formattedValues) . ']';
81 1
        }
82 4
        else if (is_numeric($value)) {
83
            // nothing to do here
84 3
        }
85 4
        else if (is_bool($value)) {
86 3
            $value = $value ? 'true' : 'false';
87 3
        }
88
        else {
89
            // make sure we receive two backslashes in the output as
90
            // plantuml needs them escaped as well
91 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...
92 3
                "\n" => '\\\\n',
93 3
                "\r" => '\\\\r',
94 3
                "\t" => '\\\\t',
95 3
            ));
96 3
            $value = '"' . $value . '"';
97
        }
98 4
        return $value;
99
    }
100
}
101