Completed
Push — master ( d75c1f...eed677 )
by Viacheslav
12s
created

PhpFunction::renderPhpDoc()   C

Complexity

Conditions 9
Paths 32

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 9.648

Importance

Changes 0
Metric Value
cc 9
eloc 14
nc 32
nop 0
dl 0
loc 23
ccs 12
cts 15
cp 0.8
crap 9.648
rs 5.8541
c 0
b 0
f 0
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 7
    public function __construct($name, $visibility = null, $isStatic = false)
38
    {
39 7
        $this->name = $name;
40 7
        $this->visibility = $visibility;
41 7
        $this->isStatic = $isStatic;
42 7
    }
43
44 7
    protected function toString()
45
    {
46 7
        $tail = '';
47 7
        if ($this->skipCodeCoverage) {
48 4
            $tail = (new PhpDocTag(PhpDoc::TAG_CODE_COVERAGE_IGNORE_END))->render() . "\n";
49
        }
50
51 7
        $body = trim($this->body);
52 7
        if ($body !== '') {
53 5
            $body = $this->indentLines($body . "\n");
54
        }
55
        return <<<PHP
56 7
{$this->headToString()}
57
{
58 7
{$body}}
59 7
$tail
60
61
PHP;
62
    }
63
64 7
    public function headToString()
65
    {
66
        return <<<PHP
67 7
{$this->renderPhpDoc()}{$this->renderVisibility()}{$this->renderIsStatic()}function {$this->name}({$this->renderArguments()})
68
PHP;
69
70
71
    }
72
73 7
    private function renderPhpDoc()
74
    {
75 7
        $result = new PhpDoc();
76 7
        if ($this->description) {
77 1
            $result->add(null, $this->description);
78
        }
79 7
        foreach ($this->arguments as $argument) {
80 6
            $result->add(PhpDoc::TAG_PARAM, $argument->renderPhpDocValue(true));
81
        }
82 7
        if ($this->result && $returnType = $this->result->renderPhpDocType()) {
83 5
            $result->add(PhpDoc::TAG_RETURN, $returnType);
84
        }
85 7
        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
            foreach ($this->throws as $throws) {
87
                if ($throwsType = $throws->renderPhpDocType()) {
88
                    $result->add(PhpDoc::TAG_THROWS, $throwsType);
89
                }
90
            }
91
        }
92 7
        if ($this->skipCodeCoverage) {
93 4
            $result->add(PhpDoc::TAG_CODE_COVERAGE_IGNORE_START);
94
        }
95 7
        return (string)$result;
96
    }
97
98 7
    private function renderArguments()
99
    {
100 7
        $result = '';
101 7
        foreach ($this->arguments as $argument) {
102 6
            $result .= "{$argument->renderArgumentType()}\${$argument->getName()}{$argument->renderDefault()}, ";
103
        }
104 7
        if ($result) {
105 6
            $result = substr($result, 0, -2);
106
        }
107 7
        return $result;
108
    }
109
110
111 6
    public function addArgument(PhpNamedVar $argument)
112
    {
113 6
        $this->arguments[] = $argument;
114 6
        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 5
    public function setBody($body)
130
    {
131 5
        $this->body = $body;
132 5
        return $this;
133
    }
134
135
    /**
136
     * @param PhpAnyType|null $result
137
     * @return PhpFunction
138
     */
139 5
    public function setResult(PhpAnyType $result = null)
140
    {
141 5
        $this->result = $result;
142 5
        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
    public function addThrows($throws)
160
    {
161
        $this->throws []= $throws;
162
        return $this;
163
    }
164
165
166
167
}