Passed
Push — main ( ddd1a4...b98cb8 )
by Michiel
08:47
created

PharDataTask::main()   B

Complexity

Conditions 8
Paths 55

Size

Total Lines 45
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9.5885

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 45
rs 8.4444
c 0
b 0
f 0
ccs 17
cts 24
cp 0.7083
cc 8
nc 55
nop 0
crap 9.5885
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
namespace Phing\Task\System;
21
22
use Exception;
23
use Phar;
24
use PharData;
25
use Phing\Exception\BuildException;
26
use Phing\Io\File;
27
use Phing\Project;
28
use Phing\Task\System\MatchingTask;
29
use Phing\Type\FileSet;
30
use UnexpectedValueException;
31
32
/**
33
 * Data task for {@link http://php.net/manual/en/class.phardata.php PharData class}.
34
 *
35
 * @package phing.tasks.ext
36
 * @author  Siad Ardroumli <[email protected]>
37
 */
38
class PharDataTask extends MatchingTask
39
{
40
    /**
41
     * @var File
42
     */
43
    private $destinationFile;
44
45
    /**
46
     * @var int
47
     */
48
    private $compression = Phar::NONE;
49
50
    /**
51
     * Base directory, from where local package paths will be calculated.
52
     *
53
     * @var File
54
     */
55
    private $baseDirectory;
56
57
    /**
58
     * @var FileSet[]
59
     */
60
    private $filesets = [];
61
62
    /**
63
     * @return FileSet
64
     */
65 7
    public function createFileSet()
66
    {
67 7
        $this->fileset = new FileSet();
68 7
        $this->filesets[] = $this->fileset;
69 7
        return $this->fileset;
70
    }
71
72
    /**
73
     * Compression type (gzip, bzip2, none) to apply to the packed files.
74
     *
75
     * @param string $compression
76
     */
77 4
    public function setCompression($compression)
78
    {
79
        /**
80
         * If we don't support passed compression, leave old one.
81
         */
82 4
        switch ($compression) {
83 4
            case 'gzip':
84 2
                $this->compression = Phar::GZ;
85 2
                break;
86 2
            case 'bzip2':
87 2
                $this->compression = Phar::BZ2;
88 2
                break;
89
            default:
90
                break;
91
        }
92 4
    }
93
94
    /**
95
     * Destination (output) file.
96
     *
97
     * @param File $destinationFile
98
     */
99 7
    public function setDestFile(File $destinationFile)
100
    {
101 7
        $this->destinationFile = $destinationFile;
102 7
    }
103
104
    /**
105
     * Base directory, which will be deleted from each included file (from path).
106
     * Paths with deleted basedir part are local paths in archive.
107
     *
108
     * @param File $baseDirectory
109
     */
110 6
    public function setBaseDir(File $baseDirectory)
111
    {
112 6
        $this->baseDirectory = $baseDirectory;
113 6
    }
114
115
    /**
116
     * @throws BuildException
117
     */
118 7
    public function main()
119
    {
120 7
        $this->checkPreconditions();
121
122
        try {
123 6
            $this->log(
124 6
                'Building archive: ' . $this->destinationFile->__toString(),
125 6
                Project::MSG_INFO
126
            );
127
128
            /**
129
             * Delete old archive, if exists.
130
             */
131 6
            if ($this->destinationFile->exists()) {
132
                $isDeleted = $this->destinationFile->delete();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $isDeleted is correct as $this->destinationFile->delete() targeting Phing\Io\File::delete() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
133
                if (!$isDeleted) {
0 ignored issues
show
introduced by
$isDeleted is defined implicitly as null, thus it is always evaluated to false.
Loading history...
134
                    $this->log("Could not delete destination file $this->destinationFile", Project::MSG_WARN);
135
                }
136
            }
137
138 6
            $pharData = new PharData($this->baseDirectory->getPath() . '/' . $this->destinationFile->getName());
139
140 6
            foreach ($this->filesets as $fileset) {
141 6
                $this->log(
142 6
                    'Adding specified files in ' . $fileset->getDir($this->project) . ' to archive',
143 6
                    Project::MSG_VERBOSE
144
                );
145
146 6
                $pharData->buildFromIterator($fileset->getIterator(), $fileset->getDir($this->project));
147
            }
148
149 6
            if ($this->compression !== Phar::NONE && $pharData->canCompress($this->compression)) {
150
                try {
151 4
                    $pharData->compress($this->compression);
152 2
                } catch (UnexpectedValueException $uve) {
153 2
                    $pharData->compressFiles($this->compression);
154
                }
155
156 6
                unset($pharData);
157
            }
158
        } catch (Exception $e) {
159
            throw new BuildException(
160
                'Problem creating archive: ' . $e->getMessage(),
161
                $e,
162
                $this->getLocation()
163
            );
164
        }
165 6
    }
166
167
    /**
168
     * @throws BuildException
169
     */
170 7
    private function checkPreconditions()
171
    {
172 7
        if (!extension_loaded('phar')) {
173
            throw new BuildException(
174
                "PharDataTask require either PHP 5.3 or better or the PECL's Phar extension"
175
            );
176
        }
177
178 7
        if (null === $this->destinationFile) {
179
            throw new BuildException("destfile attribute must be set!", $this->getLocation());
180
        }
181
182 7
        if ($this->destinationFile->exists() && $this->destinationFile->isDirectory()) {
183
            throw new BuildException("destfile is a directory!", $this->getLocation());
184
        }
185
186 7
        if (!$this->destinationFile->canWrite()) {
187
            throw new BuildException("Can not write to the specified destfile!", $this->getLocation());
188
        }
189
190 7
        if (null === $this->baseDirectory) {
191 1
            throw new BuildException("basedir cattribute must be set", $this->getLocation());
192
        }
193
194 6
        if (!$this->baseDirectory->exists()) {
195
            throw new BuildException(
196
                "basedir '" . (string) $this->baseDirectory . "' does not exist!",
197
                $this->getLocation()
198
            );
199
        }
200 6
    }
201
}
202