Skip to content

Commit 9edac44

Browse files
refactor: Update PHPStan configuration paths and create new config files for improved structure and clarity. (#48)
1 parent ad8bed7 commit 9edac44

16 files changed

+132
-179
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
- Bug #45: Improve `ServiceMap` configuration for application types (`Base`, `Console`, `Web`) (@terabytesoftw)
1616
- Bug #46: Update `README.md` to enhance clarity and structure of `docs/installation.md`, `docs/configuration.md` and `docs/examples.md` (@terabytesoftw)
1717
- Enh #47: Add `ActiveRecordGetAttributeDynamicMethodReturnTypeExtension` to provide precise type inference for `getAttribute()` method calls based on PHPDoc annotations (@terabytesoftw)
18+
- Bug #48: Update `PHPStan` configuration paths and create new config files for improved structure and clarity (@terabytesoftw)
1819

1920
## 0.2.3 June 09, 2025
2021

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,19 @@ includes:
7373
7474
parameters:
7575
level: 5
76+
7677
paths:
7778
- src
7879
- controllers
7980
- models
81+
82+
tmpDir: %currentWorkingDirectory%/tests/runtime
8083
8184
yii2:
82-
config_path: config/test.php
85+
config_path: config/phpstan-config.php
8386
```
8487

85-
Create a PHPStan-specific config file (`config/test.php`).
88+
Create a PHPStan-specific config file (`config/phpstan-config.php`).
8689

8790
```php
8891
<?php

docs/configuration.md

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ includes:
1414
1515
parameters:
1616
level: 5
17+
1718
paths:
1819
- src
20+
21+
tmpDir: %currentWorkingDirectory%/tests/runtime
1922
2023
yii2:
21-
config_path: config/phpstan.php
24+
config_path: config/phpstan-config.php
2225
```
2326

2427
### Standard Web Application
@@ -31,21 +34,24 @@ parameters:
3134
bootstrapFiles:
3235
- tests/bootstrap.php
3336
37+
excludePaths:
38+
- config/
39+
- runtime/
40+
- vendor/
41+
- web/assets/
42+
3443
level: 6
44+
3545
paths:
3646
- controllers
3747
- models
3848
- widgets
3949
- components
4050
41-
excludePaths:
42-
- config/
43-
- runtime/
44-
- vendor/
45-
- web/assets/
51+
tmpDir: %currentWorkingDirectory%/tests/runtime
4652
4753
yii2:
48-
config_path: config/phpstan.php
54+
config_path: config/phpstan-config.php
4955
```
5056

5157
## Application Type Configuration
@@ -54,7 +60,7 @@ parameters:
5460

5561
```php
5662
<?php
57-
// config/phpstan.php
63+
// config/phpstan-config.php
5864
return [
5965
'phpstan' => [
6066
'application_type' => \yii\web\Application::class,
@@ -69,7 +75,7 @@ For console applications, you **must** explicitly specify the application type.
6975

7076
```php
7177
<?php
72-
// config/phpstan-console.php
78+
// config/phpstan-console-config.php
7379
return [
7480
'phpstan' => [
7581
'application_type' => \yii\console\Application::class,
@@ -95,8 +101,10 @@ parameters:
95101
- commands
96102
- console
97103
104+
tmpDir: %currentWorkingDirectory%/tests/runtime
105+
98106
yii2:
99-
config_path: config/phpstan-console.php
107+
config_path: config/phpstan-console-config.php
100108
```
101109

102110
## Dynamic Constants Configuration
@@ -142,7 +150,7 @@ Define your application components for proper type inference:
142150

143151
```php
144152
<?php
145-
// config/phpstan.php
153+
// config/phpstan-config.php
146154
return [
147155
'components' => [
148156
// Database
@@ -279,6 +287,11 @@ includes:
279287
- vendor/yii2-extensions/phpstan/extension.neon
280288
281289
parameters:
290+
excludePaths:
291+
- src/legacy/
292+
- tests/_support/
293+
- vendor/
294+
282295
level: 8
283296
284297
paths:
@@ -288,13 +301,10 @@ parameters:
288301
- widgets
289302
- components
290303
291-
excludePaths:
292-
- src/legacy/
293-
- tests/_support/
294-
- vendor/
304+
tmpDir: %currentWorkingDirectory%/tests/runtime
295305
296306
yii2:
297-
config_path: config/phpstan.php
307+
config_path: config/phpstan-config.php
298308
299309
# Strict checks
300310
checkImplicitMixed: true
@@ -315,20 +325,20 @@ parameters:
315325
### Performance Optimization
316326

317327
```neon
318-
parameters:
319-
# Memory management
320-
tmpDir: var/cache/phpstan
321-
328+
parameters:
329+
# Bootstrap optimization
330+
bootstrapFiles:
331+
- vendor/autoload.php
332+
- config/phpstan-bootstrap.php
333+
322334
# Parallel processing
323335
parallel:
324336
jobSize: 20
325337
maximumNumberOfProcesses: 32
326338
minimumNumberOfJobsPerProcess: 2
327-
328-
# Bootstrap optimization
329-
bootstrapFiles:
330-
- vendor/autoload.php
331-
- config/phpstan-bootstrap.php
339+
340+
# Memory management
341+
tmpDir: %currentWorkingDirectory%/tests/runtime
332342
```
333343

334344
Optimized bootstrap file.
@@ -379,7 +389,8 @@ includes:
379389
380390
parameters:
381391
level: 6
382-
tmpDir: var/cache/phpstan
392+
393+
tmpDir: %currentWorkingDirectory%/tests/runtime
383394
```
384395

385396
### Web Configuration
@@ -394,9 +405,9 @@ parameters:
394405
- models
395406
- widgets
396407
- web
397-
408+
398409
yii2:
399-
config_path: config/phpstan-web.php
410+
config_path: config/phpstan-config.php
400411
```
401412

402413
### Console Configuration
@@ -409,9 +420,9 @@ parameters:
409420
paths:
410421
- commands
411422
- console
412-
423+
413424
yii2:
414-
config_path: config/phpstan-console.php
425+
config_path: config/phpstan-console-config.php
415426
```
416427

417428
### Usage

docs/installation.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,16 @@ includes:
8282
8383
parameters:
8484
level: 5
85+
8586
paths:
8687
- src
8788
- controllers
8889
- models
8990
91+
tmpDir: %currentWorkingDirectory%/tests/runtime
92+
9093
yii2:
91-
config_path: config/phpstan.php
94+
config_path: config/phpstan-config.php
9295
```
9396

9497
## Creating PHPStan Configuration File
@@ -97,7 +100,7 @@ Create a dedicated configuration file for PHPStan analysis. This should be separ
97100

98101
### Web Application Configuration
99102

100-
Create `config/phpstan.php`.
103+
Create `config/phpstan-config.php`.
101104

102105
```php
103106
<?php
@@ -138,7 +141,7 @@ return [
138141

139142
### Console Application Configuration
140143

141-
For console applications, create `config/phpstan-console.php`.
144+
For console applications, create `config/phpstan-console-config.php`.
142145

143146
```php
144147
<?php
@@ -164,7 +167,7 @@ And update your `phpstan.neon`.
164167
```neon
165168
parameters:
166169
yii2:
167-
config_path: config/phpstan-console.php
170+
config_path: config/phpstan-console-config.php
168171
```
169172

170173
## Verification

phpstan-console.neon

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ parameters:
1717
- src
1818
- tests/console
1919

20+
tmpDir: %currentWorkingDirectory%/tests/runtime
21+
22+
yii2:
23+
config_path: tests/config/phpstan-console-config.php
24+
2025
# Enable strict advanced checks
2126
checkImplicitMixed: true
2227
checkBenevolentUnionTypes: true
@@ -26,6 +31,3 @@ parameters:
2631
reportAnyTypeWideningInVarTag: true
2732
reportPossiblyNonexistentConstantArrayOffset: true
2833
reportPossiblyNonexistentGeneralArrayOffset: true
29-
30-
yii2:
31-
config_path: tests/console/config/config.php

phpstan.neon

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ parameters:
1717
- src
1818
- tests/web
1919

20+
tmpDir: %currentWorkingDirectory%/tests/runtime
21+
22+
yii2:
23+
config_path: tests/config/phpstan-config.php
24+
2025
# Enable strict advanced checks
2126
checkImplicitMixed: true
2227
checkBenevolentUnionTypes: true
@@ -26,6 +31,3 @@ parameters:
2631
reportAnyTypeWideningInVarTag: true
2732
reportPossiblyNonexistentConstantArrayOffset: true
2833
reportPossiblyNonexistentGeneralArrayOffset: true
29-
30-
yii2:
31-
config_path: tests/config/config.php

tests/ServiceMapBehaviorTest.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@
3131
*/
3232
final class ServiceMapBehaviorTest extends TestCase
3333
{
34+
/**
35+
* Base path for configuration files used in tests.
36+
*/
37+
private const BASE_PATH = __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR;
38+
3439
/**
3540
* @throws ReflectionException if the component definition is invalid or can't be resolved.
3641
*/
3742
public function testReturnBehaviorsWhenValidClassIsClassString(): void
3843
{
39-
$ds = DIRECTORY_SEPARATOR;
40-
$configPath = __DIR__ . "{$ds}config{$ds}config.php";
41-
$serviceMap = new ServiceMap($configPath);
44+
$serviceMap = new ServiceMap(self::BASE_PATH . 'phpstan-config.php');
4245

4346
$behaviors = $serviceMap->getBehaviorsByClassName(MyComponent::class);
4447

@@ -57,9 +60,7 @@ public function testReturnBehaviorsWhenValidClassIsClassString(): void
5760
*/
5861
public function testReturnBehaviorsWhenValidClassIsString(): void
5962
{
60-
$ds = DIRECTORY_SEPARATOR;
61-
$configPath = __DIR__ . "{$ds}config{$ds}config.php";
62-
$serviceMap = new ServiceMap($configPath);
63+
$serviceMap = new ServiceMap(self::BASE_PATH . 'phpstan-config.php');
6364

6465
$behaviors = $serviceMap->getBehaviorsByClassName('yii2\extensions\phpstan\tests\stub\MyComponent');
6566

@@ -78,9 +79,7 @@ public function testReturnBehaviorsWhenValidClassIsString(): void
7879
*/
7980
public function testReturnEmptyArrayWhenClassHasNotBehaviors(): void
8081
{
81-
$ds = DIRECTORY_SEPARATOR;
82-
$configPath = __DIR__ . "{$ds}config{$ds}config.php";
83-
$serviceMap = new ServiceMap($configPath);
82+
$serviceMap = new ServiceMap(self::BASE_PATH . 'phpstan-config.php');
8483

8584
$behaviors = $serviceMap->getBehaviorsByClassName('NonExistentClass');
8685

@@ -96,9 +95,7 @@ public function testReturnEmptyArrayWhenClassHasNotBehaviors(): void
9695
*/
9796
public function testReturnEmptyArrayWhenNotBehaviorsConfigured(): void
9897
{
99-
$ds = DIRECTORY_SEPARATOR;
100-
$configPath = __DIR__ . "{$ds}config{$ds}config.php";
101-
$serviceMap = new ServiceMap($configPath);
98+
$serviceMap = new ServiceMap(self::BASE_PATH . 'phpstan-config.php');
10299

103100
$behaviors = $serviceMap->getBehaviorsByClassName('AnyClass');
104101

@@ -114,36 +111,29 @@ public function testReturnEmptyArrayWhenNotBehaviorsConfigured(): void
114111
*/
115112
public function testThrowExceptionWhenBehaviorDefinitionNotArray(): void
116113
{
117-
$ds = DIRECTORY_SEPARATOR;
118-
$configPath = __DIR__ . "{$ds}config{$ds}behaviors-unsupported-definition-not-array.php";
119-
120114
$this->expectException(RuntimeException::class);
121115
$this->expectExceptionMessage('Behavior definition for \'MyComponent\' must be an array.');
122116

123-
new ServiceMap($configPath);
117+
new ServiceMap(self::BASE_PATH . 'behaviors-unsupported-definition-not-array.php');
124118
}
125119

126120
/**
127121
* @throws ReflectionException if the component definition is invalid or can't be resolved.
128122
*/
129123
public function testThrowExceptionWhenBehaviorIdNotString(): void
130124
{
131-
$ds = DIRECTORY_SEPARATOR;
132-
$configPath = __DIR__ . "{$ds}config{$ds}behaviors-unsupported-id-not-string.php";
133-
134125
$this->expectException(RuntimeException::class);
135126
$this->expectExceptionMessage('\'Behavior class\': \'ID\' must be a \'string\', got \'integer\'.');
136127

137-
new ServiceMap($configPath);
128+
new ServiceMap(self::BASE_PATH . 'behaviors-unsupported-id-not-string.php');
138129
}
139130

140131
/**
141132
* @throws ReflectionException if the component definition is invalid or can't be resolved.
142133
*/
143134
public function testThrowExceptionWhenBehaviorsNotArray(): void
144135
{
145-
$ds = DIRECTORY_SEPARATOR;
146-
$configPath = __DIR__ . "{$ds}config{$ds}behaviors-unsupported-is-not-array.php";
136+
$configPath = self::BASE_PATH . 'behaviors-unsupported-is-not-array.php';
147137

148138
$this->expectException(RuntimeException::class);
149139
$this->expectExceptionMessage("Configuration file '{$configPath}' must contain a valid 'behaviors' 'array'.");

0 commit comments

Comments
 (0)