Passed
Push — master ( e1f86a...4e1a3a )
by Siad
05:23
created

ProjectHandler::init()   F

Complexity

Conditions 20
Paths 2081

Size

Total Lines 94
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 44
CRAP Score 26.5703

Importance

Changes 0
Metric Value
cc 20
eloc 62
nc 2081
nop 2
dl 0
loc 94
ccs 44
cts 59
cp 0.7458
crap 26.5703
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
namespace Phing\Parser;
21
22
use Phing\Parser\ElementHandler;
23
use Phing\Parser\ExpatParseException;
24
use Phing\Parser\ExpatParser;
25
use Phing\Parser\AbstractHandler;
26
use Phing\Parser\TargetHandler;
27
use Phing\Util\StringHelper;
28
use Phing\Io\File;
29
use Phing\Parser\XmlContext;
30
use Phing\Parser\ProjectConfigurator;
31
32
/**
33
 * Handler class for the <project> XML element This class handles all elements
34
 * under the <project> element.
35
 *
36
 * @author    Andreas Aderhold <[email protected]>
37
 * @copyright (c) 2001,2002 THYRELL. All rights reserved
38
 * @package   phing.parser
39
 */
40
class ProjectHandler extends AbstractHandler
41
{
42
43
    /**
44
     * The phing project configurator object.
45
     *
46
     * @var ProjectConfigurator
47
     */
48
    private $configurator;
49
50
    /**
51
     * @var XmlContext
52
     */
53
    private $context;
54
55
    /**
56
     * Constructs a new ProjectHandler
57
     *
58
     * @param ExpatParser $parser the ExpatParser object
59
     * @param AbstractHandler $parentHandler the parent handler that invoked this handler
60
     * @param ProjectConfigurator $configurator the ProjectConfigurator object
61
     * @param XmlContext $context
62
     */
63 837
    public function __construct(
64
        ExpatParser $parser,
65
        AbstractHandler $parentHandler,
66
        ProjectConfigurator $configurator,
67
        XmlContext $context
68
    ) {
69 837
        parent::__construct($parser, $parentHandler);
70
71 837
        $this->configurator = $configurator;
72 837
        $this->context = $context;
73 837
    }
74
75
    /**
76
     * Executes initialization actions required to setup the project. Usually
77
     * this method handles the attributes of a tag.
78
     *
79
     * @param string $tag the tag that comes in
80
     * @param array $attrs attributes the tag carries
81
     * @throws ExpatParseException if attributes are incomplete or invalid
82
     */
83 837
    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

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