Skip to content

Commit 1a8151f

Browse files
authored
feat: phpstan level 5 (#446)
* feat: phpstan level 5 * fix: $database undefined * fix: Unsafe usage of new static() * fix: ignore NativeAppServiceProvider not existing * fix: FreshCommand constructor invoked with 1 parameter, 0 required * fix: MenuBuilder facade function duplicates and arguments * fix: Type void cannot be part of a union type declaration. * fix: Php compactor missing imports and return statement * fix: missing SeedDatabaseCommand@handle return statement * Fix: cannot assign a value to a public readonly property outside of the constructor * Fix: PowerMonitor invoked Client::get() with 2 parameters, 1 required * fix: alternative for FreshCommand migrator argument * Revert "fix: alternative for FreshCommand migrator argument" This reverts commit cac9ea1. * Revert "fix: FreshCommand constructor invoked with 1 parameter, 0 required" This reverts commit cc1cb87. * fix: trying something * fix: phpstan.yml * Revert "fix: trying something" This reverts commit 6b88d13. * fix: trying to lower the minimum laravel 10 dependency * fix: final fix 🎉 * Revert "Fix: cannot assign a value to a public readonly property outside of the constructor" This reverts commit 585fb47. * fix: put back previous fixes and ignore phpstan errors
1 parent c96a7c5 commit 1a8151f

15 files changed

+92
-60
lines changed

.github/workflows/phpstan.yml

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
11
name: PHPStan
22

33
on:
4+
workflow_dispatch:
45
push:
5-
paths:
6-
- '**.php'
7-
- 'phpstan.neon.dist'
6+
branches-ignore:
7+
- 'dependabot/npm_and_yarn/*'
88

99
jobs:
1010
phpstan:
11-
name: phpstan
12-
runs-on: ubuntu-latest
11+
runs-on: ${{ matrix.os }}
12+
strategy:
13+
matrix:
14+
os: [ubuntu-latest]
15+
php: [8.3]
16+
1317
steps:
14-
- uses: actions/checkout@v4
18+
19+
- name: Checkout code
20+
uses: actions/checkout@v4
1521

1622
- name: Setup PHP
1723
uses: shivammathur/setup-php@v2
1824
with:
19-
php-version: '8.1'
20-
coverage: none
25+
php-version: ${{ matrix.php }}
26+
27+
- name: Get composer cache directory
28+
id: composer-cache
29+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
30+
31+
- name: Cache Composer dependencies
32+
uses: actions/cache@v4
33+
with:
34+
path: ${{ steps.composer-cache.outputs.dir }}
35+
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
36+
restore-keys: ${{ runner.os }}-${{ matrix.php }}-composer-
2137

22-
- name: Install composer dependencies
23-
uses: ramsey/composer-install@v3
38+
- name: Install Dependencies
39+
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
2440

25-
- name: Run PHPStan
26-
run: ./vendor/bin/phpstan --error-format=github
41+
- name: Run analysis
42+
run: ./vendor/bin/phpstan analyse --error-format=github

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ composer.lock
55
coverage
66
docs
77
phpunit.xml
8-
phpstan.neon
98
testbench.yaml
109
vendor
1110
node_modules

composer.json

+7-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
},
3939
"require-dev": {
4040
"guzzlehttp/guzzle": "^7.0",
41+
"larastan/larastan": "^2.0|^3.0",
4142
"laravel/pint": "^1.0",
4243
"nunomaduro/collision": "^7.9",
43-
"nunomaduro/larastan": "^2.0.1",
4444
"orchestra/testbench": "^8.0",
4545
"pestphp/pest": "^2.0",
4646
"pestphp/pest-plugin-arch": "^2.0",
@@ -52,8 +52,7 @@
5252
},
5353
"autoload": {
5454
"psr-4": {
55-
"Native\\Laravel\\": "src/",
56-
"Native\\Laravel\\Database\\Factories\\": "database/factories/"
55+
"Native\\Laravel\\": "src/"
5756
}
5857
},
5958
"autoload-dev": {
@@ -62,6 +61,11 @@
6261
}
6362
},
6463
"scripts": {
64+
"qa" : [
65+
"@composer format",
66+
"@composer analyse",
67+
"@composer test"
68+
],
6569
"post-autoload-dump": "@php ./vendor/bin/testbench package:discover --ansi",
6670
"analyse": "vendor/bin/phpstan analyse",
6771
"test": "vendor/bin/pest",

phpstan-baseline.neon

Whitespace-only changes.

phpstan.neon

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
parameters:
2+
3+
paths:
4+
- src/
5+
- config/
6+
# - tests/
7+
8+
9+
# Level 9 is the highest level
10+
level: 5
11+
12+
ignoreErrors:
13+
- '#Class App\\Providers\\NativeAppServiceProvider not found#'
14+
- '#Class Native\\Laravel\\ChildProcess has an uninitialized readonly property#'
15+
16+
#
17+
# excludePaths:
18+
# - ./*/*/FileToBeExcluded.php

phpstan.neon.dist

-13
This file was deleted.

src/ChildProcess.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class ChildProcess implements ChildProcessContract
1919

2020
public readonly bool $persistent;
2121

22-
public function __construct(protected Client $client) {}
22+
final public function __construct(protected Client $client) {}
2323

24-
public function get(?string $alias = null): ?static
24+
public function get(?string $alias = null): ?self
2525
{
2626
$alias = $alias ?? $this->alias;
2727

@@ -62,7 +62,7 @@ public function start(
6262
?string $cwd = null,
6363
?array $env = null,
6464
bool $persistent = false
65-
): static {
65+
): self {
6666
$cmd = is_array($cmd) ? array_values($cmd) : [$cmd];
6767

6868
$process = $this->client->post('child-process/start', [
@@ -87,7 +87,7 @@ public function php(string|array $cmd, string $alias, ?array $env = null, ?bool
8787
$process = $this->client->post('child-process/start-php', [
8888
'alias' => $alias,
8989
'cmd' => $cmd,
90-
'cwd' => $cwd ?? base_path(),
90+
'cwd' => base_path(),
9191
'env' => $env,
9292
'persistent' => $persistent,
9393
])->json();
@@ -115,7 +115,7 @@ public function stop(?string $alias = null): void
115115
])->json();
116116
}
117117

118-
public function restart(?string $alias = null): ?static
118+
public function restart(?string $alias = null): ?self
119119
{
120120
$process = $this->client->post('child-process/restart', [
121121
'alias' => $alias ?? $this->alias,
@@ -128,7 +128,7 @@ public function restart(?string $alias = null): ?static
128128
return $this->fromRuntimeProcess($process);
129129
}
130130

131-
public function message(string $message, ?string $alias = null): static
131+
public function message(string $message, ?string $alias = null): self
132132
{
133133
$this->client->post('child-process/message', [
134134
'alias' => $alias ?? $this->alias,
@@ -138,9 +138,10 @@ public function message(string $message, ?string $alias = null): static
138138
return $this;
139139
}
140140

141-
protected function fromRuntimeProcess($process): static
141+
protected function fromRuntimeProcess($process)
142142
{
143143
if (isset($process['pid'])) {
144+
// @phpstan-ignore-next-line
144145
$this->pid = $process['pid'];
145146
}
146147

src/Commands/SeedDatabaseCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public function handle()
1515
{
1616
(new NativeServiceProvider($this->laravel))->rewriteDatabase();
1717

18-
parent::handle();
18+
return parent::handle();
1919
}
2020
}

src/Compactor/Php.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Native\Laravel\Compactor;
44

55
use PhpToken;
6+
use RuntimeException;
7+
use Webmozart\Assert\Assert;
68

79
class Php
810
{
@@ -17,7 +19,7 @@ public function compact(string $file, string $contents): string
1719
return $this->compactContent($contents);
1820
}
1921

20-
$this->compactContent($contents);
22+
return $this->compactContent($contents);
2123
}
2224

2325
protected function compactContent(string $contents): string
@@ -145,7 +147,6 @@ private function retokenizeAttribute(array &$tokens, int $opener): ?array
145147
{
146148
Assert::keyExists($tokens, $opener);
147149

148-
/** @var PhpToken $token */
149150
$token = $tokens[$opener];
150151
$attributeBody = mb_substr($token->text, 2);
151152
$subTokens = PhpToken::tokenize('<?php '.$attributeBody);

src/Dialog.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Dialog
2626

2727
protected $windowReference;
2828

29-
public function __construct(protected Client $client) {}
29+
final public function __construct(protected Client $client) {}
3030

3131
public static function new()
3232
{

src/Dock.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ public function cancelBounce()
4343
$this->client->post('dock/cancel-bounce');
4444
}
4545

46-
public function badge(?string $label = null): void|string
46+
public function badge(?string $label = null): ?string
4747
{
4848
if (is_null($label)) {
4949
return $this->client->get('dock/badge');
5050
}
5151

5252
$this->client->post('dock/badge', ['label' => $label]);
53+
54+
return null;
5355
}
5456
}

src/Facades/Menu.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Native\Laravel\Facades;
44

55
use Illuminate\Support\Facades\Facade;
6+
use Native\Laravel\Contracts\MenuItem;
67
use Native\Laravel\Menu\Items\Checkbox;
78
use Native\Laravel\Menu\Items\Label;
89
use Native\Laravel\Menu\Items\Link;
@@ -11,7 +12,7 @@
1112
use Native\Laravel\Menu\Items\Separator;
1213

1314
/**
14-
* @method static \Native\Laravel\Menu\Menu make(\Native\Laravel\Menu\Items\MenuItem ...$items)
15+
* @method static \Native\Laravel\Menu\Menu make(MenuItem ...$items)
1516
* @method static Checkbox checkbox(string $label, bool $checked = false, ?string $hotkey = null)
1617
* @method static Label label(string $label)
1718
* @method static Link link(string $url, string $label = null, ?string $hotkey = null)
@@ -23,7 +24,6 @@
2324
* @method static Role view()
2425
* @method static Role window()
2526
* @method static Role help()
26-
* @method static Role window()
2727
* @method static Role fullscreen()
2828
* @method static Role separator()
2929
* @method static Role devTools()
@@ -37,7 +37,6 @@
3737
* @method static Role minimize()
3838
* @method static Role close()
3939
* @method static Role quit()
40-
* @method static Role help()
4140
* @method static Role hide()
4241
* @method static void create(MenuItem ...$items)
4342
* @method static void default()

src/NativeServiceProvider.php

+19-14
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function packageRegistered()
4949
$this->mergeConfigFrom($this->package->basePath('/../config/nativephp-internal.php'), 'nativephp-internal');
5050

5151
$this->app->singleton(FreshCommand::class, function ($app) {
52+
/* @phpstan-ignore-next-line (beacause we support Laravel 10 & 11) */
5253
return new FreshCommand($app['migrator']);
5354
});
5455

@@ -148,13 +149,15 @@ public function rewriteDatabase()
148149
}
149150
}
150151

151-
config(['database.connections.nativephp' => [
152-
'driver' => 'sqlite',
153-
'url' => env('DATABASE_URL'),
154-
'database' => $databasePath,
155-
'prefix' => '',
156-
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
157-
]]);
152+
config([
153+
'database.connections.nativephp' => [
154+
'driver' => 'sqlite',
155+
'url' => env('DATABASE_URL'),
156+
'database' => $databasePath,
157+
'prefix' => '',
158+
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
159+
],
160+
]);
158161

159162
config(['database.default' => 'nativephp']);
160163

@@ -174,7 +177,7 @@ public function removeDatabase()
174177

175178
@unlink($databasePath);
176179
@unlink($databasePath.'-shm');
177-
@unlink($database.'-wal');
180+
@unlink($databasePath.'-wal');
178181
}
179182

180183
protected function configureDisks(): void
@@ -197,12 +200,14 @@ protected function configureDisks(): void
197200
continue;
198201
}
199202

200-
config(['filesystems.disks.'.$disk => [
201-
'driver' => 'local',
202-
'root' => env($env, ''),
203-
'throw' => false,
204-
'links' => 'skip',
205-
]]);
203+
config([
204+
'filesystems.disks.'.$disk => [
205+
'driver' => 'local',
206+
'root' => env($env, ''),
207+
'throw' => false,
208+
'links' => 'skip',
209+
],
210+
]);
206211
}
207212
}
208213
}

src/Notification.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Notification
1212

1313
protected string $event = '';
1414

15-
public function __construct(protected Client $client) {}
15+
final public function __construct(protected Client $client) {}
1616

1717
public static function new()
1818
{

src/ProgressBar.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ProgressBar
1616

1717
protected float $maxSecondsBetweenRedraws = 1;
1818

19-
public function __construct(protected int $maxSteps, protected Client $client) {}
19+
final public function __construct(protected int $maxSteps, protected Client $client) {}
2020

2121
public static function create(int $maxSteps): static
2222
{

0 commit comments

Comments
 (0)