Skip to content

Phpstan level 6 #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
level: 5
# level: 5
level: 6
paths:
- src
- tests
17 changes: 14 additions & 3 deletions src/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@
* Class to manage the comments of a translation.
*
* @phpstan-consistent-constructor
*
* @phpstan-type CommentsType array<int, string>
*
* @implements IteratorAggregate<int, string>
*/
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']);
Expand Down Expand Up @@ -51,9 +61,7 @@ public function delete(string ...$comments): self
foreach ($comments as $comment) {
$key = array_search($comment, $this->comments);

if (is_int($key)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My previous question was more related to this change.

As far as I understand, should Comments::$comments be an array of integer keys only? If that is correct, this condition would be unnecessary :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$key can be false, if the comment wasn't found.

array_splice($this->comments, $key, 1);
}
array_splice($this->comments, $key, 1);
}

return $this;
Expand All @@ -76,6 +84,9 @@ public function count(): int
return count($this->comments);
}

/**
* @return CommentsType
*/
public function toArray(): array
{
return $this->comments;
Expand Down
13 changes: 13 additions & 0 deletions src/Flags.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@
* Class to manage the flags of a translation.
*
* @phpstan-consistent-constructor
*
* @phpstan-type FlagsType array<int, string>
*
* @implements IteratorAggregate<int, string>
*/
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']);
Expand Down Expand Up @@ -83,6 +93,9 @@ public function count(): int
return count($this->flags);
}

/**
* @return FlagsType
*/
public function toArray(): array
{
return $this->flags;
Expand Down
3 changes: 3 additions & 0 deletions src/Generator/MoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

final class MoGenerator extends Generator
{
/**
* @var bool
*/
private $includeHeaders = false;

public function includeHeaders(bool $includeHeaders = true): self
Expand Down
2 changes: 2 additions & 0 deletions src/Generator/PoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string> $lines
*/
private static function appendLines(array &$lines, string $prefix, string $name, string $value): void
{
Expand Down
18 changes: 17 additions & 1 deletion src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,33 @@
* Class to manage the headers of translations.
*
* @phpstan-consistent-constructor
*
* @phpstan-type HeadersType array<string, string>
*
* @implements IteratorAggregate<string, string>
*/
class Headers implements JsonSerializable, Countable, IteratorAggregate
{
public const HEADER_LANGUAGE = 'Language';
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;
Expand Down Expand Up @@ -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
{
Expand All @@ -130,6 +143,9 @@ public function getPluralForm(): ?array
return null;
}

/**
* @return HeadersType
*/
public function toArray(): array
{
return $this->headers;
Expand Down
17 changes: 17 additions & 0 deletions src/Loader/MoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,6 +143,12 @@ private function readInt(string $byteOrder): int
return (int) array_shift($read);
}

/**
* @param string $byteOrder
* @param int $count
*
* @return array<int, int>
*/
private function readIntArray(string $byteOrder, int $count): array
{
return unpack($byteOrder.$count, $this->read(4 * $count)) ?: [];
Expand Down
5 changes: 5 additions & 0 deletions src/Loader/PoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public function loadString(string $string, ?Translations $translations = null):
return $translations;
}

/**
* @param string|null $string
*
* @return array<string, string>
*/
private static function parseHeaders(?string $string): array
{
if (empty($string)) {
Expand Down
2 changes: 2 additions & 0 deletions src/Loader/StrictPoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ private function processHeader(): void

/**
* Parses the translation header data into an array
*
* @return array<string, string>
*/
private function readHeaders(string $data): array
{
Expand Down
13 changes: 13 additions & 0 deletions src/References.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@
* Class to manage the references of a translation.
*
* @phpstan-consistent-constructor
*
* @phpstan-type ReferencesType array<string, list<int>>
*
* @implements IteratorAggregate<string, list<int>>
*/
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();
Expand Down Expand Up @@ -67,6 +77,9 @@ public function count(): int
}, 0);
}

/**
* @return ReferencesType
*/
public function toArray(): array
{
return $this->references;
Expand Down
Loading