From f886d874a639410efbea6fec81e78e1bdc9b069c Mon Sep 17 00:00:00 2001 From: dogmatic69 Date: Wed, 12 Sep 2012 18:10:50 +0100 Subject: [PATCH] simplify the code in the string class removing else statements and variables that are not needed. eg: return something(); vs $foo = something(); return $foo; --- lib/Cake/Utility/String.php | 76 ++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/lib/Cake/Utility/String.php b/lib/Cake/Utility/String.php index d3c6b0868..ecf0100af 100644 --- a/lib/Cake/Utility/String.php +++ b/lib/Cake/Utility/String.php @@ -72,10 +72,6 @@ class String { } elseif ($node !== '127.0.0.1') { $node = ip2long($node); } else { - $node = null; - } - - if (empty($node)) { $node = crc32(Configure::read('Security.salt')); } @@ -92,12 +88,10 @@ class String { } list($timeMid, $timeLow) = explode(' ', microtime()); - $uuid = sprintf( + return sprintf( "%08x-%04x-%04x-%02x%02x-%04x%08x", (int)$timeLow, (int)substr($timeMid, 2) & 0xffff, mt_rand(0, 0xfff) | 0x4000, mt_rand(0, 0x3f) | 0x80, mt_rand(0, 0xff), $pid, $node ); - - return $uuid; } /** @@ -156,7 +150,6 @@ class String { $open = true; } else { $depth--; - $open = false; } } } @@ -171,11 +164,10 @@ class String { } if (!empty($results)) { - $data = array_map('trim', $results); - } else { - $data = array(); + return array_map('trim', $results); } - return $data; + + return array(); } /** @@ -227,25 +219,25 @@ class String { $str = substr_replace($str, $val, $pos, 1); } return ($options['clean']) ? String::cleanInsert($str, $options) : $str; - } else { - asort($data); + } - $hashKeys = array(); - foreach ($data as $key => $value) { - $hashKeys[] = crc32($key); - } + asort($data); - $tempData = array_combine(array_keys($data), array_values($hashKeys)); - krsort($tempData); - foreach ($tempData as $key => $hashVal) { - $key = sprintf($format, preg_quote($key, '/')); - $str = preg_replace($key, $hashVal, $str); - } - $dataReplacements = array_combine($hashKeys, array_values($data)); - foreach ($dataReplacements as $tmpHash => $tmpValue) { - $tmpValue = (is_array($tmpValue)) ? '' : $tmpValue; - $str = str_replace($tmpHash, $tmpValue, $str); - } + $hashKeys = array(); + foreach ($data as $key => $value) { + $hashKeys[] = crc32($key); + } + + $tempData = array_combine(array_keys($data), array_values($hashKeys)); + krsort($tempData); + foreach ($tempData as $key => $hashVal) { + $key = sprintf($format, preg_quote($key, '/')); + $str = preg_replace($key, $hashVal, $str); + } + $dataReplacements = array_combine($hashKeys, array_values($data)); + foreach ($dataReplacements as $tmpHash => $tmpValue) { + $tmpValue = (is_array($tmpValue)) ? '' : $tmpValue; + $str = str_replace($tmpHash, $tmpValue, $str); } if (!isset($options['format']) && isset($options['before'])) { @@ -397,14 +389,14 @@ class String { } return preg_replace($replace, $with, $text); - } else { - $phrase = '(' . preg_quote($phrase, '|') . ')'; - if ($html) { - $phrase = "(?![^<]+>)$phrase(?![^<]+>)"; - } - - return preg_replace(sprintf($options['regex'], $phrase), $format, $text); } + + $phrase = '(' . preg_quote($phrase, '|') . ')'; + if ($html) { + $phrase = "(?![^<]+>)$phrase(?![^<]+>)"; + } + + return preg_replace(sprintf($options['regex'], $phrase), $format, $text); } /** @@ -447,9 +439,9 @@ class String { if (mb_strlen($text) <= $length) { return $text; - } else { - $truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis)); } + + $truncate = mb_substr($text, mb_strlen($text) - $length + mb_strlen($ellipsis)); if (!$exact) { $spacepos = mb_strpos($truncate, ' '); $truncate = $spacepos === false ? '' : trim(mb_substr($truncate, $spacepos)); @@ -542,9 +534,8 @@ class String { } else { if (mb_strlen($text) <= $length) { return $text; - } else { - $truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis)); } + $truncate = mb_substr($text, 0, $length - mb_strlen($ellipsis)); } if (!$exact) { $spacepos = mb_strrpos($truncate, ' '); @@ -642,9 +633,8 @@ class String { public static function toList($list, $and = 'and', $separator = ', ') { if (count($list) > 1) { return implode($separator, array_slice($list, null, -1)) . ' ' . $and . ' ' . array_pop($list); - } else { - return array_pop($list); } - } + return array_pop($list); + } }