Skip to content

Commit

Permalink
Do fewer allocations for simple default values.
Browse files Browse the repository at this point in the history
Don't allocate arrays when we are only assigning a single array key
value.
  • Loading branch information
markstory committed Aug 29, 2016
1 parent 10e5734 commit 80c123b
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 16 deletions.
6 changes: 2 additions & 4 deletions src/Auth/FallbackPasswordHasher.php
Expand Up @@ -49,10 +49,8 @@ public function __construct(array $config = [])
{
parent::__construct($config);
foreach ($this->_config['hashers'] as $key => $hasher) {
if (!is_string($hasher)) {
$hasher += [
'className' => $key,
];
if (is_array($hasher) && !isset($hasher['className'])) {
$hasher['className'] = $key;
}
$this->_hashers[] = PasswordHasherFactory::build($hasher);
}
Expand Down
8 changes: 5 additions & 3 deletions src/Controller/Component/FlashComponent.php
Expand Up @@ -88,12 +88,14 @@ public function set($message, array $options = [])
$options += $this->config();

if ($message instanceof Exception) {
$options['params'] += ['code' => $message->getCode()];
if (!isset($options['params']['code'])) {
$options['params']['code'] = $message->getCode();
}
$message = $message->getMessage();
}

if (isset($options['escape'])) {
$options['params'] += ['escape' => $options['escape']];
if (isset($options['escape']) && !isset($options['params']['escape'])) {
$options['params']['escape'] = $options['escape'];
}

list($plugin, $element) = pluginSplit($options['element']);
Expand Down
4 changes: 2 additions & 2 deletions src/Mailer/Email.php
Expand Up @@ -1390,8 +1390,8 @@ public static function deliver($to = null, $subject = null, $message = null, $tr
{
$class = __CLASS__;

if (is_array($transportConfig)) {
$transportConfig += ['transport' => 'default'];
if (is_array($transportConfig) && !isset($transportConfig['transport'])) {
$transportConfig['transport'] = 'default';
}
$instance = new $class($transportConfig);
if ($to !== null) {
Expand Down
4 changes: 3 additions & 1 deletion src/ORM/Association/BelongsToMany.php
Expand Up @@ -375,7 +375,9 @@ protected function _appendNotMatching($query, $options)
if (empty($options['negateMatch'])) {
return;
}
$options += ['conditions' => []];
if (!isset($options['conditions'])) {
$options['conditions'] = [];
}
$junction = $this->junction();
$belongsTo = $junction->association($this->source()->alias());
$conds = $belongsTo->_joinCondition(['foreignKey' => $belongsTo->foreignKey()]);
Expand Down
4 changes: 3 additions & 1 deletion src/ORM/Association/SelectableAssociationTrait.php
Expand Up @@ -85,7 +85,9 @@ protected function _buildQuery($options)

$finder = isset($options['finder']) ? $options['finder'] : $this->finder();
list($finder, $opts) = $this->_extractFinder($finder);
$options += ['fields' => []];
if (!isset($options['fields'])) {
$options['fields'] = [];
}

$fetchQuery = $this
->find($finder, $opts)
Expand Down
4 changes: 3 additions & 1 deletion src/ORM/EagerLoader.php
Expand Up @@ -200,12 +200,14 @@ public function matching($assoc = null, callable $builder = null, $options = [])
if ($assoc === null) {
return $this->_matching->contain();
}
if (!isset($options['joinType'])) {
$options['joinType'] = 'INNER';
}

$assocs = explode('.', $assoc);
$last = array_pop($assocs);
$containments = [];
$pointer =& $containments;
$options += ['joinType' => 'INNER'];
$opts = ['matching' => true] + $options;
unset($opts['negateMatch']);

Expand Down
4 changes: 3 additions & 1 deletion src/ORM/Marshaller.php
Expand Up @@ -79,7 +79,9 @@ protected function _buildPropertyMap($data, $options)
}

// Map associations
$options += ['associated' => []];
if (!isset($options['associated'])) {
$options['associated'] = [];
}
$include = $this->_normalizeAssociations($options['associated']);
foreach ($include as $key => $nested) {
if (is_int($key) && is_scalar($nested)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Routing/RouteBuilder.php
Expand Up @@ -454,8 +454,8 @@ public function resources($name, $options = [], $callback = null)
*/
public function connect($route, array $defaults = [], array $options = [])
{
if (empty($options['action'])) {
$defaults += ['action' => 'index'];
if (!isset($defaults['action'])) {
$defaults['action'] = 'index';
}

if (empty($options['_ext'])) {
Expand Down
4 changes: 3 additions & 1 deletion src/Routing/Router.php
Expand Up @@ -223,10 +223,12 @@ public static function connect($route, $defaults = [], $options = [])
*/
public static function redirect($route, $url, $options = [])
{
$options += ['routeClass' => 'Cake\Routing\Route\RedirectRoute'];
if (is_string($url)) {
$url = ['redirect' => $url];
}
if (!isset($options['routeClass'])) {
$options['routeClass'] = 'Cake\Routing\Route\RedirectRoute';
}
static::connect($route, $url, $options);
}

Expand Down

0 comments on commit 80c123b

Please sign in to comment.