Skip to content

refactor: Update PHPStan configuration paths and create new config files for improved structure and clarity. #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Bug #45: Improve `ServiceMap` configuration for application types (`Base`, `Console`, `Web`) (@terabytesoftw)
- Bug #46: Update `README.md` to enhance clarity and structure of `docs/installation.md`, `docs/configuration.md` and `docs/examples.md` (@terabytesoftw)
- Enh #47: Add `ActiveRecordGetAttributeDynamicMethodReturnTypeExtension` to provide precise type inference for `getAttribute()` method calls based on PHPDoc annotations (@terabytesoftw)
- Bug #48: Update `PHPStan` configuration paths and create new config files for improved structure and clarity (@terabytesoftw)

## 0.2.3 June 09, 2025

Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,19 @@ includes:

parameters:
level: 5

paths:
- src
- controllers
- models

tmpDir: %currentWorkingDirectory%/tests/runtime

yii2:
config_path: config/test.php
config_path: config/phpstan-config.php
```

Create a PHPStan-specific config file (`config/test.php`).
Create a PHPStan-specific config file (`config/phpstan-config.php`).

```php
<?php
Expand Down
71 changes: 41 additions & 30 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ includes:

parameters:
level: 5

paths:
- src

tmpDir: %currentWorkingDirectory%/tests/runtime

yii2:
config_path: config/phpstan.php
config_path: config/phpstan-config.php
```

### Standard Web Application
Expand All @@ -31,21 +34,24 @@ parameters:
bootstrapFiles:
- tests/bootstrap.php

excludePaths:
- config/
- runtime/
- vendor/
- web/assets/

level: 6

paths:
- controllers
- models
- widgets
- components

excludePaths:
- config/
- runtime/
- vendor/
- web/assets/
tmpDir: %currentWorkingDirectory%/tests/runtime

yii2:
config_path: config/phpstan.php
config_path: config/phpstan-config.php
```

## Application Type Configuration
Expand All @@ -54,7 +60,7 @@ parameters:

