Skip to content

Commit 147da3a

Browse files
committed
docs: Update README.md to enhance clarity and structure of docs/installation.md, docs/configuration.md and docs/examples.md.
1 parent 58ea6fb commit 147da3a

File tree

5 files changed

+1480
-158
lines changed

5 files changed

+1480
-158
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Enh #43: Add tests for session property availability in `Console` and `Web` `Applications` (@terabytesoftw)
1414
- Bug #44: Move `UserPropertiesClassReflectionExtension` to `property` directory and add testing (@terabytesoftw)
1515
- Bug #45: Improve `ServiceMap` configuration for application types (`Base`, `Console`, `Web`) (@terabytesoftw)
16+
- Bug #46: Update `README.md` to enhance clarity and structure of `docs/installation.md`, `docs/configuration.md` and `docs/examples.md` (@terabytesoftw)
1617

1718
## 0.2.3 June 09, 2025
1819

README.md

Lines changed: 83 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -26,208 +26,133 @@
2626
</a>
2727
</p>
2828

29-
## Installation
30-
31-
The preferred way to install this extension is through [composer](https://getcomposer.org/download/).
29+
A comprehensive PHPStan extension that provides enhanced static analysis for Yii2 applications with precise type
30+
inference, dynamic method resolution, and comprehensive property reflection.
3231

33-
Either run
32+
## Installation
3433

35-
```shell
36-
composer require --dev --prefer-dist yii2-extensions/phpstan:^0.2
34+
```bash
35+
composer require --dev yii2-extensions/phpstan
3736
```
3837

39-
or add
38+
## Features
4039

41-
```json
42-
"yii2-extensions/phpstan": "^0.2"
43-
```
40+
**ActiveRecord & ActiveQuery Analysis**
41+
- Array/object result type inference based on `asArray()` usage.
42+
- Dynamic return type inference for `find()`, `findOne()`, `findAll()` methods.
43+
- Generic type support for `ActiveQuery<Model>` with proper chaining.
44+
- Relation methods (`hasOne()`, `hasMany()`) with accurate return types.
4445

45-
## Usage
46+
**Application Component Resolution**
47+
- Automatic type inference for `Yii::$app->component` access.
48+
- Behavior property and method reflection.
49+
- Support for custom component configurations.
50+
- User component with `identity`, `id`, `isGuest` property types.
4651

47-
This extension provides enhanced static analysis for `Yii2` applications by adding:
52+
**Dependency Injection Container**
53+
- Service map integration for custom services.
54+
- Support for closures, singletons, and nested definitions.
55+
- Type-safe `Container::get()` method resolution.
4856

49-
- **Container service resolution** with proper type inference.
50-
- **Dynamic method return types** for `ActiveRecord` and `ActiveQuery`.
51-
- **Header collection dynamic methods** support.
52-
- **Property reflection extensions** for `Application`, `Request`, `Response`, and `User` components.
53-
- **Service map integration** for dependency injection analysis.
57+
**Framework Integration**
58+
- Header collection dynamic method types.
59+
- Stub files for different application types (web, console, base).
60+
- Support for Yii2 constants (`YII_DEBUG`, `YII_ENV_*`).
5461

55-
### Basic Configuration
62+
## Quick Start
5663

57-
To use this extension, you need to add the following configuration to your `phpstan.neon` file:
64+
Create a `phpstan.neon` file in your project root.
5865

5966
```neon
6067
includes:
6168
- vendor/yii2-extensions/phpstan/extension.neon
6269
6370
parameters:
64-
bootstrapFiles:
65-
- tests/bootstrap.php
66-
6771
level: 5
68-
6972
paths:
7073
- src
71-
72-
# Exclude paths from analysis
73-
excludePaths:
74-
- c3.php
75-
- requirements.php
76-
- config
77-
- tests
78-
- vendor
79-
74+
- controllers
75+
- models
76+
8077
yii2:
81-
# Path to your `Yii2` configuration file (optional)
82-
# If not provided or empty, will work without explicit configuration
83-
config_path: %currentWorkingDirectory%/config/test.php
78+
config_path: config/test.php
8479
```
8580

86-
### Dynamic Constants Configuration
87-
88-
The extension automatically recognizes common `Yii2` dynamic constants:
81+
Create a PHPStan-specific config file (`config/test.php`).
8982

90-
- `YII_DEBUG`
91-
- `YII_ENV`
92-
- `YII_ENV_DEV`
93-
- `YII_ENV_PROD`
94-
- `YII_ENV_TEST`
95-
96-
If you need to add additional dynamic constants, you can extend the configuration:
97-
98-
```neon
99-
includes:
100-
- vendor/yii2-extensions/phpstan/extension.neon
83+
```php
84+
<?php
85+
return [
86+
'components' => [
87+
'db' => [
88+
'class' => yii\db\Connection::class,
89+
'dsn' => 'sqlite::memory:',
90+
],
91+
'user' => [
92+
'class' => yii\web\User::class,
93+
'identityClass' => app\models\User::class,
94+
],
95+
// Add your custom components here
96+
],
97+
];
98+
```
10199

102-
parameters:
103-
# Your existing dynamic constants will be merged with the extension's defaults
104-
dynamicConstantNames:
105-
- YII_DEBUG # Already included by the extension
106-
- YII_ENV # Already included by the extension
107-
- YII_ENV_DEV # Already included by the extension
108-
- YII_ENV_PROD # Already included by the extension
109-
- YII_ENV_TEST # Already included by the extension
110-
- MY_CUSTOM_CONSTANT
111-
- ANOTHER_CONSTANT
100+
Run `PHPStan`.
112101

113-
yii2:
114-
config_path: %currentWorkingDirectory%/config/test.php
102+
```bash
103+
vendor/bin/phpstan analyse
115104
```
116105

117-
**Note:** When you define `dynamicConstantNames` in your configuration, it **replaces** the extension's default
118-
constants.
119-
To maintain the `Yii2` constants recognition, you must include them explicitly along with your custom constants, as
120-
shown above.
106+
## Type Inference Examples
121107

122-
### Strict Configuration
108+
### ActiveRecord
123109

124-
```neon
125-
includes:
126-
- phar://phpstan.phar/conf/bleedingEdge.neon
127-
- vendor/phpstan/phpstan-strict-rules/rules.neon
128-
- vendor/yii2-extensions/phpstan/extension.neon
110+
```php
111+
// ✅ Properly typed as User|null
112+
$user = User::findOne(1);
129113

130-
parameters:
131-
bootstrapFiles:
132-
- tests/bootstrap.php
133-
134-
# Complete dynamic constants list (extension defaults + custom)
135-
dynamicConstantNames:
136-
- YII_DEBUG
137-
- YII_ENV
138-
- YII_ENV_DEV
139-
- YII_ENV_PROD
140-
- YII_ENV_TEST
141-
- APP_VERSION
142-
- MAINTENANCE_MODE
143-
144-
level: 8
145-
146-
paths:
147-
- src
148-
- controllers
149-
- models
150-
- widgets
114+
// ✅ Properly typed as User[]
115+
$users = User::findAll(['status' => 'active']);
151116

152-
excludePaths:
153-
- src/legacy
154-
- tests/_support
155-
- vendor
117+
// ✅ Generic ActiveQuery<User> with method chaining
118+
$query = User::find()->where(['active' => 1])->orderBy('name');
156119

157-
yii2:
158-
config_path: %currentWorkingDirectory%/config/web.php
159-
160-
# Enable strict advanced checks
161-
checkImplicitMixed: true
162-
checkBenevolentUnionTypes: true
163-
checkUninitializedProperties: true
164-
checkMissingCallableSignature: true
165-
checkTooWideReturnTypesInProtectedAndPublicMethods: true
166-
reportAnyTypeWideningInVarTag: true
167-
reportPossiblyNonexistentConstantArrayOffset: true
168-
reportPossiblyNonexistentGeneralArrayOffset: true
120+
// ✅ Array results properly typed as array{id: int, name: string}[]
121+
$userData = User::find()->asArray()->all();
169122
```
170123

171-
### PHPstan extension installer
124+
### Application Components
172125

173-
You can use the `phpstan-extension-installer` to automatically install this extension.
174-
175-
To do this, you need to add the following configuration to your `composer.json` file:
176-
177-
```shell
178-
composer require --dev phpstan/extension-installer
179-
```
180-
181-
or, add the following to your `composer.json`:
182-
183-
```json
184-
{
185-
"require-dev": {
186-
"phpstan/extension-installer": "^1.4"
187-
},
188-
"config": {
189-
"allow-plugins": {
190-
"phpstan/extension-installer": true
191-
}
192-
},
126+
```php
127+
// ✅ Properly typed based on your configuration
128+
$mailer = Yii::$app->mailer; // MailerInterface
129+
$db = Yii::$app->db; // Connection
130+
$user = Yii::$app->user; // User
131+
132+
// ✅ User identity with proper type inference
133+
if (Yii::$app->user->isGuest === false) {
134+
$userId = Yii::$app->user->id; // int|string|null
135+
$identity = Yii::$app->user->identity; // YourUserClass
193136
}
194137
```
195138

196-
### Config `yii2` application for PHPStan
197-
198-
To configure the `yii2` application, you can use the `yii2` section in your `phpstan.neon` file:
199-
200-
```neon
201-
parameters:
202-
yii2:
203-
# Path to your `Yii2` configuration file
204-
config_path: %currentWorkingDirectory%/config/test.php
205-
```
206-
207-
`config/test.php` file should return an array with the application configuration, similar to the following:
139+
### Dependency Injection
208140

209141
```php
210-
<?php
211-
212-
declare(strict_types=1);
213-
214-
use yii2\extensions\localeurls\UrlLanguageManager;
142+
$container = new Container();
215143

216-
return [
217-
'components' => [
218-
// custom component
219-
'helper' => [
220-
'class' => \yii2\extensions\helper\Helper::class,
221-
],
222-
// your component extended
223-
'urlManager' => [
224-
'class' => UrlLanguageManager::class,
225-
],
226-
],
227-
];
144+
// ✅ Type-safe service resolution
145+
$service = $container->get(MyService::class); // MyService
146+
$logger = $container->get('logger'); // LoggerInterface (if configured)
228147
```
229148

149+
## Documentation
150+
151+
For detailed configuration options and advanced usage.
230152

153+
- 📚 [Installation Guide](docs/installation.md)
154+
- ⚙️ [Configuration Reference](docs/configuration.md)
155+
- 💡 [Usage Examples](docs/examples.md)
231156

232157
## Quality code
233158

0 commit comments

Comments
 (0)