Passed
Push — master ( e1f86a...4e1a3a )
by Siad
05:23
created

RegexpMapper::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
namespace Phing\Mapper;
21
22
use Phing\Exception\BuildException;
23
use Phing\Util\Regexp;
24
25
/**
26
 * Uses regular expressions to perform filename transformations.
27
 *
28
 * @author Andreas Aderhold <[email protected]>
29
 * @author Hans Lellelid <[email protected]>
30
 *
31
 * @package phing.mappers
32
 */
33
class RegexpMapper implements FileNameMapper
34
{
35
    /**
36
     * @var string $to
37
     */
38
    private $to;
39
40
    /**
41
     * The Regexp engine.
42
     *
43
     * @var Regexp $reg
44
     */
45
    private $reg;
46
47
    private $handleDirSep = false;
48
    private $caseSensitive = true;
49
50
    /**
51
     * Instantiage regexp matcher here.
52
     */
53
    public function __construct()
54
    {
55
        $this->reg = new Regexp();
56
        $this->reg->setIgnoreCase(!$this->caseSensitive);
57
    }
58
59
    /**
60
     * Attribute specifying whether to ignore the difference
61
     * between / and \ (the two common directory characters).
62
     *
63
     * @param boolean $handleDirSep a boolean, default is false.
64
     */
65
    public function setHandleDirSep($handleDirSep)
66
    {
67
        $this->handleDirSep = $handleDirSep;
68
    }
69
70
    /**
71
     * Attribute specifying whether to ignore the difference
72
     * between / and \ (the two common directory characters).
73
     */
74
    public function getHandleDirSep()
75
    {
76
        return $this->handleDirSep;
77
    }
78
79
    /**
80
     * Attribute specifying whether to ignore the case difference
81
     * in the names.
82
     *
83
     * @param boolean $caseSensitive a boolean, default is false.
84
     */
85
    public function setCaseSensitive($caseSensitive)
86
    {
87
        $this->caseSensitive = $caseSensitive;
88
    }
89
90
    /**
91
     * Sets the &quot;from&quot; pattern. Required.
92
     * {@inheritdoc}
93
     *
94
     * @param string $from
95
     *
96
     * @return void
97
     */
98
    public function setFrom($from)
99
    {
100
        if ($from === null) {
0 ignored issues
show
introduced by
The condition $from === null is always false.
Loading history...
101
            throw new BuildException("this mapper requires a 'from' attribute");
102
        }
103
104
        $this->reg->setPattern($from);
105
    }
106
107
    /**
108
     * Sets the &quot;to&quot; pattern. Required.
109
     *
110
     * {@inheritdoc}
111
     *
112
     * @param string $to
113
     *
114
     * @return void
115
     *
116
     * @intern [HL] I'm changing the way this works for now to just use string
117
     *              <code>$this->to = StringHelper::toCharArray($to);</code>
118
     */
119
    public function setTo($to)
120
    {
121
        if ($to === null) {
0 ignored issues
show
introduced by
The condition $to === null is always false.
Loading history...
122
            throw new BuildException("this mapper requires a 'to' attribute");
123
        }
124
125
        $this->to = $to;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     *
131
     * @param mixed $sourceFileName
132
     *
133
     * @return array|null
134
     */
135
    public function main($sourceFileName)
136
    {
137
        if ($this->handleDirSep) {
138
            if (strpos('\\', $sourceFileName) !== false) {
139
                $sourceFileName = str_replace('\\', '/', $sourceFileName);
140
            }
141
        }
142
        if ($this->reg === null || $this->to === null || !$this->reg->matches((string) $sourceFileName)) {
143
            return null;
144
        }
145
146
        return [$this->replaceReferences($sourceFileName)];
147
    }
148
149
    /**
150
     * Replace all backreferences in the to pattern with the matched groups.
151
     * groups of the source.
152
     *
153
     * @param string $source The source filename.
154
     *
155
     * @return array|null|string
156
     *
157
     * FIXME Can't we just use engine->replace() to handle this?  the Preg engine will automatically convert \1 references to $1
158
     *
159
     * @intern the expression has already been processed (when ->matches() was run in Main())
160
     *         so no need to pass $source again to the engine.
161
     *         Replaces \1 with value of reg->getGroup(1) and return the modified "to" string.
162
     */
163
    private function replaceReferences($source)
0 ignored issues
show
Unused Code introduced by
The parameter $source is not used and could be removed. ( Ignorable by Annotation )

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

163
    private function replaceReferences(/** @scrutinizer ignore-unused */ $source)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
    {
165
        return preg_replace_callback('/\\\([\d]+)/', [$this, 'replaceReferencesCallback'], $this->to);
166
    }
167
168
    /**
169
     * Gets the matched group from the Regexp engine.
170
     *
171
     * @param array $matches Matched elements.
172
     *
173
     * @return string
174
     */
175
    private function replaceReferencesCallback($matches)
176
    {
177
        return (string) $this->reg->getGroup($matches[1]);
178
    }
179
}
180