Passed
Push — master ( 742be8...9fb175 )
by Robin
04:11
created

NativeFileInfo   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Test Coverage

Coverage 81.13%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 48
dl 0
loc 159
ccs 43
cts 53
cp 0.8113
rs 10
c 2
b 0
f 0
wmc 18

13 Methods

Rating   Name   Duplication   Size   Complexity  
A stat() 0 5 2
A isArchived() 0 3 1
A isReadOnly() 0 3 1
A getMode() 0 7 2
A getSize() 0 3 1
A isHidden() 0 3 1
A getName() 0 2 1
A __construct() 0 11 3
A isDirectory() 0 3 1
A isSystem() 0 3 1
A getAcls() 0 13 2
A getPath() 0 2 1
A getMTime() 0 3 1
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 206
	public function __construct($share, $path, $name, $stat) {
51 206
		$this->share = $share;
52 206
		$this->path = $path;
53 206
		$this->name = $name;
54
55 206
		if (is_array($stat)) {
56 44
			$this->statCache = $stat;
57 204
		} elseif (is_callable($stat)) {
58 204
			$this->statCallback = $stat;
59
		} else {
60
			throw new \InvalidArgumentException('$stat needs to be an array or callback');
61
		}
62 206
	}
63
64
	/**
65
	 * @return string
66
	 */
67 202
	public function getPath() {
68 202
		return $this->path;
69
	}
70
71
	/**
72
	 * @return string
73
	 */
74 98
	public function getName() {
75 98
		return $this->name;
76
	}
77
78
	/**
79
	 * @return array
80
	 */
81 204
	protected function stat() {
82 204
		if (is_null($this->statCache)) {
83 204
			$this->statCache = call_user_func($this->statCallback);
84
		}
85 204
		return $this->statCache;
86
	}
87
88
	/**
89
	 * @return int
90
	 */
91 58
	public function getSize() {
92 58
		$stat = $this->stat();
93 58
		return $stat['size'];
94
	}
95
96
	/**
97
	 * @return int
98
	 */
99 2
	public function getMTime() {
100 2
		$stat = $this->stat();
101 2
		return $stat['mtime'];
102
	}
103
104
	/**
105
	 * @return bool
106
	 */
107 202
	public function isDirectory() {
108 202
		$stat = $this->stat();
109 202
		return !($stat['mode'] & self::MODE_FILE);
110
	}
111
112
	/**
113
	 * @return int
114
	 */
115 28
	protected function getMode() {
116 28
		if (!$this->modeCache) {
117 28
			$attribute = $this->share->getAttribute($this->path, 'system.dos_attr.mode');
118
			// parse hex string
119 28
			$this->modeCache = (int)hexdec(substr($attribute, 2));
120
		}
121 28
		return $this->modeCache;
122
	}
123
124
	/**
125
	 * @return bool
126
	 */
127 18
	public function isReadOnly() {
128 18
		$mode = $this->getMode();
129 18
		return (bool)($mode & IFileInfo::MODE_READONLY);
130
	}
131
132
	/**
133
	 * @return bool
134
	 */
135 28
	public function isHidden() {
136 28
		$mode = $this->getMode();
137 28
		return (bool)($mode & IFileInfo::MODE_HIDDEN);
138
	}
139
140
	/**
141
	 * @return bool
142
	 */
143 16
	public function isSystem() {
144 16
		$mode = $this->getMode();
145 16
		return (bool)($mode & IFileInfo::MODE_SYSTEM);
146
	}
147
148
	/**
149
	 * @return bool
150
	 */
151 16
	public function isArchived() {
152 16
		$mode = $this->getMode();
153 16
		return (bool)($mode & IFileInfo::MODE_ARCHIVE);
154
	}
155
156
	/**
157
	 * @return ACL[]
158
	 */
159
	public function getAcls(): array {
160
		$acls = [];
161
		$attribute = $this->share->getAttribute($this->path, 'system.nt_sec_desc.acl.*+');
162
163
		foreach (explode(',', $attribute) as $acl) {
164
			[$user, $permissions] = explode(':', $acl, 2);
165
			[$type, $flags, $mask] = explode('/', $permissions);
166
			$mask = hexdec($mask);
167
168
			$acls[$user] = new ACL($type, $flags, $mask);
0 ignored issues
show
Bug introduced by
It seems like $mask can also be of type double; however, parameter $mask of Icewind\SMB\ACL::__construct() does only seem to accept integer, 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

168
			$acls[$user] = new ACL($type, $flags, /** @scrutinizer ignore-type */ $mask);
Loading history...
Bug introduced by
$flags of type string is incompatible with the type integer expected by parameter $flags of Icewind\SMB\ACL::__construct(). ( Ignorable by Annotation )

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

168
			$acls[$user] = new ACL($type, /** @scrutinizer ignore-type */ $flags, $mask);
Loading history...
Bug introduced by
$type of type string is incompatible with the type integer expected by parameter $type of Icewind\SMB\ACL::__construct(). ( Ignorable by Annotation )

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

168
			$acls[$user] = new ACL(/** @scrutinizer ignore-type */ $type, $flags, $mask);
Loading history...
169
		}
170
171
		return $acls;
172
	}
173
}
174