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

ContainsRegexpSelector::setCasesensitive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 whether they contain a
22
 * particular string using regexp.
23
 *
24
 * @author  Hans Lellelid <[email protected]> (Phing)
25
 * @author  Bruce Atherton <[email protected]> (Ant)
26
 * @package phing.types.selectors
27
 */
28
class ContainsRegexpSelector extends BaseExtendSelector
29
{
30
    /**
31
     * The expression set from XML.
32
     *
33
     * @var string $userProvidedExpression
34
     */
35
    private $userProvidedExpression;
36
37
    /**
38
     * @var Regexp $myExpression
39
     */
40
    private $myExpression;
41
42
    /**
43
     * @var bool $casesensitive
44
     */
45
    private $casesensitive = true;
46
47
    /**
48
     * @var bool $casesensitive
49
     */
50
    private $multiline = false;
51
52
    /**
53
     * @var RegularExpression $myRegExp
54
     */
55
    private $myRegExp;
56
57
    const EXPRESSION_KEY = "expression";
58
    const CASE_KEY = "casesensitive";
59
    const ML_KEY = 'multiline';
60
61
    /**
62
     * @return string
63
     */
64 1
    public function __toString()
65
    {
66 1
        $buf = "{containsregexpselector expression: ";
67 1
        $buf .= $this->userProvidedExpression;
68 1
        $buf .= " casesensitive: ";
69 1
        if ($this->casesensitive) {
70 1
            $buf .= "true";
71
        } else {
72
            $buf .= "false";
73
        }
74 1
        $buf .= "}";
75
76 1
        return $buf;
77
    }
78
79
    /**
80
     * The expression to match on within a file.
81
     *
82
     * @param string $exp the string that a file must contain to be selected.
83
     */
84 1
    public function setExpression($exp)
85
    {
86 1
        $this->userProvidedExpression = $exp;
87 1
    }
88
89
    /**
90
     * Whether to ignore case in the regex match.
91
     *
92
     * @param boolean $casesensitive whether to pay attention to case sensitivity
93
     */
94
    public function setCasesensitive($casesensitive)
95
    {
96
        $this->casesensitive = $casesensitive;
97
    }
98
99
    /**
100
     * When using this as a custom selector, this method will be called.
101
     * It translates each parameter into the appropriate setXXX() call.
102
     *
103
     * @param array $parameters the complete set of parameters for this selector
104
     *
105
     * @return void
106
     */
107
    public function setParameters(array $parameters): void
108
    {
109
        parent::setParameters($parameters);
110
        if ($parameters !== null) {
0 ignored issues
show
introduced by
The condition $parameters !== null is always true.
Loading history...
111
            for ($i = 0, $size = count($parameters); $i < $size; $i++) {
112
                $paramname = $parameters[$i]->getName();
113
                switch (strtolower($paramname)) {
114
                    case self::EXPRESSION_KEY:
115
                        $this->setExpression($parameters[$i]->getValue());
116
                        break;
117
                    case self::CASE_KEY:
118
                        $this->setCasesensitive(Project::toBoolean($parameters[$i]->getValue()));
119
                        break;
120
                    case self::ML_KEY:
121
                        $this->setMultiLine(Project::toBoolean($parameters[$i]->getValue()));
0 ignored issues
show
Bug introduced by
The method setMultiLine() does not exist on ContainsRegexpSelector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
                        $this->/** @scrutinizer ignore-call */ 
122
                               setMultiLine(Project::toBoolean($parameters[$i]->getValue()));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
                        break;
123
                    default:
124
                        $this->setError("Invalid parameter " . $paramname);
125
                }
126
            } // for each param
127
        } // if params
128
    }
129
130
    /**
131
     * Checks to make sure all settings are kosher. In this case, it
132
     * means that the pattern attribute has been set.
133
     */
134 1
    public function verifySettings()
135
    {
136 1
        if ($this->userProvidedExpression === null) {
137
            $this->setError("The expression attribute is required");
138
        }
139 1
    }
140
141
    /**
142
     * The heart of the matter. This is where the selector gets to decide
143
     * on the inclusion of a file in a particular fileset.
144
     *
145
     * @param PhingFile $basedir base directory the scan is being done from
146
     * @param string $filename the name of the file to check
147
     * @param PhingFile $file PhingFile object the selector can use
148
     *
149
     * @throws BuildException
150
     *
151
     * @return bool whether the file should be selected or not
152
     */
153 1
    public function isSelected(PhingFile $basedir, $filename, PhingFile $file)
154
    {
155 1
        $this->validate();
156
157
        try {
158 1
            if ($file->isDirectory() || $file->isLink()) {
159 1
                return true;
160
            }
161
        } catch (IOException $ioe) {
162
            if (OsCondition::isOS('windows')) {
163
                return true;
164
            }
165
166
            throw new BuildException($ioe);
167
        }
168
169 1
        if ($this->myRegExp === null) {
170 1
            $this->myRegExp = new RegularExpression();
171 1
            $this->myRegExp->setPattern($this->userProvidedExpression);
172 1
            $this->myExpression = $this->myRegExp->getRegexp($this->getProject());
173
        }
174
175 1
        $in = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $in is dead and can be removed.
Loading history...
176
        try {
177 1
            $in = new BufferedReader(new FileReader($file));
178 1
            $teststr = $in->readLine();
179 1
            while ($teststr !== null) {
180 1
                $this->myExpression->setMultiline($this->multiline);
181 1
                $this->myExpression->setIgnoreCase(!$this->casesensitive);
182 1
                if ($this->myExpression->matches($teststr)) {
183 1
                    return true;
184
                }
185 1
                $teststr = $in->readLine();
186
            }
187
188 1
            $in->close();
189
190 1
            return false;
191
        } catch (IOException $ioe) {
192
            if ($in) {
0 ignored issues
show
introduced by
$in is of type BufferedReader, thus it always evaluated to true.
Loading history...
193
                $in->close();
194
            }
195
            throw new BuildException("Could not read file " . $filename);
196
        }
197
    }
198
}
199