Completed
Push — master ( 0f28ca...ccc575 )
by Siad
17:36 queued 13s
created

classes/phing/tasks/ext/phar/PharDataTask.php (2 issues)

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
require_once 'phing/tasks/system/MatchingTask.php';
21
22
/**
23
 * Data task for {@link http://php.net/manual/en/class.phardata.php PharData class}.
24
 *
25
 * @package phing.tasks.ext
26
 * @author  Siad Ardroumli <[email protected]>
27
 */
28
class PharDataTask extends MatchingTask
29
{
30
    /**
31
     * @var PhingFile
32
     */
33
    private $destinationFile;
34
35
    /**
36
     * @var int
37
     */
38
    private $compression = Phar::NONE;
39
40
    /**
41
     * Base directory, from where local package paths will be calculated.
42
     *
43
     * @var PhingFile
44
     */
45
    private $baseDirectory;
46
47
    /**
48
     * @var FileSet[]
49
     */
50
    private $filesets = [];
51
52
    /**
53
     * @return FileSet
54
     */
55 7
    public function createFileSet()
56
    {
57 7
        $this->fileset = new FileSet();
58 7
        $this->filesets[] = $this->fileset;
59 7
        return $this->fileset;
60
    }
61
62
    /**
63
     * Compression type (gzip, bzip2, none) to apply to the packed files.
64
     *
65
     * @param string $compression
66
     */
67 4
    public function setCompression($compression)
68
    {
69
        /**
70
         * If we don't support passed compression, leave old one.
71
         */
72 4
        switch ($compression) {
73 4
            case 'gzip':
74 2
                $this->compression = Phar::GZ;
75 2
                break;
76 2
            case 'bzip2':
77 2
                $this->compression = Phar::BZ2;
78 2
                break;
79
            default:
80
                break;
81
        }
82 4
    }
83
84
    /**
85
     * Destination (output) file.
86
     *
87
     * @param PhingFile $destinationFile
88
     */
89 7
    public function setDestFile(PhingFile $destinationFile)
90
    {
91 7
        $this->destinationFile = $destinationFile;
92 7
    }
93
94
    /**
95
     * Base directory, which will be deleted from each included file (from path).
96
     * Paths with deleted basedir part are local paths in archive.
97
     *
98
     * @param PhingFile $baseDirectory
99
     */
100 6
    public function setBaseDir(PhingFile $baseDirectory)
101
    {
102 6
        $this->baseDirectory = $baseDirectory;
103 6
    }
104
105
    /**
106
     * @throws BuildException
107
     */
108 7
    public function main()
109
    {
110 7
        $this->checkPreconditions();
111
112
        try {
113 6
            $this->log(
114 6
                'Building archive: ' . $this->destinationFile->__toString(),
115 6
                Project::MSG_INFO
116
            );
117
118
            /**
119
             * Delete old archive, if exists.
120
             */
121 6
            if ($this->destinationFile->exists()) {
122
                $isDeleted = $this->destinationFile->delete();
0 ignored issues
show
Are you sure the assignment to $isDeleted is correct as $this->destinationFile->delete() targeting PhingFile::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...
123
                if (!$isDeleted) {
0 ignored issues
show
$isDeleted is defined implicitly as null, thus it is always evaluated to false.
Loading history...
124
                    $this->log("Could not delete destination file $this->destinationFile", Project::MSG_WARN);
125
                }
126
            }
127
128 6
            $pharData = new PharData($this->baseDirectory->getPath() . '/' . $this->destinationFile->getName());
129
130 6
            foreach ($this->filesets as $fileset) {
131 6
                $this->log(
132 6
                    'Adding specified files in ' . $fileset->getDir($this->project) . ' to archive',
133 6
                    Project::MSG_VERBOSE
134
                );
135
136 6
                $pharData->buildFromIterator($fileset->getIterator(), $fileset->getDir($this->project));
137
            }
138
139 6
            if ($this->compression !== Phar::NONE && $pharData->canCompress($this->compression)) {
140
                try {
141 4
                    $pharData->compress($this->compression);
142 2
                } catch (UnexpectedValueException $uve) {
143 2
                    $pharData->compressFiles($this->compression);
144
                }
145
146 4
                unset($pharData);
147
            }
148
        } catch (Exception $e) {
149
            throw new BuildException(
150
                'Problem creating archive: ' . $e->getMessage(),
151
                $e,
152
                $this->getLocation()
153
            );
154
        }
155 6
    }
156
157
    /**
158
     * @throws BuildException
159
     */
160 7
    private function checkPreconditions()
161
    {
162 7
        if (!extension_loaded('phar')) {
163
            throw new BuildException(
164
                "PharDataTask require either PHP 5.3 or better or the PECL's Phar extension"
165
            );
166
        }
167
168 7
        if (null === $this->destinationFile) {
169
            throw new BuildException("destfile attribute must be set!", $this->getLocation());
170
        }
171
172 7
        if ($this->destinationFile->exists() && $this->destinationFile->isDirectory()) {
173
            throw new BuildException("destfile is a directory!", $this->getLocation());
174
        }
175
176 7
        if (!$this->destinationFile->canWrite()) {
177
            throw new BuildException("Can not write to the specified destfile!", $this->getLocation());
178
        }
179
180 7
        if (null === $this->baseDirectory) {
181 1
            throw new BuildException("basedir cattribute must be set", $this->getLocation());
182
        }
183
184 6
        if (!$this->baseDirectory->exists()) {
185
            throw new BuildException(
186
                "basedir '" . (string) $this->baseDirectory . "' does not exist!",
187
                $this->getLocation()
188
            );
189
        }
190 6
    }
191
}
192