Skip to content

Commit

Permalink
Performance 3 (layouts) (joomla#12225)
Browse files Browse the repository at this point in the history
* Don't use strlen() to check if string is empty.

* Compare strings type-safely

* Optimize non-optimal if conditions.

* Use Elvis instead of full ternary

* Remove redundant closing tags

* Replace double quotes with single quotes

* Remove unnecessary parentheses

* Remove unnecessary semicolon

* Remove unnecessary parentheses

* Some more optimization of non optimal ifs

* Some more type-safe comparisons

* Missed a type-safe comparison

* Requested changes
  • Loading branch information
frankmayer authored and rdeutz committed Oct 18, 2016
1 parent 82d3c9a commit d82a2be
Show file tree
Hide file tree
Showing 26 changed files with 50 additions and 53 deletions.
7 changes: 4 additions & 3 deletions layouts/joomla/content/blog_style_default_item_title.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
JHtml::addIncludePath(JPATH_COMPONENT.'/helpers/html');
?>

<?php if ($params->get('show_title') || $displayData->state == 0 || ($params->get('show_author') && !empty($displayData->author ))) : ?>
<?php if ($displayData->state == 0 || $params->get('show_title') || ($params->get('show_author') && !empty($displayData->author ))) : ?>
<div class="page-header">
<?php if ($params->get('show_title')) : ?>
<h2 itemprop="name">
Expand All @@ -39,8 +39,9 @@
<span class="label label-warning"><?php echo JText::_('JNOTPUBLISHEDYET'); ?></span>
<?php endif; ?>

<?php if ((strtotime($displayData->publish_down) < strtotime(JFactory::getDate()))
&& $displayData->publish_down != JFactory::getDbo()->getNullDate()) : ?>
<?php if ($displayData->publish_down != JFactory::getDbo()->getNullDate()
&& (strtotime($displayData->publish_down) < strtotime(JFactory::getDate()))
) : ?>
<span class="label label-warning"><?php echo JText::_('JEXPIRED'); ?></span>
<?php endif; ?>
</div>
Expand Down
2 changes: 1 addition & 1 deletion layouts/joomla/content/categories_default_items.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
$className = substr($extension, 4);
// This will work for the core components but not necessarily for other components
// that may have different pluralisation rules.
if (substr($className, -1) == 's')
if (substr($className, -1) === 's')
{
$className = rtrim($className, 's');
}
4 changes: 2 additions & 2 deletions layouts/joomla/content/category_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* This will work for the core components but not necessarily for other components
* that may have different pluralisation rules.
*/
if (substr($className, -1) == 's')
if (substr($className, -1) === 's')
{
$className = rtrim($className, 's');
}
Expand Down Expand Up @@ -59,7 +59,7 @@
<?php endif; ?>
<?php echo $displayData->loadTemplate($displayData->subtemplatename); ?>

<?php if ($displayData->get('children') && $displayData->maxLevel != 0) : ?>
<?php if ($displayData->maxLevel != 0 && $displayData->get('children')) : ?>
<div class="cat-children">
<?php if ($params->get('show_category_heading_title_text', 1) == 1) : ?>
<h3>
Expand Down
2 changes: 1 addition & 1 deletion layouts/joomla/content/info_block/author.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

?>
<dd class="createdby" itemprop="author" itemscope itemtype="https://schema.org/Person">
<?php $author = ($displayData['item']->created_by_alias ? $displayData['item']->created_by_alias : $displayData['item']->author); ?>
<?php $author = ($displayData['item']->created_by_alias ?: $displayData['item']->author); ?>
<?php $author = '<span itemprop="name">' . $author . '</span>'; ?>
<?php if (!empty($displayData['item']->contact_link ) && $displayData['params']->get('link_author') == true) : ?>
<?php echo JText::sprintf('COM_CONTENT_WRITTEN_BY', JHtml::_('link', $displayData['item']->contact_link, $author, array('itemprop' => 'url'))); ?>
Expand Down
8 changes: 4 additions & 4 deletions layouts/joomla/content/info_block/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
?>
<dl class="article-info muted">

<?php if ($displayData['position'] == 'above' && ($blockPosition == 0 || $blockPosition == 2)
|| $displayData['position'] == 'below' && ($blockPosition == 1)
<?php if ($displayData['position'] === 'above' && ($blockPosition == 0 || $blockPosition == 2)
|| $displayData['position'] === 'below' && ($blockPosition == 1)
) : ?>

<dt class="article-info-term">
Expand All @@ -41,8 +41,8 @@
<?php endif; ?>
<?php endif; ?>

<?php if ($displayData['position'] == 'above' && ($blockPosition == 0)
|| $displayData['position'] == 'below' && ($blockPosition == 1 || $blockPosition == 2)
<?php if ($displayData['position'] === 'above' && ($blockPosition == 0)
|| $displayData['position'] === 'below' && ($blockPosition == 1 || $blockPosition == 2)
) : ?>
<?php if ($displayData['params']->get('show_create_date')) : ?>
<?php echo JLayoutHelper::render('joomla.content.info_block.create_date', $displayData); ?>
Expand Down
2 changes: 1 addition & 1 deletion layouts/joomla/content/intro_image.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
?>
<?php $images = json_decode($displayData->images); ?>
<?php if (isset($images->image_intro) && !empty($images->image_intro)) : ?>
<?php $imgfloat = (empty($images->float_intro)) ? $params->get('float_intro') : $images->float_intro; ?>
<?php $imgfloat = empty($images->float_intro) ? $params->get('float_intro') : $images->float_intro; ?>
<div class="pull-<?php echo htmlspecialchars($imgfloat, ENT_COMPAT, 'UTF-8'); ?> item-image">
<?php if ($params->get('link_titles') && $params->get('access-view')) : ?>
<a href="<?php echo JRoute::_(ContentHelperRoute::getArticleRoute($displayData->slug, $displayData->catid, $displayData->language)); ?>"><img
Expand Down
2 changes: 1 addition & 1 deletion layouts/joomla/content/options_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
$showonarr[] = array(
'field' => $displayData->form->getFormControl() . '[' . $displayData->form->getFieldAttribute($showon[0], 'name') . ']',
'values' => explode(',', $showon[1]),
'op' => (preg_match('%\[(AND|OR)\]' . $showonfield . '%', $showonstring, $matches)) ? $matches[1] : ''
'op' => preg_match('%\[(AND|OR)\]' . $showonfield . '%', $showonstring, $matches) ? $matches[1] : ''
);
}

Expand Down
4 changes: 2 additions & 2 deletions layouts/joomla/content/readmore.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
elseif ($readmore = $item->alternative_readmore) :
echo $readmore;
if ($params->get('show_readmore_title', 0) != 0) :
echo JHtml::_('string.truncate', ($item->title), $params->get('readmore_limit'));
echo JHtml::_('string.truncate', $item->title, $params->get('readmore_limit'));
endif;
elseif ($params->get('show_readmore_title', 0) == 0) :
echo JText::sprintf('COM_CONTENT_READ_MORE_TITLE');
else :
echo JText::_('COM_CONTENT_READ_MORE');
echo JHtml::_('string.truncate', ($item->title), $params->get('readmore_limit'));
echo JHtml::_('string.truncate', $item->title, $params->get('readmore_limit'));
endif; ?>
</a>
</p>
2 changes: 1 addition & 1 deletion layouts/joomla/edit/associations.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
defined('JPATH_BASE') or die;

// JLayout for standard handling of associations fields in the administrator items edit screens.
if ($displayData->getForm()->getValue('id') != 0 && $displayData->getForm()->getValue('language') != "*")
if ($displayData->getForm()->getValue('id') != 0 && $displayData->getForm()->getValue('language') !== '*')
{
echo $displayData->getForm()->renderFieldset('item_associations');
}
Expand Down
4 changes: 2 additions & 2 deletions layouts/joomla/edit/frontediting_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// Add css class jmoddiv and data attributes for module-editing URL and for the tooltip:
$editUrl = JUri::base() . 'administrator/index.php?option=com_modules&task=module.edit&id=' . (int) $mod->id;

if ($parameters->get('redirect_edit', 'site') == 'site')
if ($parameters->get('redirect_edit', 'site') === 'site')
{
$editUrl = JUri::base() . 'index.php?option=com_config&controller=config.display.modules&id=' . (int) $mod->id . $redirectUri;
$target = '_self';
Expand All @@ -48,7 +48,7 @@
)
. '"'
// And if menu editing is enabled and allowed and it's a menu module, add data attributes for menu editing:
. ($menusEditing && $mod->module == 'mod_menu' ?
. ($menusEditing && $mod->module === 'mod_menu' ?
'" data-jmenuedittip="' . JHtml::tooltipText('JLIB_HTML_EDIT_MENU_ITEM', 'JLIB_HTML_EDIT_MENU_ITEM_ID') . '"'
:
''
Expand Down
2 changes: 1 addition & 1 deletion layouts/joomla/edit/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$input = $app->input;
$component = $input->getCmd('option', 'com_content');

if ($component == 'com_categories')
if ($component === 'com_categories')
{
$extension = $input->getCmd('extension', 'com_content');
$parts = explode('.', $extension);
Expand Down
4 changes: 2 additions & 2 deletions layouts/joomla/edit/metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<?php
// Include the real fields in this panel.
if ($name == 'jmetadata')
if ($name === 'jmetadata')
{
echo $form->renderField('metadesc');
echo $form->renderField('metakey');
Expand All @@ -31,7 +31,7 @@

foreach ($form->getFieldset($name) as $field)
{
if ($field->name != 'jform[metadata][tags][]')
if ($field->name !== 'jform[metadata][tags][]')
{
echo $field->renderField();
}
Expand Down
9 changes: 5 additions & 4 deletions layouts/joomla/edit/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
foreach ($fieldSets as $name => $fieldSet)
{
// Ensure any fieldsets we don't want to show are skipped (including repeating formfield fieldsets)
if (in_array($name, $ignoreFieldsets) || (!empty($configFieldsets) && in_array($name, $configFieldsets))
|| !empty($hiddenFieldsets) && in_array($name, $hiddenFieldsets)
|| (isset($fieldSet->repeat) && $fieldSet->repeat == true)
if ((isset($fieldSet->repeat) && $fieldSet->repeat == true)
|| in_array($name, $ignoreFieldsets)
|| (!empty($configFieldsets) && in_array($name, $configFieldsets))
|| (!empty($hiddenFieldsets) && in_array($name, $hiddenFieldsets))
)
{
continue;
Expand All @@ -54,7 +55,7 @@
else
{
$label = strtoupper('JGLOBAL_FIELDSET_' . $name);
if (JText::_($label) == $label)
if (JText::_($label) === $label)
{
$label = strtoupper($app->input->get('option') . '_' . $name . '_FIELDSET_LABEL');
}
Expand Down
10 changes: 5 additions & 5 deletions layouts/joomla/editors/buttons/button.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
?>
<?php if ($button->get('name')) : ?>
<?php
$class = ($button->get('class')) ? $button->get('class') : null;
$class .= ($button->get('modal')) ? ' modal-button' : null;
$href = ($button->get('link')) ? ' href="' . JUri::base() . $button->get('link') . '"' : null;
$onclick = ($button->get('onclick')) ? ' onclick="' . $button->get('onclick') . '"' : '';
$title = ($button->get('title')) ? $button->get('title') : $button->get('text');
$class = $button->get('class') ?: null;
$class .= $button->get('modal') ? ' modal-button' : null;
$href = $button->get('link') ? ' href="' . JUri::base() . $button->get('link') . '"' : null;
$onclick = $button->get('onclick') ? ' onclick="' . $button->get('onclick') . '"' : '';
$title = $button->get('title') ?: $button->get('text');

// Load modal popup behavior
if ($button->get('modal'))
Expand Down
3 changes: 1 addition & 2 deletions layouts/joomla/form/field/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@
?>
<a class="modal btn" title="<?php echo JText::_('JLIB_FORM_BUTTON_SELECT'); ?>" href="
<?php echo ($readonly ? ''
: ($link ? $link
: 'index.php?option=com_media&amp;view=images&amp;tmpl=component&amp;asset=' . $asset . '&amp;author='
: ($link ?: 'index.php?option=com_media&amp;view=images&amp;tmpl=component&amp;asset=' . $asset . '&amp;author='
. $authorField) . '&amp;fieldid=' . $id . '&amp;folder=' . $folder) . '"'
. ' rel="{handler: \'iframe\', size: {x: 800, y: 500}}"'; ?>>
<?php echo JText::_('JLIB_FORM_BUTTON_SELECT'); ?></a><a class="btn hasTooltip" title="<?php echo JText::_('JLIB_FORM_BUTTON_CLEAR'); ?>" href="#" onclick="jInsertFieldValue('', '<?php echo $id; ?>'); return false;">
Expand Down
2 changes: 1 addition & 1 deletion layouts/joomla/form/field/radio.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<?php foreach ($options as $i => $option) : ?>
<?php
// Initialize some option attributes.
$checked = ((string) $option->value == $value) ? 'checked="checked"' : '';
$checked = ((string) $option->value === $value) ? 'checked="checked"' : '';
$optionClass = !empty($option->class) ? 'class="' . $option->class . '"' : '';
$disabled = !empty($option->disable) || ($disabled && !$checked) ? 'disabled' : '';

Expand Down
4 changes: 2 additions & 2 deletions layouts/joomla/form/field/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
. (isset($excluded) ? ('&amp;excluded=' . base64_encode(json_encode($excluded))) : '');

// Invalidate the input value if no user selected
if (JText::_('JLIB_FORM_SELECT_USER') == htmlspecialchars($userName, ENT_COMPAT, 'UTF-8'))
if (JText::_('JLIB_FORM_SELECT_USER') === htmlspecialchars($userName, ENT_COMPAT, 'UTF-8'))
{
$userName = "";
$userName = '';
}

// Load the modal behavior script.
Expand Down
4 changes: 2 additions & 2 deletions layouts/joomla/form/renderlabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@

if (!empty($description))
{
if ($text && $text != $description)
if ($text && $text !== $description)
{
JHtml::_('bootstrap.popover');
$classes[] = 'hasPopover';
$title = ' title="' . htmlspecialchars(trim($text, ':')) . '"'
. ' data-content="'. htmlspecialchars($description) . '"';

if (JFactory::getLanguage()->isRtl() && !$position)
if (!$position && JFactory::getLanguage()->isRtl())
{
$position = ' data-placement="left" ';
}
Expand Down
10 changes: 5 additions & 5 deletions layouts/joomla/pagination/link.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@
{
// Check for "Start" item
case JText::_('JLIB_HTML_START') :
$icon = "icon-backward icon-first";
$icon = 'icon-backward icon-first';
break;

// Check for "Prev" item
case $item->text == JText::_('JPREV') :
case $item->text === JText::_('JPREV') :
$item->text = JText::_('JPREVIOUS');
$icon = "icon-step-backward icon-previous";
$icon = 'icon-step-backward icon-previous';
break;

// Check for "Next" item
case JText::_('JNEXT') :
$icon = "icon-step-forward icon-next";
$icon = 'icon-step-forward icon-next';
break;

// Check for "End" item
case JText::_('JLIB_HTML_END') :
$icon = "icon-forward icon-last";
$icon = 'icon-forward icon-last';
break;

default:
Expand Down
4 changes: 2 additions & 2 deletions layouts/joomla/pagination/links.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

if ($currentPage >= $step)
{
if ($currentPage % $step == 0)
if ($currentPage % $step === 0)
{
$range = ceil($currentPage / $step) + 1;
}
Expand Down Expand Up @@ -66,7 +66,7 @@

<?php $output = JLayoutHelper::render('joomla.pagination.link', $page); ?>
<?php if (in_array($k, range($range * $step - ($step + 1), $range * $step))) : ?>
<?php if (($k % $step == 0 || $k == $range * $step - ($step + 1)) && $k != $currentPage && $k != $range * $step - $step) :?>
<?php if (($k % $step === 0 || $k === $range * $step - ($step + 1)) && $k !== $currentPage && $k !== $range * $step - $step) :?>
<?php $output = preg_replace('#(<a.*?>).*?(</a>)#', '$1...$2', $output); ?>
<?php endif; ?>
<?php endif; ?>
Expand Down
4 changes: 2 additions & 2 deletions layouts/joomla/searchtools/default/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
$showonarr[] = array(
'field' => $showon[0],
'values' => explode(',', $showon[1]),
'op' => (preg_match('%\[(AND|OR)\]' . $showonfield . '%', $showonstring, $matches)) ? $matches[1] : ''
'op' => preg_match('%\[(AND|OR)\]' . $showonfield . '%', $showonstring, $matches) ? $matches[1] : ''
);
}
$data['view']->filterForm->setFieldAttribute($field->fieldname, 'dataShowOn', json_encode($showonarr), $field->group);
Expand All @@ -36,7 +36,7 @@
?>
<?php if ($filters) : ?>
<?php foreach ($filters as $fieldName => $field) : ?>
<?php if ($fieldName != 'filter_search') : ?>
<?php if ($fieldName !== 'filter_search') : ?>
<?php
$showOn = '';
if ($showOnData = $field->getAttribute('dataShowOn'))
Expand Down
2 changes: 1 addition & 1 deletion layouts/joomla/searchtools/grid/sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

$data = $displayData;

$title = htmlspecialchars(JText::_($data->tip ? $data->tip : $data->title));
$title = htmlspecialchars(JText::_($data->tip ?: $data->title));
JHtml::_('bootstrap.popover');
?>
<a href="#" onclick="return false;" class="js-stools-column-order hasPopover"
Expand Down
2 changes: 1 addition & 1 deletion layouts/joomla/sidebars/submenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
if ($displayData->hide) : ?>
<a class="nolink"><?php echo $item[0]; ?></a>
<?php else :
if (strlen($item[1])) : ?>
if ($item[1] !== '') : ?>
<a href="<?php echo JFilterOutput::ampReplace($item[1]); ?>"><?php echo $item[0]; ?></a>
<?php else : ?>
<?php echo $item[0]; ?>
Expand Down
2 changes: 0 additions & 2 deletions layouts/joomla/tinymce/buttons.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@

JLog::add('The layout joomla.tinymce.buttons is deprecated, use joomla.editors.buttons instead.', JLog::WARNING, 'deprecated');
echo JLayoutHelper::render('joomla.editors.buttons', $displayData);

?>
2 changes: 0 additions & 2 deletions layouts/joomla/tinymce/buttons/button.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@

JLog::add('The layout joomla.tinymce.buttons.button is deprecated, use joomla.editors.buttons.button instead.', JLog::WARNING, 'deprecated');
echo JLayoutHelper::render('joomla.editors.buttons.button', $displayData);

?>
2 changes: 1 addition & 1 deletion layouts/joomla/toolbar/popup.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('JPATH_BASE') or die;;
defined('JPATH_BASE') or die;

JHtml::_('behavior.core');

Expand Down

0 comments on commit d82a2be

Please sign in to comment.