Skip to content

Commit bf5eb4a

Browse files
committed
feat: Enhance PHPStan analysis with comprehensive type inference tests for Yii Console Application properties.
1 parent 5cca785 commit bf5eb4a

File tree

3 files changed

+187
-1
lines changed

3 files changed

+187
-1
lines changed

tests/console/config/config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22

33
declare(strict_types=1);
44

5+
use yii\base\View;
56
use yii\console\Application;
67

78
return [
89
'phpstan' => [
910
'application_type' => Application::class,
1011
],
12+
'components' => [
13+
'view' => [
14+
'class' => View::class,
15+
],
16+
],
1117
];

tests/console/data/property/ApplicationPropertiesClassReflectionType.php

Lines changed: 162 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,175 @@
55
namespace yii2\extensions\phpstan\tests\console\data\property;
66

77
use Yii;
8-
use yii\console\Application;
8+
use yii\base\{Security, View};
9+
use yii\console\{Application, Controller, ErrorHandler, Request, Response};
10+
use yii\db\Connection;
11+
use yii\i18n\{Formatter, I18N};
12+
use yii\log\Dispatcher;
13+
use yii\mail\MailerInterface;
14+
use yii\web\{AssetManager, UrlManager};
915

1016
use function PHPStan\Testing\assertType;
1117

18+
/**
19+
* Data provider for property reflection of Yii Console Application properties in PHPStan analysis.
20+
*
21+
* Validates type inference and return types for core properties and components of {@see Application} via `Yii::$app`,
22+
* ensuring that PHPStan correctly recognizes and infers types for all supported application-level properties as if they
23+
* were natively declared on the application class.
24+
*
25+
* These tests cover scenarios including direct property access, component-based properties, nullable and interface
26+
* typed properties, and type assertions for both native and dynamic attached components, verifying that type assertions
27+
* match the expected return types for each case.
28+
*
29+
* Key features.
30+
* - Coverage for all documented core console application properties and components.
31+
* - Ensures compatibility with PHPStan property reflection for Yii console application context.
32+
* - Type assertion for native, interface, and nullable properties.
33+
* - Validates correct type inference for all supported property types, including interfaces and union types.
34+
*
35+
* @copyright Copyright (C) 2023 Terabytesoftw.
36+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
37+
*/
1238
final class ApplicationPropertiesClassReflectionType
1339
{
1440
public function testReturnApplicationInstanceFromYiiApp(): void
1541
{
1642
assertType(Application::class, Yii::$app);
1743
}
44+
45+
public function testReturnAssetManagerFromComponent(): void
46+
{
47+
assertType(AssetManager::class, Yii::$app->assetManager);
48+
}
49+
50+
public function testReturnAuthManagerFromComponent(): void
51+
{
52+
assertType('yii\rbac\ManagerInterface|null', Yii::$app->authManager);
53+
}
54+
55+
public function testReturnStringFromBasePathProperty(): void
56+
{
57+
assertType('string', Yii::$app->basePath);
58+
}
59+
60+
public function testReturnCacheFromComponent(): void
61+
{
62+
assertType('yii\caching\CacheInterface|null', Yii::$app->cache);
63+
}
64+
65+
public function testReturnStringFromCharsetProperty(): void
66+
{
67+
assertType('string', Yii::$app->charset);
68+
}
69+
70+
public function testReturnControllerFromProperty(): void
71+
{
72+
assertType(Controller::class, Yii::$app->controller);
73+
}
74+
75+
public function testReturnStringFromControllerNamespaceProperty(): void
76+
{
77+
assertType('string', Yii::$app->controllerNamespace);
78+
}
79+
80+
public function testReturnDbFromComponent(): void
81+
{
82+
assertType(Connection::class, Yii::$app->db);
83+
}
84+
85+
public function testReturnStringFromDefaultRouteProperty(): void
86+
{
87+
assertType('string', Yii::$app->defaultRoute);
88+
}
89+
90+
public function testReturnBoolFromEnableCoreCommandsProperty(): void
91+
{
92+
assertType('bool', Yii::$app->enableCoreCommands);
93+
}
94+
95+
public function testReturnErrorHandlerFromComponent(): void
96+
{
97+
assertType(ErrorHandler::class, Yii::$app->errorHandler);
98+
}
99+
100+
public function testReturnFormatterFromComponent(): void
101+
{
102+
assertType(Formatter::class, Yii::$app->formatter);
103+
}
104+
105+
public function testReturnI18nFromComponent(): void
106+
{
107+
assertType(I18N::class, Yii::$app->i18n);
108+
}
109+
110+
public function testReturnStringFromLanguageProperty(): void
111+
{
112+
assertType('string', Yii::$app->language);
113+
}
114+
115+
public function testReturnLogFromComponent(): void
116+
{
117+
assertType(Dispatcher::class, Yii::$app->log);
118+
}
119+
120+
public function testReturnMailerFromComponent(): void
121+
{
122+
assertType(MailerInterface::class, Yii::$app->mailer);
123+
}
124+
125+
public function testReturnStringFromNameProperty(): void
126+
{
127+
assertType('string', Yii::$app->name);
128+
}
129+
130+
public function testReturnRequestFromComponent(): void
131+
{
132+
assertType(Request::class, Yii::$app->request);
133+
}
134+
135+
public function testReturnResponseFromComponent(): void
136+
{
137+
assertType(Response::class, Yii::$app->response);
138+
}
139+
140+
public function testReturnStringFromRuntimePathProperty(): void
141+
{
142+
assertType('string', Yii::$app->runtimePath);
143+
}
144+
145+
public function testReturnSecurityFromComponent(): void
146+
{
147+
assertType(Security::class, Yii::$app->security);
148+
}
149+
150+
public function testReturnStringFromSourceLanguageProperty(): void
151+
{
152+
assertType('string', Yii::$app->sourceLanguage);
153+
}
154+
155+
public function testReturnStringFromTimeZoneProperty(): void
156+
{
157+
assertType('string', Yii::$app->timeZone);
158+
}
159+
160+
public function testReturnStringFromUniqueIdProperty(): void
161+
{
162+
assertType('string', Yii::$app->uniqueId);
163+
}
164+
165+
public function testReturnUrlManagerFromComponent(): void
166+
{
167+
assertType(UrlManager::class, Yii::$app->urlManager);
168+
}
169+
170+
public function testReturnStringFromVendorPathProperty(): void
171+
{
172+
assertType('string', Yii::$app->vendorPath);
173+
}
174+
175+
public function testReturnViewFromComponent(): void
176+
{
177+
assertType(View::class, Yii::$app->view);
178+
}
18179
}