```php
<?php
// config/phpstan.php
// config/phpstan-config.php
return [
'phpstan' => [
'application_type' => \yii\web\Application::class,
Expand All @@ -69,7 +75,7 @@ For console applications, you **must** explicitly specify the application type.

```php
<?php
// config/phpstan-console.php
// config/phpstan-console-config.php
return [
'phpstan' => [
'application_type' => \yii\console\Application::class,
Expand All @@ -95,8 +101,10 @@ parameters:
- commands
- console

tmpDir: %currentWorkingDirectory%/tests/runtime

yii2:
config_path: config/phpstan-console.php
config_path: config/phpstan-console-config.php
```

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

```php
<?php
// config/phpstan.php
// config/phpstan-config.php
return [
'components' => [
// Database
Expand Down Expand Up @@ -279,6 +287,11 @@ includes:
- vendor/yii2-extensions/phpstan/extension.neon

parameters:
excludePaths:
- src/legacy/
- tests/_support/
- vendor/

level: 8

paths:
Expand All @@ -288,13 +301,10 @@ parameters:
- widgets
- components

excludePaths:
- src/legacy/
- tests/_support/
- vendor/
tmpDir: %currentWorkingDirectory%/tests/runtime

yii2:
config_path: config/phpstan.php
config_path: config/phpstan-config.php

# Strict checks
checkImplicitMixed: true
Expand All @@ -315,20 +325,20 @@ parameters:
### Performance Optimization

```neon
parameters:
# Memory management
tmpDir: var/cache/phpstan

parameters:
# Bootstrap optimization
bootstrapFiles:
- vendor/autoload.php
- config/phpstan-bootstrap.php

# Parallel processing
parallel:
jobSize: 20
maximumNumberOfProcesses: 32
minimumNumberOfJobsPerProcess: 2

# Bootstrap optimization
bootstrapFiles:
- vendor/autoload.php
- config/phpstan-bootstrap.php

# Memory management
tmpDir: %currentWorkingDirectory%/tests/runtime
```

Optimized bootstrap file.
Expand Down Expand Up @@ -379,7 +389,8 @@ includes:

parameters:
level: 6
tmpDir: var/cache/phpstan

tmpDir: %currentWorkingDirectory%/tests/runtime
```

### Web Configuration
Expand All @@ -394,9 +405,9 @@ parameters:
- models
- widgets
- web

yii2:
config_path: config/phpstan-web.php
config_path: config/phpstan-config.php
```

### Console Configuration
Expand All @@ -409,9 +420,9 @@ parameters:
paths:
- commands
- console

yii2:
config_path: config/phpstan-console.php
config_path: config/phpstan-console-config.php
```

### Usage
Expand Down
11 changes: 7 additions & 4 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,16 @@ includes:

parameters:
level: 5

paths:
- src
- controllers
- models

tmpDir: %currentWorkingDirectory%/tests/runtime

yii2:
config_path: config/phpstan.php
config_path: config/phpstan-config.php
```

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

### Web Application Configuration

Create `config/phpstan.php`.
Create `config/phpstan-config.php`.

```php
<?php
Expand Down Expand Up @@ -138,7 +141,7 @@ return [

### Console Application Configuration

For console applications, create `config/phpstan-console.php`.
For console applications, create `config/phpstan-console-config.php`.

```php
<?php
Expand All @@ -164,7 +167,7 @@ And update your `phpstan.neon`.
```neon
parameters:
yii2:
config_path: config/phpstan-console.php
config_path: config/phpstan-console-config.php
```

## Verification
Expand Down
8 changes: 5 additions & 3 deletions phpstan-console.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ parameters:
- src
- tests/console

tmpDir: %currentWorkingDirectory%/tests/runtime

yii2:
config_path: tests/config/phpstan-console-config.php

# Enable strict advanced checks
checkImplicitMixed: true
checkBenevolentUnionTypes: true
Expand All @@ -26,6 +31,3 @@ parameters:
reportAnyTypeWideningInVarTag: true
reportPossiblyNonexistentConstantArrayOffset: true
reportPossiblyNonexistentGeneralArrayOffset: true

yii2:
config_path: tests/console/config/config.php
8 changes: 5 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ parameters:
- src
- tests/web

tmpDir: %currentWorkingDirectory%/tests/runtime

yii2:
config_path: tests/config/phpstan-config.php

# Enable strict advanced checks
checkImplicitMixed: true
checkBenevolentUnionTypes: true
Expand All @@ -26,6 +31,3 @@ parameters:
reportAnyTypeWideningInVarTag: true
reportPossiblyNonexistentConstantArrayOffset: true
reportPossiblyNonexistentGeneralArrayOffset: true

yii2:
config_path: tests/config/config.php
34 changes: 12 additions & 22 deletions tests/ServiceMapBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@
*/
final class ServiceMapBehaviorTest extends TestCase
{
/**
* Base path for configuration files used in tests.
*/
private const BASE_PATH = __DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR;

/**
* @throws ReflectionException if the component definition is invalid or can't be resolved.
*/
public function testReturnBehaviorsWhenValidClassIsClassString(): void
{
$ds = DIRECTORY_SEPARATOR;
$configPath = __DIR__ . "{$ds}config{$ds}config.php";
$serviceMap = new ServiceMap($configPath);
$serviceMap = new ServiceMap(self::BASE_PATH . 'phpstan-config.php');

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

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

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

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

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

Expand All @@ -96,9 +95,7 @@ public function testReturnEmptyArrayWhenClassHasNotBehaviors(): void
*/
public function testReturnEmptyArrayWhenNotBehaviorsConfigured(): void
{
$ds = DIRECTORY_SEPARATOR;
$configPath = __DIR__ . "{$ds}config{$ds}config.php";
$serviceMap = new ServiceMap($configPath);
$serviceMap = new ServiceMap(self::BASE_PATH . 'phpstan-config.php');

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

Expand All @@ -114,36 +111,29 @@ public function testReturnEmptyArrayWhenNotBehaviorsConfigured(): void
*/
public function testThrowExceptionWhenBehaviorDefinitionNotArray(): void
{
$ds = DIRECTORY_SEPARATOR;
$configPath = __DIR__ . "{$ds}config{$ds}behaviors-unsupported-definition-not-array.php";

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Behavior definition for \'MyComponent\' must be an array.');

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

/**
* @throws ReflectionException if the component definition is invalid or can't be resolved.
*/
public function testThrowExceptionWhenBehaviorIdNotString(): void
{
$ds = DIRECTORY_SEPARATOR;
$configPath = __DIR__ . "{$ds}config{$ds}behaviors-unsupported-id-not-string.php";

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('\'Behavior class\': \'ID\' must be a \'string\', got \'integer\'.');

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

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

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