diff --git a/composer.json b/composer.json index 16bdde90..81f1e205 100644 --- a/composer.json +++ b/composer.json @@ -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": { @@ -41,7 +42,8 @@ "scripts": { "test": [ "phpunit", - "phpcs" + "phpcs", + "phpstan" ], "cs-fix": "php-cs-fixer fix" } diff --git a/phpstan.dist.neon b/phpstan.dist.neon new file mode 100644 index 00000000..d2554326 --- /dev/null +++ b/phpstan.dist.neon @@ -0,0 +1,5 @@ +parameters: + level: 5 + paths: + - src + - tests diff --git a/src/Comments.php b/src/Comments.php index cd706b93..ca44c97e 100644 --- a/src/Comments.php +++ b/src/Comments.php @@ -11,6 +11,8 @@ /** * Class to manage the comments of a translation. + * + * @phpstan-consistent-constructor */ class Comments implements JsonSerializable, Countable, IteratorAggregate { diff --git a/src/Flags.php b/src/Flags.php index 49add0be..e8826bb8 100644 --- a/src/Flags.php +++ b/src/Flags.php @@ -11,6 +11,8 @@ /** * Class to manage the flags of a translation. + * + * @phpstan-consistent-constructor */ class Flags implements JsonSerializable, Countable, IteratorAggregate { diff --git a/src/Headers.php b/src/Headers.php index 87c782e3..b6ad091f 100644 --- a/src/Headers.php +++ b/src/Headers.php @@ -12,6 +12,8 @@ /** * Class to manage the headers of translations. + * + * @phpstan-consistent-constructor */ class Headers implements JsonSerializable, Countable, IteratorAggregate { diff --git a/src/Loader/MoLoader.php b/src/Loader/MoLoader.php index 57406813..1e2fb68b 100644 --- a/src/Loader/MoLoader.php +++ b/src/Loader/MoLoader.php @@ -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); diff --git a/src/Loader/PoLoader.php b/src/Loader/PoLoader.php index e7e706c7..6816b5b3 100644 --- a/src/Loader/PoLoader.php +++ b/src/Loader/PoLoader.php @@ -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 diff --git a/src/Loader/StrictPoLoader.php b/src/Loader/StrictPoLoader.php index 5a9c1152..6ed0d9e9 100644 --- a/src/Loader/StrictPoLoader.php +++ b/src/Loader/StrictPoLoader.php @@ -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 @@ -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()}"); } @@ -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': @@ -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; } /** @@ -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; } /** diff --git a/src/Merge.php b/src/Merge.php index 0406b81d..e69fb908 100644 --- a/src/Merge.php +++ b/src/Merge.php @@ -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 diff --git a/src/References.php b/src/References.php index b6758b17..274219fe 100644 --- a/src/References.php +++ b/src/References.php @@ -11,6 +11,8 @@ /** * Class to manage the references of a translation. + * + * @phpstan-consistent-constructor */ class References implements JsonSerializable, Countable, IteratorAggregate { @@ -24,6 +26,10 @@ public static function __set_state(array $state): References return $references; } + public function __construct() + { + } + public function __debugInfo() { return $this->toArray(); diff --git a/src/Scanner/FunctionsHandlersTrait.php b/src/Scanner/FunctionsHandlersTrait.php index 47301427..75e4c13a 100644 --- a/src/Scanner/FunctionsHandlersTrait.php +++ b/src/Scanner/FunctionsHandlersTrait.php @@ -7,6 +7,8 @@ /** * Trait with common gettext function handlers + * + * @phpstan-ignore trait.unused */ trait FunctionsHandlersTrait { diff --git a/src/Scanner/ParsedFunction.php b/src/Scanner/ParsedFunction.php index 255df4d1..4f4477d0 100644 --- a/src/Scanner/ParsedFunction.php +++ b/src/Scanner/ParsedFunction.php @@ -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(); } diff --git a/src/Translation.php b/src/Translation.php index f4c86adb..69cb6b90 100644 --- a/src/Translation.php +++ b/src/Translation.php @@ -5,6 +5,8 @@ /** * Class to manage an individual translation. + * + * @phpstan-consistent-constructor */ class Translation { diff --git a/src/Translations.php b/src/Translations.php index 761851b5..11913ef5 100644 --- a/src/Translations.php +++ b/src/Translations.php @@ -12,6 +12,8 @@ /** * Class to manage a collection of translations under the same domain. + * + * @phpstan-consistent-constructor */ class Translations implements Countable, IteratorAggregate { diff --git a/tests/BasePoLoaderTestCase.php b/tests/BasePoLoaderTestCase.php index 76cdac63..4029d72b 100644 --- a/tests/BasePoLoaderTestCase.php +++ b/tests/BasePoLoaderTestCase.php @@ -24,7 +24,7 @@ public function testPoLoader() This file is distributed under the same license as the PACKAGE package. FIRST AUTHOR , YEAR. EOT - , + , $description ); diff --git a/tests/MoGeneratorTest.php b/tests/MoGeneratorTest.php index a3e246dc..4a0bd27e 100644 --- a/tests/MoGeneratorTest.php +++ b/tests/MoGeneratorTest.php @@ -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); diff --git a/tests/PoGeneratorTest.php b/tests/PoGeneratorTest.php index f2b6726e..11da0a9e 100644 --- a/tests/PoGeneratorTest.php +++ b/tests/PoGeneratorTest.php @@ -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');