Completed
Push — master ( 065daa...3c5e45 )
by Robin
04:15
created

NativeFileInfo::getAcls()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 9
cp 0
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Copyright (c) 2014 Robin Appelman <[email protected]>
4
 * This file is licensed under the Licensed under the MIT license:
5
 * http://opensource.org/licenses/MIT
6
 */
7
8
namespace Icewind\SMB\Native;
9
10
use Icewind\SMB\ACL;
11
use Icewind\SMB\IFileInfo;
12
13
class NativeFileInfo implements IFileInfo {
14
	const MODE_FILE = 0100000;
15
16
	/**
17
	 * @var string
18
	 */
19
	protected $path;
20
21
	/**
22
	 * @var string
23
	 */
24
	protected $name;
25
26
	/**
27
	 * @var NativeShare
28
	 */
29
	protected $share;
30
31
	/**
32
	 * @var array|null
33
	 */
34
	protected $statCache = null;
35
36
	/** @var callable|null */
37
	protected $statCallback = null;
38
39
	/**
40
	 * @var int
41
	 */
42
	protected $modeCache;
43
44
	/**
45
	 * @param NativeShare $share
46
	 * @param string $path
47
	 * @param string $name
48
	 * @param array|callable $stat
49
	 */
50 104
	public function __construct($share, $path, $name, $stat) {
51 104
		$this->share = $share;
52 104
		$this->path = $path;
53 104
		$this->name = $name;
54
55 104
		if (is_array($stat)) {
56 23
			$this->statCache = $stat;
57 102
		} elseif (is_callable($stat)) {
58 102
			$this->statCallback = $stat;
59
		} else {
60
			throw new \InvalidArgumentException('$stat needs to be an array or callback');
61
		}
62 104
	}
63
64
	/**
65
	 * @return string
66
	 */
67 101
	public function getPath() {
68 101
		return $this->path;
69
	}
70
71
	/**
72
	 * @return string
73
	 */
74 49
	public function getName() {
75 49
		return $this->name;
76
	}
77
78
	/**
79
	 * @return array
80
	 */
81 102
	protected function stat() {
82 102
		if (is_null($this->statCache)) {
83 102
			$this->statCache = call_user_func($this->statCallback);
84
		}
85 102
		return $this->statCache;
86
	}
87
88
	/**
89
	 * @return int
90
	 */
91 29
	public function getSize() {
92 29
		$stat = $this->stat();
93 29
		return $stat['size'];
94
	}
95
96
	/**
97
	 * @return int
98
	 */
99 1
	public function getMTime() {
100 1
		$stat = $this->stat();
101 1
		return $stat['mtime'];
102
	}
103
104
	/**
105
	 * @return bool
106
	 */
107 101
	public function isDirectory() {
108 101
		$stat = $this->stat();
109 101
		return !($stat['mode'] & self::MODE_FILE);
110
	}
111
112
	/**
113
	 * @return int
114
	 */
115 14
	protected function getMode() {
116 14
		if (!$this->modeCache) {
117 14
			$stat = $this->stat();
118 14
			if (isset($stat['mode'])) {
119 14
				$this->modeCache = ($stat['mode']);
120
			} else {
121
				$attribute = $this->share->getAttribute($this->path, 'system.dos_attr.mode');
122
				// parse hex string
123
				$this->modeCache = (int)hexdec(substr($attribute, 2));
124
			}
125
		}
126 14
		return $this->modeCache;
127
	}
128
129
	/**
130
	 * @return bool
131
	 */
132 9
	public function isReadOnly() {
133 9
		$mode = $this->getMode();
134 9
		return (bool)($mode & IFileInfo::MODE_READONLY);
135
	}
136
137
	/**
138
	 * @return bool
139
	 */
140 5
	public function isHidden() {
141 5
		$mode = $this->getMode();
142 5
		return (bool)($mode & IFileInfo::MODE_HIDDEN);
143
	}
144
145
	/**
146
	 * @return bool
147
	 */
148
	public function isSystem() {
149
		$mode = $this->getMode();
150
		return (bool)($mode & IFileInfo::MODE_SYSTEM);
151
	}
152
153
	/**
154
	 * @return bool
155
	 */
156 8
	public function isArchived() {
157 8
		$mode = $this->getMode();
158 8
		return (bool)($mode & IFileInfo::MODE_ARCHIVE);
159
	}
160
161
	/**
162
	 * @return ACL[]
163
	 */
164
	public function getAcls(): array {
165
		$acls = [];
166
		$attribute = $this->share->getAttribute($this->path, 'system.nt_sec_desc.acl.*+');
167
168
		foreach (explode(',', $attribute) as $acl) {
169
			[$user, $permissions] = explode(':', $acl, 2);
0 ignored issues
show
Bug introduced by
The variable $user does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $permissions does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
170
			[$type, $flags, $mask] = explode('/', $permissions);
0 ignored issues
show
Bug introduced by
The variable $type does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $flags does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $mask does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
171
			$mask = hexdec($mask);
172
173
			$acls[$user] = new ACL($type, $flags, $mask);
174
		}
175
176
		return $acls;
177
	}
178
}
179