Skip to content

Commit

Permalink
Performance optimizations #1 (joomla#12171)
Browse files Browse the repository at this point in the history
* Fixed callable call in loop termination condition

* Fixed non-optimal regular expression

* Shortened syntax for applied operations

* Don't use strlen() to check if string is empty.

* Reverted preg changes. Look into those later again.

* Replaced call_user_func_array() with call_user_func()

* Merge unset() calls

* Replaced old intval() call with modern typecasting

* Optimize away substr() as index-based access

* Replace some cases of substr() with strpos()

* Replace some stristr() with stripos()

* Replace is_null() with null === ...

* Removed unnecessary ternary operators

* Fix for Juri...

* Revert some changes for ... PHP5.3 compatibility

* Make travis happy ....

* Make codesniffer happy...

* DYC!

* Optimize non-optimal if conditions.

* codsniffer...

* Inline one-time use variables

* Codesniffer...

* Flipped comparisons
`null === ...`  to Joomla Style `... === null`
`null !== ...`  to Joomla Style `... !== null`

* Flipped comparisons
`false !== ...`  to Joomla Style `... !== falsel`

* Fix removal of ternary operator. Forgot inversion.

* More flipping of  comparisons ;)

* Removed another unnecessary set of ternary operators.

* Codesniffer...

* Type-safe comparisons on strings

* More type-safe comparisons

* Changes that occurred during PR discussion

* Remove some unnecessary parentheses

* Removed empty if-group and reversed condition

* Corrections on errors during merge (Conflict resolving)

* Missed one.

* Remove a parenthesis

* Changes according to reviewer's comment

* Revert change because of failure

* Changes according to reviewer's comment
+ a few more changes found last minute

* reversed change, though according to the method's doc that value should have been an integer...

* Hopefully fixed...

* Hopefully fixed...

* Re-apply some changes
  • Loading branch information
frankmayer authored and rdeutz committed Jun 15, 2017
1 parent 02c1f2f commit bcfbfa0
Show file tree
Hide file tree
Showing 56 changed files with 155 additions and 160 deletions.
2 changes: 1 addition & 1 deletion libraries/cms/application/administrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public static function purgeMessages()
$config = $db->loadObject();

// Check if auto_purge value set
if (is_object($config) and $config->cfg_name === 'auto_purge')
if (is_object($config) && $config->cfg_name === 'auto_purge')
{
$purge = $config->cfg_value;
}
Expand Down
10 changes: 5 additions & 5 deletions libraries/cms/application/cms.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ public function __construct(JInput $input = null, Registry $config = null, JAppl
}

// Enable sessions by default.
if (is_null($this->config->get('session')))
if ($this->config->get('session') === null)
{
$this->config->set('session', true);
}

// Set the session default name.
if (is_null($this->config->get('session_name')))
if ($this->config->get('session_name') === null)
{
$this->config->set('session_name', $this->getName());
}
Expand Down Expand Up @@ -235,7 +235,7 @@ public function checkSession()
public function enqueueMessage($msg, $type = 'message')
{
// Don't add empty messages.
if (!strlen(trim($msg)))
if (trim($msg) === '')
{
return;
}
Expand Down Expand Up @@ -595,7 +595,7 @@ public function getUserState($key, $default = null)
$session = JFactory::getSession();
$registry = $session->get('registry');

if (!is_null($registry))
if ($registry !== null)
{
return $registry->get($key, $default);
}
Expand Down Expand Up @@ -1170,7 +1170,7 @@ public function setUserState($key, $value)
$session = JFactory::getSession();
$registry = $session->get('registry');

if (!is_null($registry))
if ($registry !== null)
{
return $registry->set($key, $value);
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/application/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static function getClientInfo($id = null, $byName = false)
}

// If no client id has been passed return the whole array
if (is_null($id))
if ($id === null)
{
return self::$_clients;
}
Expand Down
6 changes: 3 additions & 3 deletions libraries/cms/application/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ protected function initialiseApp($options = array())
}
}

