Passed
Push — master ( d68b8d...e5c614 )
by Siad
10:45
created

RuntimeConfigurable::setAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
/**
21
 *  Wrapper class that holds the attributes of a Task (or elements
22
 *  nested below that level) and takes care of configuring that element
23
 *  at runtime.
24
 *
25
 *  <strong>SMART-UP INLINE DOCS</strong>
26
 *
27
 * @author  Andreas Aderhold <[email protected]>
28
 * @author  Hans Lellelid <[email protected]>
29
 * @package phing
30
 */
31
class RuntimeConfigurable
32
{
33
    private $elementTag = null;
34
35
    /**
36
     * @var array $children
37
     */
38
    private $children = [];
39
40
    /**
41
     * @var object|Task $wrappedObject
42
     */
43
    private $wrappedObject = null;
44
45
    /**
46
     * @var array $attributes
47
     */
48
    private $attributes = [];
49
50
    /**
51
     * @var string $characters
52
     */
53
    private $characters = "";
54
55
    /**
56
     * @var bool $proxyConfigured
57
     */
58
    private $proxyConfigured = false;
59
60
    /**
61
     * @param Task|object $proxy
62
     * @param mixed $elementTag The element to wrap.
63
     */
64 779
    public function __construct($proxy, $elementTag)
65
    {
66 779
        $this->wrappedObject = $proxy;
67 779
        $this->elementTag = $elementTag;
68
69 779
        if ($proxy instanceof Task) {
70 778
            $proxy->setRuntimeConfigurableWrapper($this);
71
        }
72 779
    }
73
74
    /**
75
     * @return object|Task
76
     */
77 491
    public function getProxy()
78
    {
79 491
        return $this->wrappedObject;
80
    }
81
82
    /**
83
     * @param object|Task $proxy
84
     *
85
     * @return void
86
     */
87 641
    public function setProxy($proxy)
88
    {
89 641
        $this->wrappedObject = $proxy;
90 641
        $this->proxyConfigured = false;
91 641
    }
92
93
    /**
94
     * Set's the attributes for the wrapped element.
95
     *
96
     * @param array $attributes
97
     *
98
     * @return void
99
     */
100 778
    public function setAttributes($attributes)
101
    {
102 778
        $this->attributes = $attributes;
103 778
    }
104
105
    /**
106
     * Returns the AttributeList of the wrapped element.
107
     *
108
     * @return array
109
     */
110
    public function getAttributes()
111
    {
112
        return $this->attributes;
113
    }
114
115
    /**
116
     * Adds child elements to the wrapped element.
117
     *
118
     * @param RuntimeConfigurable $child
119
     *
120
     * @return void
121
     */
122 491
    public function addChild(RuntimeConfigurable $child)
123
    {
124 491
        $this->children[] = $child;
125 491
    }
126
127
    /**
128
     * Returns the child with index
129
     *
130
     * @param int $index
131
     *
132
     * @return RuntimeConfigurable
133
     */
134 405
    public function getChild($index)
135
    {
136 405
        return $this->children[(int) $index];
137
    }
138
139
    public function getChildren()
140
    {
141
        return $this->children;
142
    }
143
144
    /**
145
     * Add characters from #PCDATA areas to the wrapped element.
146
     *
147
     * @param string $data
148
     *
149
     * @return void
150
     */
151 672
    public function addText($data)
152
    {
153 672
        $this->characters .= (string) $data;
154 672
    }
155
156
    /**
157
     * Get the text content of this element. Various text chunks are
158
     * concatenated, there is no way (currently) of keeping track of
159
     * multiple fragments.
160
     *
161
     * @return string the text content of this element.
162
     */
163 5
    public function getText()
164
    {
165 5
        return (string) $this->characters;
166
    }
167
168
    public function getElementTag()
169
    {
170
        return $this->elementTag;
171
    }
172
173
    /**
174
     * Reconfigure the element, even if it has already been configured.
175
     *
176
     * @param Project $p the project instance for this configuration.
177
     */
178
    public function reconfigure(Project $p)
179
    {
180
        $this->proxyConfigured = false;
181
        $this->maybeConfigure($p);
182
    }
183
184
    /**
185
     * Configure the wrapped element and all children.
186
     *
187
     * @param Project $project
188
     *
189
     * @return void
190
     *
191
     * @throws BuildException
192
     * @throws Exception
193
     */
194 642
    public function maybeConfigure(Project $project)
195
    {
196 642
        if ($this->proxyConfigured) {
197 1
            return;
198
        }
199
200 642
        $id = null;
201
202 642
        if ($this->attributes || (isset($this->characters) && $this->characters !== '')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->attributes of type array 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...
203 635
            ProjectConfigurator::configure($this->wrappedObject, $this->attributes, $project);
204
205 634
            if (isset($this->attributes["id"])) {
206 65
                $id = $this->attributes["id"];
207
            }
208
209 634
            if ($this->characters !== '') {
210 437
                ProjectConfigurator::addText($project, $this->wrappedObject, $this->characters);
211
            }
212 634
            if ($id !== null) {
213 65
                $project->addReference($id, $this->wrappedObject);
214
            }
215
        }
216
217 641
        $this->proxyConfigured = true;
218 641
    }
219
220 5
    public function setAttribute(string $name, $value)
221
    {
222 5
        $this->attributes[$name] = $value;
223 5
    }
224
225 5
    public function removeAttribute(string $name)
226
    {
227 5
        unset($this->attributes[$name]);
228 5
    }
229
230 5
    public function setElementTag(string $name)
231
    {
232 5
        $this->elementTag = $name;
233 5
    }
234
235 636
    public function getId()
236
    {
237 636
        return $this->attributes['id'] ?? null;
238
    }
239
}
240