Passed
Push — master ( b5ca42...8fe651 )
by Siad
05:05
created

IncludePathTask   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 89
ccs 24
cts 36
cp 0.6667
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateIncludePath() 0 28 4
A setMode() 0 7 2
A main() 0 18 4
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
use Phing\Exception\BuildException;
21
use Phing\Phing;
22
use Phing\Project;
23
use Phing\Task;
24
25
/**
26
 * Adds a normalized path to the PHP include_path.
27
 *
28
 * This provides a way to alter the include_path without editing any global php.ini settings
29
 * or PHP_CLASSPATH environment variable.
30
 *
31
 * <code>
32
 *   <includepath classpath="new/path/here"/>
33
 * </code>
34
 *
35
 * @author  Hans Lellelid <[email protected]>
36
 * @package phing.tasks.system
37
 */
38
class IncludePathTask extends Task
39
{
40
    use ClasspathAware;
41
42
    /**
43
     * Classname of task to register.
44
     * This can be a dot-path -- relative to a location on PHP include_path.
45
     * E.g. path.to.MyClass ->  path/to/MyClass.php
46
     *
47
     * @var string
48
     */
49
    private $classname;
0 ignored issues
show
introduced by
The private property $classname is not used, and could be removed.
Loading history...
50
51
    /**
52
     * Whether to prepend, append or replace the include path
53
     *
54
     * @var string
55
     */
56
    private $mode = "prepend";
57
58
    /**
59
     * @param $mode
60
     * @throws BuildException
61
     */
62
    public function setMode($mode)
63
    {
64
        if (!in_array($mode, ['append', 'prepend', 'replace'])) {
65
            throw new BuildException("Illegal mode: needs to be either append, prepend or replace");
66
        }
67
68
        $this->mode = $mode;
69
    }
70
71
    /**
72
     * Main entry point
73
     */
74 14
    public function main()
75
    {
76
77
        // Apparently casting to (string) no longer invokes __toString() automatically.
78 14
        if (is_object($this->classpath)) {
79 14
            $classpath = $this->classpath->__toString();
80
        }
81
82 14
        if (empty($classpath)) {
83
            throw new BuildException("Provided classpath was empty.");
84
        }
85
86 14
        $curr_parts = Phing::explodeIncludePath();
87 14
        $add_parts = Phing::explodeIncludePath($classpath);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $classpath does not seem to be defined for all execution paths leading up to this point.
Loading history...
88 14
        $new_parts = array_diff($add_parts, $curr_parts);
89
90 14
        if ($new_parts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $new_parts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
91 1
            $this->updateIncludePath($new_parts, $curr_parts);
92
        }
93 14
    }
94
95
    /**
96
     * @param $new_parts
97
     * @param $curr_parts
98
     */
99 1
    private function updateIncludePath($new_parts, $curr_parts)
100
    {
101 1
        $includePath = [];
102 1
        $verb = "";
103
104 1
        switch ($this->mode) {
105 1
            case "append":
106
                $includePath = array_merge($curr_parts, $new_parts);
107
                $verb = "Appending";
108
                break;
109
110 1
            case "replace":
111
                $includePath = $new_parts;
112
                $verb = "Replacing";
113
                break;
114
115 1
            case "prepend":
116 1
                $includePath = array_merge($new_parts, $curr_parts);
117 1
                $verb = "Prepending";
118 1
                break;
119
        }
120
121 1
        $this->log(
122 1
            $verb . " new include_path components: " . implode(PATH_SEPARATOR, $new_parts),
123 1
            Project::MSG_VERBOSE
124
        );
125
126 1
        set_include_path(implode(PATH_SEPARATOR, $includePath));
127 1
    }
128
}
129