resourceloader: Fix return type doc for getSelectors() method
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderImageModule.php
index 5e329e8..9b50d80 100644 (file)
@@ -28,6 +28,7 @@
  */
 class ResourceLoaderImageModule extends ResourceLoaderModule {
 
+       /** @var array|null */
        protected $definition = null;
 
        /**
@@ -38,8 +39,18 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
 
        protected $origin = self::ORIGIN_CORE_SITEWIDE;
 
+       /** @var ResourceLoaderImage[]|null */
+       protected $imageObjects = null;
+       /** @var array */
        protected $images = [];
+       /** @var string|null */
+       protected $defaultColor = null;
+       protected $useDataURI = true;
+       /** @var array|null */
+       protected $globalVariants = null;
+       /** @var array */
        protected $variants = [];
+       /** @var string|null */
        protected $prefix = null;
        protected $selectorWithoutVariant = '.{prefix}-{name}';
        protected $selectorWithVariant = '.{prefix}-{name}-{variant}';
@@ -50,7 +61,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
         *
         * @param array $options List of options; if not given or empty, an empty module will be
         *     constructed
-        * @param string $localBasePath Base path to prepend to all local paths in $options. Defaults
+        * @param string|null $localBasePath Base path to prepend to all local paths in $options. Defaults
         *     to $IP
         *
         * Below is a description for the $options array:
@@ -173,15 +184,19 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
                                                $option = [ 'default' => $option ];
                                        }
                                        foreach ( $option as $skin => $data ) {
-                                               if ( !is_array( $option ) ) {
+                                               if ( !is_array( $data ) ) {
                                                        throw new InvalidArgumentException(
-                                                               "Invalid list error. '$option' given, array expected."
+                                                               "Invalid list error. '$data' given, array expected."
                                                        );
                                                }
                                        }
                                        $this->{$member} = $option;
                                        break;
 
+                               case 'useDataURI':
+                                       $this->{$member} = (bool)$option;
+                                       break;
+                               case 'defaultColor':
                                case 'prefix':
                                case 'selectorWithoutVariant':
                                case 'selectorWithVariant':
@@ -205,7 +220,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
 
        /**
         * Get CSS selector templates used by this module.
-        * @return string
+        * @return string[]
         */
        public function getSelectors() {
                $this->loadFromDefinition();
@@ -224,7 +239,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
        public function getImage( $name, ResourceLoaderContext $context ) {
                $this->loadFromDefinition();
                $images = $this->getImages( $context );
-               return isset( $images[$name] ) ? $images[$name] : null;
+               return $images[$name] ?? null;
        }
 
        /**
@@ -234,16 +249,14 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
         */
        public function getImages( ResourceLoaderContext $context ) {
                $skin = $context->getSkin();
-               if ( !isset( $this->imageObjects ) ) {
+               if ( $this->imageObjects === null ) {
                        $this->loadFromDefinition();
                        $this->imageObjects = [];
                }
                if ( !isset( $this->imageObjects[$skin] ) ) {
                        $this->imageObjects[$skin] = [];
                        if ( !isset( $this->images[$skin] ) ) {
-                               $this->images[$skin] = isset( $this->images['default'] ) ?
-                                       $this->images['default'] :
-                                       [];
+                               $this->images[$skin] = $this->images['default'] ?? [];
                        }
                        foreach ( $this->images[$skin] as $name => $options ) {
                                $fileDescriptor = is_string( $options ) ? $options : $options['file'];
@@ -266,7 +279,8 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
                                        $this->getName(),
                                        $fileDescriptor,
                                        $this->localBasePath,
-                                       $variantConfig
+                                       $variantConfig,
+                                       $this->defaultColor
                                );
                                $this->imageObjects[$skin][$image->getName()] = $image;
                        }
@@ -283,16 +297,14 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
         */
        public function getGlobalVariants( ResourceLoaderContext $context ) {
                $skin = $context->getSkin();
-               if ( !isset( $this->globalVariants ) ) {
+               if ( $this->globalVariants === null ) {
                        $this->loadFromDefinition();
                        $this->globalVariants = [];
                }
                if ( !isset( $this->globalVariants[$skin] ) ) {
                        $this->globalVariants[$skin] = [];
                        if ( !isset( $this->variants[$skin] ) ) {
-                               $this->variants[$skin] = isset( $this->variants['default'] ) ?
-                                       $this->variants['default'] :
-                                       [];
+                               $this->variants[$skin] = $this->variants['default'] ?? [];
                        }
                        foreach ( $this->variants[$skin] as $name => $config ) {
                                if ( isset( $config['global'] ) && $config['global'] ) {
@@ -359,7 +371,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
                $script,
                $variant = null
        ) {
-               $imageDataUri = $image->getDataUri( $context, $variant, 'original' );
+               $imageDataUri = $this->useDataURI ? $image->getDataUri( $context, $variant, 'original' ) : false;
                $primaryUrl = $imageDataUri ?: $image->getUrl( $context, $script, $variant, 'original' );
                $declarations = $this->getCssDeclarations(
                        $primaryUrl,
@@ -416,7 +428,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
                        'selectorWithVariant',
                ] as $member ) {
                        $options[$member] = $this->{$member};
-               };
+               }
 
                $summary[] = [
                        'options' => $options,
@@ -430,7 +442,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
         * @param ResourceLoaderContext $context
         * @return array
         */
-       protected function getFileHashes( ResourceLoaderContext $context ) {
+       private function getFileHashes( ResourceLoaderContext $context ) {
                $this->loadFromDefinition();
                $files = [];
                foreach ( $this->getImages( $context ) as $name => $image ) {
@@ -444,7 +456,7 @@ class ResourceLoaderImageModule extends ResourceLoaderModule {
         * Extract a local base path from module definition information.
         *
         * @param array $options Module definition
-        * @param string $localBasePath Path to use if not provided in module definition. Defaults
+        * @param string|null $localBasePath Path to use if not provided in module definition. Defaults
         *     to $IP
         * @return string Local base path
         */