Completed
Pull Request — master (#10)
by
unknown
03:21
created

WriterAbstract   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 83.72%

Importance

Changes 9
Bugs 3 Features 4
Metric Value
wmc 16
c 9
b 3
f 4
lcom 1
cbo 1
dl 0
loc 111
rs 10
ccs 36
cts 43
cp 0.8372

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
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
    /**
11
     * @param Flagbit\Plantuml\TokenReflection\WriterOptions $writerOptions
12
     */
13
    public function __construct(WriterOptions $writerOptions=null)
14
    {
15
        if (!is_null($writerOptions)) {
16
            $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
        } else {
18
            $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
    }
21
    /**
22 10
     * @var string
23
     */
24 10
    private $indent = '    ';
25 10
26
    /**
27
     * @var string
28
     */
29
    private $linebreak = "\n";
30
31 10
    /**
32
     * @var  Flagbit\Plantuml\TokenReflection\WriterOptions
33 10
     */
34
    protected $writerOptions = null;
35
36
    /**
37
     * @param string $indent
38
     */
39
    protected function setIndent($indent = '    ')
40
    {
41 3
        $this->indent = (string) $indent;
42
    }
43 3
44 3
    /**
45
     * @param string $string
46
     * @return string
47
     */
48
    public function formatLine($string)
49 3
    {
50 3
        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 10
     */
58
    protected function expandNamespaceAlias(IReflectionClass $declaringClass, $aliasedClassName)
59 10
    {
60 10
        $aliasedClassName = trim($aliasedClassName);
61
        foreach ($declaringClass->getNamespaceAliases() as $namespaceAlias) {
62
            if (1 === preg_match('/\\\\' . preg_quote($aliasedClassName) . '$/', $namespaceAlias)) {
63 10
                $aliasedClassName = $namespaceAlias;
64
                break;
65
            }
66
        }
67
        return $aliasedClassName;
68
    }
69
70 4
    /**
71
     * @param string $className
72 4
     * @return string
73
     */
74
    protected function formatClassName($className)
75 4
    {
76 1
        $className = str_replace('\\', '.', trim($className));
77 1
        if ('.' === $className[0]) {
78 1
            $className = substr($className, 1);
79 1
        }
80 1
        return $className;
81 1
    }
82 4
83
    /**
84 3
     * @param mixed $value
85 4
     * @return string
86 3
     */
87 3
    protected function formatValue($value)
88
    {
89
        if (is_null($value)) {
90
            $value = 'null';
91 3
        }
92 3
        else if (is_array($value)) {
93 3
            $formattedValues = array();
94 3
            foreach ($value as $currentValue) {
95 3
                $formattedValues[] = $this->formatValue($currentValue);
96 3
            }
97
            $value = '[' .implode(', ', $formattedValues) . ']';
98 4
        }
99
        else if (is_numeric($value)) {
100
            // nothing to do here
101 1
        }
102
        else if (is_bool($value)) {
103
            $value = $value ? 'true' : 'false';
104
        }
105
        else {
106
            // make sure we receive two backslashes in the output as
107
            // plantuml needs them escaped as well
108
            $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
                "\n" => '\\\\n',
110
                "\r" => '\\\\r',
111
                "\t" => '\\\\t',
112
            ));
113
            $value = '"' . $value . '"';
114
        }
115
        return $value;
116
    }
117
}
118