Skip to content

Commit 1f56798

Browse files
authored
Merge pull request #304 from devnix/add-phpstan
Add PHPStan level 5
2 parents 95820f0 + 61282ae commit 1f56798

17 files changed

+56
-19
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"squizlabs/php_codesniffer": "^3.0",
2727
"brick/varexporter": "^0.3.5",
2828
"friendsofphp/php-cs-fixer": "^3.2",
29-
"oscarotero/php-cs-fixer-config": "^2.0"
29+
"oscarotero/php-cs-fixer-config": "^2.0",
30+
"phpstan/phpstan": "^1|^2"
3031
},
3132
"autoload": {
3233
"psr-4": {
@@ -41,7 +42,8 @@
4142
"scripts": {
4243
"test": [
4344
"phpunit",
44-
"phpcs"
45+
"phpcs",
46+
"phpstan"
4547
],
4648
"cs-fix": "php-cs-fixer fix"
4749
}

phpstan.dist.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 5
3+
paths:
4+
- src
5+
- tests

src/Comments.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
/**
1313
* Class to manage the comments of a translation.
14+
*
15+
* @phpstan-consistent-constructor
1416
*/
1517
class Comments implements JsonSerializable, Countable, IteratorAggregate
1618
{

src/Flags.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
/**
1313
* Class to manage the flags of a translation.
14+
*
15+
* @phpstan-consistent-constructor
1416
*/
1517
class Flags implements JsonSerializable, Countable, IteratorAggregate
1618
{

src/Headers.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
/**
1414
* Class to manage the headers of translations.
15+
*
16+
* @phpstan-consistent-constructor
1517
*/
1618
class Headers implements JsonSerializable, Countable, IteratorAggregate
1719
{

src/Loader/MoLoader.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ private function seekTo(int $position): void
125125

126126
private function readInt(string $byteOrder): int
127127
{
128-
if (($read = $this->read(4)) === false) {
129-
return 0;
130-
}
128+
$read = $this->read(4);
131129

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

src/Loader/PoLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public function loadString(string $string, ?Translations $translations = null):
2424
$nextLine = next($lines);
2525

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

3131
//Multiline

src/Loader/StrictPoLoader.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ final class StrictPoLoader extends Loader
3535
private $warnings = [];
3636
/** @var bool */
3737
private $isDisabled;
38-
/** @var bool */
39-
private $displayLineColumn;
4038

4139
/**
4240
* Generates a Translations object from a .po based string
@@ -194,11 +192,11 @@ private function readQuotedString(?string $context = null): string
194192
case '\\':
195193
$data .= $this->readEscape();
196194
break;
197-
// Unexpected newline
195+
// Unexpected newline
198196
case "\r":
199197
case "\n":
200198
throw new Exception("Newline character must be escaped{$this->getErrorPosition()}");
201-
// Unexpected end of file
199+
// Unexpected end of file
202200
case null:
203201
throw new Exception("Expected a closing quote{$this->getErrorPosition()}");
204202
}
@@ -229,6 +227,7 @@ private function readEscape(): string
229227
return chr($decimal);
230228
case 'x':
231229
$value = $this->readCharset($hexDigits, 1, PHP_INT_MAX, 'hexadecimal');
230+
232231
// GNU reads all valid hexadecimal chars, but only uses the last pair
233232
return hex2bin(str_pad(substr($value, -2), 2, '0', STR_PAD_LEFT));
234233
case 'U':
@@ -325,8 +324,15 @@ private function readIdentifier(string $identifier, bool $throwIfNotFound = fals
325324
*/
326325
private function readContext(): bool
327326
{
328-
return ($data = $this->readIdentifier('msgctxt')) !== null
329-
&& ($this->translation = $this->translation->withContext($data));
327+
$data = $this->readIdentifier('msgctxt');
328+
329+
if ($data === null) {
330+
return false;
331+
}
332+
333+
$this->translation = $this->translation->withContext($data);
334+
335+
return true;
330336
}
331337

332338
/**
@@ -342,7 +348,15 @@ private function readOriginal(): void
342348
*/
343349
private function readPlural(): bool
344350
{
345-
return ($data = $this->readIdentifier('msgid_plural')) !== null && $this->translation->setPlural($data);
351+
$data = $this->readIdentifier('msgid_plural');
352+
353+
if ($data === null) {
354+
return false;
355+
}
356+
357+
$this->translation->setPlural($data);
358+
359+
return true;
346360
}
347361

348362
/**

src/Merge.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ final class Merge
3030

3131
//Merge strategies
3232
public const SCAN_AND_LOAD =
33-
Merge::HEADERS_OVERRIDE
33+
Merge::HEADERS_OVERRIDE
3434
| Merge::TRANSLATIONS_OURS
3535
| Merge::TRANSLATIONS_OVERRIDE
3636
| Merge::EXTRACTED_COMMENTS_OURS

src/References.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
/**
1313
* Class to manage the references of a translation.
14+
*
15+
* @phpstan-consistent-constructor
1416
*/
1517
class References implements JsonSerializable, Countable, IteratorAggregate
1618
{
@@ -24,6 +26,10 @@ public static function __set_state(array $state): References
2426
return $references;
2527
}
2628

29+
public function __construct()
30+
{
31+
}
32+
2733
public function __debugInfo()
2834
{
2935
return $this->toArray();

src/Scanner/FunctionsHandlersTrait.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
/**
99
* Trait with common gettext function handlers
10+
*
11+
* @phpstan-ignore trait.unused
1012
*/
1113
trait FunctionsHandlersTrait
1214
{

src/Scanner/ParsedFunction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct(string $name, string $filename, int $line, ?int $las
2424
$this->lastLine = isset($lastLine) ? $lastLine : $line;
2525
}
2626

27-
public function __debugInfo()
27+
public function __debugInfo(): array
2828
{
2929
return $this->toArray();
3030
}

src/Translation.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
/**
77
* Class to manage an individual translation.
8+
*
9+
* @phpstan-consistent-constructor
810
*/
911
class Translation
1012
{

src/Translations.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
/**
1414
* Class to manage a collection of translations under the same domain.
15+
*
16+
* @phpstan-consistent-constructor
1517
*/
1618
class Translations implements Countable, IteratorAggregate
1719
{

tests/BasePoLoaderTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testPoLoader()
2424
This file is distributed under the same license as the PACKAGE package.
2525
FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
2626
EOT
27-
,
27+
,
2828
$description
2929
);
3030

tests/MoGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testMoGenerator()
2626
$translation->translate('Orixinal');
2727
$translations->add($translation);
2828

29-
$translation = Translation::create('context-1', 'Other comment', 'Other comments');
29+
$translation = Translation::create('context-1', 'Other comment');
3030
$translation->translate('Outro comentario');
3131
$translation->translatePlural('Outros comentarios');
3232
$translations->add($translation);

tests/PoGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testPoLoader()
3333
$translation->getReferences()->add('/my/template.php', 45);
3434
$translations->add($translation);
3535

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

0 commit comments

Comments
 (0)