PropertyWriter::writeElement()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

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 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Flagbit\Plantuml\TokenReflection;
4
5
use TokenReflection\IReflectionProperty;
6
7
class PropertyWriter extends WriterAbstract
8
{
9
    /**
10
     * @param IReflectionProperty $property
11
     *
12
     * @return string
13
     */
14 8
    public function writeElement(IReflectionProperty $property)
15
    {
16 8
        return $this->formatLine($this->writeVisibility($property) . $property->getName()
17 8
            . $this->writeType($property) . $this->writeValue($property));
18
    }
19
20
    /**
21
     * @param IReflectionProperty[] $properties
22
     *
23
     * @return string
24
     */
25 10
    public function writeElements(array $properties)
26
    {
27
        // see https://bugs.php.net/bug.php?id=50688
28 10
        @usort($properties, function(IReflectionProperty $a, IReflectionProperty $b) {
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
29 3
            return strnatcasecmp($a->getName(), $b->getName());
30 10
        });
31
32 10
        $propertiesString = '';
33 10
        foreach ($properties as $property) {
34
            /** @var $property IReflectionProperty */
35 3
            $propertiesString .= $this->writeElement($property);
36 10
        }
37 10
        return $propertiesString;
38
    }
39
40
    /**
41
     * @param IReflectionProperty $property
42
     *
43
     * @return string
44
     */
45 8
    public function writeVisibility(IReflectionProperty $property)
46
    {
47 8
        return $property->isPrivate() ? '-' : ($property->isProtected() ? '#' : '+');
48
    }
49
50
    /**
51
     * @param IReflectionProperty $property
52
     *
53
     * @return string
54
     */
55 8 View Code Duplication
    public function writeType(IReflectionProperty $property)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57 8
        $type = '';
58 8
        preg_match('/\*\h+@var\h+([^\h]+)/', (string) $property->getDocComment(), $matches);
59 8
        if (isset($matches[1])) {
60 4
            $type = $matches[1];
61 4
            if ($property->getDeclaringClass()) {
62 2
                $type = $this->expandNamespaceAlias($property->getDeclaringClass(), $type);
63 2
            }
64 4
            $type = ' : ' . $this->formatClassName($type);
65 4
        }
66 8
        return $type;
67
    }
68
69
    /**
70
     * @param IReflectionProperty $property
71
     *
72
     * @return string
73
     */
74 8
    public function writeValue(IReflectionProperty $property)
75
    {
76 8
        $value = '';
77 8
        if ($property->getDeclaringClass() && $defaultProperties = $property->getDeclaringClass()->getDefaultProperties()) {
78 3
            if (!is_null($defaultProperties[$property->getName()])) {
79 2
               $value = ' = ' . $this->formatValue($defaultProperties[$property->getName()]);
80 2
            }
81 3
        }
82 8
        return $value;
83
    }
84
}
85