Completed
Push — master ( 03fc7e...44f12a )
by Henry
15:26
created

includes/Filesystem/Directory.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Filesystem;
3
4
use RecursiveIteratorIterator;
5
6
/**
7
 * children class to handle a directory in the filesystem
8
 *
9
 * @since 3.2.0
10
 *
11
 * @category Filesystem
12
 * @package Redaxscript
13
 * @author Henry Ruhs
14
 */
15
16
class Directory extends File
17
{
18
	/**
19
	 * create the directory
20
	 *
21
	 * @since 3.2.0
22
	 *
23
	 * @param string $directory name of the directory
24
	 * @param int $mode file access mode
25
	 *
26
	 * @return bool
27
	 */
28
29 2
	public function createDirectory(string $directory = null, int $mode = 0777) : bool
30
	{
31 2
		$path = $this->_root . DIRECTORY_SEPARATOR . $directory;
32 2
		return !is_dir($path) && mkdir($path, $mode);
33
	}
34
35
	/**
36
	 * remove the directory
37
	 *
38
	 * @since 3.2.0
39
	 *
40
	 * @param string $directory name of the directory
41
	 *
42
	 * @return bool
43
	 */
44
45 3
	public function removeDirectory(string $directory = null) : bool
46
	{
47 3
		if ($directory)
0 ignored issues
show
Bug Best Practice introduced by
The expression $directory of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
48
		{
49 2
			$path = $this->_root . DIRECTORY_SEPARATOR . $directory;
50 2
			$this->_remove($path);
51 2
			return rmdir($path);
52
		}
53 1
		return false;
54
	}
55
56
	/**
57
	 * clear the directory
58
	 *
59
	 * @since 3.2.0
60
	 */
61
62 2
	public function clearDirectory()
63
	{
64 2
		$this->_remove($this->_root);
65 2
	}
66
67
	/**
68
	 * remove the directory
69
	 *
70
	 * @param string $directory name of the directory
71
	 */
72
73 4
	protected function _remove(string $directory = null)
74
	{
75 4
		$iterator = $this->_scan($directory);
76
77
		/* handle recursive */
78
79 4
		if ($this->_recursive)
80
		{
81 3
			$iterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST);
82
		}
83
84
		/* process iterator */
85
86 4
		foreach ($iterator as $item)
87
		{
88 4
			$path = $item->getPathName();
89 4
			$item->isDir() ? rmdir($path) : unlink($path);
90
		}
91 4
	}
92
}
93