Passed
Push — master ( fedd27...f10ad5 )
by Michiel
11:13
created

ElementHandler::init()   B

Complexity

Conditions 6
Paths 46

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6.1893

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 46
nop 2
dl 0
loc 35
ccs 19
cts 23
cp 0.8261
crap 6.1893
rs 8.9137
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
 * The generic element handler class.
22
 *
23
 * This class handles the occurrence of runtime registered tags like
24
 * datatypes (fileset, patternset, etc) and it's possible nested tags. It
25
 * introspects the implementation of the class and sets up the data structures.
26
 *
27
 * @author    Michiel Rook <[email protected]>
28
 * @copyright 2001,2002 THYRELL. All rights reserved
29
 * @package   phing.parser
30
 */
31
class ElementHandler extends AbstractHandler
32
{
33
34
    /**
35
     * Reference to the parent object that represents the parent tag
36
     * of this nested element
37
     *
38
     * @var object
39
     */
40
    private $parent;
41
42
    /**
43
     * Reference to the child object that represents the child tag
44
     * of this nested element
45
     *
46
     * @var UnknownElement
47
     */
48
    private $child;
49
50
    /**
51
     *  Reference to the parent wrapper object
52
     *
53
     * @var RuntimeConfigurable
54
     */
55
    private $parentWrapper;
56
57
    /**
58
     *  Reference to the child wrapper object
59
     *
60
     * @var RuntimeConfigurable
61
     */
62
    private $childWrapper;
63
64
    /**
65
     *  Reference to the related target object
66
     *
67
     * @var Target the target instance
68
     */
69
    private $target;
70
71
    /**
72
     * @var ProjectConfigurator
73
     */
74
    private $configurator;
75
76
    /**
77
     *  Constructs a new NestedElement handler and sets up everything.
78
     *
79
     * @param AbstractSAXParser $parser the ExpatParser object
80
     * @param AbstractHandler $parentHandler the parent handler that invoked this handler
81
     * @param ProjectConfigurator $configurator the ProjectConfigurator object
82
     * @param UnknownElement $parent the parent object this element is contained in
83
     * @param RuntimeConfigurable $parentWrapper the parent wrapper object
84
     * @param Target $target the target object this task is contained in
85
     */
86 784
    public function __construct(
87
        AbstractSAXParser $parser,
88
        AbstractHandler $parentHandler,
89
        ProjectConfigurator $configurator,
90
        UnknownElement $parent = null,
91
        RuntimeConfigurable $parentWrapper = null,
92
        Target $target = null
93
    ) {
94 784
        parent::__construct($parser, $parentHandler);
95 784
        $this->configurator = $configurator;
96 784
        if ($parentWrapper != null) {
97 495
            $this->parent = $parentWrapper->getProxy();
98
        } else {
99 784
            $this->parent = $parent;
100
        }
101 784
        $this->parentWrapper = $parentWrapper;
102 784
        $this->target = $target;
103 784
    }
104
105
    /**
106
     * Executes initialization actions required to setup the data structures
107
     * related to the tag.
108
     * <p>
109
     * This includes:
110
     * <ul>
111
     * <li>creation of the nested element</li>
112
     * <li>calling the setters for attributes</li>
113
     * <li>adding the element to the container object</li>
114
     * <li>adding a reference to the element (if id attribute is given)</li>
115
     * </ul>
116
     *
117
     * @param  string $tag the tag that comes in
118
     * @param  array $attrs attributes the tag carries
119
     * @throws ExpatParseException if the setup process fails
120
     */
121 784
    public function init($tag, $attrs)
122
    {
123 784
        $configurator = $this->configurator;
124 784
        $project = $this->configurator->project;
125
126
        try {
127 784
            $this->child = new UnknownElement(strtolower($tag));
128 784
            $this->child->setTaskName($tag);
129 784
            $this->child->setTaskType($tag);
130 784
            $this->child->setProject($project);
131 784
            $this->child->setLocation($this->parser->getLocation());
0 ignored issues
show
Bug introduced by
The method getLocation() does not exist on AbstractSAXParser. Since it exists in all sub-types, consider adding an abstract or default implementation to AbstractSAXParser. ( Ignorable by Annotation )

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

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