Passed
Push — master ( 63319e...b594f7 )
by Robin
03:29
created

NativeFileInfo::getSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
	/**
15
	 * @var string
16
	 */
17
	protected $path;
18
19
	/**
20
	 * @var string
21
	 */
22
	protected $name;
23
24
	/**
25
	 * @var NativeShare
26
	 */
27
	protected $share;
28
29
	/**
30
	 * @var array|null
31
	 */
32
	protected $attributeCache = null;
33
34
	/**
35
	 * @var int
36
	 */
37
	protected $modeCache;
38
39
	/**
40
	 * @param NativeShare $share
41
	 * @param string $path
42
	 * @param string $name
43
	 */
44 113
	public function __construct($share, $path, $name) {
45 113
		$this->share = $share;
46 113
		$this->path = $path;
47 113
		$this->name = $name;
48 113
	}
49
50
	/**
51
	 * @return string
52
	 */
53 101
	public function getPath() {
54 101
		return $this->path;
55
	}
56
57
	/**
58
	 * @return string
59
	 */
60 49
	public function getName() {
61 49
		return $this->name;
62
	}
63
64
	/**
65
	 * @return array
66
	 */
67 113
	protected function stat() {
68 113
		if (is_null($this->attributeCache)) {
69 113
			$rawAttributes = explode(',', $this->share->getAttribute($this->path, 'system.dos_attr.*'));
70 103
			$this->attributeCache = [];
71 103
			foreach ($rawAttributes as $rawAttribute) {
72 103
				[$name, $value] = explode(':', $rawAttribute);
73 103
				$name = strtolower($name);
74 103
				if ($name == 'mode') {
75 103
					$this->attributeCache[$name] = (int)hexdec(substr($value, 2));
76
				} else {
77 103
					$this->attributeCache[$name] = (int)$value;
78
				}
79
			}
80
		}
81 103
		return $this->attributeCache;
82
	}
83
84
	/**
85
	 * @return int
86
	 */
87 48
	public function getSize() {
88 48
		$stat = $this->stat();
89 38
		return $stat['size'];
90
	}
91
92
	/**
93
	 * @return int
94
	 */
95 1
	public function getMTime() {
96 1
		$stat = $this->stat();
97 1
		return $stat['change_time'];
98
	}
99
100
	/**
101
	 * @return int
102
	 */
103 101
	protected function getMode() {
104 101
		return $this->stat()['mode'];
105
	}
106
107
	/**
108
	 * @return bool
109
	 */
110 101
	public function isDirectory() {
111 101
		$mode = $this->getMode();
112 101
		return (bool)($mode & IFileInfo::MODE_DIRECTORY);
113
	}
114
115
	/**
116
	 * @return bool
117
	 */
118 9
	public function isReadOnly() {
119 9
		$mode = $this->getMode();
120 9
		return (bool)($mode & IFileInfo::MODE_READONLY);
121
	}
122
123
	/**
124
	 * @return bool
125
	 */
126 14
	public function isHidden() {
127 14
		$mode = $this->getMode();
128 14
		return (bool)($mode & IFileInfo::MODE_HIDDEN);
129
	}
130
131
	/**
132
	 * @return bool
133
	 */
134 8
	public function isSystem() {
135 8
		$mode = $this->getMode();
136 8
		return (bool)($mode & IFileInfo::MODE_SYSTEM);
137
	}
138
139
	/**
140
	 * @return bool
141
	 */
142 8
	public function isArchived() {
143 8
		$mode = $this->getMode();
144 8
		return (bool)($mode & IFileInfo::MODE_ARCHIVE);
145
	}
146
147
	/**
148
	 * @return ACL[]
149
	 */
150
	public function getAcls(): array {
151
		$acls = [];
152
		$attribute = $this->share->getAttribute($this->path, 'system.nt_sec_desc.acl.*+');
153
154
		foreach (explode(',', $attribute) as $acl) {
155
			[$user, $permissions] = explode(':', $acl, 2);
156
			[$type, $flags, $mask] = explode('/', $permissions);
157
			$mask = hexdec($mask);
158
159
			$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

159
			$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

159
			$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

159
			$acls[$user] = new ACL(/** @scrutinizer ignore-type */ $type, $flags, $mask);
Loading history...
160
		}
161
162
		return $acls;
163
	}
164
}
165