MethodWriter::writeElements()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
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