Directory::rename()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 3
Ratio 37.5 %

Importance

Changes 0
Metric Value
dl 3
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
/**
3
 * Class Directory
4
 *
5
 * @filesource   Directory.php
6
 * @created      03.03.2017
7
 * @package      chillerlan\Filereader
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Filereader;
14
15
use chillerlan\Filereader\Drivers\FSDriverInterface;
16
17
class Directory extends FSAbstract{
18
19
	/**
20
	 * Directory constructor.
21
	 *
22
	 * @param \chillerlan\Filereader\Drivers\FSDriverInterface $driver
23
	 * @param string                                           $path
24
	 */
25
	public function __construct(FSDriverInterface $driver, string $path){
26
		parent::__construct($driver);
27
28
		$this->path = $path;
29
	}
30
31
	/**
32
	 * @param string $path
33
	 *
34
	 * @return \chillerlan\Filereader\Directory
35
	 */
36
	public function change(string $path):Directory{
37
		$this->path = $path;
38
39
		return $this;
40
	}
41
42
	/**
43
	 * Reads a directory and returns the contents as an array of \stdClass
44
	 *
45
	 * @return array
46
	 * @throws \chillerlan\Filereader\FilereaderException
47
	 */
48
	public function read():array{
49
50
		if(!$this->filereader->isDir($this->path)){
51
			throw new FilereaderException('Directory not found: '.$this->path);
52
		}
53
54
		$dir = scandir($this->path);
55
56
		$filelist = [];
57
58
		if(is_array($dir)){
59
60
			foreach($dir as $file){
61
62
				if(in_array($file, ['.', '..'], true)){
63
					continue;
64
				}
65
66
				$filelist[] = new File($this->filereader, $this, $file);
67
			}
68
		}
69
70
		return $filelist;
71
	}
72
73
	/**
74
	 * @param string|null $subdir
75
	 *
76
	 * @return bool
77
	 */
78 View Code Duplication
	public function create(string $subdir = null):bool{
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
80
		if($subdir && $this->filereader->isDir($this->path)){
0 ignored issues
show
Bug Best Practice introduced by
The expression $subdir 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...
81
			return $this->filereader->makeDir($this->path.DIRECTORY_SEPARATOR.$subdir);
82
		}
83
84
		return $this->filereader->makeDir($this->path);
85
	}
86
87
	/**
88
	 * @param string|null $subdir
89
	 *
90
	 * @return bool
91
	 */
92 View Code Duplication
	public function delete(string $subdir = null):bool{
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
94
		if($subdir && $this->filereader->isDir($this->path)){
0 ignored issues
show
Bug Best Practice introduced by
The expression $subdir 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...
95
			return $this->filereader->deleteDir($this->path.DIRECTORY_SEPARATOR.$subdir);
96
		}
97
98
		return $this->filereader->deleteDir($this->path);
99
	}
100
101
	/**
102
	 * @param string $newname
103
	 * @param bool   $overwrite
104
	 *
105
	 * @return \chillerlan\Filereader\Directory
106
	 * @throws \chillerlan\Filereader\FilereaderException
107
	 */
108
	public function rename(string $newname, bool $overwrite = true):Directory{
109
110 View Code Duplication
		if(!$this->filereader->rename($this->path, $newname, $overwrite)){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
			throw new FilereaderException('cannot rename '.$this->path.' to '.$newname); // @codeCoverageIgnore
112
		}
113
114
		return $this->change($newname);
115
	}
116
117
}
118