Completed
Push — master ( 489ae1...d5799e )
by Siad
06:35
created

MkdirTask   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 81
ccs 20
cts 28
cp 0.7143
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A main() 0 26 6
A setMode() 0 3 1
A setDir() 0 3 1
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
 * Task to create a directory.
22
 *
23
 * @author  Andreas Aderhold, [email protected]
24
 * @package phing.tasks.system
25
 */
26
class MkdirTask extends Task
27
{
28
29
    /**
30
     * Directory to create.
31
     *
32
     * @var PhingFile $dir
33
     */
34
    private $dir;
35
36
    /**
37
     * Mode to create directory with
38
     *
39
     * @var integer
40
     */
41
    private $mode = 0;
42
43
    /**
44
     * Sets up this object internal stuff. i.e. the default mode.
45
     */
46 121
    public function __construct()
47
    {
48 121
        parent::__construct();
49 121
        $this->mode = 0777 - umask();
50 121
    }
51
52
    /**
53
     * create the directory and all parents
54
     *
55
     * @throws BuildException if dir is somehow invalid, or creation failed.
56
     */
57 121
    public function main()
58
    {
59 121
        if ($this->dir === null) {
60
            throw new BuildException("dir attribute is required", $this->getLocation());
61
        }
62 121
        if ($this->dir->isFile()) {
63
            throw new BuildException(
64
                "Unable to create directory as a file already exists with that name: " . $this->dir->getAbsolutePath()
65
            );
66
        }
67 121
        if (!$this->dir->exists()) {
68 116
            $result = $this->dir->mkdirs($this->mode);
69 116
            if (!$result) {
70
                if ($this->dir->exists()) {
71
                    $this->log("A different process or task has already created " . $this->dir->getAbsolutePath());
72
73
                    return;
74
                }
75
                $msg = "Directory " . $this->dir->getAbsolutePath() . " creation was not successful for an unknown reason";
76
                throw new BuildException($msg, $this->getLocation());
77
            }
78 116
            $this->log("Created dir: " . $this->dir->getAbsolutePath());
79
        } else {
80 11
            $this->log(
81 11
                'Skipping ' . $this->dir->getAbsolutePath() . ' because it already exists.',
82 11
                Project::MSG_VERBOSE
83
            );
84
        }
85 121
    }
86
87
    /**
88
     * The directory to create; required.
89
     *
90
     * @param  PhingFile $dir
91
     * @return void
92
     */
93 121
    public function setDir(PhingFile $dir)
94
    {
95 121
        $this->dir = $dir;
96 121
    }
97
98
    /**
99
     * Sets mode to create directory with
100
     *
101
     * @param  mixed $mode
102
     * @return void
103
     */
104 1
    public function setMode($mode)
105
    {
106 1
        $this->mode = base_convert((int) $mode, 8, 10);
0 ignored issues
show
Documentation Bug introduced by
The property $mode was declared of type integer, but base_convert((int)$mode, 8, 10) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
107 1
    }
108
}
109