Skip to content
This repository
Browse code

Simplify the code for sanitize class

removing else statements and variables that are not needed.
eg: return something(); vs $foo = something(); return $foo;
  • Loading branch information...
commit 44f8f84cd7e334535fda8edb725ad10b6e8f9bac 1 parent b227ff4
Carl Sutton authored September 12, 2012

Showing 1 changed file with 51 additions and 51 deletions. Show diff stats Hide diff stats

  1. 102  lib/Cake/Utility/Sanitize.php
102  lib/Cake/Utility/Sanitize.php
@@ -46,14 +46,15 @@ public static function paranoid($string, $allowed = array()) {
46 46
 			}
47 47
 		}
48 48
 
49  
-		if (is_array($string)) {
50  
-			$cleaned = array();
51  
-			foreach ($string as $key => $clean) {
52  
-				$cleaned[$key] = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $clean);
53  
-			}
54  
-		} else {
55  
-			$cleaned = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $string);
  49
+		if (!is_array($string)) {
  50
+			return preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $string);
  51
+		}
  52
+
  53
+		$cleaned = array();
  54
+		foreach ($string as $key => $clean) {
  55
+			$cleaned[$key] = preg_replace("/[^{$allow}a-zA-Z0-9]/", '', $clean);
56 56
 		}
  57
+
57 58
 		return $cleaned;
58 59
 	}
59 60
 
@@ -70,14 +71,12 @@ public static function escape($string, $connection = 'default') {
70 71
 			return $string;
71 72
 		}
72 73
 		$string = $db->value($string, 'string');
73  
-		if ($string[0] === 'N') {
74  
-			$string = substr($string, 2);
75  
-		} else {
76  
-			$string = substr($string, 1);
  74
+		$start = 1;
  75
+		if ($string{0} === 'N') {
  76
+			$start = 2;
77 77
 		}
78 78
 
79  
-		$string = substr($string, 0, -1);
80  
-		return $string;
  79
+		return substr(substr($string, 1), 0, -1);
81 80
 	}
82 81
 
83 82
 /**
@@ -128,8 +127,7 @@ public static function html($string, $options = array()) {
128 127
  * @return string whitespace sanitized string
129 128
  */
130 129
 	public static function stripWhitespace($str) {
131  
-		$r = preg_replace('/[\n\r\t]+/', '', $str);
132  
-		return preg_replace('/\s{2,}/u', ' ', $r);
  130
+		return preg_replace('/\s{2,}/u', ' ', preg_replace('/[\n\r\t]+/', '', $str));
133 131
 	}
134 132
 
135 133
 /**
@@ -139,10 +137,13 @@ public static function stripWhitespace($str) {
139 137
  * @return string Sting with images stripped.
140 138
  */
141 139
 	public static function stripImages($str) {
142  
-		$str = preg_replace('/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i', '$1$3$5<br />', $str);
143  
-		$str = preg_replace('/(<img[^>]+alt=")([^"]*)("[^>]*>)/i', '$2<br />', $str);
144  
-		$str = preg_replace('/<img[^>]*>/i', '', $str);
145  
-		return $str;
  140
+		$preg = array(
  141
+			'/(<a[^>]*>)(<img[^>]+alt=")([^"]*)("[^>]*>)(<\/a>)/i' => '$1$3$5<br />',
  142
+			'/(<img[^>]+alt=")([^"]*)("[^>]*>)/i' => '$2<br />',
  143
+			'/<img[^>]*>/i' => ''
  144
+		);
  145
+
  146
+		return preg_replace(array_keys($preg), array_values($preg), $str);
146 147
 	}
147 148
 
148 149
 /**
@@ -152,7 +153,8 @@ public static function stripImages($str) {
152 153
  * @return string String with <script>, <style>, <link>, <img> elements removed.
153 154
  */
154 155
 	public static function stripScripts($str) {
155  
-		return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/is', '', $str);
  156
+		$regex = '/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/is';
  157
+		return preg_replace($regex, '', $str);
156 158
 	}
157 159
 
158 160
 /**
@@ -162,10 +164,11 @@ public static function stripScripts($str) {
162 164
  * @return string sanitized string
163 165
  */
164 166
 	public static function stripAll($str) {
165  
-		$str = Sanitize::stripWhitespace($str);
166  
-		$str = Sanitize::stripImages($str);
167  
-		$str = Sanitize::stripScripts($str);
168  
-		return $str;
  167
+		return Sanitize::stripScripts(
  168
+			Sanitize::stripImages(
  169
+				Sanitize::stripWhitespace($str)
  170
+			)
  171
+		);
169 172
 	}
170 173
 
171 174
 /**
@@ -212,10 +215,8 @@ public static function clean($data, $options = array()) {
212 215
 			return $data;
213 216
 		}
214 217
 
215  
-		if (is_string($options)) {
  218
+		if (!is_array($options)) {
216 219
 			$options = array('connection' => $options);
217  
-		} elseif (!is_array($options)) {
218  
-			$options = array();
219 220
 		}
220 221
 
221 222
 		$options = array_merge(array(
@@ -235,30 +236,29 @@ public static function clean($data, $options = array()) {
235 236
 				$data[$key] = Sanitize::clean($val, $options);
236 237
 			}
237 238
 			return $data;
238  
-		} else {
239  
-			if ($options['odd_spaces']) {
240  
-				$data = str_replace(chr(0xCA), '', $data);
241  
-			}
242  
-			if ($options['encode']) {
243  
-				$data = Sanitize::html($data, array('remove' => $options['remove_html']));
244  
-			}
245  
-			if ($options['dollar']) {
246  
-				$data = str_replace("\\\$", "$", $data);
247  
-			}
248  
-			if ($options['carriage']) {
249  
-				$data = str_replace("\r", "", $data);
250  
-			}
251  
-			if ($options['unicode']) {
252  
-				$data = preg_replace("/&amp;#([0-9]+);/s", "&#\\1;", $data);
253  
-			}
254  
-			if ($options['escape']) {
255  
-				$data = Sanitize::escape($data, $options['connection']);
256  
-			}
257  
-			if ($options['backslash']) {
258  
-				$data = preg_replace("/\\\(?!&amp;#|\?#)/", "\\", $data);
259  
-			}
260  
-			return $data;
261 239
 		}
262  
-	}
263 240
 
  241
+		if ($options['odd_spaces']) {
  242
+			$data = str_replace(chr(0xCA), '', $data);
  243
+		}
  244
+		if ($options['encode']) {
  245
+			$data = Sanitize::html($data, array('remove' => $options['remove_html']));
  246
+		}
  247
+		if ($options['dollar']) {
  248
+			$data = str_replace("\\\$", "$", $data);
  249
+		}
  250
+		if ($options['carriage']) {
  251
+			$data = str_replace("\r", "", $data);
  252
+		}
  253
+		if ($options['unicode']) {
  254
+			$data = preg_replace("/&amp;#([0-9]+);/s", "&#\\1;", $data);
  255
+		}
  256
+		if ($options['escape']) {
  257
+			$data = Sanitize::escape($data, $options['connection']);
  258
+		}
  259
+		if ($options['backslash']) {
  260
+			$data = preg_replace("/\\\(?!&amp;#|\?#)/", "\\", $data);
  261
+		}
  262
+		return $data;
  263
+	}
264 264
 }

0 notes on commit 44f8f84

Please sign in to comment.
Something went wrong with that request. Please try again.