Skip to content

Commit 2a8bbfb

Browse files
committed
Fix phpstan errors
1 parent 5f0061d commit 2a8bbfb

13 files changed

+141
-60
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ trim_trailing_whitespace = true
1111
[*.md]
1212
trim_trailing_whitespace = false
1313

14-
[*.yml]
14+
[{*.yml,*.yaml,*.neon}]
1515
indent_size = 2

.gitattributes

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
* text=auto
2-
3-
/.github export-ignore
4-
5-
/tests export-ignore
6-
phpunit.xml export-ignore
1+
* text=auto eol=lf
2+
*.pp eol=lf linguist-language=EBNF
3+
*.pp2 eol=lf linguist-language=EBNF
74

85
.editorconfig export-ignore
6+
.php-cs-fixer.php export-ignore
97
.gitattributes export-ignore
108
.gitignore export-ignore
119

10+
phpunit.xml export-ignore
11+
psalm.xml export-ignore
12+
rector.php export-ignore
13+
14+
/.github export-ignore
15+
/example export-ignore
16+
/tests export-ignore

.github/workflows/codestyle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
php: [ '8.2' ]
14+
php: [ '8.3' ]
1515
os: [ ubuntu-latest ]
1616
steps:
1717
- name: Set Git To Use LF

.github/workflows/main.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ name: build
33
on:
44
push:
55
pull_request:
6-
schedule:
7-
- cron: '0 0 * * *'
86

97
jobs:
108
tests:
@@ -13,7 +11,7 @@ jobs:
1311
strategy:
1412
fail-fast: false
1513
matrix:
16-
php: [ '7.4', '8.0', '8.1', '8.2' ]
14+
php: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
1715
ts: [ 'ts', 'nts' ]
1816
os: [ ubuntu-latest, macos-latest, windows-latest ]
1917
stability: [ prefer-lowest, prefer-stable ]
@@ -38,4 +36,4 @@ jobs:
3836
max_attempts: 5
3937
command: composer update --prefer-dist --no-interaction --no-progress
4038
- name: Execute Tests
41-
run: vendor/bin/phpunit --testdox
39+
run: composer test

.github/workflows/security.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
php: [ '8.2' ]
16+
php: [ '8.3' ]
1717
os: [ ubuntu-latest ]
1818
steps:
1919
- name: Set Git To Use LF

.github/workflows/static-analysis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
php: [ '8.2' ]
14+
php: [ '8.3' ]
1515
os: [ ubuntu-latest ]
1616
steps:
1717
- name: Set Git To Use LF
@@ -33,5 +33,4 @@ jobs:
3333
max_attempts: 5
3434
command: composer update --prefer-dist --no-interaction --no-progress
3535
- name: Static Analysis
36-
continue-on-error: true
37-
run: composer psalm:check
36+
run: composer linter

