Skip to content

Commit

Permalink
Don't use create_function().
Browse files Browse the repository at this point in the history
It's deprecated and unsafe and closures should be used instead.
  • Loading branch information
yunosh committed Feb 13, 2017
1 parent 8375aa2 commit b81e19e
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 121 deletions.
2 changes: 1 addition & 1 deletion chora/templates/diff/header.inc
Expand Up @@ -16,7 +16,7 @@
<div class="commitList">
<?php
$view = $injector->createInstance('Horde_View');
echo $view->renderPartial('app/views/logMessage', array('collection' => array_map(create_function('$a', 'return $a->toHash();'), $log_messages), 'locals' => array('diff_page' => 1)));
echo $view->renderPartial('app/views/logMessage', array('collection' => array_map(function ($a) { return $a->toHash(); }, $log_messages), 'locals' => array('diff_page' => 1)));
?>
</div>
<?php endif; ?>
Expand Up @@ -15,7 +15,10 @@ public function setUp()
public function providerClassNames()
{
return array_map(
create_function('$a', '$a[1] = str_replace("/", DIRECTORY_SEPARATOR, $a[1]); return $a;'),
function ($a) {
$a[1] = str_replace('/', DIRECTORY_SEPARATOR, $a[1]);
return $a;
},
array(
array('Module_Action_Suffix', 'dir/Module/Action/Suffix.php'),
array('MyModule_Action_Suffix', 'dir/MyModule/Action/Suffix.php'),
Expand Down
15 changes: 14 additions & 1 deletion framework/Data/lib/Horde/Data/Csv.php
Expand Up @@ -332,7 +332,20 @@ public static function getCsv($file, array $params = array())

if ($row) {
$row = (strlen($params['quote']) && strlen($params['escape']))
? array_map(create_function('$a', 'return str_replace(\'' . str_replace('\'', '\\\'', $params['escape'] . $params['quote']) . '\', \'' . str_replace('\'', '\\\'', $params['quote']) . '\', $a);'), $row)
? array_map(
function ($a) use ($params) {
return str_replace(
str_replace(
'\'',
'\\\'',
$params['escape'] . $params['quote']
),
str_replace('\'', '\\\'', $params['quote']),
$a
);
},
$row
)
: array_map('trim', $row);

if (!empty($params['length'])) {
Expand Down
13 changes: 12 additions & 1 deletion framework/Date_Parser/lib/Horde/Date/Parser.php
Expand Up @@ -44,7 +44,18 @@ public static function getLocales()
foreach (new DirectoryIterator($dir) as $f) {
if ($f->isFile()) {
$locale = str_replace('.php', '', $f->getFilename());
$locale = preg_replace_callback('/([A-Z][a-z]*)([A-Z].*)?/', create_function('$m', 'if (!isset($m[2])) { return Horde_String::lower($m[1]); } else { return Horde_String::lower($m[1]) . "_" . Horde_String::upper($m[2]); }'), $locale);
$locale = preg_replace_callback(
'/([A-Z][a-z]*)([A-Z].*)?/',
function ($m) {
if (!isset($m[2])) {
return Horde_String::lower($m[1]);
} else {
return Horde_String::lower($m[1])
. '_' . Horde_String::upper($m[2]);
}
},
$locale
);
$locales[] = $locale;
}
}
Expand Down
31 changes: 25 additions & 6 deletions framework/Date_Parser/lib/Horde/Date/Parser/Locale/Base.php
Expand Up @@ -97,7 +97,9 @@ public function parse($text, $specifiedOptions = array())
}

// strip any non-tagged tokens
$taggedTokens = array_values(array_filter($tokens, create_function('$t', 'return $t->tagged();')));
$taggedTokens = array_values(array_filter(
$tokens, function ($t) { return $t->tagged(); }
));

// Remove tokens we know we don't want - for example, if the first token
// is a separator, drop it.
Expand Down Expand Up @@ -197,7 +199,10 @@ public function numericizeOrdinals($text)
*/
public function preTokenize($text)
{
return array_map(create_function('$w', 'return new Horde_Date_Parser_Token($w);'), preg_split('/\s+/', $text));
return array_map(
function ($w) { return new Horde_Date_Parser_Token($w); },
preg_split('/\s+/', $text)
);
}

/**
Expand Down Expand Up @@ -292,7 +297,9 @@ public function tokensToSpan($tokens, $options)
// maybe it's a specific date
foreach ($this->definitions['date'] as $handler) {
if ($handler->match($tokens, $this->definitions)) {
$goodTokens = array_values(array_filter($tokens, create_function('$o', 'return !$o->getTag("separator");')));
$goodTokens = array_values(array_filter(
$tokens, function ($o) { return !$o->getTag('separator'); }
));
$this->debug($handler->handlerMethod, $goodTokens, $options);
return call_user_func(array($this, $handler->handlerMethod), $goodTokens, $options);
}
Expand All @@ -301,7 +308,9 @@ public function tokensToSpan($tokens, $options)
// I guess it's not a specific date, maybe it's just an anchor
foreach ($this->definitions['anchor'] as $handler) {
if ($handler->match($tokens, $this->definitions)) {
$goodTokens = array_values(array_filter($tokens, create_function('$o', 'return !$o->getTag("separator");')));
$goodTokens = array_values(array_filter(
$tokens, function ($o) { return !$o->getTag('separator'); }
));
$this->debug($handler->handlerMethod, $goodTokens, $options);
return call_user_func(array($this, $handler->handlerMethod), $goodTokens, $options);
}
Expand All @@ -310,7 +319,14 @@ public function tokensToSpan($tokens, $options)
// not an anchor, perhaps it's an arrow
foreach ($this->definitions['arrow'] as $handler) {
if ($handler->match($tokens, $this->definitions)) {
$goodTokens = array_values(array_filter($tokens, create_function('$o', 'return !$o->getTag("separator_at") && !$o->getTag("separator_slash_or_dash") && !$o->getTag("separator_comma");')));
$goodTokens = array_values(array_filter(
$tokens,
function ($o) {
return !$o->getTag('separator_at') &&
!$o->getTag('separator_slash_or_dash') &&
!$o->getTag('separator_comma');
}
));
$this->debug($handler->handlerMethod, $goodTokens, $options);
return call_user_func(array($this, $handler->handlerMethod), $goodTokens, $options);
}
Expand Down Expand Up @@ -603,7 +619,10 @@ public function getRepeaters($tokens)
}

// Return repeaters in order from widest (years) to smallest (seconds)
usort($repeaters, create_function('$a, $b', 'return $b->width() > $a->width();'));
usort(
$repeaters,
function ($a, $b) { return $b->width() > $a->width(); }
);
return $repeaters;
}

Expand Down
4 changes: 3 additions & 1 deletion framework/Date_Parser/lib/Horde/Date/Parser/Locale/Pt.php
Expand Up @@ -100,7 +100,9 @@ public function parse($text, $specifiedOptions = array())
}

// strip any non-tagged tokens
$taggedTokens = array_values(array_filter($tokens, create_function('$t', 'return $t->tagged();')));
$taggedTokens = array_values(array_filter(
$tokens, function ($t) { return $t->tagged(); }
));

// Remove tokens we know we don't want - for example, if the first
// token is a separator, drop it.
Expand Down
16 changes: 12 additions & 4 deletions framework/Date_Parser/lib/Horde/Date/Parser/Result.php
Expand Up @@ -28,14 +28,22 @@ public function guess()

public function taggedText()
{
$taggedTokens = array_values(array_filter($this->tokens, create_function('$t', 'return $t->tagged();')));
return implode(' ', array_map(create_function('$t', 'return $t->word;'), $taggedTokens));
$taggedTokens = array_values(array_filter(
$this->tokens, function ($t) { return $t->tagged(); }
));
return implode(
' ', array_map(function ($t) { return $t->word; }, $taggedTokens)
);
}

public function untaggedText()
{
$untaggedTokens = array_values(array_filter($this->tokens, create_function('$t', 'return ! $t->tagged();')));
return implode(' ', array_map(create_function('$t', 'return $t->word;'), $untaggedTokens));
$untaggedTokens = array_values(array_filter(
$this->tokens, function ($t) { return !$t->tagged(); }
));
return implode(
' ', array_map(function ($t) { return $t->word; }, $untaggedTokens)
);
}

}
14 changes: 12 additions & 2 deletions framework/Date_Parser/lib/Horde/Date/Parser/Token.php
Expand Up @@ -23,7 +23,12 @@ public function tag($tagClass, $tag)
*/
public function untag($tagClass)
{
$this->tags = array_filter($this->tags, create_function('$t', 'return substr($t[0], 0, ' . strlen($tagClass) . ') != "' . $tagClass . '";'));
$this->tags = array_filter(
$this->tags,
function ($t) use ($tagClass) {
return substr($t[0], 0, strlen($tagClass)) != $tagClass;
}
);
}

/**
Expand All @@ -39,7 +44,12 @@ public function tagged()
*/
public function getTag($tagClass)
{
$matches = array_filter($this->tags, create_function('$t', 'return substr($t[0], 0, ' . strlen($tagClass) . ') == "' . $tagClass . '";'));
$matches = array_filter(
$this->tags,
function ($t) use ($tagClass) {
return substr($t[0], 0, strlen($tagClass)) == $tagClass;
}
);
$match = array_shift($matches);
return $match[1];
}
Expand Down
91 changes: 50 additions & 41 deletions framework/Db/lib/Horde/Db/Adapter/Sqlite/Schema.php
Expand Up @@ -240,13 +240,9 @@ public function addColumn($tableName, $columnName, $type, $options = array())
$this->_alterTable(
$tableName,
array(),
create_function(
'$definition',
sprintf(
'$definition->column("%s", "%s", %s);',
$columnName, $type, var_export($options, true)
)
)
function ($definition) use ($columnName, $type, $options) {
$definition->column($columnName, $type, $options);
}
);
} else {
parent::addColumn($tableName, $columnName, $type, $options);
Expand All @@ -269,8 +265,10 @@ public function removeColumn($tableName, $columnName)
return $this->_alterTable(
$tableName,
array(),
create_function('$definition',
'unset($definition["' . $columnName . '"]);'));
function ($definition) use ($columnName) {
unset($definition[$columnName]);
}
);
}

/**
Expand All @@ -288,39 +286,51 @@ public function changeColumn($tableName, $columnName, $type, $options = array())
$this->_clearTableCache($tableName);

$defs = array(
sprintf('$definition["%s"]->setType("%s");', $columnName, $type),
sprintf('if ("%s" == "autoincrementKey") $definition->primaryKey(false);', $type)
function ($definition) use ($columnName, $type) {
$definition[$columnName]->setType($type);
},
function ($definition) use ($type) {
if ($type == 'autoincrementKey') {
$definition->primaryKey(false);
}
}
);
if (isset($options['limit'])) {
$defs[] = sprintf('$definition["%s"]->setLimit("%s");', $columnName, $options['limit']);
$defs[] = function ($definition) use ($columnName, $options) {
$definition[$columnName]->setLimit($options['limit']);
};
}
if (isset($options['null'])) {
$defs[] = sprintf('$definition["%s"]->setNull((bool)%d);', $columnName, $options['null']);
$defs[] = function ($definition) use ($columnName, $options) {
$definition[$columnName]->setNull((bool)$options['null']);
};
}
if (isset($options['precision'])) {
$defs[] = sprintf('$definition["%s"]->setPrecision("%s");', $columnName, $options['precision']);
$defs[] = function ($definition) use ($columnName, $options) {
$definition[$columnName]->setPrecision($options['precision']);
};
}
if (isset($options['scale'])) {
$defs[] = sprintf('$definition["%s"]->setScale("%s");', $columnName, $options['scale']);
$defs[] = function ($definition) use ($columnName, $options) {
$definition[$columnName]->setScale($options['scale']);
};
}

if (array_key_exists('default', $options)) {
if ($options['default'] === true) {
$default = 'true';
} elseif ($options['default'] === false) {
$default = 'false';
} elseif ($options['default'] === null) {
$default = 'null';
} else {
$default = '"' . $options['default'] . '"';
}
$defs[] = sprintf('$definition["%s"]->setDefault(%s);', $columnName, $default);
$defs[] = function ($definition) use ($columnName, $options) {
$definition[$columnName]->setDefault($options['default']);
};
}

return $this->_alterTable(
$tableName,
array(),
create_function('$definition', implode("\n", $defs)));
function ($definition) use ($defs) {
foreach ($defs as $callback) {
$callback($definition);
}
}
);
}

/**
Expand All @@ -337,13 +347,13 @@ public function changeColumnDefault($tableName, $columnName, $default)
{
$this->_clearTableCache($tableName);

$default = is_null($default) ? 'null' : '"' . $default . '"';
return $this->_alterTable(
$tableName,
array(),
create_function('$definition',
sprintf('$definition["%s"]->setDefault(%s);',
$columnName, $default)));
function ($definition) use ($columnName, $default) {
$definition[$columnName]->setDefault($default);
}
);
}

/**
Expand Down Expand Up @@ -374,13 +384,9 @@ public function addPrimaryKey($tableName, $columns)
{
$this->_clearTableCache($tableName);
$columns = (array)$columns;
foreach ($columns as &$column) {
$column = '"' . $column . '"';
}
$callback = create_function(
'$definition',
sprintf('$definition->primaryKey(array(%s));',
implode(', ', $columns)));
$callback = function ($definition) use ($columns) {
$definition->primaryKey($columns);
};
$this->_alterTable($tableName, array(), $callback);
}

Expand All @@ -394,8 +400,9 @@ public function addPrimaryKey($tableName, $columns)
public function removePrimaryKey($tableName)
{
$this->_clearTableCache($tableName);
$callback = create_function('$definition',
'$definition->primaryKey(false);');
$callback = function ($definition) {
$definition->primaryKey(false);
};
$this->_alterTable($tableName, array(), $callback);
}

Expand Down Expand Up @@ -647,8 +654,10 @@ protected function _copyTable($from, $to, $options = array(),
$this->_copyTableContents(
$from,
$to,
array_map(create_function('$c', 'return $c->getName();'),
iterator_to_array($definition)),
array_map(
function($c) { return $c->getName(); },
iterator_to_array($definition)
),
isset($options['rename']) ? $options['rename'] : array());
}

Expand Down
2 changes: 1 addition & 1 deletion framework/Db/test/Horde/Db/Adapter/TestBase.php
Expand Up @@ -488,7 +488,7 @@ public function testIndexes()
$this->assertEquals(3, count($indexes));

// sort by name so we can predict the order of indexes
usort($indexes, create_function('$a, $b', 'return strcmp($a->name, $b->name);'));
usort($indexes, function ($a, $b) { return strcmp($a->name, $b->name); });

// multi-column index
$col = array('integer_value', 'string_value');
Expand Down
2 changes: 1 addition & 1 deletion framework/Rpc/lib/Horde/Rpc/Soap.php
Expand Up @@ -87,7 +87,7 @@ function getResponse($request)
Horde::log(
sprintf('SOAP call: %s(%s) by %s serviced in %d seconds, sent %d bytes in response',
$GLOBALS['__horde_rpc_PhpSoap']['lastMethodCalled'],
implode(', ', array_map(create_function('$a', 'return is_array($a) ? "Array" : $a;'),
implode(', ', array_map(function ($a) { return is_array($a) ? 'Array' : $a; },
$GLOBALS['__horde_rpc_PhpSoap']['lastMethodParams'])),
$GLOBALS['registry']->getAuth(),
time() - $beginTime,
Expand Down

0 comments on commit b81e19e

Please sign in to comment.