@@ -18,16 +18,17 @@
Loading
18 18
 */
19 19
20 20
/**
21 -
 * fileHash
21 +
 * FileSizeTask
22 22
 *
23 -
 * Calculate either MD5 or SHA hash value of a specified file and retun the
24 -
 * value in a property
23 +
 * Returns the size of a file
25 24
 *
26 25
 * @author  Johan Persson <johan162@gmail.com>
27 26
 * @package phing.tasks.ext
28 27
 */
29 28
class FileSizeTask extends Task
30 29
{
30 +
    const UNITS = ['B', 'K', 'M', 'G', 'T', 'P'];
31 +
31 32
    /**
32 33
     * Property for File
33 34
     *
@@ -38,17 +39,27 @@
Loading
38 39
    /**
39 40
     * Property where the file size will be stored
40 41
     *
41 -
     * @var string $property
42 +
     * @var string
42 43
     */
43 44
    private $propertyName = "filesize";
44 45
46 +
    /**
47 +
     * Return size in this unit
48 +
     *
49 +
     * @var string
50 +
     */
51 +
    private $unit = self::UNITS[0];
52 +
45 53
    /**
46 54
     * Which file to calculate the file size of
47 55
     *
48 56
     * @param PhingFile $file
49 57
     */
50 -
    public function setFile($file)
58 +
    public function setFile(PhingFile $file)
51 59
    {
60 +
        if (!$file->canRead()) {
61 +
            throw new BuildException(sprintf('Input file does not exist or is not readable: %s', $file->getName()));
62 +
        }
52 63
        $this->file = $file;
53 64
    }
54 65
@@ -58,11 +69,23 @@
Loading
58 69
     * @param  $property
59 70
     * @return void
60 71
     */
61 -
    public function setPropertyName($property)
72 +
    public function setPropertyName(string $property)
62 73
    {
74 +
        if (empty($property)) {
75 +
            throw new BuildException('Property name cannot be empty');
76 +
        }
63 77
        $this->propertyName = $property;
64 78
    }
65 79
80 +
    public function setUnit(string $originalUnit)
81 +
    {
82 +
        $unit = strtoupper($originalUnit);
83 +
        if (!in_array($unit, self::UNITS)) {
84 +
            throw new BuildException(sprintf('Invalid unit: %s', $originalUnit));
85 +
        }
86 +
        $this->unit = $unit;
87 +
    }
88 +
66 89
    /**
67 90
     * Main-Method for the Task
68 91
     *
@@ -71,58 +94,25 @@
Loading
71 94
     */
72 95
    public function main()
73 96
    {
74 -
        $this->checkFile();
75 -
        $this->checkPropertyName();
97 +
        if (!($this->file instanceof PhingFile)) {
98 +
            throw new BuildException('Input file not specified');
99 +
        }
76 100
77 101
        $size = filesize($this->file);
78 102
79 103
        if ($size === false) {
80 -
            throw new BuildException(sprintf('[FileSize] Cannot determine size of file: %s', $this->file));
104 +
            throw new BuildException(sprintf('Cannot determine size of file: %s', $this->file));
81 105
        }
106 +
        $this->log(sprintf('%s %s', $size, self::UNITS[0]), Project::MSG_VERBOSE);
82 107
83 -
        // publish hash value
84 -
        $this->project->setProperty($this->propertyName, $size);
85 -
    }
108 +
        $size = $size / pow(1024, array_search($this->unit, self::UNITS, true));
86 109
87 -
    /**
88 -
     * checks file attribute
89 -
     *
90 -
     * @return void
91 -
     * @throws BuildException
92 -
     */
93 -
    private function checkFile()
94 -
    {
95 -
        // check File
96 -
        if (
97 -
            $this->file === null
98 -
            || strlen($this->file) == 0
99 -
        ) {
100 -
            throw new BuildException('[FileSize] You must specify an input file.', $this->file);
110 +
        if (is_float($size)) {
111 +
            $size = round($size, 2);
101 112
        }
113 +
        $this->log(sprintf('%s %s', $size, $this->unit), Project::MSG_INFO);
102 114
103 -
        if (!is_readable($this->file)) {
104 -
            throw new BuildException(
105 -
                sprintf(
106 -
                    '[FileSize] Input file does not exist or is not readable: %s',
107 -
                    $this->file
108 -
                )
109 -
            );
110 -
        }
111 -
    }
112 -
113 -
    /**
114 -
     * checks property attribute
115 -
     *
116 -
     * @return void
117 -
     * @throws BuildException
118 -
     */
119 -
    private function checkPropertyName()
120 -
    {
121 -
        if (
122 -
            null === $this->propertyName
123 -
            || strlen($this->propertyName) === 0
124 -
        ) {
125 -
            throw new BuildException('[FileSize] Property name for publishing file size is not set');
126 -
        }
115 +
        // publish hash value
116 +
        $this->project->setProperty($this->propertyName, $size);
127 117
    }
128 118
}
Files Complexity Coverage
classes/phing 9,125 54.14%
Project Totals (468 files) 9125 54.14%
4798.10
7.3=.3
TRAVIS_OS_NAME=linux
1
ignore:
2
- "docs"
3
- "etc"
4
- "vendor"
5
coverage:
6
  status:
7
    patch: off
8
    project:
9
      default:
10
        threshold: 2
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading