Skip to content

Commit 6f7c906

Browse files
Make $configPath optional in constructor ServiceMap class and update docs README.md. (#22)
1 parent f69c0ff commit 6f7c906

File tree

5 files changed

+138
-18
lines changed

5 files changed

+138
-18
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Change Log
22

3-
## 0.2.2 Under development
3+
## 0.2.2 June 04, 2025
4+
5+
- Bug #22: Make `$configPath` optional in constructor `ServiceMap` class and update docs `README.md` (@terabytesoftw)
46

57
## 0.2.1 June 03, 2025
68

README.md

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The preferred way to install this extension is through [composer](https://getcom
3232

3333
Either run
3434

35-
```shel
35+
```shell
3636
composer require --dev --prefer-dist yii2-extensions/phpstan:^0.2
3737
```
3838

@@ -44,33 +44,119 @@ or add
4444

4545
## Usage
4646

47+
This extension provides enhanced static analysis for `Yii2` applications by adding:
48+
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.
54+
55+
### Basic Configuration
56+
4757
To use this extension, you need to add the following configuration to your `phpstan.neon` file:
4858

4959
```neon
5060
includes:
51-
- vendor/yii2-extensions/phpstan/extension.neon
61+
- vendor/yii2-extensions/phpstan/extension.neon
5262
5363
parameters:
5464
bootstrapFiles:
5565
- tests/bootstrap.php
5666
57-
dynamicConstantNames:
58-
- YII_DEBUG
59-
- YII_ENV
60-
6167
level: 5
6268
6369
paths:
6470
- src
6571
66-
scanFiles:
67-
- vendor/yiisoft/yii2/Yii.php
72+
# Exclude paths from analysis
73+
excludePaths:
74+
- c3.php
75+
- requirements.php
76+
- config
77+
- tests
78+
- vendor
6879
6980
yii2:
70-
# Path to your Yii2 configuration file
81+
# Path to your `Yii2` configuration file (optional)
82+
# If not provided or empty, will work without explicit configuration
7183
config_path: %currentWorkingDirectory%/config/test.php
7284
```
7385

86+
### Dynamic Constants Configuration
87+
88+
The extension automatically recognizes common `Yii2` dynamic constants:
89+
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
101+
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
112+
113+
yii2:
114+
config_path: %currentWorkingDirectory%/config/test.php
115+
```
116+
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.
121+
122+
### Advanced Configuration Example
123+
124+
```neon
125+
includes:
126+
- vendor/yii2-extensions/phpstan/extension.neon
127+
128+
parameters:
129+
level: 8
130+
131+
paths:
132+
- src
133+
- controllers
134+
- models
135+
- widgets
136+
137+
excludePaths:
138+
- src/legacy
139+
- tests/_support
140+
- vendor
141+
142+
bootstrapFiles:
143+
- config/bootstrap.php
144+
- tests/bootstrap.php
145+
146+
# Complete dynamic constants list (extension defaults + custom)
147+
dynamicConstantNames:
148+
- YII_DEBUG
149+
- YII_ENV
150+
- YII_ENV_DEV
151+
- YII_ENV_PROD
152+
- YII_ENV_TEST
153+
- APP_VERSION
154+
- MAINTENANCE_MODE
155+
156+
yii2:
157+
config_path: %currentWorkingDirectory%/config/web.php
158+
```
159+
74160
## Quality code
75161

76162
[![phpstan-level](https://img.shields.io/badge/PHPStan%20level-9-blue)](https://github.com/yii2-extensions/phpstan/actions/workflows/static.yml)

extension.neon

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
parameters:
2+
dynamicConstantNames:
3+
- YII_DEBUG
4+
- YII_ENV
5+
- YII_ENV_DEV
6+
- YII_ENV_PROD
7+
- YII_ENV_TEST
8+
29
yii2:
3-
config_path: null
10+
config_path: ''
11+
412
stubFiles:
513
- stubs/BaseYii.stub
14+
- vendor/yiisoft/yii2/Yii.php
615

716
parametersSchema:
8-
yii2: structure([
9-
config_path: schema(string(), nullable())
10-
])
17+
yii2: structure(
18+
[
19+
config_path: schema(string())
20+
]
21+
)
1122

1223
services:
1324
-

src/ServiceMap.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,16 @@ final class ServiceMap
7070
/**
7171
* Creates a new instance of the {@see ServiceMap} class.
7272
*
73-
* @param string $configPath Path to the Yii application configuration file.
73+
* @param string $configPath Path to the Yii application configuration file (default: `''`). If provided, the
74+
* configuration file must exist and be valid. If empty or not provided, operates with empty service/component maps.
7475
*
7576
* @throws InvalidArgumentException If the provided config path doesn't exist.
7677
* @throws ReflectionException If the service definitions can't be resolved or are invalid.
7778
* @throws RuntimeException If the provided configuration path doesn't exist or is invalid.
7879
*/
79-
public function __construct(string $configPath)
80+
public function __construct(string $configPath = '')
8081
{
81-
if (file_exists($configPath) === false) {
82+
if ($configPath !== '' && file_exists($configPath) === false) {
8283
throw new InvalidArgumentException(sprintf('Provided config path %s must exist', $configPath));
8384
}
8485

@@ -87,7 +88,7 @@ public function __construct(string $configPath)
8788
defined('YII_ENV_PROD') || define('YII_ENV_PROD', false);
8889
defined('YII_ENV_TEST') || define('YII_ENV_TEST', true);
8990

90-
$config = require $configPath;
91+
$config = $configPath !== '' ? require $configPath : [];
9192

9293
foreach ($config['container']['singletons'] ?? [] as $id => $service) {
9394
$this->addServiceDefinition($id, $service);

tests/ServiceMapTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ public function testItLoadsServicesAndComponents(): void
122122
);
123123
}
124124

125+
/**
126+
* @throws ReflectionException if the service definition is invalid or can't be resolved.
127+
*/
128+
public function testItAllowsWithoutEmptyConfigPath(): void
129+
{
130+
$this->expectNotToPerformAssertions();
131+
132+
new ServiceMap();
133+
}
134+
135+
/**
136+
* @throws ReflectionException if the service definition is invalid or can't be resolved.
137+
*/
138+
public function testItAllowsWithoutEmptyConfigPathStringValue(): void
139+
{
140+
$this->expectNotToPerformAssertions();
141+
142+
new ServiceMap('');
143+
}
144+
125145
/**
126146
* @throws ReflectionException if the service definition is invalid or can't be resolved.
127147
*/

0 commit comments

Comments
 (0)