Completed
Push — master ( 2e664e...522f88 )
by Siad
14:25
created

RuntimeConfigurable::maybeConfigure()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 36
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 12
nc 10
nop 1
dl 0
loc 36
ccs 13
cts 13
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
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 763
    public function __construct($proxy, $elementTag)
65
    {
66 763
        $this->wrappedObject = $proxy;
67 763
        $this->elementTag = $elementTag;
68
69 763
        if ($proxy instanceof Task) {
70 762
            $proxy->setRuntimeConfigurableWrapper($this);
71
        }
72 763
    }
73
74
    /**
75
     * @return object|Task
76
     */
77 475
    public function getProxy()
78
    {
79 475
        return $this->wrappedObject;
80
    }
81
82
    /**
83
     * @param object|Task $proxy
84
     *
85
     * @return void
86
     */
87 625
    public function setProxy($proxy)
88
    {
89 625
        $this->wrappedObject = $proxy;
90 625
        $this->proxyConfigured = false;
91 625
    }
92
93
    /**
94
     * Set's the attributes for the wrapped element.
95
     *
96
     * @param array $attributes
97
     *
98
     * @return void
99
     */
100 762
    public function setAttributes($attributes)
101
    {
102 762
        $this->attributes = $attributes;
103 762
    }
104
105
    /**
106
     * Returns the AttributeList of the wrapped element.
107
     *
108
     * @return array
109
     */
110 620
    public function getAttributes()
111
    {
112 620
        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 475
    public function addChild(RuntimeConfigurable $child)
123
    {
124 475
        $this->children[] = $child;
125 475
    }
126
127
    /**
128
     * Returns the child with index
129
     *
130
     * @param int $index
131
     *
132
     * @return RuntimeConfigurable
133
     */
134 379
    public function getChild($index)
135
    {
136 379
        return $this->children[(int) $index];
137
    }
138
139
    /**
140
     * Add characters from #PCDATA areas to the wrapped element.
141
     *
142
     * @param string $data
143
     *
144
     * @return void
145
     */
146 657
    public function addText($data)
147
    {
148 657
        $this->characters .= (string) $data;
149 657
    }
150
151
    /**
152
     * Get the text content of this element. Various text chunks are
153
     * concatenated, there is no way (currently) of keeping track of
154
     * multiple fragments.
155
     *
156
     * @return string the text content of this element.
157
     */
158 5
    public function getText()
159
    {
160 5
        return (string) $this->characters;
161
    }
162
163
    public function getElementTag()
164
    {
165
        return $this->elementTag;
166
    }
167
168
    /**
169
     * Configure the wrapped element and all children.
170
     *
171
     * @param Project $project
172
     *
173
     * @return void
174
     *
175
     * @throws BuildException
176
     * @throws Exception
177
     */
178 626
    public function maybeConfigure(Project $project)
179
    {
180 626
        if ($this->proxyConfigured) {
181 14
            return;
182
        }
183
184 626
        $id = null;
185
186
        // DataType configured in ProjectConfigurator
187
        //        if ( is_a($this->wrappedObject, "DataType") )
188
        //            return;
189
190 626
        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...
191 619
            ProjectConfigurator::configure($this->wrappedObject, $this->attributes, $project);
192
193 618
            if (isset($this->attributes["id"])) {
194 32
                $id = $this->attributes["id"];
195
            }
196
197 618
            if (isset($this->characters) && $this->characters != '') {
198 413
                ProjectConfigurator::addText($project, $this->wrappedObject, (string) $this->characters);
199
            }
200 618
            if ($id !== null) {
201 32
                $project->addReference($id, $this->wrappedObject);
202
            }
203
        }
204
205
        /*if ( is_array($this->children) && !empty($this->children) ) {
206
            // Configure all child of this object ...
207
            foreach ($this->children as $child) {
208
                $child->maybeConfigure($project);
209
                ProjectConfigurator::storeChild($project, $this->wrappedObject, $child->wrappedObject, strtolower($child->getElementTag()));
210
            }
211
        }*/
212
213 625
        $this->proxyConfigured = true;
214 625
    }
215
}
216