ElementHandler::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 8
c 1
b 1
f 0
dl 0
loc 17
rs 10
cc 2
nc 2
nop 6
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Parser;
22
23
use Phing\Exception\BuildException;
24
use Phing\RuntimeConfigurable;
25
use Phing\Target;
26
use Phing\UnknownElement;
27
28
/**
29
 * The generic element handler class.
30
 *
31
 * This class handles the occurrence of runtime registered tags like
32
 * datatypes (fileset, patternset, etc) and it's possible nested tags. It
33
 * introspects the implementation of the class and sets up the data structures.
34
 *
35
 * @author    Michiel Rook <[email protected]>
36
 * @copyright 2001,2002 THYRELL. All rights reserved
37
 */
38
class ElementHandler extends AbstractHandler
39
{
40
    /**
41
     * Reference to the parent object that represents the parent tag
42
     * of this nested element.
43
     *
44
     * @var object
45
     */
46
    private $parent;
47
48
    /**
49
     * Reference to the child object that represents the child tag
50
     * of this nested element.
51
     *
52
     * @var UnknownElement
53
     */
54
    private $child;
55
56
    /**
57
     *  Reference to the parent wrapper object.
58
     *
59
     * @var RuntimeConfigurable
60
     */
61
    private $parentWrapper;
62
63
    /**
64
     *  Reference to the child wrapper object.
65
     *
66
     * @var RuntimeConfigurable
67
     */
68
    private $childWrapper;
69
70
    /**
71
     *  Reference to the related target object.
72
     *
73
     * @var Target the target instance
74
     */
75
    private $target;
76
77
    /**
78
     * @var ProjectConfigurator
79
     */
80
    private $configurator;
81
82
    /**
83
     *  Constructs a new NestedElement handler and sets up everything.
84
     *
85
     * @param AbstractSAXParser   $parser        the ExpatParser object
86
     * @param AbstractHandler     $parentHandler the parent handler that invoked this handler
87
     * @param ProjectConfigurator $configurator  the ProjectConfigurator object
88
     * @param UnknownElement      $parent        the parent object this element is contained in
89
     * @param RuntimeConfigurable $parentWrapper the parent wrapper object
90
     * @param Target              $target        the target object this task is contained in
91
     */
92
    public function __construct(
93
        AbstractSAXParser $parser,
94
        AbstractHandler $parentHandler,
95
        ProjectConfigurator $configurator,
96
        ?UnknownElement $parent = null,
97
        ?RuntimeConfigurable $parentWrapper = null,
98
        ?Target $target = null
99
    ) {
100
        parent::__construct($parser, $parentHandler);
101
        $this->configurator = $configurator;
102
        if (null != $parentWrapper) {
103
            $this->parent = $parentWrapper->getProxy();
104
        } else {
105
            $this->parent = $parent;
106
        }
107
        $this->parentWrapper = $parentWrapper;
108
        $this->target = $target;
109
    }
110
111
    /**
112
     * Executes initialization actions required to setup the data structures
113
     * related to the tag.
114
     * <p>
115
     * This includes:
116
     * <ul>
117
     * <li>creation of the nested element</li>
118
     * <li>calling the setters for attributes</li>
119
     * <li>adding the element to the container object</li>
120
     * <li>adding a reference to the element (if id attribute is given)</li>
121
     * </ul>.
122
     *
123
     * @param string $tag   the tag that comes in
124
     * @param array  $attrs attributes the tag carries
125
     *
126
     * @throws ExpatParseException if the setup process fails
127
     */
128
    public function init($tag, $attrs)
129
    {
130
        $configurator = $this->configurator;
131
        $project = $this->configurator->project;
132
133
        try {
134
            $this->child = new UnknownElement(strtolower($tag));
135
            $this->child->setTaskName($tag);
136
            $this->child->setTaskType($tag);
137
            $this->child->setProject($project);
138
            $this->child->setLocation($this->parser->getLocation());
0 ignored issues
show
Bug introduced by
The method getLocation() does not exist on Phing\Parser\AbstractSAXParser. Since it exists in all sub-types, consider adding an abstract or default implementation to Phing\Parser\AbstractSAXParser. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
            $this->child->setLocation($this->parser->/** @scrutinizer ignore-call */ getLocation());
Loading history...
139
140
            if (null !== $this->target) {
141
                $this->child->setOwningTarget($this->target);
142
            }
143
144
            if (null !== $this->parent) {
145
                $this->parent->addChild($this->child);
146
            } elseif (null !== $this->target) {
147
                $this->target->addTask($this->child);
148
            }
149
150
            $configurator->configureId($this->child, $attrs);
151
152
            $this->childWrapper = new RuntimeConfigurable($this->child, $tag);
153
            $this->childWrapper->setAttributes($attrs);
154
155
            if (null !== $this->parentWrapper) {
156
                $this->parentWrapper->addChild($this->childWrapper);
157
            }
158
        } catch (BuildException $exc) {
159
            throw new ExpatParseException(
160
                "Error initializing nested element <{$tag}>",
161
                $exc,
162
                $this->parser->getLocation()
163
            );
164
        }
165
    }
166
167
    /**
168
     * Handles character data.
169
     *
170
     * @param string $data the CDATA that comes in
171
     *
172
     * @throws ExpatParseException if the CDATA could not be set-up properly
173
     */
174
    public function characters($data)
175
    {
176
        $this->childWrapper->addText($data);
177
    }
178
179
    /**
180
     * Checks for nested tags within the current one. Creates and calls
181
     * handlers respectively.
182
     *
183
     * @param string $name  the tag that comes in
184
     * @param array  $attrs attributes the tag carries
185
     */
186
    public function startElement($name, $attrs)
187
    {
188
        $eh = new ElementHandler(
189
            $this->parser,
190
            $this,
191
            $this->configurator,
192
            $this->child,
193
            $this->childWrapper,
194
            $this->target
195
        );
196
        $eh->init($name, $attrs);
197
    }
198
}
199