Skip to content

Add PHPStan level 5 #304

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

Merged
merged 13 commits into from
Apr 25, 2025
Merged
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"squizlabs/php_codesniffer": "^3.0",
"brick/varexporter": "^0.3.5",
"friendsofphp/php-cs-fixer": "^3.2",
"oscarotero/php-cs-fixer-config": "^2.0"
"oscarotero/php-cs-fixer-config": "^2.0",
"phpstan/phpstan": "^1|^2"
},
"autoload": {
"psr-4": {
Expand All @@ -41,7 +42,8 @@
"scripts": {
"test": [
"phpunit",
"phpcs"
"phpcs",
"phpstan"
],
"cs-fix": "php-cs-fixer fix"
}
Expand Down
5 changes: 5 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 5
paths:
- src
- tests
2 changes: 2 additions & 0 deletions src/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/**
* Class to manage the comments of a translation.
*
* @phpstan-consistent-constructor
*/
class Comments implements JsonSerializable, Countable, IteratorAggregate
{
Expand Down
2 changes: 2 additions & 0 deletions src/Flags.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/**
* Class to manage the flags of a translation.
*
* @phpstan-consistent-constructor
*/
class Flags implements JsonSerializable, Countable, IteratorAggregate
{
Expand Down
2 changes: 2 additions & 0 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

/**
* Class to manage the headers of translations.
*
* @phpstan-consistent-constructor
*/
class Headers implements JsonSerializable, Countable, IteratorAggregate
{
Expand Down
4 changes: 1 addition & 3 deletions src/Loader/MoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ private function seekTo(int $position): void

private function readInt(string $byteOrder): int
{
if (($read = $this->read(4)) === false) {
return 0;
}
$read = $this->read(4);

$read = (array) unpack($byteOrder, $read);

Expand Down
4 changes: 2 additions & 2 deletions src/Loader/PoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function loadString(string $string, ?Translations $translations = null):
$nextLine = next($lines);

// Treat empty comments as empty lines https://github.com/php-gettext/Gettext/pull/296
if ($line === "#") {
$line = "";
if ($line === '#') {
$line = '';
}

//Multiline
Expand Down
28 changes: 21 additions & 7 deletions src/Loader/StrictPoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ final class StrictPoLoader extends Loader
private $warnings = [];
/** @var bool */
private $isDisabled;
/** @var bool */
private $displayLineColumn;

/**
* Generates a Translations object from a .po based string
Expand Down Expand Up @@ -194,11 +192,11 @@ private function readQuotedString(?string $context = null): string
case '\\':
$data .= $this->readEscape();
break;
// Unexpected newline
// Unexpected newline
case "\r":
case "\n":
throw new Exception("Newline character must be escaped{$this->getErrorPosition()}");
// Unexpected end of file
// Unexpected end of file
case null:
throw new Exception("Expected a closing quote{$this->getErrorPosition()}");
}
Expand Down Expand Up @@ -229,6 +227,7 @@ private function readEscape(): string
return chr($decimal);
case 'x':
$value = $this->readCharset($hexDigits, 1, PHP_INT_MAX, 'hexadecimal');

// GNU reads all valid hexadecimal chars, but only uses the last pair
return hex2bin(str_pad(substr($value, -2), 2, '0', STR_PAD_LEFT));
case 'U':
Expand Down Expand Up @@ -325,8 +324,15 @@ private function readIdentifier(string $identifier, bool $throwIfNotFound = fals
*/
private function readContext(): bool
{
return ($data = $this->readIdentifier('msgctxt')) !== null
&& ($this->translation = $this->translation->withContext($data));
$data = $this->readIdentifier('msgctxt');

if ($data === null) {
return false;
}

$this->translation = $this->translation->withContext($data);

return true;
}

/**
Expand All @@ -342,7 +348,15 @@ private function readOriginal(): void
*/
private function readPlural(): bool
{
return ($data = $this->readIdentifier('msgid_plural')) !== null && $this->translation->setPlural($data);
$data = $this->readIdentifier('msgid_plural');

if ($data === null) {
return false;
}

$this->translation->setPlural($data);

return true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class Merge

//Merge strategies
public const SCAN_AND_LOAD =
Merge::HEADERS_OVERRIDE
Merge::HEADERS_OVERRIDE
| Merge::TRANSLATIONS_OURS
| Merge::TRANSLATIONS_OVERRIDE
| Merge::EXTRACTED_COMMENTS_OURS
Expand Down
6 changes: 6 additions & 0 deletions src/References.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

/**
* Class to manage the references of a translation.
*
* @phpstan-consistent-constructor
*/
class References implements JsonSerializable, Countable, IteratorAggregate
{
Expand All @@ -24,6 +26,10 @@ public static function __set_state(array $state): References
return $references;
}

public function __construct()
{
}

public function __debugInfo()
{
return $this->toArray();
Expand Down
2 changes: 2 additions & 0 deletions src/Scanner/FunctionsHandlersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/**
* Trait with common gettext function handlers
*
* @phpstan-ignore trait.unused
*/
trait FunctionsHandlersTrait
{
Expand Down
2 changes: 1 addition & 1 deletion src/Scanner/ParsedFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(string $name, string $filename, int $line, ?int $las
$this->lastLine = isset($lastLine) ? $lastLine : $line;
}

public function __debugInfo()
public function __debugInfo(): array
{
return $this->toArray();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

/**
* Class to manage an individual translation.
*
* @phpstan-consistent-constructor
*/
class Translation
{
Expand Down
2 changes: 2 additions & 0 deletions src/Translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

/**
* Class to manage a collection of translations under the same domain.
*
* @phpstan-consistent-constructor
*/
class Translations implements Countable, IteratorAggregate
{
Expand Down
2 changes: 1 addition & 1 deletion tests/BasePoLoaderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testPoLoader()
This file is distributed under the same license as the PACKAGE package.
FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
EOT
,
,
$description
);

Expand Down
2 changes: 1 addition & 1 deletion tests/MoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testMoGenerator()
$translation->translate('Orixinal');
$translations->add($translation);

$translation = Translation::create('context-1', 'Other comment', 'Other comments');
$translation = Translation::create('context-1', 'Other comment');
$translation->translate('Outro comentario');
$translation->translatePlural('Outros comentarios');
$translations->add($translation);
Expand Down
2 changes: 1 addition & 1 deletion tests/PoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testPoLoader()
$translation->getReferences()->add('/my/template.php', 45);
$translations->add($translation);

$translation = Translation::create('context-1', 'Other comment', 'Other comments');
$translation = Translation::create('context-1', 'Other comment');
$translation->translate('Outro comentario');
$translation->translatePlural('Outros comentarios');
$translation->getExtractedComments()->add('Not sure about this');
Expand Down