PropertyWriter   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 78
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 4
Metric Value
wmc 13
c 6
b 0
f 4
lcom 1
cbo 1
dl 13
loc 78
ccs 33
cts 33
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A writeElement() 0 5 1
A writeElements() 0 14 2
A writeVisibility() 0 4 3
A writeType() 13 13 3
A writeValue() 0 10 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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