Passed
Push — master ( 06f87c...33b6d3 )
by Siad
08:23
created

SizeSelector::setUnits()   D

Complexity

Conditions 20
Paths 36

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 420

Importance

Changes 0
Metric Value
cc 20
eloc 22
nc 36
nop 1
dl 0
loc 27
ccs 0
cts 23
cp 0
crap 420
rs 4.1666
c 0
b 0
f 0

How to fix   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
 * Selector that filters files based on their size.
22
 *
23
 * @author Hans Lellelid <[email protected]> (Phing)
24
 * @author Bruce Atherton <[email protected]> (Ant)
25
 *
26
 * @package phing.types.selectors
27
 */
28
class SizeSelector extends BaseExtendSelector
29
{
30
    /**
31
     * @var float
32
     */
33
    private $bytes = -1;
34
35
    /**
36
     * @var string
37
     */
38
    private $value = '';
39
40
    /**
41
     * @var string 'less', 'equal' or 'more'.
42
     */
43
    private $when = self::WHEN[0];
44
45
    private const VALUE_KEY = 'value';
46
    private const WHEN_KEY  = 'when';
47
    private const WHEN      = [-1 => 'less',
48
                               0  => 'equal',
49
                               1  => 'more',];
50
51
    public function __toString(): string
52
    {
53
        $format = '{%s value: %s compare: %s}';
54
        return sprintf($format, __CLASS__, $this->value, $this->when);
55
    }
56
57
    /**
58
     * Filesize
59
     *
60
     * @param string $value Values like '1024', '5000B', '300M', '2G'.
61
     *
62
     * @return void
63
     */
64
    public function setValue(string $value)
65
    {
66
        $this->value = $value;
67
        $this->bytes = SizeHelper::fromHumanToBytes($value);
68
    }
69
70
    /**
71
     * This specifies when the file should be selected, whether it be
72
     * when the file matches a particular size, when it is smaller,
73
     * or whether it is larger.
74
     *
75
     * @param string $when
76
     */
77
    public function setWhen(string $when): void
78
    {
79
        if (!in_array($when, self::WHEN, true)) {
80
            throw new BuildException("Invalid 'when' value '$when'");
81
        }
82
        $this->when = $when;
83
    }
84
85
    /**
86
     * When using this as a custom selector, this method will be called.
87
     * It translates each parameter into the appropriate setXXX() call.
88
     *
89
     * {@inheritdoc}
90
     *
91
     * @param Parameter[] $parameters the complete set of parameters for this selector
92
     *
93
     * @return void
94
     *
95
     * @throws BuildException
96
     */
97
    public function setParameters(array $parameters): void
98
    {
99
        try {
100
            parent::setParameters($parameters);
101
            foreach ($parameters as $param) {
102
                switch (strtolower($param->getName())) {
103
                    case self::VALUE_KEY:
104
                        $this->setValue($param->getValue());
105
                        break;
106
                    case self::WHEN_KEY:
107
                        $this->setWhen($param->getValue());
108
                        break;
109
                    default:
110
                        throw new BuildException(sprintf('Invalid parameter %s', $param->getName()));
111
                }
112
            }
113
        } catch (Exception $exception) {
114
            $this->setError($exception->getMessage(), $exception);
115
        }
116
    }
117
118
    /**
119
     * <p>Checks to make sure all settings are kosher. In this case, it
120
     * means that the size attribute has been set (to a positive value),
121
     * that the multiplier has a valid setting, and that the size limit
122
     * is valid. Since the latter is a calculated value, this can only
123
     * fail due to a programming error.
124
     * </p>
125
     * <p>If a problem is detected, the setError() method is called.
126
     * </p>
127
     *
128
     * {@inheritdoc}
129
     *
130
     * @return void
131
     */
132
    public function verifySettings()
133
    {
134
        if ($this->value === '') {
135
            $this->setError("The 'value' attribute is required");
136
        }
137
        if ($this->bytes < 0) {
138
            $this->setError("The 'value' attribute must be positive");
139
        }
140
    }
141
142
    /**
143
     * The heart of the matter. This is where the selector gets to decide
144
     * on the inclusion of a file in a particular fileset.
145
     *
146
     * {@inheritdoc}
147
     *
148
     * @param PhingFile $basedir  A PhingFile object for the base directory
149
     * @param string    $filename The name of the file to check
150
     * @param PhingFile $file     A PhingFile object for this filename
151
     *
152
     * @return bool whether the file should be selected or not
153
     * @throws IOException
154
     */
155
    public function isSelected(PhingFile $basedir, $filename, PhingFile $file): bool
156
    {
157
        $this->validate();
158
159
        // Directory size never selected for
160
        if ($file->isDirectory()) {
161
            return true;
162
        }
163
        $expected = array_search($this->when, self::WHEN);
164
165
        return ($file->length() <=> $this->bytes) === $expected;
166
    }
167
}
168