Browse Source

Fix up TypeError cases found by PHPStan level 7/8

mscherer 3 years ago
parent
commit
c97f4c73ba

+ 4 - 1
src/Collection/Iterator/TreeIterator.php

@@ -104,8 +104,11 @@ class TreeIterator extends RecursiveIteratorIterator implements CollectionInterf
             };
         }
 
+        /** @var \RecursiveIterator $iterator */
+        $iterator = $this->getInnerIterator();
+
         return new TreePrinter(
-            $this->getInnerIterator(),
+            $iterator,
             $valuePath,
             $keyPath,
             $spacer,

+ 6 - 1
src/Console/ConsoleOutput.php

@@ -16,6 +16,7 @@ declare(strict_types=1);
  */
 namespace Cake\Console;
 
+use Cake\Core\Exception\CakeException;
 use InvalidArgumentException;
 
 /**
@@ -164,7 +165,11 @@ class ConsoleOutput
      */
     public function __construct(string $stream = 'php://stdout')
     {
-        $this->_output = fopen($stream, 'wb');
+        $resource = fopen($stream, 'wb');
+        if ($resource === false) {
+            throw new CakeException(sprintf('Cannot open stream `%s`', $stream));
+        }
+        $this->_output = $resource;
 
         if (
             (

+ 3 - 0
src/Core/StaticConfigTrait.php

@@ -256,6 +256,9 @@ REGEXP;
         }
 
         $exists = [];
+        /**
+         * @var string|int $k
+         */
         foreach ($parsed as $k => $v) {
             if (is_int($k)) {
                 unset($parsed[$k]);

+ 1 - 0
src/Event/EventManager.php

@@ -485,6 +485,7 @@ class EventManager implements EventManagerInterface
         if ($this->_eventList) {
             $count = count($this->_eventList);
             for ($i = 0; $i < $count; $i++) {
+                /** @var \Cake\Event\EventInterface $event */
                 $event = $this->_eventList[$i];
                 try {
                     $subject = $event->getSubject();

+ 5 - 1
src/Http/Client/Adapter/Stream.php

@@ -314,7 +314,11 @@ class Stream implements AdapterInterface
             return true;
         });
         try {
-            $this->_stream = fopen($url, 'rb', false, $this->_context);
+            $stream = fopen($url, 'rb', false, $this->_context);
+            if ($stream === false) {
+                $stream = null;
+            }
+            $this->_stream = $stream;
         } finally {
             restore_error_handler();
         }

+ 9 - 0
src/I18n/Parser/MoFileParser.php

@@ -60,12 +60,16 @@ class MoFileParser
     public function parse(string $file): array
     {
         $stream = fopen($file, 'rb');
+        if ($stream === false) {
+            throw new CakeException(sprintf('Cannot open resource `%s`', $file));
+        }
 
         $stat = fstat($stream);
 
         if ($stat['size'] < self::MO_HEADER_SIZE) {
             throw new CakeException('Invalid format for MO translations file');
         }
+        /** @var array $magic */
         $magic = unpack('V1', (string)fread($stream, 4));
         $magic = hexdec(substr(dechex(current($magic)), -8));
 
@@ -115,6 +119,10 @@ class MoFileParser
 
             fseek($stream, $offsetTranslated + $i * 8);
             $length = $this->_readLong($stream, $isBigEndian);
+            if ($length < 0) {
+                throw new CakeException('Length must be > 0');
+            }
+
             $offset = $this->_readLong($stream, $isBigEndian);
             fseek($stream, $offset);
             $translated = (string)fread($stream, $length);
@@ -154,6 +162,7 @@ class MoFileParser
      */
     protected function _readLong($stream, bool $isBigEndian): int
     {
+        /** @var array $result */
         $result = unpack($isBigEndian ? 'N1' : 'V1', (string)fread($stream, 4));
         $result = current($result);
 

+ 4 - 0
src/I18n/Parser/PoFileParser.php

@@ -16,6 +16,7 @@ declare(strict_types=1);
  */
 namespace Cake\I18n\Parser;
 
+use Cake\Core\Exception\CakeException;
 use Cake\I18n\Translator;
 
 /**
@@ -73,6 +74,9 @@ class PoFileParser
     public function parse(string $resource): array
     {
         $stream = fopen($resource, 'rb');
+        if ($stream === false) {
+            throw new CakeException(sprintf('Cannot open resource `%s`', $resource));
+        }
 
         $defaults = [
             'ids' => [],