Folder   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 85
Duplicated Lines 32.94 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 8
Bugs 5 Features 2
Metric Value
wmc 14
c 8
b 5
f 2
lcom 0
cbo 2
dl 28
loc 85
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ensure() 9 9 3
A delete() 9 9 3
C deleteContents() 0 39 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace nochso\Omni;
3
4
/**
5
 * Folder handles file system folders.
6
 */
7
final class Folder {
8
	/**
9
	 * Ensure a folder exists by creating it if missing and throw an exception on failure.
10
	 *
11
	 * @param string $path
12
	 * @param int    $mode Optional, defaults to 0777.
13
	 *
14
	 * @throws \RuntimeException
15
	 */
16 View Code Duplication
	public static function ensure($path, $mode = 0777) {
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...
17
		if (is_dir($path)) {
18
			return;
19
		}
20
		if (@mkdir($path, $mode, true)) {
21
			return;
22
		}
23
		throw new \RuntimeException(sprintf("Unable to create folder '%s': %s", $path, error_get_last()['message']));
24
	}
25
26
	/**
27
	 * Delete a directory and all of its contents recursively.
28
	 *
29
	 * @param string $path Path of folder to delete
30
	 *
31
	 * @throws \RuntimeException Thrown when something could not be deleted.
32
	 */
33 View Code Duplication
	public static function delete($path) {
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...
34
		if (!is_dir($path)) {
35
			return;
36
		}
37
		self::deleteContents($path);
38
		if (@rmdir($path) === false) {
39
			throw new \RuntimeException(sprintf("Unable to delete folder '%s': %s", $path, error_get_last()['message']));
40
		}
41
	}
42
43
	/**
44
	 * deleteContents of a folder recursively, but not the folder itself.
45
	 *
46
	 * On Windows systems this will try to remove the read-only attribute if needed.
47
	 *
48
	 * @param string $path Path of folder whose contents will be deleted
49
	 *
50
	 * @throws \RuntimeException Thrown when something could not be deleted.
51
	 */
52
	public static function deleteContents($path) {
53
		if (!is_dir($path)) {
54
			return;
55
		}
56
		/** @var \SplFileInfo[] $files */
57
		$files = new \RecursiveIteratorIterator(
58
			new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS),
59
			\RecursiveIteratorIterator::CHILD_FIRST
60
		);
61
		foreach ($files as $fileInfo) {
62
			if ($fileInfo->isDir()) {
63
				if (@rmdir($fileInfo->getRealPath()) === false) {
64
					throw new \RuntimeException(
65
						sprintf(
66
							"Unable to delete child folder '%s' in '%s': %s",
67
							$fileInfo->getFilename(),
68
							$fileInfo->getPathname(),
69
							error_get_last()['message']
70
						)
71
					);
72
				}
73
			} elseif (@unlink($fileInfo->getRealPath()) === false) {
74
				if (OS::isWindows()) {
75
					Exec::create('attrib', '-R', $fileInfo->getRealPath())->run();
76
					if (@unlink($fileInfo->getPathname())) {
77
						continue;
78
					}
79
				}
80
				throw new \RuntimeException(
81
					sprintf(
82
						"Unable to delete file '%s' in folder '%s': %s",
83
						$fileInfo->getFilename(),
84
						$fileInfo->getPathname(),
85
						error_get_last()['message']
86
					)
87
				);
88
			}
89
		}
90
	}
91
}
92