Passed
Push — master ( 3a6c9c...7c86a5 )
by Siad
06:49
created

UnknownElement::handleChildren()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 18
nc 16
nop 2
dl 0
loc 29
ccs 19
cts 19
cp 1
crap 6
rs 9.0444
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 all information necessary to create a task
22
 * that did not exist when Phing started.
23
 *
24
 * <em> This has something to do with phing encountering an task XML element
25
 * it is not aware of at start time. This is a situation where special steps
26
 * need to be taken so that the element is then known.</em>
27
 *
28
 * @author  Andreas Aderhold <[email protected]>
29
 * @author  Hans Lellelid <[email protected]>
30
 * @package phing
31
 */
32
class UnknownElement extends Task
33
{
34
    private $elementName;
35
    private $realThing;
36
    private $children = [];
37
38
    /**
39
     * Constructs a UnknownElement object
40
     *
41
     * @param string  The XML element name that is unknown
42
     */
43 744
    public function __construct($elementName)
44
    {
45 744
        parent::__construct();
46 744
        $this->elementName = (string) $elementName;
47 744
    }
48
49
    /**
50
     * Return the XML element name that this <code>UnnownElement</code>
51
     * handles.
52
     *
53
     * @return string The XML element name that is unknown
54
     */
55 607
    public function getTag()
56
    {
57 607
        return (string) $this->elementName;
58
    }
59
60
    /**
61
     * Tries to configure the unknown element
62
     *
63
     * @throws BuildException if the element can not be configured
64
     */
65 607
    public function maybeConfigure()
66
    {
67 607
        $this->realThing = $this->makeObject($this, $this->wrapper);
68 607
        $this->wrapper->setProxy($this->realThing);
69 607
        if ($this->realThing instanceof Task) {
70 606
            $this->realThing->setRuntimeConfigurableWrapper($this->wrapper);
71 606
            $this->realThing->maybeConfigure();
72
        } else {
73 32
            $this->wrapper->maybeConfigure($this->getProject());
74
        }
75 606
        $this->handleChildren($this->realThing, $this->wrapper);
76 605
    }
77
78
    /**
79
     * Called when the real task has been configured for the first time.
80
     *
81
     * @throws BuildException if the task can not be created
82
     */
83 598
    public function main()
84
    {
85 598
        if ($this->realThing === null) {
86
            // plain impossible to get here, maybeConfigure should
87
            // have thrown an exception.
88
            throw new BuildException("Should not be executing UnknownElement::main() -- task/type: {$this->elementName}");
89
        }
90
91 598
        if ($this->realThing instanceof Task) {
92 597
            $this->realThing->main();
93
        }
94 570
    }
95
96
    /**
97
     * Add a child element to the unknown element
98
     *
99
     * @param    UnknownElement $child
100
     * @internal param The $object object representing the child element
101
     */
102 467
    public function addChild(UnknownElement $child)
103
    {
104 467
        $this->children[] = $child;
105 467
    }
106
107
    /**
108
     *  Handle child elemets of the unknown element, if any.
109
     *
110
     * @param object $parent The parent object the unknown element belongs to
111
     * @param object $parentWrapper The parent wrapper object
112
     */
113 606
    public function handleChildren($parent, $parentWrapper)
114
    {
115 606
        if ($parent instanceof TaskAdapter) {
116 217
            $parent = $parent->getProxy();
117
        }
118
119 606
        $parentClass = $parent === null ? get_class() : get_class($parent);
120 606
        $ih = IntrospectionHelper::getHelper($parentClass);
121
122 606
        for ($i = 0, $childrenCount = count($this->children); $i < $childrenCount; $i++) {
123 371
            $childWrapper = $parentWrapper->getChild($i);
124 371
            $child = $this->children[$i];
125
126 371
            $realChild = null;
127 371
            if ($parent instanceof TaskContainer) {
128 188
                $parent->addTask($child);
129 188
                continue;
130
            }
131
132 367
            $project = $this->project ?? $parent->project;
133 367
            $realChild = $ih->createElement($project, $parent, $child->getTag());
134
135 367
            $childWrapper->setProxy($realChild);
136 367
            if ($realChild instanceof Task) {
137 189
                $realChild->setRuntimeConfigurableWrapper($childWrapper);
138
            }
139
140 367
            $childWrapper->maybeConfigure($this->project);
141 365
            $child->handleChildren($realChild, $childWrapper);
142
        }
143 606
    }
144
145
    /**
146
     * Creates a named task or data type. If the real object is a task,
147
     * it is configured up to the init() stage.
148
     *
149
     * @param  UnknownElement $ue The unknown element to create the real object for.
150
     *                                 Must not be <code>null</code>.
151
     * @param  RuntimeConfigurable $w Ignored in this implementation.
152
     * @throws BuildException
153
     * @return object              The Task or DataType represented by the given unknown element.
154
     */
155 607
    protected function makeObject(UnknownElement $ue, RuntimeConfigurable $w)
156
    {
157 607
        $o = $this->makeTask($ue, $w, true);
158 607
        if ($o === null) {
159 32
            $o = $this->project->createDataType($ue->getTag());
160
        }
161 607
        if ($o === null) {
162
            throw new BuildException(
163
                "Could not create task/type: '" . $ue->getTag() . "'. Make sure that this class has been declared using taskdef / typedef."
164
            );
165
        }
166
167 607
        return $o;
168
    }
169
170
    /**
171
     *  Create a named task and configure it up to the init() stage.
172
     *
173
     * @param  UnknownElement $ue The unknwon element to create a task from
174
     * @param  RuntimeConfigurable $w The wrapper object
175
     * @param  boolean $onTopLevel Whether to treat this task as if it is top-level.
176
     * @throws BuildException
177
     * @return Task                The freshly created task
178
     */
179 607
    protected function makeTask(UnknownElement $ue, RuntimeConfigurable $w, $onTopLevel = false)
180
    {
181 607
        $task = $this->project->createTask($ue->getTag());
182
183 607
        if ($task === null) {
184 32
            if (!$onTopLevel) {
185
                throw new BuildException("Could not create task of type: '" . $this->elementName . "'. Make sure that this class has been declared using taskdef.");
186
            }
187
188 32
            return null;
189
        }
190
191
        // used to set the location within the xmlfile so that exceptions can
192
        // give detailed messages
193
194 606
        $task->setLocation($this->getLocation());
195 606
        $attrs = $w->getAttributes();
196 606
        if (isset($attrs['id'])) {
197 6
            $this->project->addReference($attrs['id'], $task);
198
        }
199
200 606
        if ($this->target !== null) {
201 606
            $task->setOwningTarget($this->target);
202
        }
203
204 606
        $task->init();
205
206 606
        return $task;
207
    }
208
209
    /**
210
     *  Get the name of the task to use in logging messages.
211
     *
212
     * @return string The task's name
213
     */
214 2
    public function getTaskName()
215
    {
216 2
        return $this->realThing === null || !$this->realThing instanceof Task
217 1
            ? parent::getTaskName()
218 2
            : $this->realThing->getTaskName();
219
    }
220
221
    /**
222
     * Return the configured object
223
     *
224
     * @return object the real thing whatever it is
225
     */
226 598
    public function getRealThing()
227
    {
228 598
        return $this->realThing;
229
    }
230
231
    /**
232
     * Set the configured object
233
     *
234
     * @param object $realThing the configured object
235
     */
236 570
    public function setRealThing($realThing)
237
    {
238 570
        $this->realThing = $realThing;
239 570
    }
240
}
241