Browse Source

Fix errors reported by psalm under I18n namespace

ADmad 6 years ago
parent
commit
4ef61c8bb3

+ 0 - 1
phpstan.neon

@@ -23,7 +23,6 @@ parameters:
         - '#Return type \(void\) of method Cake\\Shell\\[A-Za-z]+Shell::main\(\) should be compatible with return type \(bool|int|null\) of method Cake\\Console\\Shell::main\(\)#'
         - '#Return type \(null\) of method Cake\\Collection\\Iterator\\NoChildrenIterator::getChildren\(\) should be compatible with return type \(RecursiveIterator\) of method RecursiveIterator::getChildren\(\)#'
         - '#Call to an undefined method DateTimeInterface::setTimezone\(\)#'
-        - '#Parameter \#1 \$filename of function fopen expects string, resource given.#'
         - '#Parameter \#1 \$it of method AppendIterator::append\(\) expects Iterator, Traversable given.#'
         - '#Parameter \#1 \$it of class .+Iterator constructor expects Iterator, Traversable given.#'
         - '#Parameter \#1 \$assoc of method Cake\\ORM\\Marshaller::_belongsToMany\(\) expects .+BelongsToMany, Cake\\ORM\\Association given.#'

+ 0 - 90
psalm-baseline.xml

@@ -235,96 +235,6 @@
       <code>$_SESSION</code>
     </TypeDoesNotContainType>
   </file>
-  <file src="src/I18n/DateFormatTrait.php">
-    <PossiblyInvalidArgument occurrences="2">
-      <code>static::$niceFormat</code>
-      <code>static::$_jsonEncodeFormat</code>
-    </PossiblyInvalidArgument>
-    <PossiblyNullReference occurrences="1">
-      <code>toIso8601String</code>
-    </PossiblyNullReference>
-  </file>
-  <file src="src/I18n/Formatter/IcuFormatter.php">
-    <PossiblyInvalidArgument occurrences="2">
-      <code>$message</code>
-      <code>$message</code>
-    </PossiblyInvalidArgument>
-  </file>
-  <file src="src/I18n/Formatter/SprintfFormatter.php">
-    <PossiblyInvalidArgument occurrences="1">
-      <code>$message</code>
-    </PossiblyInvalidArgument>
-  </file>
-  <file src="src/I18n/FrozenTime.php">
-    <MissingParamType occurrences="2">
-      <code>$time</code>
-      <code>$tz</code>
-    </MissingParamType>
-  </file>
-  <file src="src/I18n/Number.php">
-    <PossiblyInvalidOperand occurrences="1">
-      <code>$value</code>
-    </PossiblyInvalidOperand>
-    <PossiblyNullArgument occurrences="1">
-      <code>$currency</code>
-    </PossiblyNullArgument>
-  </file>
-  <file src="src/I18n/Parser/MoFileParser.php">
-    <InvalidArgument occurrences="1">
-      <code>$resource</code>
-    </InvalidArgument>
-  </file>
-  <file src="src/I18n/Parser/PoFileParser.php">
-    <InvalidOperand occurrences="1">
-      <code>$count</code>
-    </InvalidOperand>
-    <PossiblyFalseOperand occurrences="1">
-      <code>$size</code>
-    </PossiblyFalseOperand>
-    <PossiblyInvalidArrayOffset occurrences="1">
-      <code>$item[$stage[0]][$stage[1]]</code>
-    </PossiblyInvalidArrayOffset>
-    <PossiblyInvalidOperand occurrences="1">
-      <code>$item[$stage[0]]</code>
-    </PossiblyInvalidOperand>
-    <PossiblyNullArgument occurrences="1">
-      <code>$stage</code>
-    </PossiblyNullArgument>
-    <PossiblyNullArrayAccess occurrences="1">
-      <code>$item[$stage[0]][$stage[1]]</code>
-    </PossiblyNullArrayAccess>
-    <PossiblyNullOperand occurrences="1">
-      <code>$item[$stage[0]]</code>
-    </PossiblyNullOperand>
-    <PossiblyUndefinedArrayOffset occurrences="1">
-      <code>$stage[1]</code>
-    </PossiblyUndefinedArrayOffset>
-  </file>
-  <file src="src/I18n/Time.php">
-    <InvalidOperand occurrences="1">
-      <code>$this-&gt;format('m')</code>
-    </InvalidOperand>
-    <MissingParamType occurrences="2">
-      <code>$time</code>
-      <code>$tz</code>
-    </MissingParamType>
-    <PossiblyInvalidArgument occurrences="1">
-      <code>static::$niceFormat</code>
-    </PossiblyInvalidArgument>
-  </file>
-  <file src="src/I18n/Translator.php">
-    <PossiblyInvalidArrayOffset occurrences="1">
-      <code>$message['_context']</code>
-    </PossiblyInvalidArrayOffset>
-  </file>
-  <file src="src/I18n/TranslatorRegistry.php">
-    <ImplementedReturnTypeMismatch occurrences="1">
-      <code>\Aura\Intl\TranslatorInterface|null</code>
-    </ImplementedReturnTypeMismatch>
-    <PossiblyNullArgument occurrences="1">
-      <code>$locale</code>
-    </PossiblyNullArgument>
-  </file>
   <file src="src/Shell/Task/CommandTask.php">
     <DeprecatedClass occurrences="1">
       <code>Shell</code>

