resourceloader: Merge $fileCache conditional blocks
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 25 Jul 2019 22:57:08 +0000 (23:57 +0100)
committerKrinkle <krinklemail@gmail.com>
Sat, 27 Jul 2019 14:29:37 +0000 (14:29 +0000)
Also avoid conditional existence of variables ($fileCache). This avoids
isset(), which means we won't hide problems during refactor.

Raised by Codehealth (sonarcloud.io) as Major Code Smell:

> Merge this if statement with the enclosing one.
> https://sonarcloud.io/organizations/wmftest/rules?open=php%3AS1066&rule_key=php%3AS1066

Change-Id: Iacebbe6a68dac46cdfd1415a33a547d105b24b98

includes/resourceloader/ResourceLoader.php

index 671fe86..4427a53 100644 (file)
@@ -739,6 +739,8 @@ class ResourceLoader implements LoggerAwareInterface {
                        if ( $this->tryRespondFromFileCache( $fileCache, $context, $etag ) ) {
                                return; // output handled
                        }
+               } else {
+                       $fileCache = null;
                }
 
                // Generate a response
@@ -753,15 +755,17 @@ class ResourceLoader implements LoggerAwareInterface {
                        }
                }
 
-               // Save response to file cache unless there are errors
-               if ( isset( $fileCache ) && !$this->errors && $missing === [] ) {
-                       // Cache single modules and images...and other requests if there are enough hits
-                       if ( ResourceFileCache::useFileCache( $context ) ) {
-                               if ( $fileCache->isCacheWorthy() ) {
-                                       $fileCache->saveText( $response );
-                               } else {
-                                       $fileCache->incrMissesRecent( $context->getRequest() );
-                               }
+               // Consider saving the response to file cache (unless there are errors).
+               if ( $fileCache &&
+                       !$this->errors &&
+                       $missing === [] &&
+                       ResourceFileCache::useFileCache( $context )
+               ) {
+                       if ( $fileCache->isCacheWorthy() ) {
+                               // There were enough hits, save the response to the cache
+                               $fileCache->saveText( $response );
+                       } else {
+                               $fileCache->incrMissesRecent( $context->getRequest() );
                        }
                }