diff --git a/composer.json b/composer.json index 92e59f66..61a944cd 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ }, "require": { "php": "^7.2|^8.0", - "gettext/languages": "^2.3" + "gettext/languages": "^2.3", + "ext-json": "*" }, "require-dev": { "phpunit/phpunit": "^8.0|^9.0", diff --git a/phpstan.dist.neon b/phpstan.dist.neon index d2554326..f226e5e2 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -1,5 +1,6 @@ parameters: - level: 5 +# level: 5 + level: 6 paths: - src - tests diff --git a/src/Comments.php b/src/Comments.php index ca44c97e..03525f74 100644 --- a/src/Comments.php +++ b/src/Comments.php @@ -13,11 +13,21 @@ * Class to manage the comments of a translation. * * @phpstan-consistent-constructor + * + * @phpstan-type CommentsType array + * + * @implements IteratorAggregate */ class Comments implements JsonSerializable, Countable, IteratorAggregate { + /** + * @var CommentsType + */ protected $comments = []; + /** + * @param array{comments: CommentsType} $state + */ public static function __set_state(array $state): Comments { return new static(...$state['comments']); @@ -51,9 +61,7 @@ public function delete(string ...$comments): self foreach ($comments as $comment) { $key = array_search($comment, $this->comments); - if (is_int($key)) { - array_splice($this->comments, $key, 1); - } + array_splice($this->comments, $key, 1); } return $this; @@ -76,6 +84,9 @@ public function count(): int return count($this->comments); } + /** + * @return CommentsType + */ public function toArray(): array { return $this->comments; diff --git a/src/Flags.php b/src/Flags.php index e8826bb8..a78079ca 100644 --- a/src/Flags.php +++ b/src/Flags.php @@ -13,11 +13,21 @@ * Class to manage the flags of a translation. * * @phpstan-consistent-constructor + * + * @phpstan-type FlagsType array + * + * @implements IteratorAggregate */ class Flags implements JsonSerializable, Countable, IteratorAggregate { + /** + * @var FlagsType + */ protected $flags = []; + /** + * @param array{flags: FlagsType} $state + */ public static function __set_state(array $state): Flags { return new static(...$state['flags']); @@ -83,6 +93,9 @@ public function count(): int return count($this->flags); } + /** + * @return FlagsType + */ public function toArray(): array { return $this->flags; diff --git a/src/Generator/MoGenerator.php b/src/Generator/MoGenerator.php index e8151016..59444733 100644 --- a/src/Generator/MoGenerator.php +++ b/src/Generator/MoGenerator.php @@ -8,6 +8,9 @@ final class MoGenerator extends Generator { + /** + * @var bool + */ private $includeHeaders = false; public function includeHeaders(bool $includeHeaders = true): self diff --git a/src/Generator/PoGenerator.php b/src/Generator/PoGenerator.php index 000c1a6a..9cb849f4 100644 --- a/src/Generator/PoGenerator.php +++ b/src/Generator/PoGenerator.php @@ -102,6 +102,8 @@ public function generateString(Translations $translations): string /** * Add one or more lines depending whether the string is multiline or not. + * + * @param array $lines */ private static function appendLines(array &$lines, string $prefix, string $name, string $value): void { diff --git a/src/Headers.php b/src/Headers.php index b6ad091f..0e3125d7 100644 --- a/src/Headers.php +++ b/src/Headers.php @@ -14,6 +14,10 @@ * Class to manage the headers of translations. * * @phpstan-consistent-constructor + * + * @phpstan-type HeadersType array + * + * @implements IteratorAggregate */ class Headers implements JsonSerializable, Countable, IteratorAggregate { @@ -21,13 +25,22 @@ class Headers implements JsonSerializable, Countable, IteratorAggregate public const HEADER_PLURAL = 'Plural-Forms'; public const HEADER_DOMAIN = 'X-Domain'; + /** + * @var HeadersType + */ protected $headers = []; + /** + * @param array{headers: HeadersType} $state + */ public static function __set_state(array $state): Headers { return new static($state['headers']); } + /** + * @param HeadersType $headers + */ public function __construct(array $headers = []) { $this->headers = $headers; @@ -115,7 +128,7 @@ public function setPluralForm(int $count, string $rule): self /** * Returns the parsed plural definition. * - * @return array|null [count, rule] + * @return array{int, string}|null [count, rule] */ public function getPluralForm(): ?array { @@ -130,6 +143,9 @@ public function getPluralForm(): ?array return null; } + /** + * @return HeadersType + */ public function toArray(): array { return $this->headers; diff --git a/src/Loader/MoLoader.php b/src/Loader/MoLoader.php index 5fc9419e..903ad737 100644 --- a/src/Loader/MoLoader.php +++ b/src/Loader/MoLoader.php @@ -11,8 +11,19 @@ */ final class MoLoader extends Loader { + /** + * @var string + */ private $string; + + /** + * @var int<0, max> + */ private $position; + + /** + * @var int<0, max> + */ private $length; private const MAGIC1 = -1794895138; @@ -132,6 +143,12 @@ private function readInt(string $byteOrder): int return (int) array_shift($read); } + /** + * @param string $byteOrder + * @param int $count + * + * @return array + */ private function readIntArray(string $byteOrder, int $count): array { return unpack($byteOrder.$count, $this->read(4 * $count)) ?: []; diff --git a/src/Loader/PoLoader.php b/src/Loader/PoLoader.php index ca7c281e..92bcaa1b 100644 --- a/src/Loader/PoLoader.php +++ b/src/Loader/PoLoader.php @@ -153,6 +153,11 @@ public function loadString(string $string, ?Translations $translations = null): return $translations; } + /** + * @param string|null $string + * + * @return array + */ private static function parseHeaders(?string $string): array { if (empty($string)) { diff --git a/src/Loader/StrictPoLoader.php b/src/Loader/StrictPoLoader.php index fe66c86d..ab81b88f 100644 --- a/src/Loader/StrictPoLoader.php +++ b/src/Loader/StrictPoLoader.php @@ -437,6 +437,8 @@ private function processHeader(): void /** * Parses the translation header data into an array + * + * @return array */ private function readHeaders(string $data): array { diff --git a/src/References.php b/src/References.php index 274219fe..3ec70803 100644 --- a/src/References.php +++ b/src/References.php @@ -13,11 +13,21 @@ * Class to manage the references of a translation. * * @phpstan-consistent-constructor + * + * @phpstan-type ReferencesType array> + * + * @implements IteratorAggregate> */ class References implements JsonSerializable, Countable, IteratorAggregate { + /** + * @var ReferencesType + */ protected $references = []; + /** + * @param array{references: ReferencesType} $state + */ public static function __set_state(array $state): References { $references = new static(); @@ -67,6 +77,9 @@ public function count(): int }, 0); } + /** + * @return ReferencesType + */ public function toArray(): array { return $this->references;