Skip to content

Commit d47e089

Browse files
authored
Upgrade to PHPStan 2 (#47)
1 parent 20e911c commit d47e089

File tree

9 files changed

+99
-20
lines changed

9 files changed

+99
-20
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
/.gitignore export-ignore
55
/.php-cs-fixer.php export-ignore
66
/Makefile export-ignore
7+
/Dockerfile export-ignore
8+
/docker-compose.yml export-ignore
79
/composer-require-checker.json export-ignore
810
/phpstan-baseline.neon export-ignore
911
/phpstan.neon export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/.phpunit.cache/
33
/vendor/
44
/coverage/
5+
/.env
56
/.php-cs-fixer.cache
67
/.phpunit.result.cache
78
/composer.lock

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM php:8.3
2+
3+
ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
4+
5+
RUN install-php-extensions @composer pcov
6+
7+
ARG USER_ID
8+
ARG GROUP_ID
9+
10+
RUN groupadd --gid ${GROUP_ID} code \
11+
&& useradd --create-home --shell /bin/bash --uid ${USER_ID} --gid code code
12+
13+
USER code

Makefile

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
CSFIX_PHP_BIN=PHP_CS_FIXER_IGNORE_ENV=1 php8.2
2-
PHP_BIN=php8.2 -d zend.assertions=1
3-
COMPOSER_BIN=$(shell command -v composer)
1+
ifdef CI
2+
DOCKER_PHP_EXEC :=
3+
else
4+
DOCKER_PHP_EXEC := docker compose run --rm php
5+
endif
46

57
all: csfix static-analysis test
68
@echo "Done."
79

8-
vendor: composer.json
9-
$(PHP_BIN) $(COMPOSER_BIN) update
10-
$(PHP_BIN) $(COMPOSER_BIN) bump
11-
touch vendor
10+
.env: /etc/passwd /etc/group Makefile
11+
printf "USER_ID=%s\nGROUP_ID=%s\n" `id --user "${USER}"` `id --group "${USER}"` > .env
12+
13+
vendor: .env docker-compose.yml Dockerfile composer.json
14+
docker compose build --pull
15+
$(DOCKER_PHP_EXEC) composer update
16+
$(DOCKER_PHP_EXEC) composer bump
17+
touch --no-create $@
1218

1319
.PHONY: csfix
1420
csfix: vendor
15-
$(CSFIX_PHP_BIN) vendor/bin/php-cs-fixer fix -v $(arg)
21+
$(DOCKER_PHP_EXEC) vendor/bin/php-cs-fixer fix -v $(arg)
1622

1723
.PHONY: static-analysis
1824
static-analysis: vendor
19-
$(PHP_BIN) vendor/bin/phpstan analyse $(PHPSTAN_ARGS)
25+
$(DOCKER_PHP_EXEC) php -d zend.assertions=1 vendor/bin/phpstan analyse $(PHPSTAN_ARGS)
2026

2127
.PHONY: test
2228
test: vendor
23-
$(PHP_BIN) vendor/bin/phpunit $(PHPUNIT_ARGS)
29+
$(DOCKER_PHP_EXEC) php -d zend.assertions=1 vendor/bin/phpunit $(PHPUNIT_ARGS)

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
"php": "~8.3.0 || ~8.4.0"
1515
},
1616
"require-dev": {
17-
"phpstan/phpstan": "^1.12.9",
18-
"phpstan/phpstan-phpunit": "^1.4.0",
19-
"phpstan/phpstan-strict-rules": "^1.6.1",
17+
"phpstan/phpstan": "^2",
18+
"phpstan/phpstan-phpunit": "^2",
19+
"phpstan/phpstan-strict-rules": "^2",
2020
"phpunit/phpunit": "^11.4.3",
21-
"slam/php-cs-fixer-extensions": "^3.12.0",
21+
"slam/php-cs-fixer-extensions": "^3.12",
2222
"symfony/console": "^7.1.7"
2323
},
2424
"autoload": {

docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
php:
3+
build:
4+
context: .
5+
args:
6+
USER_ID: ${USER_ID}
7+
GROUP_ID: ${GROUP_ID}
8+
volumes:
9+
- .:${PWD}
10+
working_dir: ${PWD}

lib/ErrorHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ public function exceptionHandler(Throwable $exception): void
246246

247247
$this->outputError(\PHP_EOL);
248248
$this->outputError(\sprintf('<error> %s </error>', \str_repeat(' ', $width)));
249-
foreach ($lines as $line) {
250-
$this->outputError(\sprintf('<error> %s%s </error>', $line, \str_repeat(' ', \max(0, $width - \strlen($line)))));
249+
foreach ($lines as $line2) {
250+
$this->outputError(\sprintf('<error> %s%s </error>', $line2, \str_repeat(' ', \max(0, $width - \strlen($line2)))));
251251
}
252252
$this->outputError(\sprintf('<error> %s </error>', \str_repeat(' ', $width)));
253253
$this->outputError(\PHP_EOL);

phpstan-baseline.neon

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,62 @@
11
parameters:
22
ignoreErrors:
33
-
4-
message: "#^Expression \"\\$arrayPerVerificaErrori\\['undefined_index'\\]\" on a separate line does not do anything\\.$#"
4+
message: '#^Parameter \#1 \$search of function str_replace expects array\<string\>\|string, mixed given\.$#'
5+
identifier: argument.type
6+
count: 1
7+
path: lib/ErrorHandler.php
8+
9+
-
10+
message: '#^Parameter \#2 \$array of function implode expects array\<string\>, mixed given\.$#'
11+
identifier: argument.type
12+
count: 1
13+
path: lib/ErrorHandler.php
14+
15+
-
16+
message: '#^Parameter \#2 \$array of function implode expects array\|null, mixed given\.$#'
17+
identifier: argument.type
18+
count: 1
19+
path: lib/ErrorHandler.php
20+
21+
-
22+
message: '#^Parameter \#3 \.\.\.\$values of function sprintf expects bool\|float\|int\|string\|null, mixed given\.$#'
23+
identifier: argument.type
24+
count: 1
25+
path: lib/ErrorHandler.php
26+
27+
-
28+
message: '#^Expression "\$arrayPerVerificaErrori\[''undefined_index''\]" on a separate line does not do anything\.$#'
29+
identifier: expr.resultUnused
530
count: 1
631
path: tests/ErrorHandlerTest.php
732

833
-
9-
message: "#^Expression \"@\\$arrayPerVerificaErrori\\['no_exception_thrown_on_undefined_index_now'\\]\" on a separate line does not do anything\\.$#"
34+
message: '#^Expression "@\$arrayPerVerificaErrori\[''no_exception_thrown_on_undefined_index_now''\]" on a separate line does not do anything\.$#'
35+
identifier: expr.resultUnused
1036
count: 1
1137
path: tests/ErrorHandlerTest.php
1238

1339
-
14-
message: "#^Offset 'no_exception_thrown…' does not exist on array\\{\\}\\.$#"
40+
message: '#^Offset ''no_exception_thrown_on_undefined_index_now'' does not exist on array\{\}\.$#'
41+
identifier: offsetAccess.notFound
1542
count: 1
1643
path: tests/ErrorHandlerTest.php
1744

1845
-
19-
message: "#^Offset 'undefined_index' does not exist on array\\{\\}\\.$#"
46+
message: '#^Offset ''undefined_index'' does not exist on array\{\}\.$#'
47+
identifier: offsetAccess.notFound
2048
count: 1
2149
path: tests/ErrorHandlerTest.php
2250

51+
-
52+
message: '#^Parameter \#1 \$needle of static method PHPUnit\\Framework\\Assert\:\:assertStringContainsString\(\) expects string, mixed given\.$#'
53+
identifier: argument.type
54+
count: 2
55+
path: tests/ErrorHandlerTest.php
56+
57+
-
58+
message: '#^Parameter \#1 \$needle of static method PHPUnit\\Framework\\Assert\:\:assertStringNotContainsString\(\) expects string, mixed given\.$#'
59+
identifier: argument.type
60+
count: 2
61+
path: tests/ErrorHandlerTest.php
62+

tests/ErrorHandlerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ final class ErrorHandlerTest extends TestCase
1919
/** @var list<array{subject: string, body: string}> */
2020
private array $emailsSent = [];
2121
private ErrorHandler $errorHandler;
22+
private bool $unregister = false;
2223

2324
protected function setUp(): void
2425
{
@@ -44,6 +45,10 @@ protected function tearDown(): void
4445
\putenv('COLUMNS');
4546
\ini_set('error_log', $this->backupErrorLog);
4647
@\unlink($this->errorLog);
48+
if ($this->unregister) {
49+
\restore_exception_handler();
50+
\restore_error_handler();
51+
}
4752
}
4853

4954
public function testDefaultConfiguration(): void
@@ -76,6 +81,7 @@ public function testRegisterBuiltinHandlers(): void
7681
{
7782
\error_reporting(\E_ALL);
7883
$this->errorHandler->register();
84+
$this->unregister = true;
7985
$arrayPerVerificaErrori = [];
8086

8187
@ $arrayPerVerificaErrori['no_exception_thrown_on_undefined_index_now'];
@@ -96,6 +102,7 @@ public function testScream(): void
96102

97103
\error_reporting(\E_ALL);
98104
$this->errorHandler->register();
105+
$this->unregister = true;
99106

100107
@ \trigger_error(\uniqid('deprecated_'), \E_USER_DEPRECATED);
101108

0 commit comments

Comments
 (0)