Skip to content

Commit fd08806

Browse files
committed
Phpstan level 5
1 parent fa4f5a3 commit fd08806

14 files changed

+79
-22
lines changed

Makefile

+14-11
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,15 @@ help:
3030
@grep -E '^[-a-zA-Z0-9_\.\/]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[32m%-15s\033[0m\t%s\n", $$1, $$2}'
3131

3232

33-
build: tests-all cs-check ## Run all necessary stuff before commit.
33+
build: tests-all cs-check phpstan ## Run all necessary stuff before commit.
3434

3535

36-
tests: CMD=composer tests -- $(ARGS)
37-
tests: docker-run ## Run tests on recent PHP version. Pass args to phpunit via ARGS=""
36+
tests: ## Run tests on recent PHP version. Pass args to phpunit via ARGS=""
37+
@$(call DOCKER_RUN,$(COVERAGE_PHP),composer tests -- $(ARGS))
3838

3939

40-
tests-coverage: CMD=composer tests-coverage -- $(ARGS)
4140
tests-coverage: ## Runs tests and creates ./clover.xml. Pass args to phpunit via ARGS=""
42-
@$(call DOCKER_RUN,$(COVERAGE_PHP),$(CMD))
41+
@$(call DOCKER_RUN,$(COVERAGE_PHP),composer tests-coverage -- $(ARGS))
4342

4443

4544
tests-all: ## Run tests on all supported PHP versions. Pass args to phpunit via ARGS=""
@@ -52,16 +51,20 @@ tests-all: ## Run tests on all supported PHP versions. Pass args to phpunit via
5251
done
5352

5453

55-
cs-check: CMD=composer cs-check
56-
cs-check: docker-run ## Check code style
54+
cs-check: ## Check code style
55+
@$(call DOCKER_RUN,$(LATEST_PHP),composer cs-check)
5756

5857

59-
cs-fix: CMD=composer cs-fix
60-
cs-fix: docker-run ## Fix code style
58+
phpstan: ## Run phpstan
59+
@$(call DOCKER_RUN,$(LATEST_PHP),composer phpstan)
6160

6261

63-
performance-tests: CMD=composer performance-tests
64-
performance-tests: docker-run ## Run performance tests
62+
cs-fix: ## Fix code style
63+
@$(call DOCKER_RUN,$(LATEST_PHP),composer cs-fix)
64+
65+
66+
performance-tests: ## Run performance tests
67+
@$(call DOCKER_RUN,$(LATEST_PHP),composer performance-tests)
6568

6669

6770
release: .env build

composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"tests-coverage": "build/composer-update.sh && XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-clover clover.xml",
1414
"cs-check": "build/composer-update.sh && vendor/bin/php-cs-fixer fix --dry-run --verbose --allow-risky=yes",
1515
"cs-fix": "build/composer-update.sh && vendor/bin/php-cs-fixer fix --verbose --allow-risky=yes",
16+
"phpstan": "build/composer-update.sh && vendor/bin/phpstan analyse",
1617
"performance-tests": "php -d xdebug.mode=off -d opcache.enable_cli=1 -d opcache.jit_buffer_size=100M test/performance/testPerformance.php"
1718
},
1819
"config": {
@@ -25,6 +26,7 @@
2526
"require-dev": {
2627
"ext-json": "*",
2728
"friendsofphp/php-cs-fixer": "^3.0",
29+
"phpstan/phpstan": "^1.10",
2830
"phpunit/phpunit": "^8.0"
2931
},
3032
"suggest": {

phpstan.neon

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
parameters:
2+
level: 5
3+
paths:
4+
- src
5+
- test
6+
bootstrapFiles:
7+
- %rootDir%/../../../test/bootstrap.php
8+
treatPhpDocTypesAsCertain: false
9+
exceptions:
10+
implicitThrows: false
11+
check:
12+
missingCheckedExceptionInThrows: true
13+
tooWideThrowType: true
14+
reportUncheckedExceptionDeadCatch: true
15+
checkedExceptionClasses:
16+
- JsonMachine\Exception\PathNotFoundException
17+
- JsonMachine\Exception\SyntaxErrorException
18+
- JsonMachine\Exception\UnexpectedEndSyntaxErrorException
19+
uncheckedExceptionClasses:
20+
- JsonMachine\Exception\InvalidArgumentException
21+
- JsonMachine\Exception\JsonMachineException

src/Exception/SyntaxErrorException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class SyntaxErrorException extends JsonMachineException
88
{
9-
public function __construct($message, $position)
9+
public function __construct(string $message, int $position)
1010
{
1111
parent::__construct($message." At position $position.");
1212
}

src/FileChunks.php

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace JsonMachine;
66

7+
/**
8+
* @implements \IteratorAggregate<int, string>
9+
*/
710
class FileChunks implements \IteratorAggregate
811
{
912
/** @var string */

src/Parser.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ class Parser implements \IteratorAggregate, PositionAware
3737
/** @var ItemDecoder */
3838
private $jsonDecoder;
3939

40-
/** @var string */
40+
/** @var string|null */
4141
private $matchedJsonPointer;
4242

4343
/** @var array */
4444
private $paths;
4545

46-
/** @var array */
46+
/** @var array|null */
4747
private $currentPath;
4848

4949
/** @var array */
@@ -247,7 +247,7 @@ public function getIterator()
247247
}
248248

249249
if ($token === null) {
250-
$this->error('Cannot iterate empty JSON', $token);
250+
$this->error('Cannot iterate empty JSON', '');
251251
}
252252

253253
if ($currentLevel > -1 && ! $subtreeEnded) {
@@ -352,7 +352,7 @@ public function getMatchedJsonPointer(): string
352352
*/
353353
private function error($msg, $token, $exception = SyntaxErrorException::class)
354354
{
355-
throw new $exception($msg." '".$token."'", $this->tokens->getPosition());
355+
throw new $exception($msg." '".$token."'", $this->tokens instanceof PositionAware ? $this->tokens->getPosition() : 0);
356356
}
357357

358358
/**

src/StreamChunks.php

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
use JsonMachine\Exception\InvalidArgumentException;
88

9+
/**
10+
* @implements \IteratorAggregate<int, string>
11+
*/
912
class StreamChunks implements \IteratorAggregate
1013
{
1114
/** @var resource */

src/TokensWithDebugging.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44

55
namespace JsonMachine;
66

7+
/**
8+
* @implements \IteratorAggregate<int, string>
9+
*/
710
class TokensWithDebugging implements \IteratorAggregate, PositionAware
811
{
9-
/** @var iterable */
12+
/** @var iterable<int, string> */
1013
private $jsonChunks;
1114

15+
/** @var int */
1216
private $position = 0;
17+
18+
/** @var int */
1319
private $line = 1;
20+
21+
/** @var int */
1422
private $column = 0;
1523

1624
/**
17-
* @param iterable<string> $jsonChunks
25+
* @param iterable<int, string> $jsonChunks
1826
*/
1927
public function __construct($jsonChunks)
2028
{
@@ -53,6 +61,7 @@ public function getIterator()
5361
foreach ($this->jsonChunks as $bytes) {
5462
$bytesLength = strlen($bytes);
5563
for ($i = 0; $i < $bytesLength; ++$i) {
64+
/** @var string $byte */
5665
$byte = $bytes[$i];
5766
if ($inString) {
5867
if ($byte == '"' && ! $escaping) {

src/ValidJsonPointers.php

+13
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,23 @@
88

99
final class ValidJsonPointers
1010
{
11+
/** @var string[] */
1112
private $jsonPointers = [];
1213

14+
/** @var bool */
1315
private $validated = false;
1416

17+
/**
18+
* @param string[] $jsonPointers
19+
*/
1520
public function __construct(array $jsonPointers)
1621
{
1722
$this->jsonPointers = array_values($jsonPointers);
1823
}
1924

2025
/**
26+
* @return string[]
27+
*
2128
* @throws InvalidArgumentException
2229
*/
2330
public function toArray(): array
@@ -30,6 +37,8 @@ public function toArray(): array
3037
}
3138

3239
/**
40+
* @return void
41+
*
3342
* @throws InvalidArgumentException
3443
*/
3544
private function validate()
@@ -40,6 +49,8 @@ private function validate()
4049
}
4150

4251
/**
52+
* @return void
53+
*
4354
* @throws InvalidArgumentException
4455
*/
4556
private function validateFormat()
@@ -54,6 +65,8 @@ private function validateFormat()
5465
}
5566

5667
/**
68+
* @return void
69+
*
5770
* @throws InvalidArgumentException
5871
*/
5972
private function validateJsonPointersDoNotIntersect()

src/autoloader.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
*/
2020
class Autoloading
2121
{
22-
static public function autoloader($class)
22+
/**
23+
* @return void
24+
*/
25+
static public function autoloader(string $class)
2326
{
2427
$prefix = 'JsonMachine\\';
2528
$baseDir = __DIR__.DIRECTORY_SEPARATOR;

test/JsonMachineTest/Exception/SyntaxErrorExceptionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class SyntaxErrorExceptionTest extends TestCase
1414
{
1515
public function testMessageContainsDataFromConstructor()
1616
{
17-
$exception = new SyntaxErrorException('msg 42', '24');
17+
$exception = new SyntaxErrorException('msg 42', 24);
1818

1919
$this->assertContains('msg 42', $exception->getMessage());
2020
$this->assertContains('24', $exception->getMessage());

test/JsonMachineTest/JsonDecoder/DecodingErrorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class DecodingErrorTest extends TestCase
1414
{
1515
public function testGetMalformedJson()
1616
{
17-
$decodingError = new DecodingError('"json\"', null);
17+
$decodingError = new DecodingError('"json\"', '');
1818

1919
$this->assertSame('"json\"', $decodingError->getMalformedJson());
2020
}

test/JsonMachineTest/StreamChunksTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class StreamChunksTest extends \PHPUnit_Framework_TestCase
1515
public function testThrowsIfNoResource()
1616
{
1717
$this->expectException(InvalidArgumentException::class);
18+
/* @phpstan-ignore-next-line */
1819
new StreamChunks(false);
1920
}
2021

test/JsonMachineTest/ValidJsonPointersTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class ValidJsonPointersTest extends \PHPUnit_Framework_TestCase
1616
* @dataProvider data_testThrowsOnIntersectingPaths
1717
*
1818
* @param $jsonPointers
19-
* @param ParserTest $parserTest
2019
*/
2120
public function testThrowsOnIntersectingPaths($jsonPointers)
2221
{

0 commit comments

Comments
 (0)