Passed
Push — master ( 7a9799...2aee06 )
by Michiel
06:03
created

PropertySelector::main()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 41
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 41
rs 8.4444
c 0
b 0
f 0
ccs 29
cts 29
cp 1
cc 8
nc 16
nop 0
crap 8
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\Task\System\Property;
21
22
use Phing\Exception\BuildException;
23
use Phing\Task\System\Property\AbstractPropertySetterTask;
24
use Phing\Type\RegularExpression;
25
26
/**
27
 * PropertySelector Task
28
 *
29
 * @author  Siad Ardroumli <[email protected]>
30
 * @package phing.tasks.ext.property
31
 */
32
class PropertySelector extends AbstractPropertySetterTask
33
{
34
    /**
35
     * @var RegularExpression $match
36
     */
37
    private $match;
38
    private $select = "\\0";
39
    private $delim = ',';
40
    private $caseSensitive = true;
41
    private $distinct = false;
42
43 1
    public function setMatch($match)
44
    {
45 1
        $this->match = new RegularExpression();
46 1
        $this->match->setPattern($match);
47 1
    }
48
49 1
    public function setSelect($select)
50
    {
51 1
        $this->select = $select;
52 1
    }
53
54 1
    public function setCaseSensitive($caseSensitive)
55
    {
56 1
        $this->caseSensitive = $caseSensitive;
57 1
    }
58
59 1
    public function setDelimiter($delim)
60
    {
61 1
        $this->delim = $delim;
62 1
    }
63
64
    public function setDistinct($distinct)
65
    {
66
        $this->distinct = $distinct;
67
    }
68
69 1
    protected function validate()
70
    {
71 1
        parent::validate();
72 1
        if ($this->match == null) {
73
            throw new BuildException("No match expression specified.");
74
        }
75 1
    }
76
77 1
    public function main()
78
    {
79 1
        $this->validate();
80
81 1
        $regex = $this->match->getRegexp($this->project);
82 1
        $regex->setIgnoreCase(!$this->caseSensitive);
83 1
        $props = $this->project->getProperties();
84 1
        $e = array_keys($props);
85 1
        $buf = '';
86 1
        $cnt = 0;
87
88 1
        $used = [];
89
90 1
        foreach ($e as $key) {
91 1
            if ($regex->matches($key)) {
92 1
                $output = $this->select;
93 1
                $groups = $regex->getGroups();
94 1
                $sz = count($groups);
95 1
                for ($i = 0; $i < $sz; $i++) {
96 1
                    $s = $groups[$i];
97
98 1
                    $result = new RegularExpression();
99 1
                    $result->setPattern("\\\\" . $i);
100 1
                    $sregex = $result->getRegexp($this->project);
101 1
                    $sregex->setReplace($output);
102 1
                    $output = $sregex->replace($s);
103
                }
104
105 1
                if (!($this->distinct && in_array($output, $used))) {
106 1
                    $used[] = $output;
107 1
                    if ($cnt !== 0) {
108 1
                        $buf .= $this->delim;
109
                    }
110 1
                    $buf .= $output;
111 1
                    $cnt++;
112
                }
113
            }
114
        }
115
116 1
        if ($buf !== '') {
117 1
            $this->setPropertyValue($buf);
118
        }
119 1
    }
120
}
121