if ($this->getLanguageFilter() && empty($options['language']))
if (empty($options['language']) && $this->getLanguageFilter())
{
// Detect cookie language
$lang = $this->input->cookie->get(md5($this->get('secret') . 'language'), null, 'string');
Expand All @@ -646,7 +646,7 @@ protected function initialiseApp($options = array())
}
}

if ($this->getDetectBrowser() && empty($options['language']))
if (empty($options['language']) && $this->getDetectBrowser())
{
// Detect browser language
$lang = JLanguageHelper::detectLanguage();
Expand Down Expand Up @@ -748,7 +748,7 @@ protected function render()
$template = $this->getTemplate(true);
$file = $this->input->get('tmpl', 'index');

if (!$this->get('offline') && $file === 'offline')
if ($file === 'offline' && !$this->get('offline'))
{
$this->set('themeFile', 'index.php');
}
Expand Down
4 changes: 2 additions & 2 deletions libraries/cms/captcha/captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function initialise($id)
public function display($name, $id, $class = '')
{
// Check if captcha is already loaded.
if (is_null($this->_captcha))
if ($this->_captcha === null)
{
return;
}
Expand Down Expand Up @@ -188,7 +188,7 @@ public function display($name, $id, $class = '')
public function checkAnswer($code)
{
// Check if captcha is already loaded
if (is_null(($this->_captcha)))
if ($this->_captcha === null)
{
return;
}
Expand Down
7 changes: 1 addition & 6 deletions libraries/cms/component/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,7 @@ public static function filterText($text)
$whiteListTags = array_unique($whiteListTags);
$whiteListAttributes = array_unique($whiteListAttributes);

// Unfiltered assumes first priority.
if ($unfiltered)
{
// Dont apply filtering.
}
else
if (!$unfiltered)
{
// Custom blacklist precedes Default blacklist
if ($customList)
Expand Down
10 changes: 5 additions & 5 deletions libraries/cms/editor/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public function detach($observer)
public function initialise()
{
// Check if editor is already loaded
if (is_null(($this->_editor)))
if ($this->_editor === null)
{
return;
}
Expand All @@ -261,7 +261,7 @@ public function initialise()

$document = JFactory::getDocument();

if (method_exists($document, 'addCustomTag') && !empty($return))
if (!empty($return) && method_exists($document, 'addCustomTag'))
{
$document->addCustomTag($return);
}
Expand Down Expand Up @@ -293,7 +293,7 @@ public function display($name, $html, $width, $height, $col, $row, $buttons = tr
$this->_loadEditor($params);

// Check whether editor is already loaded
if (is_null(($this->_editor)))
if ($this->_editor === null)
{
JFactory::getApplication()->enqueueMessage(JText::_('JLIB_NO_EDITOR_PLUGIN_PUBLISHED'), 'error');

Expand Down Expand Up @@ -349,7 +349,7 @@ public function save($editor)
$this->_loadEditor();

// Check whether editor is already loaded
if (is_null(($this->_editor)))
if ($this->_editor === null)
{
return;
}
Expand Down Expand Up @@ -517,7 +517,7 @@ public function getButtons($editor, $buttons = true)
protected function _loadEditor($config = array())
{
// Check whether editor is already loaded
if (!is_null(($this->_editor)))
if ($this->_editor !== null)
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/form/field/captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function setup(SimpleXMLElement $element, $value, $group = null)

if (strpos($this->class, 'required') === false)
{
$this->class = $this->class . ' required';
$this->class .= ' required';
}
}

Expand Down
12 changes: 6 additions & 6 deletions libraries/cms/form/field/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ public function __set($name, $value)
case 'buttons':
$value = (string) $value;

if ($value === 'true' || $value === 'yes' || $value == '1')
if ($value === 'true' || $value === 'yes' || $value === '1')
{
$this->buttons = true;
}
elseif ($value === 'false' || $value === 'no' || $value == '0')
elseif ($value === 'false' || $value === 'no' || $value === '0')
{
$this->buttons = false;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ public function setup(SimpleXMLElement $element, $value, $group = null)
{
$result = parent::setup($element, $value, $group);

if ($result == true)
if ($result === true)
{
$this->height = $this->element['height'] ? (string) $this->element['height'] : '500';
$this->width = $this->element['width'] ? (string) $this->element['width'] : '100%';
Expand All @@ -210,11 +210,11 @@ public function setup(SimpleXMLElement $element, $value, $group = null)
$hide = (string) $this->element['hide'];
$editorType = (string) $this->element['editor'];

if ($buttons === 'true' || $buttons === 'yes' || $buttons == '1')
if ($buttons === 'true' || $buttons === 'yes' || $buttons === '1')
{
$this->buttons = true;
}
elseif ($buttons === 'false' || $buttons === 'no' || $buttons == '0')
elseif ($buttons === 'false' || $buttons === 'no' || $buttons === '0')
{
$this->buttons = false;
}
Expand Down Expand Up @@ -296,7 +296,7 @@ protected function getEditor()
}

// Create the JEditor instance based on the given editor.
if (is_null($editor))
if ($editor === null)
{
$editor = JFactory::getConfig()->get('editor');
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/form/field/menuitem.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function setup(SimpleXMLElement $element, $value, $group = null)
{
$result = parent::setup($element, $value, $group);

if ($result == true)
if ($result === true)
{
$this->menuType = (string) $this->element['menu_type'];
$this->clientId = (int) $this->element['client_id'];
Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/form/field/moduleposition.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function setup(SimpleXMLElement $element, $value, $group = null)
{
$result = parent::setup($element, $value, $group);

if ($result == true)
if ($result === true)
{
// Get the client id.
$clientId = $this->element['client_id'];
Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/form/field/ordering.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function setup(SimpleXMLElement $element, $value, $group = null)
{
$result = parent::setup($element, $value, $group);

if ($result == true)
if ($result === true)
{
$this->contentType = (string) $this->element['content_type'];
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/form/field/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ protected function prepareOptionsNested(&$options)
*/
public function isNested()
{
if (is_null($this->isNested))
if ($this->isNested === null)
{
// If mode="nested" || ( mode not set & config = nested )
if (isset($this->element['mode']) && $this->element['mode'] === 'nested'
Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/form/field/templatestyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function setup(SimpleXMLElement $element, $value, $group = null)
{
$result = parent::setup($element, $value, $group);

if ($result == true)
if ($result === true)
{
// Get the clientName template.
$this->clientName = $this->element['client'] ? (string) $this->element['client'] : 'site';
Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/form/rule/notequals.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function test(SimpleXMLElement $element, $value, $group = null, Registry
throw new UnexpectedValueException(sprintf('$field empty in %s::test', get_class($this)));
}

if (is_null($input))
if ($input === null)
{
throw new InvalidArgumentException(sprintf('The value for $input must not be null in %s', get_class($this)));
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/help/help.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static function createUrl($ref, $useComponent = false, $override = null,
$local = false;
$app = JFactory::getApplication();

if (is_null($component))
if ($component === null)
{
$component = JApplicationHelper::getComponentName();
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/helper/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static function _getActions($categoryId = 0, $id = 0, $assetName = '')
public static function getActions($component = '', $section = '', $id = 0)
{
// Check for deprecated arguments order
if (is_int($component) || is_null($component))
if (is_int($component) || $component === null)
{
$result = self::_getActions($component, $section, $id);

Expand Down
9 changes: 4 additions & 5 deletions libraries/cms/helper/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public function canUpload($file, $component = 'com_media')
foreach ($html_tags as $tag)
{
// A tag is '<tagname ', so we need to add < and a space or '<tagname>'
if (stristr($xss_check, '<' . $tag . ' ') || stristr($xss_check, '<' . $tag . '>'))
if (stripos($xss_check, '<' . $tag . ' ') !== false || stripos($xss_check, '<' . $tag . '>') !== false)
{
$app->enqueueMessage(JText::_('JLIB_MEDIA_ERROR_WARNIEXSS'), 'error');

Expand Down Expand Up @@ -360,15 +360,14 @@ public function countFiles($dir)
{
$d = dir($dir);

while (false !== ($entry = $d->read()))
while (($entry = $d->read()) !== false)
{
if (substr($entry, 0, 1) !== '.' && is_file($dir . DIRECTORY_SEPARATOR . $entry)
&& strpos($entry, '.html') === false && strpos($entry, '.php') === false)
if ($entry[0] !== '.' && strpos($entry, '.html') === false && strpos($entry, '.php') === false && is_file($dir . DIRECTORY_SEPARATOR . $entry))
{
$total_file++;
}

if (substr($entry, 0, 1) !== '.' && is_dir($dir . DIRECTORY_SEPARATOR . $entry))
if ($entry[0] !== '.' && is_dir($dir . DIRECTORY_SEPARATOR . $entry))
{
$total_dir++;
}
Expand Down
4 changes: 2 additions & 2 deletions libraries/cms/helper/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected function findItem($needles = array())
$language = isset($needles['language']) ? $needles['language'] : '*';

// $this->extension may not be set if coming from a static method, check it
if (is_null($this->extension))
if ($this->extension === null)
{
$this->extension = $app->input->getCmd('option');
}
Expand Down Expand Up @@ -180,7 +180,7 @@ protected function findItem($needles = array())
* $language != * can override existing entries
* $language == * cannot override existing entries
*/
if (!isset(static::$lookup[$language][$view][$item->query['id']]) || $item->language !== '*')
if ($item->language !== '*' || !isset(static::$lookup[$language][$view][$item->query['id']]))
{
static::$lookup[$language][$view][$item->query['id']] = $item->id;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/helper/tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function createTagsFromField($tags)
foreach ($tags as $key => $tag)
{
// User is not allowed to create tags, so don't create.
if (strpos($tag, '#new#') !== false && !$canCreate)
if (!$canCreate && strpos($tag, '#new#') !== false)
{
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions libraries/cms/helper/usergroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function count()
*/
public static function getInstance()
{
if (null === static::$instance)
if (static::$instance === null)
{
// Only here to avoid code style issues...
$groups = array();
Expand Down Expand Up @@ -165,7 +165,7 @@ public function getAll()
*/
public function has($id)
{
return (array_key_exists($id, $this->groups) && false !== $this->groups[$id]);
return (array_key_exists($id, $this->groups) && $this->groups[$id] !== false);
}

/**
Expand All @@ -189,7 +189,7 @@ private function isSingleton()
*/
public function total()
{
if (null === $this->total)
if ($this->total === null)
{
$db = JFactory::getDbo();

Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/html/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public static function assetgrouplist($name, $selected, $attribs = null, $config
$name,
array(
'id' => isset($config['id']) ? $config['id'] : 'assetgroups_' . (++$count),
'list.attr' => is_null($attribs) ? 'class="inputbox" size="3"' : $attribs,
'list.attr' => $attribs === null ? 'class="inputbox" size="3"' : $attribs,
'list.select' => (int) $selected,
)
);
Expand Down
2 changes: 1 addition & 1 deletion libraries/cms/html/behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public static function keepalive()
static::core();
static::polyfill('event', 'lt IE 9');

// Add keepalive script options.
// Add keepalive script options.
JFactory::getDocument()->addScriptOptions('system.keepalive', array('interval' => $refreshTime * 1000, 'uri' => JRoute::_($uri)));

// Add script.
Expand Down
Loading

0 comments on commit bcfbfa0

Please sign in to comment.