Passed
Push — master ( 5545f1...83deac )
by Kirill
03:22
created

Method::isStatic()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Reactor\Partial;
13
14
use Spiral\Reactor\AbstractDeclaration;
15
use Spiral\Reactor\Aggregator\Parameters;
16
use Spiral\Reactor\NamedInterface;
17
use Spiral\Reactor\ReplaceableInterface;
18
use Spiral\Reactor\Traits\AccessTrait;
19
use Spiral\Reactor\Traits\CommentTrait;
20
use Spiral\Reactor\Traits\NamedTrait;
21
22
/**
23
 * Represent class method.
24
 */
25
class Method extends AbstractDeclaration implements ReplaceableInterface, NamedInterface
26
{
27
    use NamedTrait;
28
    use CommentTrait;
29
    use AccessTrait;
30
31
    /** @var bool */
32
    private $static = false;
33
34
    /** @var string */
35
    private $return;
36
37
    /** @var Parameters */
38
    private $parameters;
39
40
    /** @var Source */
41
    private $source;
42
43
    /**
44
     * @param string       $name
45
     * @param string|array $source
46
     * @param string|array $comment
47
     */
48
    public function __construct(string $name, $source = '', $comment = '')
49
    {
50
        $this->setName($name);
51
        $this->parameters = new Parameters([]);
52
        $this->initSource($source);
53
        $this->initComment($comment);
54
    }
55
56
    /**
57
     * @param bool $static
58
     * @return self
59
     */
60
    public function setStatic(bool $static = true): Method
61
    {
62
        $this->static = $static;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param string $return
69
     * @return self
70
     */
71
    public function setReturn(string $return): Method
72
    {
73
        $this->return = $return;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return bool
80
     */
81
    public function isStatic(): bool
82
    {
83
        return $this->static;
84
    }
85
86
    /**
87
     * Rename to getSource()?
88
     *
89
     * @return Source
90
     */
91
    public function getSource(): Source
92
    {
93
        return $this->source;
94
    }
95
96
    /**
97
     * Set method source.
98
     *
99
     * @param string|array $source
100
     * @return self
101
     */
102
    public function setSource($source): Method
103
    {
104
        if (!empty($source)) {
105
            if (is_array($source)) {
106
                $this->source->setLines($source);
107
            } elseif (is_string($source)) {
0 ignored issues
show
introduced by
The condition is_string($source) is always true.
Loading history...
108
                $this->source->setString($source);
109
            }
110
        }
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return Parameters|Parameter[]
117
     */
118
    public function getParameters(): Parameters
119
    {
120
        return $this->parameters;
121
    }
122
123
    /**
124
     * @param string $name
125
     * @return Parameter
126
     */
127
    public function parameter(string $name): Parameter
128
    {
129
        return $this->parameters->get($name);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     * @return $this
135
     */
136
    public function replace($search, $replace): Method
137
    {
138
        $this->docComment->replace($search, $replace);
139
140
        return $this;
141
    }
142
143
    /**
144
     * @param int $indentLevel
145
     * @return string
146
     */
147
    public function render(int $indentLevel = 0): string
148
    {
149
        $result = '';
150
        if (!$this->docComment->isEmpty()) {
151
            $result .= $this->docComment->render($indentLevel) . "\n";
152
        }
153
154
        $method = $this->renderModifiers();
155
        if (!$this->parameters->isEmpty()) {
156
            $method .= "({$this->parameters->render()})";
157
        } else {
158
            $method .= '()';
159
        }
160
161
        if ($this->return) {
162
            $method .= ": {$this->return}";
163
        }
164
165
        $result .= $this->addIndent($method, $indentLevel) . "\n";
166
        $result .= $this->addIndent('{', $indentLevel) . "\n";
167
168
        if (!$this->source->isEmpty()) {
169
            $result .= $this->source->render($indentLevel + 1) . "\n";
170
        }
171
172
        $result .= $this->addIndent('}', $indentLevel);
173
174
        return $result;
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    private function renderModifiers(): string
181
    {
182
        $chunks = [$this->getAccess()];
183
184
        if ($this->isStatic()) {
185
            $chunks[] = 'static';
186
        }
187
188
        $chunks[] = "function {$this->getName()}";
189
190
        return implode(' ', $chunks);
191
    }
192
193
    /**
194
     * Init source value.
195
     *
196
     * @param string|array $source
197
     */
198
    private function initSource($source): void
199
    {
200
        if (empty($this->source)) {
201
            $this->source = new Source();
202
        }
203
204
        if (!empty($source)) {
205
            if (is_array($source)) {
206
                $this->source->setLines($source);
207
            } elseif (is_string($source)) {
0 ignored issues
show
introduced by
The condition is_string($source) is always true.
Loading history...
208
                $this->source->setString($source);
209
            }
210
        }
211
    }
212
}
213