Passed
Push — master ( ef6ec0...e80653 )
by Michiel
08:36
created

ProjectHandler::init()   F

Complexity

Conditions 20
Paths 2081

Size

Total Lines 94
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 45
CRAP Score 25.345

Importance

Changes 0
Metric Value
cc 20
eloc 62
nc 2081
nop 2
dl 0
loc 94
ccs 45
cts 59
cp 0.7627
crap 25.345
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * Handler class for the <project> XML element This class handles all elements
22
 * under the <project> element.
23
 *
24
 * @author    Andreas Aderhold <[email protected]>
25
 * @copyright (c) 2001,2002 THYRELL. All rights reserved
26
 * @package   phing.parser
27
 */
28
class ProjectHandler extends AbstractHandler
29
{
30
31
    /**
32
     * The phing project configurator object.
33
     *
34
     * @var ProjectConfigurator
35
     */
36
    private $configurator;
37
38
    /**
39
     * @var PhingXMLContext
40
     */
41
    private $context;
42
43
    /**
44
     * Constructs a new ProjectHandler
45
     *
46
     * @param ExpatParser $parser the ExpatParser object
47
     * @param AbstractHandler $parentHandler the parent handler that invoked this handler
48
     * @param ProjectConfigurator $configurator the ProjectConfigurator object
49
     * @param PhingXMLContext $context
50
     */
51 755
    public function __construct(
52
        ExpatParser $parser,
53
        AbstractHandler $parentHandler,
54
        ProjectConfigurator $configurator,
55
        PhingXMLContext $context
56
    ) {
57 755
        parent::__construct($parser, $parentHandler);
58
59 755
        $this->configurator = $configurator;
60 755
        $this->context = $context;
61 755
    }
62
63
    /**
64
     * Executes initialization actions required to setup the project. Usually
65
     * this method handles the attributes of a tag.
66
     *
67
     * @param  string $tag the tag that comes in
68
     * @param  array $attrs attributes the tag carries
69
     * @throws ExpatParseException if attributes are incomplete or invalid
70
     */
71 755
    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

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