Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@dogmatic69 dogmatic69 authored
Showing with 32 additions and 42 deletions.
  1. +32 −42 lib/Cake/Utility/String.php
View
74 lib/Cake/Utility/String.php
@@ -72,10 +72,6 @@ public static function uuid() {
} 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 @@ public static function uuid() {
}
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 @@ public static function tokenize($data, $separator = ',', $leftBound = '(', $righ
$open = true;
} else {
$depth--;
- $open = false;
}
}
}
@@ -171,11 +164,10 @@ public static function tokenize($data, $separator = ',', $leftBound = '(', $righ
}
if (!empty($results)) {
- $data = array_map('trim', $results);
- } else {
- $data = array();
+ return array_map('trim', $results);
}
- return $data;
+
+ return array();
}
/**
@@ -227,25 +219,25 @@ public static function insert($str, $data, $options = array()) {
$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 @@ public static function highlight($text, $phrase, $options = array()) {
}
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_exists('Multibyte');
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_exists('Multibyte');
} 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 @@ public static function excerpt($text, $phrase, $radius = 100, $ellipsis = '...')
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);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.