Completed
Push — ylebre-svn-fixes ( 2859de )
by
unknown
64:29
created

createsymlinks.php ➔ createsyms_rec()   D

Complexity

Conditions 18
Paths 46

Size

Total Lines 64

Duplication

Lines 12
Ratio 18.75 %

Importance

Changes 0
Metric Value
cc 18
nc 46
nop 3
dl 12
loc 64
rs 4.8666
c 0
b 0
f 0

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
#!/usr/bin/env php
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) {
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);
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