Permalink
Browse files

Closes #5792, Inflector optimization by returning replaced values ins…

…tead of assining before returning

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7940 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
1 parent d5b86bc commit eb653b8cba2da46aaa2a28cf50301a23fc516e0a @phpnut phpnut committed Dec 18, 2008
Showing with 9 additions and 21 deletions.
  1. +9 −20 cake/libs/inflector.php
  2. +0 −1 cake/tests/cases/libs/inflector.test.php
View
@@ -137,7 +137,6 @@ function &getInstance() {
$instance[0]->__singularRules = $singularRules;
$instance[0]->__uninflectedSingular = $uninflectedPlural;
$instance[0]->__irregularSingular = array_flip($irregularPlural);
-
}
}
return $instance[0];
@@ -216,7 +215,6 @@ function __initPluralRules() {
'trilby' => 'trilbys',
'turf' => 'turfs');
-
$pluralRules = Set::pushDiff($this->__pluralRules, $corePluralRules);
$uninflected = Set::pushDiff($this->__uninflectedPlural, $coreUninflectedPlural);
$irregular = Set::pushDiff($this->__irregularPlural, $coreIrregularPlural);
@@ -242,8 +240,8 @@ function pluralize($word) {
if (isset($_this->pluralized[$word])) {
return $_this->pluralized[$word];
}
-
extract($_this->pluralRules);
+
if (!isset($regexUninflected) || !isset($regexIrregular)) {
$regexUninflected = __enclose(join( '|', $uninflected));
$regexIrregular = __enclose(join( '|', array_keys($irregular)));
@@ -267,8 +265,6 @@ function pluralize($word) {
return $_this->pluralized[$word];
}
}
- $_this->pluralized[$word] = $word;
- return $word;
}
/**
* Initializes singular inflection rules.
@@ -379,8 +375,8 @@ function singularize($word) {
if (isset($_this->singularized[$word])) {
return $_this->singularized[$word];
}
-
extract($_this->singularRules);
+
if (!isset($regexUninflected) || !isset($regexIrregular)) {
$regexUninflected = __enclose(join( '|', $uninflected));
$regexIrregular = __enclose(join( '|', array_keys($irregular)));
@@ -417,8 +413,7 @@ function singularize($word) {
* @link http://book.cakephp.org/view/572/Class-methods
*/
function camelize($lowerCaseAndUnderscoredWord) {
- $replace = str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
- return $replace;
+ return str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
}
/**
* Returns the given camelCasedWord as an underscored_word.
@@ -430,8 +425,7 @@ function camelize($lowerCaseAndUnderscoredWord) {
* @link http://book.cakephp.org/view/572/Class-methods
*/
function underscore($camelCasedWord) {
- $replace = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
- return $replace;
+ return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
}
/**
* Returns the given underscored_word_group as a Human Readable Word Group.
@@ -444,8 +438,7 @@ function underscore($camelCasedWord) {
* @link http://book.cakephp.org/view/572/Class-methods
*/
function humanize($lowerCaseAndUnderscoredWord) {
- $replace = ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord));
- return $replace;
+ return ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord));
}
/**
* Returns corresponding table name for given model $className. ("people" for the model class "Person").
@@ -457,8 +450,7 @@ function humanize($lowerCaseAndUnderscoredWord) {
* @link http://book.cakephp.org/view/572/Class-methods
*/
function tableize($className) {
- $replace = Inflector::pluralize(Inflector::underscore($className));
- return $replace;
+ return Inflector::pluralize(Inflector::underscore($className));
}
/**
* Returns Cake model class name ("Person" for the database table "people".) for given database table.
@@ -470,8 +462,7 @@ function tableize($className) {
* @link http://book.cakephp.org/view/572/Class-methods
*/
function classify($tableName) {
- $replace = Inflector::camelize(Inflector::singularize($tableName));
- return $replace;
+ return Inflector::camelize(Inflector::singularize($tableName));
}
/**
* Returns camelBacked version of an underscored string.
@@ -485,8 +476,7 @@ function classify($tableName) {
function variable($string) {
$string = Inflector::camelize(Inflector::underscore($string));
$replace = strtolower(substr($string, 0, 1));
- $variable = preg_replace('/\\w/', $replace, $string, 1);
- return $variable;
+ return preg_replace('/\\w/', $replace, $string, 1);
}
/**
* Returns a string with all spaces converted to underscores (by default), accented
@@ -522,8 +512,7 @@ function slug($string, $replacement = '_') {
'/\\s+/' => $replacement,
String::insert('/^[:replacement]+|[:replacement]+$/', array('replacement' => preg_quote($replacement, '/'))) => '',
);
- $string = preg_replace(array_keys($map), array_values($map), $string);
- return $string;
+ return preg_replace(array_keys($map), array_values($map), $string);
}
}
/**
@@ -25,7 +25,6 @@
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Core', 'Inflector');
-
/**
* Short description for class.
*

0 comments on commit eb653b8

Please sign in to comment.