composer.json

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,33 @@
2929
}
3030
},
3131
"require-dev": {
32-
"friendsofphp/php-cs-fixer": "^3.22",
33-
"vimeo/psalm": "^5.14",
34-
"phpunit/phpunit": "^9.6"
32+
"phpunit/phpunit": "^9.5",
33+
"friendsofphp/php-cs-fixer": "^3.53",
34+
"phpstan/phpstan": "^2.0",
35+
"phpstan/phpstan-deprecation-rules": "^2.0",
36+
"phpstan/phpstan-strict-rules": "^2.0"
3537
},
3638
"autoload-dev": {
3739
"psr-4": {
3840
"FFI\\WorkDirectory\\Tests\\": "tests"
3941
}
4042
},
4143
"config": {
42-
"sort-packages": true,
43-
"optimize-autoloader": true
44+
"optimize-autoloader": true,
45+
"preferred-install": {
46+
"*": "dist"
47+
},
48+
"sort-packages": true
4449
},
4550
"scripts": {
46-
"psalm": "@psalm:check",
47-
"psalm:check": "psalm --no-cache",
48-
"psalm:fix": "psalm --no-cache --alter",
51+
"test": ["@test:unit"],
52+
"test:unit": "phpunit --testdox --testsuite=unit",
53+
"linter": "@linter:check",
54+
"linter:check": "phpstan analyse --configuration phpstan.neon",
55+
"linter:baseline": "@linter:check -- --generate-baseline",
4956
"phpcs": "@phpcs:check",
50-
"phpcs:check": "php-cs-fixer fix --config=.php-cs-fixer.php --allow-risky=yes --dry-run",
51-
"phpcs:fix": "php-cs-fixer fix --config=.php-cs-fixer.php --allow-risky=yes",
52-
"test": "@test:unit",
53-
"test:unit": "phpunit --testdox"
57+
"phpcs:check": "@phpcs:fix -- --dry-run",
58+
"phpcs:fix": "php-cs-fixer fix --config=.php-cs-fixer.php --allow-risky=yes --verbose --diff"
5459
},
5560
"extra": {
5661
"branch-alias": {

phpstan.neon

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
includes:
2+
- phar://phpstan.phar/conf/bleedingEdge.neon
3+
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
4+
- vendor/phpstan/phpstan-strict-rules/rules.neon
5+
parameters:
6+
level: max
7+
phpVersion:
8+
min: 70400
9+
max: 80400
10+
parallel:
11+
jobSize: 20
12+
maximumNumberOfProcesses: 4
13+
minimumNumberOfJobsPerProcess: 2
14+
paths:
15+
- src
16+
tmpDir: vendor/.cache.phpstan
17+
rememberPossiblyImpureFunctionValues: false
18+
checkTooWideReturnTypesInProtectedAndPublicMethods: true
19+
checkImplicitMixed: true
20+
checkBenevolentUnionTypes: true
21+
reportPossiblyNonexistentGeneralArrayOffset: true
22+
reportPossiblyNonexistentConstantArrayOffset: true
23+
reportAlwaysTrueInLastCondition: true
24+
reportAnyTypeWideningInVarTag: true
25+
checkMissingOverrideMethodAttribute: false
26+
inferPrivatePropertyTypeFromConstructor: true
27+
tipsOfTheDay: false
28+
checkMissingCallableSignature: true

psalm.xml

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Driver/NonThreadSafeDriver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ final class NonThreadSafeDriver implements DriverInterface
1212
{
1313
public function get(): ?string
1414
{
15+
// Allow short ternary operator
16+
// @phpstan-ignore ternary.shortNotAllowed
1517
return \getcwd() ?: null;
1618
}
1719

src/Driver/ThreadSafeDriver.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,58 @@ public function __construct()
2929
*/
3030
private function getInitialCurrentWorkingDirectory(): ?string
3131
{
32-
if ($directory = \getcwd()) {
32+
if ((bool) ($directory = \getcwd()) !== false) {
3333
return $directory;
3434
}
3535

36-
if (isset($_SERVER['SCRIPT_NAME']) && $directory = \dirname($_SERVER['SCRIPT_NAME'])) {
36+
if (($directory = $this->fetchVariable('SCRIPT_NAME')) !== null) {
3737
return $directory;
3838
}
3939

40-
if (isset($_SERVER['SCRIPT_FILENAME']) && $directory = \dirname($_SERVER['SCRIPT_FILENAME'])) {
40+
if (($directory = $this->fetchVariable('SCRIPT_FILENAME')) !== null) {
4141
return $directory;
4242
}
4343

44-
if (isset($_SERVER['PHP_SELF']) && $directory = \dirname($_SERVER['PHP_SELF'])) {
44+
if (($directory = $this->fetchVariable('PHP_SELF')) !== null) {
4545
return $directory;
4646
}
4747

4848
return null;
4949
}
5050

5151
/**
52-
* @return array{fallback:non-empty-string|null}
52+
* @param non-empty-string $variable
53+
* @return non-empty-string|null
54+
*/
55+
private function fetchVariable(string $variable): ?string
56+
{
57+
if (!isset($_SERVER[$variable]) || !\is_string($_SERVER[$variable])) {
58+
return null;
59+
}
60+
61+
$directory = \dirname($_SERVER[$variable]);
62+
63+
return $directory === '' ? null : $directory;
64+
}
65+
66+
/**
67+
* @return array{
68+
* fallback: non-empty-string|null,
69+
* ...
70+
* }
5371
*/
5472
public function __serialize(): array
5573
{
56-
return ['fallback' => $this->fallback];
74+
return [
75+
'fallback' => $this->fallback,
76+
];
5777
}
5878

5979
/**
60-
* @param array{fallback:non-empty-string|null} $data
80+
* @param array{
81+
* fallback?: non-empty-string|null,
82+
* ...
83+
* } $data
6184
*/
6285
public function __unserialize(array $data): void
6386
{

src/Driver/UnixAwareThreadSafeDriver.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function __construct()
3939

4040
private function boot(): void
4141
{
42+
// @phpstan-ignore-next-line : PHPStan false-positive
4243
$this->ffi = \FFI::cdef(self::STDLIB);
4344
}
4445

@@ -55,19 +56,23 @@ public function get(): ?string
5556
* modified by the program, but may be overwritten by a subsequent
5657
* call to the getenv function.
5758
*
58-
* @var CData $directory
59+
* @var CData|null $directory
60+
* @phpstan-ignore-next-line : PHPStan false-positive
5961
*/
6062
$directory = $this->ffi->getenv(static::getEnvVariableName());
6163

6264
if ($directory === null) {
6365
return $this->fallback;
6466
}
6567

68+
// Allow short ternary operator
69+
// @phpstan-ignore ternary.shortNotAllowed
6670
return \FFI::string($directory) ?: $this->fallback;
6771
}
6872

6973
public function set(string $directory): bool
7074
{
75+
// @phpstan-ignore-next-line : PHPStan false-positive
7176
return $this->ffi->setenv(static::getEnvVariableName(), $directory, 1) === 0;
7277
}
7378

0 commit comments

Comments
 (0)