Completed
Push — master ( 7fc664...742be8 )
by Robin
02:27
created

NativeFileInfo   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 81.13%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 2
dl 0
loc 161
ccs 43
cts 53
cp 0.8113
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 3
A getPath() 0 3 1
A getName() 0 3 1
A stat() 0 6 2
A getSize() 0 4 1
A getMTime() 0 4 1
A isDirectory() 0 4 1
A getMode() 0 8 2
A isReadOnly() 0 4 1
A isHidden() 0 4 1
A isSystem() 0 4 1
A isArchived() 0 4 1
A getAcls() 0 14 2
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);
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...
165
			[$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...
166
			$mask = hexdec($mask);
167
168
			$acls[$user] = new ACL($type, $flags, $mask);
169
		}
170
171
		return $acls;
172
	}
173
}
174