Completed
Push — master ( 50d944...054c72 )
by Siad
12:51
created

SourceFileScanner::restrict()   D

Complexity

Conditions 18
Paths 64

Size

Total Lines 97
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 44
CRAP Score 23.3219

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 18
eloc 62
c 1
b 1
f 0
nc 64
nop 5
dl 0
loc 97
ccs 44
cts 59
cp 0.7458
crap 23.3219
rs 4.8666

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
 * 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
 *  Utility class that collects the functionality of the various
22
 *  scanDir methods that have been scattered in several tasks before.
23
 *
24
 *  The only method returns an array of source files. The array is a
25
 *  subset of the files given as a parameter and holds only those that
26
 *  are newer than their corresponding target files.
27
 *
28
 * @package phing.util
29
 */
30
class SourceFileScanner
31
{
32
33
    /**
34
     * Instance of FileUtils
35
     */
36
    private $fileUtils;
37
38
    /**
39
     * Task this class is working for -- for logging purposes.
40
     */
41
    private $task;
42
43
    /**
44
     * @param Task $task The task we should log messages through
45
     */
46 50
    public function __construct($task)
47
    {
48 50
        $this->task = $task;
49 50
        $this->fileUtils = new FileUtils();
50 50
    }
51
52
    /**
53
     * Restrict the given set of files to those that are newer than
54
     * their corresponding target files.
55
     *
56
     * @param  iterable $files the original set of files
57
     * @param  PhingFile $srcDir all files are relative to this directory
58
     * @param  PhingFile $destDir target files live here. if null file names
59
     *                                returned by the mapper are assumed to be
60
     *                                absolute.
61
     * @param  FilenameMapper $mapper knows how to construct a target file names from
62
     *                source file names.
63
     * @param  bool $force Boolean that determines if the files should be
64
     *                                forced to be copied.
65
     * @return array
66
     */
67 33
    public function restrict(&$files, $srcDir, $destDir, $mapper, $force = false)
68
    {
69 33
        $now = time();
70 33
        $targetList = "";
0 ignored issues
show
Unused Code introduced by
The assignment to $targetList is dead and can be removed.
Loading history...
71
72
        /*
73
          If we're on Windows, we have to munge the time up to 2 secs to
74
          be able to check file modification times.
75
          (Windows has a max resolution of two secs for modification times)
76
        */
77 33
        $osname = strtolower(Phing::getProperty('os.name'));
78
79
        // indexOf()
80 33
        $index = ((($res = strpos($osname, 'win')) === false) ? -1 : $res);
81 33
        if ($index >= 0) {
82
            $now += 2000;
83
        }
84
85 33
        $v = [];
86
87 33
        for ($i = 0, $size = count($files); $i < $size; $i++) {
88 33
            $targets = $mapper->main($files[$i]);
89 33
            if (empty($targets)) {
90 3
                $this->task->log($files[$i] . " skipped - don't know how to handle it", Project::MSG_VERBOSE);
91 3
                continue;
92
            }
93
94 33
            $src = null;
95
            try {
96 33
                if ($srcDir === null) {
97
                    $src = new PhingFile($files[$i]);
98
                } else {
99 33
                    $src = $this->fileUtils->resolveFile($srcDir, $files[$i]);
100
                }
101
102 33
                if ($src->lastModified() > $now) {
103
                    $this->task->log(
104
                        "Warning: " . $files[$i] . " modified in the future (" . $src->lastModified() . " > " . $now . ")",
105
                        Project::MSG_WARN
106
                    );
107
                }
108
            } catch (IOException $ioe) {
109
                $this->task->log("Unable to read file " . $files[$i] . " (skipping): " . $ioe->getMessage());
110
                continue;
111
            }
112
113 33
            $added = false;
114 33
            $targetList = "";
115
116 33
            for ($j = 0, $_j = count($targets); (!$added && $j < $_j); $j++) {
117 33
                $dest = null;
118 33
                if ($destDir === null) {
119 3
                    $dest = new PhingFile($targets[$j]);
120
                } else {
121 30
                    $dest = $this->fileUtils->resolveFile($destDir, $targets[$j]);
122
                }
123
124 33
                if (!$dest->exists()) {
125 30
                    $this->task->log(
126 30
                        ($files[$i] ?: ".") . " added as " . $dest->__toString() . " doesn't exist.",
127 30
                        Project::MSG_VERBOSE
128
                    );
129 30
                    $v[] = $files[$i];
130 30
                    $added = true;
131 21
                } elseif ($src->lastModified() > $dest->lastModified()) {
132 1
                    $this->task->log(
133 1
                        $files[$i] . " added as " . $dest->__toString() . " is outdated.",
134 1
                        Project::MSG_VERBOSE
135
                    );
136 1
                    $v[] = $files[$i];
137 1
                    $added = true;
138 20
                } elseif ($force === true) {
139
                    $this->task->log(
140
                        $files[$i] . " added as " . $dest->__toString() . " is forced to be overwritten.",
141
                        Project::MSG_VERBOSE
142
                    );
143
                    $v[] = $files[$i];
144
                    $added = true;
145
                } else {
146 20
                    if (strlen($targetList) > 0) {
147
                        $targetList .= ", ";
148
                    }
149 20
                    $targetList .= $dest->getAbsolutePath();
150
                }
151
            }
152
153 33
            if (!$added) {
154 20
                $this->task->log(
155 20
                    $files[$i] . " omitted as " . $targetList . " " . (count(
156
                        $targets
157 20
                    ) === 1 ? " is " : " are ") . "up to date.",
158 20
                    Project::MSG_VERBOSE
159
                );
160
            }
161
        }
162
163 33
        return $v;
164
    }
165
166
    /**
167
     * Convenience layer on top of restrict that returns the source
168
     * files as PhingFile objects (containing absolute paths if srcDir is
169
     * absolute).
170
     *
171
     * @param  $files
172
     * @param  $srcDir
173
     * @param  $destDir
174
     * @param  $mapper
175
     * @return array
176
     */
177
    public function restrictAsFiles(&$files, &$srcDir, &$destDir, &$mapper)
178
    {
179
        $res = $this->restrict($files, $srcDir, $destDir, $mapper);
180
        $result = [];
181
        for ($i = 0, $resultsCount = count($res); $i < $resultsCount; $i++) {
182
            $result[$i] = new PhingFile($srcDir, $res[$i]);
183
        }
184
185
        return $result;
186
    }
187
}
188