Skip to content
This repository
Browse code

simplify the code in the string class

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

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

  1. 74  lib/Cake/Utility/String.php
74  lib/Cake/Utility/String.php
@@ -72,10 +72,6 @@ public static function uuid() {
72 72
 		} elseif ($node !== '127.0.0.1') {
73 73
 			$node = ip2long($node);
74 74
 		} else {
75  
-			$node = null;
76  
-		}
77  
-
78  
-		if (empty($node)) {
79 75
 			$node = crc32(Configure::read('Security.salt'));
80 76
 		}
81 77
 
@@ -92,12 +88,10 @@ public static function uuid() {
92 88
 		}
93 89
 
94 90
 		list($timeMid, $timeLow) = explode(' ', microtime());
95  
-		$uuid = sprintf(
  91
+		return sprintf(
96 92
 			"%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff,
97 93
 			mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), $pid, $node
98 94
 		);
99  
-
100  
-		return $uuid;
101 95
 	}
102 96
 
103 97
 /**
@@ -156,7 +150,6 @@ public static function tokenize($data, $separator = ',', $leftBound = '(', $righ
156 150
 							$open = true;
157 151
 						} else {
158 152
 							$depth--;
159  
-							$open = false;
160 153
 						}
161 154
 					}
162 155
 				}
@@ -171,11 +164,10 @@ public static function tokenize($data, $separator = ',', $leftBound = '(', $righ
171 164
 		}
172 165
 
173 166
 		if (!empty($results)) {
174  
-			$data = array_map('trim', $results);
175  
-		} else {
176  
-			$data = array();
  167
+			return array_map('trim', $results);
177 168
 		}
178  
-		return $data;
  169
+
  170
+		return array();
179 171
 	}
180 172
 
181 173
 /**
@@ -227,25 +219,25 @@ public static function insert($str, $data, $options = array()) {
227 219
 				$str = substr_replace($str, $val, $pos, 1);
228 220
 			}
229 221
 			return ($options['clean']) ? String::cleanInsert($str, $options) : $str;
230  
-		} else {
231  
-			asort($data);
  222
+		}
232 223
 
233  
-			$hashKeys = array();
234  
-			foreach ($data as $key => $value) {
235  
-				$hashKeys[] = crc32($key);
236  
-			}
  224
+		asort($data);
237 225
 
238  
-			$tempData = array_combine(array_keys($data), array_values($hashKeys));
239  
-			krsort($tempData);
240  
-			foreach ($tempData as $key => $hashVal) {
241  
-				$key = sprintf($format, preg_quote($key, '/'));
242  
-				$str = preg_replace($key, $hashVal, $str);
243  
-			}
244  
-			$dataReplacements = array_combine($hashKeys, array_values($data));
245  
-			foreach ($dataReplacements as $tmpHash => $tmpValue) {
246  
-				$tmpValue = (is_array($tmpValue)) ? '' : $tmpValue;
247  
-				$str = str_replace($tmpHash, $tmpValue, $str);
248  
-			}
  226
+		$hashKeys = array();
  227
+		foreach ($data as $key => $value) {
  228
+			$hashKeys[] = crc32($key);
  229
+		}
  230
+
  231
+		$tempData = array_combine(array_keys($data), array_values($hashKeys));
  232
+		krsort($tempData);
  233
+		foreach ($tempData as $key => $hashVal) {
  234
+			$key = sprintf($format, preg_quote($key, '/'));
  235
+			$str = preg_replace($key, $hashVal, $str);
  236
+		}
  237
+		$dataReplacements = array_combine($hashKeys, array_values($data));
  238
+		foreach ($dataReplacements as $tmpHash => $tmpValue) {
  239
+			$tmpValue = (is_array($tmpValue)) ? '' : $tmpValue;
  240
+			$str = str_replace($tmpHash, $tmpValue, $str);
249 241
 		}
250 242
 
251 243
 		if (!isset($options['format']) && isset($options['before'])) {
@@ -397,14 +389,14 @@ public static function highlight($text, $phrase, $options = array()) {
397 389
 			}
398 390
 
399 391
 			return preg_replace($replace, $with, $text);
400  
-		} else {
401  
-			$phrase = '(' . preg_quote($phrase, '|') . ')';
402  
-			if ($html) {
403  
-				$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
404  
-			}
  392
+		}
405 393
 
406  
-			return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
  394
+		$phrase = '(' . preg_quote($phrase, '|') . ')';
  395
+		if ($html) {
  396
+			$phrase = "(?![^<]+>)$phrase(?![^<]+>)";
407 397
 		}
  398
+
  399
+		return preg_replace(sprintf($options['regex'], $phrase), $format, $text);
408 400
 	}
409 401
 
410 402
 /**
@@ -447,9 +439,9 @@ class_exists('Multibyte');
447 439
 
448 440
 		if (mb_strlen($text) <= $length) {
449 441
 			return $text;
450  
-		} else {
451  
-			$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis));
452 442
 		}
  443
+
  444
+		$truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis));
453 445
 		if (!$exact) {
454 446
 			$spacepos = mb_strpos($truncate, ' ');
455 447
 			$truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos));
@@ -542,9 +534,8 @@ class_exists('Multibyte');
542 534
 		} else {
543 535
 			if (mb_strlen($text) <= $length) {
544 536
 				return $text;
545  
-			} else {
546  
-				$truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis));
547 537
 			}
  538
+			$truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis));
548 539
 		}
549 540
 		if (!$exact) {
550 541
 			$spacepos = mb_strrpos($truncate, ' ');
@@ -642,9 +633,8 @@ public static function excerpt($text, $phrase, $radius = 100, $ellipsis = '...')
642 633
 	public static function toList($list, $and = 'and', $separator = ', ') {
643 634
 		if (count($list) > 1) {
644 635
 			return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list);
645  
-		} else {
646  
-			return array_pop($list);
647 636
 		}
648  
-	}
649 637
 
  638
+		return array_pop($list);
  639
+	}
650 640
 }

0 notes on commit f886d87

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