Browse Source

Cleanup element caching code.

ADmad 4 years ago
parent
commit
116c215944
2 changed files with 14 additions and 14 deletions
  1. 0 5
      psalm-baseline.xml
  2. 14 9
      src/View/View.php

+ 0 - 5
psalm-baseline.xml

@@ -399,9 +399,4 @@
       <code>defaultCurrency</code>
     </DeprecatedMethod>
   </file>
-  <file src="src/View/View.php">
-    <ArgumentTypeCoercion occurrences="1">
-      <code>$options</code>
-    </ArgumentTypeCoercion>
-  </file>
 </files>

+ 14 - 9
src/View/View.php

@@ -636,12 +636,17 @@ class View implements EventDispatcherInterface
      * @return string Rendered Element
      * @throws \Cake\View\Exception\MissingElementException When an element is missing and `ignoreMissing`
      *   is false.
+     * @psalm-param array{cache?:array|true, callbacks?:bool, plugin?:string|false, ignoreMissing?:bool} $options
      */
     public function element(string $name, array $data = [], array $options = []): string
     {
-        $options += ['callbacks' => false, 'cache' => null, 'plugin' => null];
+        $options += ['callbacks' => false, 'cache' => null, 'plugin' => null, 'ignoreMissing' => false];
         if (isset($options['cache'])) {
-            $options['cache'] = $this->_elementCache($name, $data, $options);
+            $options['cache'] = $this->_elementCache(
+                $name,
+                $data,
+                array_diff_key($options, ['callbacks' => false, 'plugin' => null, 'ignoreMissing' => null])
+            );
         }
 
         $pluginCheck = $options['plugin'] !== false;
@@ -655,13 +660,13 @@ class View implements EventDispatcherInterface
             return $this->_renderElement($file, $data, $options);
         }
 
-        if (empty($options['ignoreMissing'])) {
-            [$plugin, $elementName] = $this->pluginSplit($name, $pluginCheck);
-            $paths = iterator_to_array($this->getElementPaths($plugin));
-            throw new MissingElementException([$name . $this->_ext, $elementName . $this->_ext], $paths);
+        if ($options['ignoreMissing']) {
+            return '';
         }
 
-        return '';
+        [$plugin, $elementName] = $this->pluginSplit($name, $pluginCheck);
+        $paths = iterator_to_array($this->getElementPaths($plugin));
+        throw new MissingElementException([$name . $this->_ext, $elementName . $this->_ext], $paths);
     }
 
     /**
@@ -1605,12 +1610,12 @@ class View implements EventDispatcherInterface
      * @param array $data Data
      * @param array<string, mixed> $options Element options
      * @return array Element Cache configuration.
-     * @psalm-param array{cache:(array{key:string, config:string}|string|null), callbacks:mixed, plugin:mixed} $options
      * @psalm-return array{key:string, config:string}
      */
     protected function _elementCache(string $name, array $data, array $options): array
     {
         if (isset($options['cache']['key'], $options['cache']['config'])) {
+            /** @psalm-var array{key:string, config:string}*/
             $cache = $options['cache'];
             $cache['key'] = 'element_' . $cache['key'];
 
@@ -1626,7 +1631,7 @@ class View implements EventDispatcherInterface
         $elementKey = str_replace(['\\', '/'], '_', $name);
 
         $cache = $options['cache'];
-        unset($options['cache'], $options['callbacks'], $options['plugin']);
+        unset($options['cache']);
         $keys = array_merge(
             [$pluginKey, $elementKey],
             array_keys($options),