Optimize PHP code

* Use ' instead of ".
* Use === instead of ==.
* Use !== instead of !=.
* Use explicit type cast.
* Combine array assignment.
* Use null coalescing operator.

Change-Id: Ic2fe4f62556df77262915b5bcbe1b11a1d907e6d
This commit is contained in:
Fomafix 2021-09-02 08:33:30 +00:00
parent 031e5bf358
commit ed87adce49
3 changed files with 59 additions and 66 deletions

View file

@ -61,16 +61,12 @@ class CategoryTree {
// ensure default values and order of options.
// Order may become important, it may influence the cache key!
foreach ( $wgCategoryTreeDefaultOptions as $option => $default ) {
if ( isset( $options[$option] ) ) {
$this->mOptions[$option] = $options[$option];
} else {
$this->mOptions[$option] = $default;
}
$this->mOptions[$option] = $options[$option] ?? $default;
}
$this->mOptions['mode'] = self::decodeMode( $this->mOptions['mode'] );
if ( $this->mOptions['mode'] == CategoryTreeMode::PARENTS ) {
if ( $this->mOptions['mode'] === CategoryTreeMode::PARENTS ) {
// namespace filter makes no sense with CategoryTreeMode::PARENTS
$this->mOptions['namespaces'] = false;
}
@ -82,7 +78,7 @@ class CategoryTree {
if ( $this->mOptions['namespaces'] ) {
# automatically adjust mode to match namespace filter
if ( count( $this->mOptions['namespaces'] ) === 1
&& $this->mOptions['namespaces'][0] == NS_CATEGORY ) {
&& $this->mOptions['namespaces'][0] === NS_CATEGORY ) {
$this->mOptions['mode'] = CategoryTreeMode::CATEGORIES;
} elseif ( !in_array( NS_FILE, $this->mOptions['namespaces'] ) ) {
$this->mOptions['mode'] = CategoryTreeMode::PAGES;
@ -104,7 +100,7 @@ class CategoryTree {
* @return bool
*/
private function isInverse() {
return $this->getOption( 'mode' ) == CategoryTreeMode::PARENTS;
return $this->getOption( 'mode' ) === CategoryTreeMode::PARENTS;
}
/**
@ -135,7 +131,7 @@ class CategoryTree {
if ( is_numeric( $n ) ) {
$ns = (int)$n;
} elseif ( $n == '-' || $n == '_' || $n == '*' || $lower == 'main' ) {
} elseif ( $n === '-' || $n === '_' || $n === '*' || $lower === 'main' ) {
$ns = NS_MAIN;
} else {
$ns = $contLang->getNsIndex( $n );
@ -172,15 +168,15 @@ class CategoryTree {
return (int)$mode;
}
if ( $mode == 'all' ) {
if ( $mode === 'all' ) {
$mode = CategoryTreeMode::ALL;
} elseif ( $mode == 'pages' ) {
} elseif ( $mode === 'pages' ) {
$mode = CategoryTreeMode::PAGES;
} elseif ( $mode == 'categories' || $mode == 'sub' ) {
} elseif ( $mode === 'categories' || $mode === 'sub' ) {
$mode = CategoryTreeMode::CATEGORIES;
} elseif ( $mode == 'parents' || $mode == 'super' || $mode == 'inverse' ) {
} elseif ( $mode === 'parents' || $mode === 'super' || $mode === 'inverse' ) {
$mode = CategoryTreeMode::PARENTS;
} elseif ( $mode == 'default' ) {
} elseif ( $mode === 'default' ) {
$mode = $wgCategoryTreeDefaultOptions['mode'];
}
@ -209,15 +205,15 @@ class CategoryTree {
return ( (int)$value > 0 );
}
if ( $value == 'yes' || $value == 'y'
|| $value == 'true' || $value == 't' || $value == 'on'
if ( $value === 'yes' || $value === 'y'
|| $value === 'true' || $value === 't' || $value === 'on'
) {
return true;
} elseif ( $value == 'no' || $value == 'n'
|| $value == 'false' || $value == 'f' || $value == 'off'
} elseif ( $value === 'no' || $value === 'n'
|| $value === 'false' || $value === 'f' || $value === 'off'
) {
return false;
} elseif ( $value == 'null' || $value == 'default' || $value == 'none' || $value == 'x' ) {
} elseif ( $value === 'null' || $value === 'default' || $value === 'none' || $value === 'x' ) {
return null;
} else {
return false;
@ -246,21 +242,21 @@ class CategoryTree {
$value = trim( strtolower( $value ) );
if ( $value == 'yes' || $value == 'y'
|| $value == 'true' || $value == 't' || $value == 'on'
if ( $value === 'yes' || $value === 'y'
|| $value === 'true' || $value === 't' || $value === 'on'
) {
return CategoryTreeHidePrefix::ALWAYS;
} elseif ( $value == 'no' || $value == 'n'
|| $value == 'false' || $value == 'f' || $value == 'off'
} elseif ( $value === 'no' || $value === 'n'
|| $value === 'false' || $value === 'f' || $value === 'off'
) {
return CategoryTreeHidePrefix::NEVER;
} elseif ( $value == 'always' ) {
} elseif ( $value === 'always' ) {
return CategoryTreeHidePrefix::ALWAYS;
} elseif ( $value == 'never' ) {
} elseif ( $value === 'never' ) {
return CategoryTreeHidePrefix::NEVER;
} elseif ( $value == 'auto' ) {
} elseif ( $value === 'auto' ) {
return CategoryTreeHidePrefix::AUTO;
} elseif ( $value == 'categories' || $value == 'category' || $value == 'smart' ) {
} elseif ( $value === 'categories' || $value === 'category' || $value === 'smart' ) {
return CategoryTreeHidePrefix::CATEGORIES;
} else {
return $wgCategoryTreeDefaultOptions['hideprefix'];
@ -284,9 +280,9 @@ class CategoryTree {
* @throws Exception
*/
protected static function encodeOptions( array $options, $enc ) {
if ( $enc == 'mode' || $enc == '' ) {
if ( $enc === 'mode' || $enc === '' ) {
$opt = $options['mode'];
} elseif ( $enc == 'json' ) {
} elseif ( $enc === 'json' ) {
$opt = FormatJson::encode( $options );
} else {
throw new Exception( 'Unknown encoding for CategoryTree options: ' . $enc );
@ -300,7 +296,7 @@ class CategoryTree {
* @return string
*/
public function getOptionsAsCacheKey( $depth = null ) {
$key = "";
$key = '';
foreach ( $this->mOptions as $k => $v ) {
if ( is_array( $v ) ) {
@ -310,7 +306,7 @@ class CategoryTree {
}
if ( $depth !== null ) {
$key .= ";depth=" . $depth;
$key .= ';depth=' . $depth;
}
return $key;
}
@ -402,7 +398,7 @@ class CategoryTree {
public function renderChildren( Title $title, $depth = 1 ) {
global $wgCategoryTreeMaxChildren, $wgCategoryTreeUseCategoryTable;
if ( $title->getNamespace() != NS_CATEGORY ) {
if ( $title->getNamespace() !== NS_CATEGORY ) {
// Non-categories can't have children. :)
return '';
}
@ -436,8 +432,8 @@ class CategoryTree {
// NOTE: we assume that the $namespaces array contains only integers!
// decodeNamepsaces makes it so.
$where['page_namespace'] = $namespaces;
} elseif ( $mode != CategoryTreeMode::ALL ) {
if ( $mode == CategoryTreeMode::PAGES ) {
} elseif ( $mode !== CategoryTreeMode::ALL ) {
if ( $mode === CategoryTreeMode::PAGES ) {
$where['cl_type'] = [ 'page', 'subcat' ];
} else {
$where['cl_type'] = 'subcat';
@ -506,13 +502,13 @@ class CategoryTree {
$cat = null;
if ( $doCount && $row->page_namespace == NS_CATEGORY ) {
if ( $doCount && (int)$row->page_namespace === NS_CATEGORY ) {
$cat = Category::newFromRow( $row, $t );
}
$s = $this->renderNodeInfo( $t, $cat, $depth - 1 );
if ( $row->page_namespace == NS_CATEGORY ) {
if ( (int)$row->page_namespace === NS_CATEGORY ) {
$categories .= $s;
} else {
$other .= $s;
@ -572,7 +568,7 @@ class CategoryTree {
public function renderNode( Title $title, $children = 0 ) {
global $wgCategoryTreeUseCategoryTable;
if ( $wgCategoryTreeUseCategoryTable && $title->getNamespace() == NS_CATEGORY
if ( $wgCategoryTreeUseCategoryTable && $title->getNamespace() === NS_CATEGORY
&& !$this->isInverse()
) {
$cat = Category::newFromTitle( $title );
@ -599,12 +595,12 @@ class CategoryTree {
$hideprefix = $this->getOption( 'hideprefix' );
if ( $hideprefix == CategoryTreeHidePrefix::ALWAYS ) {
if ( $hideprefix === CategoryTreeHidePrefix::ALWAYS ) {
$hideprefix = true;
} elseif ( $hideprefix == CategoryTreeHidePrefix::AUTO ) {
$hideprefix = ( $mode == CategoryTreeMode::CATEGORIES );
} elseif ( $hideprefix == CategoryTreeHidePrefix::CATEGORIES ) {
$hideprefix = ( $ns == NS_CATEGORY );
} elseif ( $hideprefix === CategoryTreeHidePrefix::AUTO ) {
$hideprefix = ( $mode === CategoryTreeMode::CATEGORIES );
} elseif ( $hideprefix === CategoryTreeHidePrefix::CATEGORIES ) {
$hideprefix = ( $ns === NS_CATEGORY );
} else {
$hideprefix = true;
}
@ -632,11 +628,11 @@ class CategoryTree {
$attr = [ 'class' => 'CategoryTreeBullet' ];
if ( $ns == NS_CATEGORY ) {
if ( $ns === NS_CATEGORY ) {
if ( $cat ) {
if ( $mode == CategoryTreeMode::CATEGORIES ) {
if ( $mode === CategoryTreeMode::CATEGORIES ) {
$count = intval( $cat->getSubcatCount() );
} elseif ( $mode == CategoryTreeMode::PAGES ) {
} elseif ( $mode === CategoryTreeMode::PAGES ) {
$count = intval( $cat->getPageCount() ) - intval( $cat->getFileCount() );
} else {
$count = intval( $cat->getPageCount() );
@ -646,16 +642,16 @@ class CategoryTree {
$bullet = '';
$attr['class'] = 'CategoryTreeEmptyBullet';
} else {
$linkattr = [];
$linkattr = [
'class' => 'CategoryTreeToggle',
'data-ct-title' => $key,
];
$linkattr[ 'class' ] = "CategoryTreeToggle";
$linkattr['data-ct-title'] = $key;
if ( $children == 0 ) {
$linkattr[ 'data-ct-state' ] = 'collapsed';
if ( $children === 0 ) {
$linkattr['data-ct-state'] = 'collapsed';
} else {
$linkattr[ 'data-ct-loaded' ] = true;
$linkattr[ 'data-ct-state' ] = 'expanded';
$linkattr['data-ct-loaded'] = true;
$linkattr['data-ct-state'] = 'expanded';
}
$bullet = Html::element( 'span', $linkattr ) . ' ';
@ -677,13 +673,13 @@ class CategoryTree {
'div',
[
'class' => 'CategoryTreeChildren',
'style' => $children > 0 ? "display:block" : "display:none"
'style' => $children > 0 ? 'display:block' : 'display:none'
]
);
if ( $ns == NS_CATEGORY && $children > 0 ) {
if ( $ns === NS_CATEGORY && $children > 0 ) {
$children = $this->renderChildren( $title, $children );
if ( $children == '' ) {
if ( $children === '' ) {
switch ( $mode ) {
case CategoryTreeMode::CATEGORIES:
$msg = 'categorytree-no-subcategories';
@ -783,7 +779,7 @@ class CategoryTree {
# The title must be in the category namespace
# Ignore a leading Category: if there is one
$t = Title::newFromText( $title, NS_CATEGORY );
if ( !$t || $t->getNamespace() != NS_CATEGORY || $t->getInterwiki() != '' ) {
if ( !$t || $t->getNamespace() !== NS_CATEGORY || $t->getInterwiki() !== '' ) {
// If we were given something like "Wikipedia:Foo" or "Template:",
// try it again but forced.
$title = "Category:$title";

View file

@ -118,10 +118,10 @@ class CategoryTreePage extends SpecialPage {
private function executeInputForm() {
$namespaces = $this->getRequest()->getVal( 'namespaces', '' );
// mode may be overriden by namespaces option
$mode = ( $namespaces == '' ? $this->getOption( 'mode' ) : CategoryTreeMode::ALL );
if ( $mode == CategoryTreeMode::CATEGORIES ) {
$mode = ( $namespaces === '' ? $this->getOption( 'mode' ) : CategoryTreeMode::ALL );
if ( $mode === CategoryTreeMode::CATEGORIES ) {
$modeDefault = 'categories';
} elseif ( $mode == CategoryTreeMode::PAGES ) {
} elseif ( $mode === CategoryTreeMode::PAGES ) {
$modeDefault = 'pages';
} else {
$modeDefault = 'all';
@ -183,7 +183,7 @@ class CategoryTreePage extends SpecialPage {
}
$parents = $this->tree->renderParents( $title );
if ( $parents == '' ) {
if ( $parents === '' ) {
$parents = $this->msg( 'categorytree-no-parent-categories' )->parse();
}

View file

@ -235,7 +235,7 @@ class Hooks implements
* @return bool|void True or no return value to continue or false to abort
*/
public function onArticleFromTitle( $title, &$article, $context ) {
if ( $title->getNamespace() == NS_CATEGORY ) {
if ( $title->getNamespace() === NS_CATEGORY ) {
$article = new CategoryTreeCategoryPage( $title );
}
}
@ -327,10 +327,7 @@ class Hooks implements
return;
}
$cat = null;
if ( isset( $specialPage->categoryTreeCategories[$catTitle->getDBkey()] ) ) {
$cat = $specialPage->categoryTreeCategories[$catTitle->getDBkey()];
}
$cat = $specialPage->categoryTreeCategories[$catTitle->getDBkey()] ?? null;
$html .= CategoryTree::createCountString( $specialPage->getContext(), $cat, 0 );
}