From ffeb714a6ee4dc18a517be3895fd0bbc25a233b7 Mon Sep 17 00:00:00 2001 From: thiemowmde Date: Thu, 8 Feb 2024 12:12:03 +0100 Subject: [PATCH] Various code style updates in ParserFileProcessingHookHandlers Notably: * Utilize the new ??= operator. * No need to count when nobody cares about the number. * More robust ratio calculation. * More straightforward check for the "notpageimage" class name. We don't need an array of all the classes when all we care about is a single, specific class. * Fix misspelled "no(t)pageimage". Change-Id: Ibad1d395a5438bc58e026022d08c38fe54c48653 --- .../ParserFileProcessingHookHandlers.php | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/includes/Hooks/ParserFileProcessingHookHandlers.php b/includes/Hooks/ParserFileProcessingHookHandlers.php index dc34909..102a940 100644 --- a/includes/Hooks/ParserFileProcessingHookHandlers.php +++ b/includes/Hooks/ParserFileProcessingHookHandlers.php @@ -196,7 +196,7 @@ class ParserFileProcessingHookHandlers implements * @return array The best image, and the best free image */ private function findBestImages( array $images ) { - if ( !count( $images ) ) { + if ( !$images ) { return [ false, false ]; } @@ -206,13 +206,9 @@ class ParserFileProcessingHookHandlers implements $counter = 0; foreach ( $images as $image ) { + $score = $this->getScore( $image, $counter++ ); $fileName = $image->getFileName(); - - if ( !isset( $scores[$fileName] ) ) { - $scores[$fileName] = -1; - } - - $scores[$fileName] = max( $scores[$fileName], $this->getScore( $image, $counter++ ) ); + $scores[$fileName] = max( $scores[$fileName] ?? -1, $score ); } $bestImageName = false; @@ -257,11 +253,8 @@ class ParserFileProcessingHookHandlers implements */ private function processThisTitle( PageReference $pageReference ) { global $wgPageImagesNamespaces; - static $flipped = false; - - if ( $flipped === false ) { - $flipped = array_flip( $wgPageImagesNamespaces ); - } + static $flipped = null; + $flipped ??= array_flip( $wgPageImagesNamespaces ); return isset( $flipped[$pageReference->getNamespace()] ); } @@ -307,9 +300,8 @@ class ParserFileProcessingHookHandlers implements protected function getScore( PageImageCandidate $image, $position ) { global $wgPageImagesScores; - $classes = preg_split( '/\s+/', $image->getFrameClass(), -1, PREG_SPLIT_NO_EMPTY ); - if ( in_array( 'notpageimage', $classes ) ) { - // Exclude images with class=nopageimage + // Exclude images with class="notpageimage" + if ( preg_match( '/(?:^|\s)notpageimage(?=\s|$)/', $image->getFrameClass() ) ) { return -1000; } @@ -409,12 +401,7 @@ class ParserFileProcessingHookHandlers implements protected function getRatio( PageImageCandidate $image ) { $width = $image->getFullWidth(); $height = $image->getFullHeight(); - - if ( !$width || !$height ) { - return 0; - } - - return $width / $height; + return $width > 0 && $height > 0 ? $width / $height : 0; } /**