Passed
Push — master ( 9b2b63...6189fc )
by Siad
11:25
created

PosixPermissionsSelector::isSelected()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
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
 * A selector that selects files based on their POSIX permissions.
22
 *
23
 * @author  Siad Ardroumli <[email protected]>
24
 * @package phing.types.selectors
25
 */
26
class PosixPermissionsSelector implements FileSelector
27
{
28
    /** @var string $permissions */
29
    private $permissions;
30
31
    /**
32
     * Sets the permissions to look for.
33
     * @param string $permissions the permissions string (rwxrwxrwx or octal)
34
     */
35
    public function setPermissions(string $permissions): void
36
    {
37
        $this->validate($permissions);
38
39
        if (strlen($permissions) === 3) {
40
            $this->permissions = $permissions;
41
            return;
42
        }
43
44
        $this->permissions = implode(
45
            '',
46
            array_map(
47
                'array_sum',
48
                array_chunk(
49
                    str_split(
50
                        strtr(
51
                            $permissions,
52
                            array_combine(
0 ignored issues
show
Bug introduced by
It seems like array_combine(array('r',...-'), array(4, 2, 1, 0)) can also be of type false; however, parameter $replace_pairs of strtr() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

52
                            /** @scrutinizer ignore-type */ array_combine(
Loading history...
53
                                ['r', 'w', 'x', '-'],
54
                                [4, 2, 1, 0]
55
                            )
56
                        )
57
                    ),
58
                    3
59
                )
60
            )
61
        );
62
    }
63
64
    private function validate(string $permissions): void
65
    {
66
        if (
67
            preg_match('/^[0-7]{3}$/', $permissions) !== 1 &&
68
            preg_match('/^[r-][w-][x-][r-][w-][x-][r-][w-][x-]$/', $permissions) !== 1
69
        ) {
70
            throw new BuildException("the permissions attribute {$permissions} is invalid");
71
        }
72
    }
73
74
    public function isSelected(PhingFile $basedir, $filename, PhingFile $file): bool
75
    {
76
        if ($this->permissions === null) {
77
            throw new BuildException('the permissions attribute is required');
78
        }
79
80
        return decoct(fileperms($file->getPath()) & 0777) === $this->permissions;
81
    }
82
}
83