MethodWriter   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 98
Duplicated Lines 13.27 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 1 Features 4
Metric Value
wmc 17
c 7
b 1
f 4
lcom 1
cbo 1
dl 13
loc 98
ccs 45
cts 45
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A writeElement() 0 6 2
A writeElements() 0 13 2
A writeVisibility() 0 4 3
A writeParameters() 0 9 2
B writeParameter() 0 20 5
A writeReturnType() 13 13 3

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\IReflectionMethod;
6
use TokenReflection\IReflectionParameter;
7
8
class MethodWriter extends \Flagbit\Plantuml\TokenReflection\WriterAbstract
9
{
10
    /**
11
     * @param \TokenReflection\IReflectionMethod $method
12
     * @return string
13
     */
14 13
    public function writeElement(IReflectionMethod $method)
15
    {
16 13
        return $this->formatLine($this->writeVisibility($method)
17 13
            . ($method->isStatic() ? '{static}' : '') . $method->getName()
18 13
            . $this->writeParameters($method) . $this->writeReturnType($method));
19
    }
20
21
    /**
22
     * @param IReflectionMethod[] $methods
23
     * @return string
24
     */
25 10
    public function writeElements(array $methods)
26
    {
27
        // see https://bugs.php.net/bug.php?id=50688
28 10
        @usort($methods, function(IReflectionMethod $a, IReflectionMethod $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 4
            return strnatcasecmp($a->getName(), $b->getName());
30 10
        });
31
32 10
        $methodsString = '';
33 10
        foreach ($methods as $method) {
34 5
            $methodsString .= $this->writeElement($method);
35 10
        }
36 10
        return $methodsString;
37
    }
38
39
    /**
40
     * @param \TokenReflection\IReflectionMethod $method
41
     * @return string
42
     */
43 13
    private function writeVisibility(IReflectionMethod $method)
44
    {
45 13
        return $method->isPrivate() ? '-' : ($method->isProtected() ? '#' : '+');
46
    }
47
48
    /**
49
     * @param \TokenReflection\IReflectionMethod $method
50
     * @return string
51
     */
52 13
    private function writeParameters(IReflectionMethod $method)
53
    {
54 13
        $parameters = array();
55 13
        foreach ($method->getParameters() as $parameter) {
56
            /** @var $parameter \TokenReflection\IReflectionParameter */
57 5
            $parameters[] = $this->writeParameter($method, $parameter);
58 13
        }
59 13
        return '(' . implode(', ' , $parameters) . ')';
60
    }
61
62
    /**
63
     * @param IReflectionMethod $method
64
     * @param IReflectionParameter $parameter
65
     * @return string
66
     */
67 5
    private function writeParameter(IReflectionMethod $method, IReflectionParameter $parameter)
68
    {
69 5
        $parameterString = $parameter->getName();
70
71 5
        if ($parameter->getClassName()) {
72 1
            $parameterString .= ' : ' . $this->formatClassName($parameter->getClassName());
73 1
        }
74
        else {
75 5
            preg_match('/\*\h+@param\h+([^\h]+)\h+\$' . preg_quote($parameterString). '\s/', (string) $method->getDocComment(), $matches);
76 5
            if (isset($matches[1])) {
77 2
                $parameterString .= ' : ' . $this->formatClassName($matches[1]);
78 2
            }
79
        }
80
81 5
        if ($parameter->isOptional() && $parameter->isDefaultValueAvailable()) {
82 2
            $parameterString .= ' = ' . $this->formatValue($parameter->getDefaultValue());
83 2
        }
84
85 5
        return $parameterString;
86
    }
87
88
    /**
89
     * @param \TokenReflection\IReflectionMethod $method
90
     * @return string
91
     */
92 13 View Code Duplication
    private function writeReturnType(IReflectionMethod $method)
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...
93
    {
94 13
        $returnType = '';
95 13
        preg_match('/\*\h+@return\h+([^\h]+)/', (string) $method->getDocComment(), $matches);
96 13
        if (isset($matches[1])) {
97 4
            $returnType = $matches[1];
98 4
            if ($method->getDeclaringClass()) {
99 2
                $returnType = $this->expandNamespaceAlias($method->getDeclaringClass(), $returnType);
100 2
            }
101 4
            $returnType = ' : ' . $this->formatClassName($returnType);
102 4
        }
103 13
        return $returnType;
104
    }
105
}
106