Directory::listContents()   F
last analyzed

Complexity

Conditions 20
Paths 384

Size

Total Lines 101
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 101
rs 3.6338
cc 20
eloc 45
nc 384
nop 4

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package    Fuel\FileSystem
4
 * @version    2.0
5
 * @author     Fuel Development Team
6
 * @license    MIT License
7
 * @copyright  2010 - 2015 Fuel Development Team
8
 * @link       http://fuelphp.com
9
 */
10
11
namespace Fuel\FileSystem;
12
13
14
class Directory extends Handler
15
{
16
	/**
17
	 * Deletes a directory recursively
18
	 *
19
	 * @return boolean
20
	 */
21
	public function deleteRecursive()
22
	{
23
		return $this->delete(true);
24
	}
25
26
	/**
27
	 * Deletes a directory
28
	 *
29
	 * @param boolean $recursive
30
	 *
31
	 * @return boolean
32
	 */
33
	public function delete($recursive = false)
34
	{
35
		if ( ! $recursive)
36
		{
37
			return parent::delete();
38
		}
39
40
		$finder = new Finder();
41
		$contents = $finder->listContents($this->path);
0 ignored issues
show
Bug introduced by
The method listContents() does not seem to exist on object<Fuel\FileSystem\Finder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
43
		foreach($contents as $item)
44
		{
45
			$item->delete(true);
46
		}
47
48
		return parent::delete();
49
	}
50
51
	/**
52
	 * Lists all files in a directory
53
	 *
54
	 * @param integer $depth
55
	 * @param mixed   $filter
56
	 * @param boolean $asHandlers
57
	 *
58
	 * @return array
59
	 */
60
	public function listFiles($depth = 0, $filter = null, $asHandlers = false)
61
	{
62
		return $this->listContents($depth, $filter, 'file', $asHandlers);
63
	}
64
65
	/**
66
	 * Lists all files in a directory as Handlers
67
	 *
68
	 * @param integer $depth
69
	 * @param mixed   $filter
70
	 *
71
	 * @return array
72
	 */
73
	public function listFileHandlers($depth = 0, $filter = null)
74
	{
75
		return $this->listContents($depth, $filter, 'file', true);
76
	}
77
78
	/**
79
	 * Lists all directories in a directory
80
	 *
81
	 * @param integer $depth
82
	 * @param mixed   $filter
83
	 * @param boolean $asHandlers
84
	 *
85
	 * @return array
86
	 */
87
	public function listDirs($depth = 0, $filter = null, $asHandlers = false)
88
	{
89
		return $this->listContents($depth, $filter, 'dir', $asHandlers);
90
	}
91
92
	/**
93
	 * Lists all directories in a directory
94
	 *
95
	 * @param integer $depth
96
	 * @param mixed   $filter
97
	 *
98
	 * @return array
99
	 */
100
	public function listDirHandlers($depth = 0, $filter = null)
101
	{
102
		return $this->listContents($depth, $filter, 'dir', true);
103
	}
104
105
	/**
106
	 * Lists all files and directories in a directory
107
	 *
108
	 * @param integer  $depth
109
	 * @param mixed    $filter
110
	 * @param string   $type
111
	 * @param boolean  $asHandlers
112
	 *
113
	 * @return array
114
	 */
115
	public function listContents($depth = 0, $filter = null, $type = 'all', $asHandlers = false)
116
	{
117
		$pattern = $this->path.'/*';
118
119
		if (is_array($filter))
120
		{
121
			$filters = $filter;
122
			$filter = new Filter;
123
124
			foreach ($filters as $f => $type)
125
			{
126
				if ( ! is_int($f))
127
				{
128
					$f = $type;
129
					$type = null;
130
				}
131
132
				$expected = true;
133
134
				if (strpos($f, '!') === 0)
135
				{
136
					$f = substr($f, 1);
137
					$expected = false;
138
				}
139
140
				$filter->addFilter($f, $expected, $type);
141
			}
142
		}
143
144
		if ($filter instanceof \Closure)
145
		{
146
			$callback = $filter;
147
			$filter = new Filter();
148
			$callback($filter);
149
		}
150
151
		if ( ! $filter)
152
		{
153
			$filter = new Filter;
154
		}
155
156
		$flags = GLOB_MARK;
157
158
		if ($type === 'file' and ! pathinfo($pattern, PATHINFO_EXTENSION))
159
		{
160
			// Add an extension wildcard
161
			$pattern .= '.*';
162
		}
163
		elseif ($type === 'dir')
164
		{
165
			$flags = GLOB_MARK | GLOB_ONLYDIR;
166
		}
167
168
		$contents = glob($pattern, $flags);
169
170
		// Filter the content.
171
		$contents = $filter->filter($contents);
172
173
		// Lower the depth for a recursive call
174
		if ($depth and $depth !== true)
175
		{
176
			$depth--;
177
		}
178
179
		$formatted = array();
180
181
		foreach ($contents as $item)
182
		{
183
			if ($filter->isCorrectType('dir', $item))
184
			{
185
				$_contents = array();
186
187
				if (($depth === true or $depth === 0) and ! $asHandlers)
188
				{
189
					$dir = new Directory($item);
190
191
					$_contents = $dir->listContents($item, $filter, $depth, $type);
192
				}
193
194
				if ($asHandlers)
195
				{
196
					$formatted[] = new Directory($item);
197
				}
198
				else
199
				{
200
					$formatted[$item] = $_contents;
201
				}
202
			}
203
			elseif ($filter->isCorrectType('file', $item))
204
			{
205
				if ($asHandlers)
206
				{
207
					$item = new File($item);
208
				}
209
210
				$formatted[] = $item;
211
			}
212
		}
213
214
		return $formatted;
215
	}
216
}
217