Completed
Push — master ( 0e6e64...9192b1 )
by Viacheslav
12s
created

PhpFunction   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 91.07%

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 153
ccs 51
cts 56
cp 0.9107
rs 10
c 0
b 0
f 0
wmc 23

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setResult() 0 4 1
A setThrows() 0 4 1
A setBody() 0 4 1
A addArgument() 0 4 1
A getBody() 0 3 1
A headToString() 0 4 1
A __construct() 0 5 1
A renderArguments() 0 10 3
A toString() 0 16 3
B renderPhpDoc() 0 23 9
A addThrows() 0 4 1
1
<?php
2
3
namespace Swaggest\PhpCodeBuilder;
4
5
6
use Swaggest\PhpCodeBuilder\Traits\Description;
7
use Swaggest\PhpCodeBuilder\Traits\StaticFlag;
8
use Swaggest\PhpCodeBuilder\Traits\Visibility;
9
10
class PhpFunction extends PhpTemplate
11
{
12
    use Visibility;
13
    use StaticFlag;
14
    use Description;
15
16
    private $name;
17
18
    /** @var PhpNamedVar[] */
19
    private $arguments = array();
20
21
    /** @var PhpAnyType|null */
22
    private $result;
23
24
    /** @var PhpAnyType[] */
25
    private $throws;
26
27
    private $body;
28
    public $skipCodeCoverage = false;
29
30
31
    /**
32
     * PhpFunction constructor.
33
     * @param string $name
34
     * @param string|null $visibility
35
     * @param bool $isStatic
36
     */
37 13
    public function __construct($name, $visibility = null, $isStatic = false)
38
    {
39 13
        $this->name = $name;
40 13
        $this->visibility = $visibility;
41 13
        $this->isStatic = $isStatic;
42 13
    }
43
44 13
    protected function toString()
45
    {
46 13
        $tail = '';
47 13
        if ($this->skipCodeCoverage) {
48 6
            $tail = (new PhpDocTag(PhpDoc::TAG_CODE_COVERAGE_IGNORE_END))->render() . "\n";
49
        }
50
51 13
        $body = trim($this->body);
52 13
        if ($body !== '') {
53 11
            $body = $this->indentLines($body . "\n");
54
        }
55
        return <<<PHP
56 13
{$this->headToString()}
57
{
58 13
{$body}}
59 13
$tail
60
61
PHP;
62
    }
63
64 13
    public function headToString()
65
    {
66
        return <<<PHP
67 13
{$this->renderPhpDoc()}{$this->renderVisibility()}{$this->renderIsStatic()}function {$this->name}({$this->renderArguments()})
68
PHP;
69
70
71
    }
72
73 13
    private function renderPhpDoc()
74
    {
75 13
        $result = new PhpDoc();
76 13
        if ($this->description) {
77 1
            $result->add(null, $this->description);
78
        }
79 13
        foreach ($this->arguments as $argument) {
80 12
            $result->add(PhpDoc::TAG_PARAM, $argument->renderPhpDocValue(true));
81
        }
82 13
        if ($this->result && $returnType = $this->result->renderPhpDocType()) {
83 7
            $result->add(PhpDoc::TAG_RETURN, $returnType);
84
        }
85 13
        if ($this->throws) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->throws of type Swaggest\PhpCodeBuilder\PhpAnyType[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
86 4
            foreach ($this->throws as $throws) {
87 4
                if ($throwsType = $throws->renderPhpDocType()) {
88 4
                    $result->add(PhpDoc::TAG_THROWS, $throwsType);
89
                }
90
            }
91
        }
92 13
        if ($this->skipCodeCoverage) {
93 6
            $result->add(PhpDoc::TAG_CODE_COVERAGE_IGNORE_START);
94
        }
95 13
        return (string)$result;
96
    }
97
98 13
    private function renderArguments()
99
    {
100 13
        $result = '';
101 13
        foreach ($this->arguments as $argument) {
102 12
            $result .= "{$argument->renderArgumentType()}\${$argument->getName()}{$argument->renderDefault()}, ";
103
        }
104 13
        if ($result) {
105 12
            $result = substr($result, 0, -2);
106
        }
107 13
        return $result;
108
    }
109
110
111 12
    public function addArgument(PhpNamedVar $argument)
112
    {
113 12
        $this->arguments[] = $argument;
114 12
        return $this;
115
    }
116
117
    /**
118
     * @return mixed
119
     */
120
    public function getBody()
121
    {
122
        return $this->body;
123
    }
124
125
    /**
126
     * @param mixed $body
127
     * @return PhpFunction
128
     */
129 11
    public function setBody($body)
130
    {
131 11
        $this->body = $body;
132 11
        return $this;
133
    }
134
135
    /**
136
     * @param PhpAnyType|null $result
137
     * @return PhpFunction
138
     */
139 7
    public function setResult(PhpAnyType $result = null)
140
    {
141 7
        $this->result = $result;
142 7
        return $this;
143
    }
144
145
    /**
146
     * @param PhpAnyType $throws
147
     * @return PhpFunction
148
     */
149
    public function setThrows($throws)
150
    {
151
        $this->throws = array($throws);
152
        return $this;
153
    }
154
155
    /**
156
     * @param PhpAnyType $throws
157
     * @return PhpFunction
158
     */
159 4
    public function addThrows($throws)
160
    {
161 4
        $this->throws []= $throws;
162 4
        return $this;
163
    }
164
165
166
167
}