tests/console/property/ApplicationPropertiesClassReflectionExtensionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77
use PHPStan\Testing\TypeInferenceTestCase;
88
use PHPUnit\Framework\Attributes\DataProvider;
99

10+
/**
11+
* Test suite for type inference of property reflection in Yii Console Application for PHPStan analysis.
12+
*
13+
* Validates that PHPStan correctly infers types for properties provided by the Yii Console Application, using
14+
* fixture-based assertions for direct property access, parameterized properties, and shared property resolution.
15+
*
16+
* The test class loads type assertions from a fixture file and delegates checks to the parent
17+
* {@see TypeInferenceTestCase}, ensuring that extension logic for {@see ApplicationPropertiesClassReflectionExtension}
18+
* is robust and consistent with expected behavior in the console context.
19+
*
20+
* Key features.
21+
* - Ensures compatibility with PHPStan extension configuration for console applications.
22+
* - Loads and executes type assertions from a dedicated fixture file.
23+
* - Uses PHPUnit DataProvider for parameterized test execution.
24+
* - Validates type inference for native and application-provided properties.
25+
*
26+
* @copyright Copyright (C) 2023 Terabytesoftw.
27+
* @license https://opensource.org/license/bsd-3-clause BSD 3-Clause License.
28+
*/
1029
final class ApplicationPropertiesClassReflectionExtensionTest extends TypeInferenceTestCase
1130
{
1231
/**

0 commit comments

Comments
 (0)