Passed
Push — master ( 808df2...02b674 )
by Michiel
11:42
created

FileSizeTask::setUnit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
 * FileSizeTask
22
 *
23
 * Returns the size of a file
24
 *
25
 * @author  Johan Persson <[email protected]>
26
 * @package phing.tasks.ext
27
 */
28
class FileSizeTask extends Task
29
{
30
    const UNITS = ['B', 'K', 'M', 'G', 'T', 'P'];
31
32
    /**
33
     * Property for File
34
     *
35
     * @var PhingFile file
36
     */
37
    private $file;
38
39
    /**
40
     * Property where the file size will be stored
41
     *
42
     * @var string
43
     */
44
    private $propertyName = "filesize";
45
46
    /**
47
     * Return size in this unit
48
     *
49
     * @var string
50
     */
51
    private $unit = self::UNITS[0];
52
53
    /**
54
     * Which file to calculate the file size of
55
     *
56
     * @param PhingFile $file
57
     */
58 22
    public function setFile(PhingFile $file)
59
    {
60 22
        if (!$file->canRead()) {
61 1
            throw new BuildException(sprintf('Input file does not exist or is not readable: %s', $file->getName()));
62
        }
63 21
        $this->file = $file;
64 21
    }
65
66
    /**
67
     * Set the name of the property to store the file size
68
     *
69
     * @param  $property
70
     * @return void
71
     */
72 9
    public function setPropertyName(string $property)
73
    {
74 9
        if (empty($property)) {
75 1
            throw new BuildException('Property name cannot be empty');
76
        }
77 8
        $this->propertyName = $property;
78 8
    }
79
80 11
    public function setUnit(string $originalUnit)
81
    {
82 11
        $unit = strtoupper($originalUnit);
83 11
        if (!in_array($unit, self::UNITS)) {
84 2
            throw new BuildException(sprintf('Invalid unit: %s', $originalUnit));
85
        }
86 9
        $this->unit = $unit;
87 9
    }
88
89
    /**
90
     * Main-Method for the Task
91
     *
92
     * @return void
93
     * @throws BuildException
94
     */
95 19
    public function main()
96
    {
97 19
        if (!($this->file instanceof PhingFile)) {
0 ignored issues
show
introduced by
$this->file is always a sub-type of PhingFile.
Loading history...
98 1
            throw new BuildException('Input file not specified');
99
        }
100
101 18
        $size = filesize($this->file);
102
103 18
        if ($size === false) {
104
            throw new BuildException(sprintf('Cannot determine size of file: %s', $this->file));
105
        }
106 18
        $this->log(sprintf('%s %s', $size, self::UNITS[0]), Project::MSG_VERBOSE);
107
108 18
        $size = $size / pow(1024, array_search($this->unit, self::UNITS, true));
0 ignored issues
show
Bug introduced by
It seems like array_search($this->unit, self::UNITS, true) can also be of type false and string; however, parameter $exp of pow() does only seem to accept double|integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

108
        $size = $size / pow(1024, /** @scrutinizer ignore-type */ array_search($this->unit, self::UNITS, true));
Loading history...
109
110 18
        if (is_float($size)) {
0 ignored issues
show
introduced by
The condition is_float($size) is always false.
Loading history...
111 6
            $size = round($size, 2);
112
        }
113 18
        $this->log(sprintf('%s %s', $size, $this->unit), Project::MSG_INFO);
114
115
        // publish hash value
116 18
        $this->project->setProperty($this->propertyName, $size);
117 18
    }
118
}
119