Passed
Push — master ( a862c8...fef60d )
by Siad
05:54
created

DifferentSelector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 72
ccs 15
cts 20
cp 0.75
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setIgnoreContents() 0 3 1
A setIgnoreFileTimes() 0 3 1
B selectionTest() 0 31 7
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
 * This selector selects files against a mapped set of target files, selecting
22
 * all those files which are different.
23
 * Files with different lengths are deemed different
24
 * automatically
25
 * Files with identical timestamps are viewed as matching by
26
 * default, unless you specify otherwise.
27
 * Contents are compared if the lengths are the same
28
 * and the timestamps are ignored or the same,
29
 * except if you decide to ignore contents to gain speed.
30
 * <p>
31
 * This is a useful selector to work with programs and tasks that don't handle
32
 * dependency checking properly; Even if a predecessor task always creates its
33
 * output files, followup tasks can be driven off copies made with a different
34
 * selector, so their dependencies are driven on the absolute state of the
35
 * files, not a timestamp.
36
 * <p>
37
 * Clearly, however, bulk file comparisons is inefficient; anything that can
38
 * use timestamps is to be preferred. If this selector must be used, use it
39
 * over as few files as possible, perhaps following it with an &lt;uptodate;&gt
40
 * to keep the descendant routines conditional.
41
 */
42
class DifferentSelector extends MappingSelector
43
{
44
    /**
45
     * @var bool $ignoreFileTimes
46
     */
47
    private $ignoreFileTimes = true;
48
49
    /**
50
     * @var bool $ignoreContents
51
     */
52
    private $ignoreContents = false;
53
54
    /**
55
     * This flag tells the selector to ignore file times in the comparison
56
     *
57
     * @param bool $ignoreFileTimes if true ignore file times
58
     */
59 2
    public function setIgnoreFileTimes($ignoreFileTimes)
60
    {
61 2
        $this->ignoreFileTimes = $ignoreFileTimes;
62 2
    }
63
64
    /**
65
     * This flag tells the selector to ignore contents
66
     *
67
     * @param bool $ignoreContents if true ignore contents
68
     */
69 2
    public function setIgnoreContents($ignoreContents)
70
    {
71 2
        $this->ignoreContents = $ignoreContents;
72 2
    }
73
74
    /**
75
     * This test is our selection test that compared the file with the destfile.
76
     *
77
     * @param  PhingFile $srcfile the source file
78
     * @param  PhingFile $destfile the destination file
79
     * @return bool true if the files are different
80
     *
81
     * @throws BuildException
82
     */
83 2
    protected function selectionTest(PhingFile $srcfile, PhingFile $destfile)
84
    {
85
        try {
86
            // if either of them is missing, they are different
87 2
            if ($srcfile->exists() !== $destfile->exists()) {
88
                return true;
89
            }
90
91 2
            if ($srcfile->length() !== $destfile->length()) {
92
                // different size => different files
93
                return true;
94
            }
95
96 2
            if (!$this->ignoreFileTimes) {
97
                // different dates => different files
98 2
                if ($destfile->lastModified() !== $srcfile->lastModified()) {
99 1
                    return true;
100
                }
101
            }
102
103 2
            if (!$this->ignoreContents) {
104
                //here do a bulk comparison
105
                $fu = new FileUtils();
106
107 2
                return !$fu->contentEquals($srcfile, $destfile);
0 ignored issues
show
Bug Best Practice introduced by
The expression return ! $fu->contentEquals($srcfile, $destfile) returns the type boolean which is incompatible with the return type mandated by MappingSelector::selectionTest() of true.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
108
            }
109
        } catch (IOException $e) {
110
            throw new BuildException("while comparing $srcfile and $destfile", $e);
111
        }
112
113 2
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by MappingSelector::selectionTest() of true.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
114
    }
115
}
116