ProjectHandler::canonicalName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Io\File;
24
use Phing\Util\StringHelper;
25
26
/**
27
 * Handler class for the <project> XML element This class handles all elements
28
 * under the <project> element.
29
 *
30
 * @author    Andreas Aderhold <[email protected]>
31
 * @copyright (c) 2001,2002 THYRELL. All rights reserved
32
 */
33
class ProjectHandler extends AbstractHandler
34
{
35
    /**
36
     * The phing project configurator object.
37
     *
38
     * @var ProjectConfigurator
39
     */
40
    private $configurator;
41
42
    /**
43
     * @var XmlContext
44
     */
45
    private $context;
46
47
    /**
48
     * Constructs a new ProjectHandler.
49
     *
50
     * @param ExpatParser         $parser        the ExpatParser object
51
     * @param AbstractHandler     $parentHandler the parent handler that invoked this handler
52
     * @param ProjectConfigurator $configurator  the ProjectConfigurator object
53
     */
54
    public function __construct(
55
        ExpatParser $parser,
56
        AbstractHandler $parentHandler,
57
        ProjectConfigurator $configurator,
58
        XmlContext $context
59
    ) {
60
        parent::__construct($parser, $parentHandler);
61
62
        $this->configurator = $configurator;
63
        $this->context = $context;
64
    }
65
66
    /**
67
     * Executes initialization actions required to setup the project. Usually
68
     * this method handles the attributes of a tag.
69
     *
70
     * @param string $tag   the tag that comes in
71
     * @param array  $attrs attributes the tag carries
72
     *
73
     * @throws ExpatParseException if attributes are incomplete or invalid
74
     */
75
    public function init($tag, $attrs)
0 ignored issues
show
Unused Code introduced by
The parameter $tag is not used and could be removed. ( Ignorable by Annotation )

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

75
    public function init(/** @scrutinizer ignore-unused */ $tag, $attrs)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
    {
77
        $def = null;
78
        $name = null;
79
        $id = null;
80
        $desc = null;
81
        $baseDir = null;
82
        $ver = null;
83
        $strict = null;
84
85
        // some shorthands
86
        $project = $this->configurator->project;
87
        $buildFileParent = $this->configurator->buildFileParent;
88
89
        foreach ($attrs as $key => $value) {
90
            if ('default' === $key) {
91
                $def = $value;
92
            } elseif ('name' === $key) {
93
                $name = $value;
94
            } elseif ('id' === $key) {
95
                $id = $value;
96
            } elseif ('basedir' === $key) {
97
                $baseDir = $value;
98
            } elseif ('description' === $key) {
99
                $desc = $value;
100
            } elseif ('phingVersion' === $key) {
101
                $ver = $value;
102
            } elseif ('strict' === $key) {
103
                $strict = $value;
104
            } else {
105
                throw new ExpatParseException("Unexpected attribute '{$key}'");
106
            }
107
        }
108
        // these things get done no matter what
109
        if (null == $name) {
110
            $name = basename($this->configurator->getBuildFile());
111
        }
112
113
        $canonicalName = self::canonicalName($name);
114
        $this->configurator->setCurrentProjectName($canonicalName);
115
        $path = (string) $this->configurator->getBuildFile();
116
        $project->setUserProperty("phing.file.{$canonicalName}", $path);
117
        $project->setUserProperty("phing.dir.{$canonicalName}", dirname($path));
118
119
        if ($this->configurator->isIgnoringProjectTag()) {
120
            return;
121
        }
122
123
        if (null === $def) {
124
            throw new ExpatParseException(
125
                'The default attribute of project is required'
126
            );
127
        }
128
129
        $project->setDefaultTarget($def);
130
131
        if (null !== $name) {
132
            $project->setName($name);
133
            $project->addReference($name, $project);
134
        }
135
136
        if (null !== $id) {
137
            $project->addReference($id, $project);
138
        }
139
140
        if (null !== $desc) {
141
            $project->setDescription($desc);
142
        }
143
144
        if (null !== $ver) {
145
            $project->setPhingVersion($ver);
146
        }
147
148
        if (null !== $strict) {
149
            $project->setStrictMode(StringHelper::booleanValue($strict));
150
        }
151
152
        if (null !== $project->getProperty('project.basedir')) {
153
            $project->setBasedir($project->getProperty('project.basedir'));
154
        } else {
155
            if (null === $baseDir) {
156
                $project->setBasedir($buildFileParent->getAbsolutePath());
157
            } else {
158
                // check whether the user has specified an absolute path
159
                $f = new File($baseDir);
160
                if ($f->isAbsolute()) {
161
                    $project->setBasedir($baseDir);
162
                } else {
163
                    $project->setBaseDir($project->resolveFile($baseDir, $buildFileParent));
164
                }
165
            }
166
        }
167
168
        $project->addTarget('', $this->context->getImplicitTarget());
169
    }
170
171
    /**
172
     * Handles start elements within the <project> tag by creating and
173
     * calling the required handlers for the detected element.
174
     *
175
     * @param string $name  the tag that comes in
176
     * @param array  $attrs attributes the tag carries
177
     *
178
     * @throws ExpatParseException if a unxepected element occurs
179
     */
180
    public function startElement($name, $attrs)
181
    {
182
        $project = $this->configurator->project;
183
        $types = $project->getDataTypeDefinitions();
0 ignored issues
show
Unused Code introduced by
The assignment to $types is dead and can be removed.
Loading history...
184
185
        if ('target' === $name || 'extension-point' === $name) {
186
            $tf = new TargetHandler($this->parser, $this, $this->configurator, $this->context);
187
            $tf->init($name, $attrs);
188
        } else {
189
            $tf = new ElementHandler(
190
                $this->parser,
191
                $this,
192
                $this->configurator,
193
                null,
194
                null,
195
                $this->context->getImplicitTarget()
196
            );
197
            $tf->init($name, $attrs);
198
        }
199
    }
200
201
    /**
202
     * @param $name
203
     */
204
    public static function canonicalName($name)
205
    {
206
        return preg_replace('/\W/', '_', strtolower($name));
207
    }
208
}
209