Completed
Push — master ( d52b96...e2f758 )
by Michiel
11:35
created

SourceFileScanner::restrict()   D

Complexity

Conditions 18
Paths 64

Size

Total Lines 97
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 28.8171

Importance

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

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 49
    public function __construct($task)
47
    {
48 49
        $this->task = $task;
49 49
        $this->fileUtils = new FileUtils();
50 49
    }
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 32
    public function restrict(&$files, $srcDir, $destDir, $mapper, $force = false)
68
    {
69 32
        $now = time();
70 32
        $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 32
        $osname = strtolower(Phing::getProperty('os.name'));
78
79
        // indexOf()
80 32
        $index = ((($res = strpos($osname, 'win')) === false) ? -1 : $res);
81 32
        if ($index >= 0) {
82
            $now += 2000;
83
        }
84
85 32
        $v = [];
86
87 32
        for ($i = 0, $size = count($files); $i < $size; $i++) {
88 31
            $targets = $mapper->main($files[$i]);
89 31
            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 31
            $src = null;
95
            try {
96 31
                if ($srcDir === null) {
97
                    $src = new PhingFile($files[$i]);
98
                } else {
99 31
                    $src = $this->fileUtils->resolveFile($srcDir, $files[$i]);
100
                }
101
102 31
                if ($src->lastModified() > $now) {
103
                    $this->task->log(
104
                        "Warning: " . $files[$i] . " modified in the future (" . $src->lastModified() . " > " . $now . ")",
105 31
                        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 31
            $added = false;
114 31
            $targetList = "";
115
116 31
            for ($j = 0, $_j = count($targets); (!$added && $j < $_j); $j++) {
117 31
                $dest = null;
118 31
                if ($destDir === null) {
119 3
                    $dest = new PhingFile($targets[$j]);
120
                } else {
121 28
                    $dest = $this->fileUtils->resolveFile($destDir, $targets[$j]);
122
                }
123
124 31
                if (!$dest->exists()) {
125 29
                    $this->task->log(
126 29
                        ($files[$i] ?: ".") . " added as " . $dest->__toString() . " doesn't exist.",
127 29
                        Project::MSG_VERBOSE
128
                    );
129 29
                    $v[] = $files[$i];
130 29
                    $added = true;
131 19
                } elseif ($src->lastModified() > $dest->lastModified()) {
132
                    $this->task->log(
133
                        $files[$i] . " added as " . $dest->__toString() . " is outdated.",
134
                        Project::MSG_VERBOSE
135
                    );
136
                    $v[] = $files[$i];
137
                    $added = true;
138 19
                } 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 19
                    if (strlen($targetList) > 0) {
147
                        $targetList .= ", ";
148
                    }
149 19
                    $targetList .= $dest->getAbsolutePath();
150
                }
151
            }
152
153 31
            if (!$added) {
154 19
                $this->task->log(
155 19
                    $files[$i] . " omitted as " . $targetList . " " . (count(
156
                        $targets
157 19
                    ) === 1 ? " is " : " are ") . "up to date.",
158 19
                    Project::MSG_VERBOSE
159
                );
160
            }
161
        }
162
163 32
        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