Passed
Push — master ( 3a6c9c...7c86a5 )
by Siad
06:49
created

TruncateTask::setMkdirs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
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
 * @author  Siad Ardroumli <[email protected]>
22
 * @package phing.tasks.system
23
 */
24
class TruncateTask extends Task
25
{
26
    private $create = true;
27
    private $mkdirs = false;
28
29
    private $length;
30
    private $adjust;
31
    private $file;
32
33
    /**
34
     * Set a single target File.
35
     *
36
     * @param  PhingFile|string $f the single File
37
     * @throws \IOException
38
     * @throws \NullPointerException
39
     */
40 8
    public function setFile($f)
41
    {
42 8
        if (is_string($f)) {
43 8
            $f = new PhingFile($f);
44
        }
45 8
        $this->file = $f;
46 8
    }
47
48
    /**
49
     * Set the amount by which files' lengths should be adjusted.
50
     * It is permissible to append K / M / G / T / P.
51
     *
52
     * @param $adjust (positive or negative) adjustment amount.
0 ignored issues
show
Documentation Bug introduced by
The doc comment (positive at position 1 could not be parsed: Expected ')' at position 1, but found 'positive'.
Loading history...
53
     */
54 3
    public function setAdjust($adjust)
55
    {
56 3
        $this->adjust = $adjust;
57 3
    }
58
59
    /**
60
     * Set the length to which files should be set.
61
     * It is permissible to append K / M / G / T / P.
62
     *
63
     * @param $length (positive) adjustment amount.
0 ignored issues
show
Bug introduced by
The type positive was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
64
     *
65
     * @throws \BuildException
66
     */
67 8
    public function setLength($length)
68
    {
69 8
        $this->length = $length;
70 8
        if ($this->length !== null && $this->length < 0) {
71 1
            throw new BuildException('Cannot truncate to length ' . $this->length);
72
        }
73 7
    }
74
75
    /**
76
     * Set whether to create nonexistent files.
77
     *
78
     * @param boolean $create default <code>true</code>.
79
     */
80 1
    public function setCreate($create)
81
    {
82 1
        $this->create = $create;
83 1
    }
84
85
    /**
86
     * Set whether, when creating nonexistent files, nonexistent directories
87
     * should also be created.
88
     *
89
     * @param boolean $mkdirs default <code>false</code>.
90
     */
91 1
    public function setMkdirs($mkdirs)
92
    {
93 1
        $this->mkdirs = $mkdirs;
94 1
    }
95
96
    /**
97
     * {@inheritDoc}.
98
     *
99
     * @throws \BuildException
100
     */
101 8
    public function main()
102
    {
103 8
        if ($this->length !== null && $this->adjust !== null) {
104 1
            throw new BuildException(
105 1
                'length and adjust are mutually exclusive options'
106
            );
107
        }
108 7
        if ($this->length === null && $this->adjust === null) {
109 1
            $this->length = 0;
110
        }
111 7
        if ($this->file === null) {
112 1
            throw new BuildException('No files specified.');
113
        }
114
115 6
        if ($this->shouldProcess($this->file)) {
116 5
            $this->process($this->file);
117
        }
118 6
    }
119
120
    /**
121
     * @param PhingFile $f
122
     * @return bool
123
     * @throws \BuildException
124
     */
125 6
    private function shouldProcess(PhingFile $f)
126
    {
127 6
        if ($f->isFile()) {
128 2
            return true;
129
        }
130 6
        if (!$this->create) {
131 1
            return false;
132
        }
133 5
        $exception = null;
134
        try {
135
            /**
136
             * @var PhingFile $parent
137
             */
138 5
            $parent = $f->getParentFile();
139 5
            if ($this->mkdirs && !$parent->exists()) {
140 1
                $parent->mkdirs();
141
            }
142
143 5
            if ($f->createNewFile()) {
144 5
                return true;
145
            }
146
        } catch (IOException $e) {
147
            $exception = $e;
148
        }
149
        $msg = "Unable to create " . $f;
150
        if ($exception === null) {
151
            $this->log($msg, Project::MSG_WARN);
152
            return false;
153
        }
154
        throw new BuildException($msg, $exception);
155
    }
156
157 5
    private function process(PhingFile $f)
158
    {
159 5
        $len = $f->length();
160 5
        $newLength = $this->length ?? $len + $this->adjust;
161
162 5
        if ($len === $newLength) {
163
            //nothing to do!
164 1
            return;
165
        }
166
167 4
        $splFile = new SplFileObject($f->getPath(), 'a+');
168
169 4
        if (!$splFile->ftruncate((int) $newLength)) {
170
            throw new BuildException("Exception working with " . (string) $splFile);
171
        }
172
173 4
        $splFile->rewind();
174 4
        clearstatcache();
175 4
    }
176
}
177