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

Dirname   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 48
ccs 12
cts 15
cp 0.8
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setProperty() 0 3 1
A main() 0 11 3
A setFile() 0 6 2
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\Io\File;
22
use Phing\Task;
23
24
/**
25
 * Determines the directory name of the specified file.
26
 *
27
 * This task can accept the following attributes:
28
 * <ul>
29
 * <li>file
30
 * <li>property
31
 * </ul>
32
 * Both <b>file</b> and <b>property</b> are required.
33
 * <p>
34
 * When this task executes, it will set the specified property to the
35
 * value of the specified file up to, but not including, the last path
36
 * element. If file is a file, the directory will be the current
37
 * directory.
38
 *
39
 * @author  Siad Ardroumli <[email protected]>
40
 * @package phing.tasks.system
41
 */
42
class Dirname extends Task
43
{
44
    /**
45
     * @var File $file
46
     */
47
    private $file;
48
    private $property;
49
50
    /**
51
     * Path to take the dirname of.
52
     *
53
     * @param string|File file a <code>File</code> value
54
     */
55 1
    public function setFile($file)
56
    {
57 1
        if ($file instanceof File) {
58
            $this->file = $file;
59
        } else {
60 1
            $this->file = new File($file);
61
        }
62 1
    }
63
64
    /**
65
     * The name of the property to set.
66
     *
67
     * @param string $property the name of the property
68
     */
69 1
    public function setProperty($property)
70
    {
71 1
        $this->property = $property;
72 1
    }
73
74
    /**
75
     * Execute this task.
76
     *
77
     * @throws BuildException on error
78
     */
79 1
    public function main()
80
    {
81 1
        if ($this->property == null) {
82
            throw new BuildException("property attribute required", $this->getLocation());
83
        }
84 1
        if ($this->file == null) {
85
            throw new BuildException("file attribute required", $this->getLocation());
86
        }
87
88 1
        $value = $this->file->getAbsoluteFile()->getParent();
89 1
        $this->getProject()->setNewProperty($this->property, $value);
90 1
    }
91
}
92