Completed
Push — master ( e5ef44...29e0f1 )
by Auke
18s
created

createsymlinks.php ➔ makedir()   D

Complexity

Conditions 10
Paths 54

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 10
eloc 22
nc 54
nop 1
dl 0
loc 35
rs 4.8196

How to fix   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
#!/usr/bin/env php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 29 and the first side effect is on line 1.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
<?php
3
4
5
6
// EDIT CREATESYMLINKS.INI TO ADD EXTENSIONS
7
8
	$OK = "\x1B[1m\x1B[32m[OK]\x1B[0m";
9
	$FAILED = "\x1B[1m\x1B[31m[FAILED]\x1B[0m";
10
	$COLS = 50;
11
	$silent = false;
12
13
	$options = getopt("hsv", array("help", "silent", "verbose"));
14
15
	if (isset($options['silent']) || isset($options['s'])) {
16
		$silent = true;
17
	}
18
	if (isset($options['verbose']) || isset($options['v'])) {
19
		$verbose = true;
20
	}
21
	if (isset($options['help']) || isset($options['h'])) {
22
		echo "Usage:\n" . ltrim($argv[0], "./") . " [options]\n\n";
23
		echo str_pad("-h, --help", 25) . "This message\n";
24
		echo str_pad("-s, --silent", 25) . "Don't produce any output on console\n";
25
		echo str_pad("-v, --verbose", 25) . "Produce verbose output\n";
26
		exit();
27
	}
28
29
	function makedir($path) {
30
		global $silent;
31
		global $verbose;
32
		global $OK;
33
		global $FAILED;
34
		global $COLS;
35
36
		if ($path[strlen($path)-1] !== '/') {
37
			$path .= '/';
38
		}
39
40
		if (preg_match('|(/?.*/)?(.+/)|i', $path, $regs)) {
41
			$parent = $regs[1];
42
43
			if ($parent && !file_exists($parent)) {
44
				makedir($parent);
45
			}
46
		}
47
48
		if (!file_exists($path)) {
49
			if(!$silent) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
50
			}
51
			if(mkdir($path, 0775)) {
52
				if($verbose) {
53
					echo str_pad("Creating path: " . basename($path), $COLS);
54
					echo $OK."\n";
55
				}
56
			} else {
57
				if(!$silent) {
58
					echo str_pad("Creating path: " . basename($path), $COLS);
59
					echo $FAILED." (permission denied)\n";
60
				}
61
			}
62
		}
63
	}
64
65
	function createsyms($srcdir, $dstdir){
66
		global $OK;
67
		global $FAILED;
68
		global $COLS;
69
		global $silent;
70
		if( !file_exists($srcdir) || !file_exists($dstdir) ) {
71
			if( !$silent ) {
72
				echo str_pad("Linking: " . str_replace(getcwd()."/", "", $srcdir)." to ". str_replace(getcwd()."/", "", $dstdir), $COLS);
73
				echo $FAILED;
74
				echo " (";
75
				echo !file_exists($srcdir) ? "source does not exist" : "destination does not exist";
76
				echo ")\n";
77
			}
78
			return;
79
		}
80
		if( !$silent ) {
81
			echo str_pad("Linking: " . str_replace(getcwd()."/", "", $srcdir)." to ". str_replace(getcwd()."/", "", $dstdir), $COLS);
82
			echo $OK."\n";
83
		}
84
		createsyms_rec($srcdir, $dstdir, $srcdir);
85
	}
86
87
	function createsyms_rec(&$srcdir, &$dstdir, $path) {
88
		global $silent;
89
		global $verbose;
90
		global $OK;
91
		global $FAILED;
92
		global $COLS;
93
94
		$dh = @opendir($path);
95
96
		while ($file = @readdir($dh)) {
97
			if ($file != "." && $file != "..") {
98
				$f = $path . $file;
99
				if (is_file($f)) {
100
					/* do not link backupfiles */
101
					if (!preg_match('/^.*~$/i', $f)) {
102
						$targetpath = $dstdir . substr($path, strlen($srcdir));
103
						$target = $targetpath . $file;
104
105
						if (!file_exists($targetpath)) {
106
							makedir($targetpath);
107
						}
108
109
						if (@is_link($target)) {
110
							unlink($target);
111
						}
112
113
						$symError = "";
114
						if (!is_file($target) ){
115
							ob_start();
116
								$symResult = symlink($f, $target);
117
								if (!$symResult) {
118
									$symError = str_replace("\n", "", ob_get_contents());
119
								}
120
							ob_end_clean();
121
122
							if($symResult) {
123
								if($verbose) {
124
									echo str_pad("Creating link: " . basename($target), $COLS) . $OK."\n";
125
								}
126 View Code Duplication
							} else {
127
								if(!$silent) {
128
									echo str_pad("Creating link: " . substr( $path, strlen($srcdir) ).basename($target), $COLS) . $FAILED;
129
									echo " ($symError)\n";
130
								}
131
							}
132 View Code Duplication
						} else {
133
							if(!$silent) {
134
								echo str_pad("Creating link: " . substr( $path, strlen($srcdir) ).basename($target), $COLS) . $FAILED;
135
								echo " (File already exists)\n";
136
							}
137
						}
138
					}
139
				} else if (is_dir($f)) {
140
					/* skip CVS directories */
141
					if ($file != "CVS" && $file != ".svn" && $file != ".git") {
142
						$targetpath = $dstdir.substr("$path$file/", strlen($srcdir));
143
						makedir($targetpath);
144
						createsyms_rec($srcdir,$dstdir,"$f/");
145
					}
146
				}
147
			}
148
		}
149
		@closedir($dh);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
150
	}
151
	if(!$silent) {
152
		echo "\nStarting link procedure, fasten seatbelts please!\n\n";
153
	}
154
155
156
	$settings = parse_ini_file("createsymlinks.ini", true);
157
158
	$root = "../../";
159
	chdir($root);
160
161
	$destination = getcwd()."/".$settings["destination"];
162
	$local = getcwd()."/".$settings["local"];
163
	$ariadne = getcwd()."/".$settings["ariadne"];
164
	$extensions = $settings["extensions"];
165
166
	createsyms($ariadne, $destination);
167
168
	if( is_array( $extensions ) ) {
169
		foreach( $extensions as $extension ) {
170
			createsyms(getcwd()."/".$extension, $destination);
171
		}
172
	}
173
174
	createsyms($local, $destination);
175