Passed
Push — main ( 95b369...5c384e )
by Michiel
17:35 queued 12s
created

TarTask::main()   D

Complexity

Conditions 19
Paths 75

Size

Total Lines 88
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 380

Importance

Changes 0
Metric Value
eloc 53
dl 0
loc 88
ccs 0
cts 56
cp 0
rs 4.5166
c 0
b 0
f 0
cc 19
nc 75
nop 0
crap 380

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Task\Ext;
22
23
use Archive_Tar;
24
use PEAR;
25
use Phing\Exception\BuildException;
26
use Phing\Io\IOException;
27
use Phing\Io\File;
28
use Phing\Io\SourceFileScanner;
29
use Phing\Mapper\MergeMapper;
30
use Phing\Project;
31
use Phing\Task\System\MatchingTask;
32
use Phing\Type\FileSet;
33
34
/**
35
 * Creates a tar archive using PEAR Archive_Tar.
36
 *
37
 * @author Hans Lellelid <[email protected]> (Phing)
38
 * @author Stefano Mazzocchi <[email protected]> (Ant)
39
 * @author Stefan Bodewig <[email protected]> (Ant)
40
 * @author Magesh Umasankar
41
 *
42
 * @package phing.tasks.ext
43
 */
44
class TarTask extends MatchingTask
45
{
46
    public const TAR_NAMELEN = 100;
47
48
    public const WARN = "warn";
49
    public const FAIL = "fail";
50
    public const OMIT = "omit";
51
52
    /**
53
     * @var File
54
     */
55
    private $tarFile;
56
57
    /**
58
     * @var File
59
     */
60
    private $baseDir;
61
62
    private $includeEmpty = true; // Whether to include empty dirs in the TAR
63
64
    private $longFileMode = "warn";
65
66
    /**
67
     * @var TarFileSet[]
68
     */
69
    private $filesets = [];
70
71
    /**
72
     * Indicates whether the user has been warned about long files already.
73
     */
74
    private $longWarningGiven = false;
0 ignored issues
show
introduced by
The private property $longWarningGiven is not used, and could be removed.
Loading history...
75
76
    /**
77
     * Compression mode.  Available options "gzip", "bzip2", "none" (null).
78
     */
79
    private $compression = null;
80
81
    /**
82
     * File path prefix in the tar archive
83
     *
84
     * @var string
85
     */
86
    private $prefix = null;
87
88
    /**
89
     * Ensures that PEAR lib exists.
90
     */
91
    public function init()
92
    {
93
        if (!class_exists('Archive_Tar')) {
94
            throw new BuildException("You must have installed the pear/archive_tar package use TarTask.");
95
        }
96
    }
97
98
    /**
99
     * Add a new fileset
100
     *
101
     * @return FileSet
102
     */
103
    public function createTarFileSet()
104
    {
105
        $this->fileset = new TarFileSet();
106
        $this->filesets[] = $this->fileset;
107
108
        return $this->fileset;
109
    }
110
111
    /**
112
     * Add a new fileset.  Alias to createTarFileSet() for backwards compatibility.
113
     *
114
     * @return FileSet
115
     * @see    createTarFileSet()
116
     */
117
    public function createFileSet()
118
    {
119
        $this->fileset = new TarFileSet();
120
        $this->filesets[] = $this->fileset;
121
122
        return $this->fileset;
123
    }
124
125
    /**
126
     * Set is the name/location of where to create the tar file.
127
     *
128
     * @param File $destFile The output of the tar
129
     */
130
    public function setDestFile(File $destFile)
131
    {
132
        $this->tarFile = $destFile;
133
    }
134
135
    /**
136
     * This is the base directory to look in for things to tar.
137
     *
138
     * @param File $baseDir
139
     */
140
    public function setBasedir(File $baseDir)
141
    {
142
        $this->baseDir = $baseDir;
143
    }
144
145
    /**
146
     * Set the include empty dirs flag.
147
     *
148
     * @param boolean $bool Flag if empty dirs should be tarred too
149
     *
150
     * @return void
151
     */
152
    public function setIncludeEmptyDirs($bool)
153
    {
154
        $this->includeEmpty = (bool) $bool;
155
    }
156
157
    /**
158
     * Set how to handle long files, those with a path&gt;100 chars.
159
     * Optional, default=warn.
160
     * <p>
161
     * Allowable values are
162
     * <ul>
163
     * <li>  truncate - paths are truncated to the maximum length
164
     * <li>  fail - paths greater than the maximim cause a build exception
165
     * <li>  warn - paths greater than the maximum cause a warning and GNU is used
166
     * <li>  gnu - GNU extensions are used for any paths greater than the maximum.
167
     * <li>  omit - paths greater than the maximum are omitted from the archive
168
     * </ul>
169
     *
170
     * @param $mode
171
     */
172
    public function setLongfile($mode)
173
    {
174
        $this->longFileMode = $mode;
175
    }
176
177
    /**
178
     * Set compression method.
179
     * Allowable values are
180
     * <ul>
181
     * <li>  none - no compression
182
     * <li>  gzip - Gzip compression
183
     * <li>  bzip2 - Bzip2 compression
184
     * </ul>
185
     *
186
     * @param string $mode
187
     */
188
    public function setCompression($mode)
189
    {
190
        switch ($mode) {
191
            case "gzip":
192
                $this->compression = "gz";
193
                break;
194
            case "bzip2":
195
                $this->compression = "bz2";
196
                break;
197
            case "lzma2":
198
                $this->compression = "lzma2";
199
                break;
200
            case "none":
201
                $this->compression = null;
202
                break;
203
            default:
204
                $this->log("Ignoring unknown compression mode: " . $mode, Project::MSG_WARN);
205
                $this->compression = null;
206
        }
207
    }
208
209
    /**
210
     * Sets the file path prefix for file in the tar file.
211
     *
212
     * @param string $prefix Prefix
213
     *
214
     * @return void
215
     */
216
    public function setPrefix($prefix)
217
    {
218
        $this->prefix = $prefix;
219
    }
220
221
    /**
222
     * do the work
223
     *
224
     * @throws BuildException
225
     */
226
    public function main()
227
    {
228
        if ($this->tarFile === null) {
229
            throw new BuildException("tarfile attribute must be set!", $this->getLocation());
230
        }
231
232
        if ($this->tarFile->exists() && $this->tarFile->isDirectory()) {
233
            throw new BuildException("tarfile is a directory!", $this->getLocation());
234
        }
235
236
        if ($this->tarFile->exists() && !$this->tarFile->canWrite()) {
237
            throw new BuildException("Can not write to the specified tarfile!", $this->getLocation());
238
        }
239
240
        // shouldn't need to clone, since the entries in filesets
241
        // themselves won't be modified -- only elements will be added
242
        $savedFileSets = $this->filesets;
243
244
        try {
245
            if ($this->baseDir !== null) {
246
                if (!$this->baseDir->exists()) {
247
                    throw new BuildException(
248
                        "basedir '" . (string) $this->baseDir . "' does not exist!",
249
                        $this->getLocation()
250
                    );
251
                }
252
                if (empty($this->filesets)) { // if there weren't any explicit filesets specivied, then
253
                    // create a default, all-inclusive fileset using the specified basedir.
254
                    $mainFileSet = new TarFileSet($this->fileset);
255
                    $mainFileSet->setDir($this->baseDir);
256
                    $mainFileSet->setProject($this->project);
257
                    $this->filesets[] = $mainFileSet;
258
                }
259
            }
260
261
            if (empty($this->filesets)) {
262
                throw new BuildException(
263
                    "You must supply either a basedir "
264
                    . "attribute or some nested filesets.",
265
                    $this->getLocation()
266
                );
267
            }
268
269
            // check if tar is out of date with respect to each fileset
270
            if ($this->tarFile->exists() && $this->isArchiveUpToDate()) {
271
                $this->log("Nothing to do: " . $this->tarFile->__toString() . " is up to date.", Project::MSG_INFO);
272
                return;
273
            }
274
275
            $this->log("Building tar: " . $this->tarFile->__toString(), Project::MSG_INFO);
276
277
            $tar = new Archive_Tar($this->tarFile->getAbsolutePath(), $this->compression);
278
            $pear = new PEAR();
279
280
            if ($pear::isError($tar->error_object)) {
281
                throw new BuildException($tar->error_object->getMessage());
282
            }
283
284
            foreach ($this->filesets as $fs) {
285
                $files = $fs->getIterator($this->includeEmpty);
286
                if (count($files) > 1 && strlen($fs->getFullpath()) > 0) {
287
                    throw new BuildException(
288
                        "fullpath attribute may only "
289
                        . "be specified for "
290
                        . "filesets that specify a "
291
                        . "single file."
292
                    );
293
                }
294
                $fsBasedir = $fs->getDir($this->project);
295
                $filesToTar = [];
296
                for ($i = 0, $fcount = count($files); $i < $fcount; $i++) {
297
                    $f = new File($fsBasedir, $files[$i]);
298
                    $filesToTar[] = $f->getAbsolutePath();
299
                    $this->log("Adding file " . $f->getPath() . " to archive.", Project::MSG_VERBOSE);
300
                }
301
                $tar->addModify($filesToTar, $this->prefix, $fsBasedir->getAbsolutePath());
302
303
                if ($pear::isError($tar->error_object)) {
304
                    throw new BuildException($tar->error_object->getMessage());
305
                }
306
            }
307
        } catch (IOException $ioe) {
308
            $msg = "Problem creating TAR: " . $ioe->getMessage();
309
            $this->filesets = $savedFileSets;
310
            throw new BuildException($msg, $ioe, $this->getLocation());
311
        }
312
313
        $this->filesets = $savedFileSets;
314
    }
315
316
    /**
317
     * @param  \ArrayIterator $files array of filenames
318
     * @param  File $dir
319
     *
320
     * @return boolean
321
     */
322
    protected function areFilesUpToDate($files, $dir)
323
    {
324
        $sfs = new SourceFileScanner($this);
325
        $mm = new MergeMapper();
326
        $mm->setTo($this->tarFile->getAbsolutePath());
327
328
        return count($sfs->restrict($files, $dir, null, $mm)) == 0;
329
    }
330
331
    /**
332
     * @return bool
333
     * @throws BuildException
334
     */
335
    private function isArchiveUpToDate()
336
    {
337
        foreach ($this->filesets as $fs) {
338
            $files = $fs->getIterator($this->includeEmpty);
339
            if (!$this->areFilesUpToDate($files, $fs->getDir($this->project))) {
340
                return false;
341
            }
342
            for ($i = 0, $fcount = count($files); $i < $fcount; $i++) {
343
                if ($this->tarFile->equals(new File($fs->getDir($this->project), $files[$i]))) {
344
                    throw new BuildException("A tar file cannot include itself", $this->getLocation());
345
                }
346
            }
347
        }
348
        return true;
349
    }
350
}
351