+ 2 - 1
src/I18n/DateFormatTrait.php

@@ -135,7 +135,7 @@ trait DateFormatTrait
      * You can control the default locale used through `Time::setDefaultLocale()`.
      * If empty, the default will be taken from the `intl.default_locale` ini config.
      *
-     * @param string|int|null $format Format string.
+     * @param string|int|array|null $format Format string.
      * @param string|\DateTimeZone|null $timezone Timezone string or DateTimeZone object
      * in which the date will be displayed. The timezone stored for this object will not
      * be changed.
@@ -447,6 +447,7 @@ trait DateFormatTrait
      */
     public function __debugInfo(): array
     {
+        /** @psalm-suppress PossiblyNullReference */
         return [
             'time' => $this->toIso8601String(),
             'timezone' => $this->getTimezone()->getName(),

+ 3 - 3
src/I18n/Formatter/IcuFormatter.php

@@ -30,7 +30,7 @@ class IcuFormatter implements FormatterInterface
      * message. Variables are interpolated using the MessageFormatter class.
      *
      * @param string $locale The locale in which the message is presented.
-     * @param string|array $message The message to be translated
+     * @param string $message The message to be translated
      * @param array $vars The list of values to interpolate in the message
      * @return string The formatted message
      * @throws \Aura\Intl\Exception\CannotFormat
@@ -47,7 +47,7 @@ class IcuFormatter implements FormatterInterface
      * Does the actual formatting using the MessageFormatter class
      *
      * @param string $locale The locale in which the message is presented.
-     * @param string|array $message The message to be translated
+     * @param string $message The message to be translated
      * @param array $vars The list of values to interpolate in the message
      * @return string The formatted message
      * @throws \Aura\Intl\Exception\CannotInstantiateFormatter if any error occurred
@@ -55,7 +55,7 @@ class IcuFormatter implements FormatterInterface
      * @throws \Aura\Intl\Exception\CannotFormat If any error related to the passed
      * variables is found
      */
-    protected function _formatMessage(string $locale, $message, array $vars): string
+    protected function _formatMessage(string $locale, string $message, array $vars): string
     {
         if ($message === '') {
             return $message;

+ 1 - 1
src/I18n/Formatter/SprintfFormatter.php

@@ -29,7 +29,7 @@ class SprintfFormatter implements FormatterInterface
      * message. Variables are interpolated using the sprintf format.
      *
      * @param string $locale The locale in which the message is presented.
-     * @param string|array $message The message to be translated
+     * @param string $message The message to be translated
      * @param array $vars The list of values to interpolate in the message
      * @return string The formatted message
      */

+ 4 - 1
src/I18n/FrozenTime.php

@@ -122,7 +122,10 @@ class FrozenTime extends Chronos implements I18nDateTimeInterface
     public const UNIX_TIMESTAMP_FORMAT = 'unixTimestampFormat';
 
     /**
-     * @inheritDoc
+     * Create a new immutable time instance.
+     *
+     * @param string|int|\DateTimeInterface|null $time Fixed or relative time
+     * @param \DateTimeZone|string|null $tz The timezone for the instance
      */
     public function __construct($time = null, $tz = null)
     {

+ 9 - 4
src/I18n/Number.php

@@ -118,7 +118,7 @@ class Number
     {
         $options += ['multiply' => false];
         if ($options['multiply']) {
-            $value *= 100;
+            $value = (float)$value * 100;
         }
 
         return static::precision($value, $precision, $options) . '%';
@@ -234,10 +234,15 @@ class Number
             return static::format($value, ['precision' => 0, $pos => $options['fractionSymbol']]);
         }
 
-        $before = $options['before'] ?? null;
-        $after = $options['after'] ?? null;
+        $before = $options['before'] ?? '';
+        $after = $options['after'] ?? '';
+        if ($currency) {
+            $value = $formatter->formatCurrency($value, $currency);
+        } else {
+            $formatter->format($value, NumberFormatter::TYPE_CURRENCY);
+        }
 
-        return $before . $formatter->formatCurrency($value, $currency) . $after;
+        return $before . $value . $after;
     }
 
     /**

+ 3 - 3
src/I18n/Parser/MoFileParser.php

@@ -53,14 +53,14 @@ class MoFileParser
      * Parses machine object (MO) format, independent of the machine's endian it
      * was created on. Both 32bit and 64bit systems are supported.
      *
-     * @param resource $resource The file to be parsed.
+     * @param string $file The file to be parsed.
      *
      * @return array List of messages extracted from the file
      * @throws \RuntimeException If stream content has an invalid format.
      */
-    public function parse($resource): array
+    public function parse($file): array
     {
-        $stream = fopen($resource, 'rb');
+        $stream = fopen($file, 'rb');
 
         $stat = fstat($stream);
 

+ 17 - 3
src/I18n/Parser/PoFileParser.php

@@ -82,7 +82,7 @@ class PoFileParser
 
         $messages = [];
         $item = $defaults;
-        $stage = null;
+        $stage = [];
 
         while ($line = fgets($stream)) {
             $line = trim($line);
@@ -91,7 +91,7 @@ class PoFileParser
                 // Whitespace indicated current item is done
                 $this->_addMessage($messages, $item);
                 $item = $defaults;
-                $stage = null;
+                $stage = [];
             } elseif (substr($line, 0, 7) === 'msgid "') {
                 // We start a new msg so save previous
                 $this->_addMessage($messages, $item);
@@ -106,10 +106,23 @@ class PoFileParser
             } elseif ($line[0] === '"') {
                 switch (count($stage)) {
                     case 2:
+                        /**
+                         * @psalm-suppress PossiblyUndefinedArrayOffset
+                         * @psalm-suppress PossiblyInvalidArrayOffset
+                         * @psalm-suppress PossiblyInvalidOperand
+                         * @psalm-suppress PossiblyNullOperand
+                         * @psalm-suppress PossiblyNullArrayAccess
+                         */
                         $item[$stage[0]][$stage[1]] .= substr($line, 1, -1);
                         break;
 
                     case 1:
+                        /**
+                         * @psalm-suppress PossiblyUndefinedArrayOffset
+                         * @psalm-suppress PossiblyInvalidArrayOffset
+                         * @psalm-suppress PossiblyInvalidOperand
+                         * @psalm-suppress PossiblyNullOperand
+                         */
                         $item[$stage[0]] .= substr($line, 1, -1);
                         break;
                 }
@@ -117,6 +130,7 @@ class PoFileParser
                 $item['ids']['plural'] = substr($line, 14, -1);
                 $stage = ['ids', 'plural'];
             } elseif (substr($line, 0, 7) === 'msgstr[') {
+                /** @var int $size */
                 $size = strpos($line, ']');
                 $row = (int)substr($line, 7, 1);
                 $item['translated'][$row] = substr($line, $size + 3, -1);
@@ -166,7 +180,7 @@ class PoFileParser
 
             // Make sure every index is filled.
             end($plurals);
-            $count = key($plurals);
+            $count = (int)key($plurals);
 
             // Fill missing spots with an empty string.
             $empties = array_fill(0, $count + 1, '');

+ 5 - 2
src/I18n/Time.php

@@ -120,7 +120,10 @@ class Time extends MutableDateTime implements I18nDateTimeInterface
     public const UNIX_TIMESTAMP_FORMAT = 'unixTimestampFormat';
 
     /**
-     * @inheritDoc
+     * Create a new mutable time instance.
+     *
+     * @param string|int|\DateTimeInterface|null $time Fixed or relative time
+     * @param \DateTimeZone|string|null $tz The timezone for the instance
      */
     public function __construct($time = null, $tz = null)
     {
@@ -189,7 +192,7 @@ class Time extends MutableDateTime implements I18nDateTimeInterface
      */
     public function toQuarter(bool $range = false)
     {
-        $quarter = (int)ceil($this->format('m') / 3);
+        $quarter = (int)ceil((int)$this->format('m') / 3);
         if ($range === false) {
             return $quarter;
         }

+ 2 - 2
src/I18n/Translator.php

@@ -91,11 +91,11 @@ class Translator extends BaseTranslator
      * Resolve a message's context structure.
      *
      * @param string $key The message key being handled.
-     * @param string|array $message The message content.
+     * @param array $message The message content.
      * @param array $vars The variables containing the `_context` key.
      * @return string|array
      */
-    protected function resolveContext(string $key, $message, array $vars)
+    protected function resolveContext(string $key, array $message, array $vars)
     {
         $context = $vars['_context'] ?? null;
 

+ 3 - 2
src/I18n/TranslatorRegistry.php

@@ -128,6 +128,7 @@ class TranslatorRegistry extends TranslatorLocator
      * @return \Aura\Intl\TranslatorInterface|null A translator object.
      * @throws \Aura\Intl\Exception If no translator with that name could be found
      * for the given locale.
+     * @psalm-suppress ImplementedReturnTypeMismatch
      */
     public function get($name, $locale = null)
     {
@@ -163,11 +164,11 @@ class TranslatorRegistry extends TranslatorLocator
      * Gets a translator from the registry by package for a locale.
      *
      * @param string $name The translator package to retrieve.
-     * @param string|null $locale The locale to use; if empty, uses the default
+     * @param string $locale The locale to use; if empty, uses the default
      * locale.
      * @return \Aura\Intl\TranslatorInterface A translator object.
      */
-    protected function _getTranslator(string $name, ?string $locale): TranslatorInterface
+    protected function _getTranslator(string $name, string $locale): TranslatorInterface
     {
         try {
             return parent::get($name, $locale);