Skip to content

Commit

Permalink
Fix phpstan errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Dec 17, 2024
1 parent 5f0061d commit 2a8bbfb
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false

[*.yml]
[{*.yml,*.yaml,*.neon}]
indent_size = 2
17 changes: 11 additions & 6 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
* text=auto

/.github export-ignore

/tests export-ignore
phpunit.xml export-ignore
* text=auto eol=lf
*.pp eol=lf linguist-language=EBNF
*.pp2 eol=lf linguist-language=EBNF

.editorconfig export-ignore
.php-cs-fixer.php export-ignore
.gitattributes export-ignore
.gitignore export-ignore

phpunit.xml export-ignore
psalm.xml export-ignore
rector.php export-ignore

/.github export-ignore
/example export-ignore
/tests export-ignore
2 changes: 1 addition & 1 deletion .github/workflows/codestyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '8.2' ]
php: [ '8.3' ]
os: [ ubuntu-latest ]
steps:
- name: Set Git To Use LF
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name: build
on:
push:
pull_request:
schedule:
- cron: '0 0 * * *'

jobs:
tests:
Expand All @@ -13,7 +11,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '7.4', '8.0', '8.1', '8.2' ]
php: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
ts: [ 'ts', 'nts' ]
os: [ ubuntu-latest, macos-latest, windows-latest ]
stability: [ prefer-lowest, prefer-stable ]
Expand All @@ -38,4 +36,4 @@ jobs:
max_attempts: 5
command: composer update --prefer-dist --no-interaction --no-progress
- name: Execute Tests
run: vendor/bin/phpunit --testdox
run: composer test
2 changes: 1 addition & 1 deletion .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '8.2' ]
php: [ '8.3' ]
os: [ ubuntu-latest ]
steps:
- name: Set Git To Use LF
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ '8.2' ]
php: [ '8.3' ]
os: [ ubuntu-latest ]
steps:
- name: Set Git To Use LF
Expand All @@ -33,5 +33,4 @@ jobs:
max_attempts: 5
command: composer update --prefer-dist --no-interaction --no-progress
- name: Static Analysis
continue-on-error: true
run: composer psalm:check
run: composer linter
29 changes: 17 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,33 @@
}
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.22",
"vimeo/psalm": "^5.14",
"phpunit/phpunit": "^9.6"
"phpunit/phpunit": "^9.5",
"friendsofphp/php-cs-fixer": "^3.53",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0"
},
"autoload-dev": {
"psr-4": {
"FFI\\WorkDirectory\\Tests\\": "tests"
}
},
"config": {
"sort-packages": true,
"optimize-autoloader": true
"optimize-autoloader": true,
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"scripts": {
"psalm": "@psalm:check",
"psalm:check": "psalm --no-cache",
"psalm:fix": "psalm --no-cache --alter",
"test": ["@test:unit"],
"test:unit": "phpunit --testdox --testsuite=unit",
"linter": "@linter:check",
"linter:check": "phpstan analyse --configuration phpstan.neon",
"linter:baseline": "@linter:check -- --generate-baseline",
"phpcs": "@phpcs:check",
"phpcs:check": "php-cs-fixer fix --config=.php-cs-fixer.php --allow-risky=yes --dry-run",
"phpcs:fix": "php-cs-fixer fix --config=.php-cs-fixer.php --allow-risky=yes",
"test": "@test:unit",
"test:unit": "phpunit --testdox"
"phpcs:check": "@phpcs:fix -- --dry-run",
"phpcs:fix": "php-cs-fixer fix --config=.php-cs-fixer.php --allow-risky=yes --verbose --diff"
},
"extra": {
"branch-alias": {
Expand Down
28 changes: 28 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
includes:
- phar://phpstan.phar/conf/bleedingEdge.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
parameters:
level: max
phpVersion:
min: 70400
max: 80400
parallel:
jobSize: 20
maximumNumberOfProcesses: 4
minimumNumberOfJobsPerProcess: 2
paths:
- src
tmpDir: vendor/.cache.phpstan
rememberPossiblyImpureFunctionValues: false
checkTooWideReturnTypesInProtectedAndPublicMethods: true
checkImplicitMixed: true
checkBenevolentUnionTypes: true
reportPossiblyNonexistentGeneralArrayOffset: true
reportPossiblyNonexistentConstantArrayOffset: true
reportAlwaysTrueInLastCondition: true
reportAnyTypeWideningInVarTag: true
checkMissingOverrideMethodAttribute: false
inferPrivatePropertyTypeFromConstructor: true
tipsOfTheDay: false
checkMissingCallableSignature: true
19 changes: 0 additions & 19 deletions psalm.xml

This file was deleted.

2 changes: 2 additions & 0 deletions src/Driver/NonThreadSafeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ final class NonThreadSafeDriver implements DriverInterface
{
public function get(): ?string
{
// Allow short ternary operator
// @phpstan-ignore ternary.shortNotAllowed
return \getcwd() ?: null;
}

Expand Down
37 changes: 30 additions & 7 deletions src/Driver/ThreadSafeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,58 @@ public function __construct()
*/
private function getInitialCurrentWorkingDirectory(): ?string
{
if ($directory = \getcwd()) {
if ((bool) ($directory = \getcwd()) !== false) {
return $directory;
}

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

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

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

return null;
}

/**
* @return array{fallback:non-empty-string|null}
* @param non-empty-string $variable
* @return non-empty-string|null
*/
private function fetchVariable(string $variable): ?string
{
if (!isset($_SERVER[$variable]) || !\is_string($_SERVER[$variable])) {
return null;
}

$directory = \dirname($_SERVER[$variable]);

return $directory === '' ? null : $directory;
}

/**
* @return array{
* fallback: non-empty-string|null,
* ...
* }
*/
public function __serialize(): array
{
return ['fallback' => $this->fallback];
return [
'fallback' => $this->fallback,
];
}

/**
* @param array{fallback:non-empty-string|null} $data
* @param array{
* fallback?: non-empty-string|null,
* ...
* } $data
*/
public function __unserialize(array $data): void
{
Expand Down
7 changes: 6 additions & 1 deletion src/Driver/UnixAwareThreadSafeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct()

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

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

if ($directory === null) {
return $this->fallback;
}

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

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

Expand Down
Loading

0 comments on commit 2a8bbfb

Please sign in to comment.