ProjectHandler::init()   F
last analyzed

Complexity

Conditions 20
Paths 2081

Size

Total Lines 94
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 44
CRAP Score 27.588

Importance

Changes 0
Metric Value
eloc 62
c 0
b 0
f 0
dl 0
loc 94
ccs 44
cts 60
cp 0.7332
rs 0
cc 20
nc 2081
nop 2
crap 27.588

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
/**
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 905
    public function __construct(
55
        ExpatParser $parser,
56
        AbstractHandler $parentHandler,
57
        ProjectConfigurator $configurator,
58
        XmlContext $context
59
    ) {
60 905
        parent::__construct($parser, $parentHandler);
61
62 905
        $this->configurator = $configurator;
63 905
        $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 905
    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 905
        $def = null;
78 905
        $name = null;
79 905
        $id = null;
80 905
        $desc = null;
81 905
        $baseDir = null;
82 905
        $ver = null;
83 905
        $strict = null;
84
85
        // some shorthands
86 905
        $project = $this->configurator->project;
87 905
        $buildFileParent = $this->configurator->buildFileParent;
88
89 905
        foreach ($attrs as $key => $value) {
90 905
            if ('default' === $key) {
91 905
                $def = $value;
92 896
            } elseif ('name' === $key) {
93 896
                $name = $value;
94 99
            } elseif ('id' === $key) {
95
                $id = $value;
96 99
            } elseif ('basedir' === $key) {
97 99
                $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 905
        if (null == $name) {
110 18
            $name = basename($this->configurator->getBuildFile());
111
        }
112
113 905
        $canonicalName = self::canonicalName($name);
114 905
        $this->configurator->setCurrentProjectName($canonicalName);
115 905
        $path = (string) $this->configurator->getBuildFile();
116 905
        $project->setUserProperty("phing.file.{$canonicalName}", $path);
117 905
        $project->setUserProperty("phing.dir.{$canonicalName}", dirname($path));
118
119 905
        if ($this->configurator->isIgnoringProjectTag()) {
120 101
            return;
121
        }
122
123 905
        if (null === $def) {
124
            throw new ExpatParseException(
125
                'The default attribute of project is required'
126
            );
127
        }
128
129 905
        $project->setDefaultTarget($def);
130
131 905
        if (null !== $name) {
132 905
            $project->setName($name);
133 905
            $project->addReference($name, $project);
134
        }
135
136 905
        if (null !== $id) {
137
            $project->addReference($id, $project);
138
        }
139
140 905
        if (null !== $desc) {
141
            $project->setDescription($desc);
142
        }
143
144 905
        if (null !== $ver) {
145
            $project->setPhingVersion($ver);
146
        }
147
148 905
        if (null !== $strict) {
149
            $project->setStrictMode(StringHelper::booleanValue($strict));
150
        }
151
152 905
        if (null !== $project->getProperty('project.basedir')) {
153 26
            $project->setBasedir($project->getProperty('project.basedir'));
154
        } else {
155 905
            if (null === $baseDir) {
156 807
                $project->setBasedir($buildFileParent->getAbsolutePath());
157
            } else {
158
                // check whether the user has specified an absolute path
159 99
                $f = new File($baseDir);
160 99
                if ($f->isAbsolute()) {
161
                    $project->setBasedir($baseDir);
162
                } else {
163 99
                    $project->setBaseDir($project->resolveFile($baseDir, $buildFileParent));
164
                }
165
            }
166
        }
167
168 905
        $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 905
    public function startElement($name, $attrs)
181
    {
182 905
        $project = $this->configurator->project;
183 905
        $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 905
        if ('target' === $name || 'extension-point' === $name) {
186 905
            $tf = new TargetHandler($this->parser, $this, $this->configurator, $this->context);
187 905
            $tf->init($name, $attrs);
188
        } else {
189 527
            $tf = new ElementHandler(
190 527
                $this->parser,
191 527
                $this,
192 527
                $this->configurator,
193 527
                null,
194 527
                null,
195 527
                $this->context->getImplicitTarget()
196 527
            );
197 527
            $tf->init($name, $attrs);
198
        }
199
    }
200
201
    /**
202
     * @param $name
203
     */
204 905
    public static function canonicalName($name)
205
    {
206 905
        return preg_replace('/\W/', '_', strtolower($name));
207
    }
208
}
209