1212use yii \base \{BaseObject , InvalidArgumentException };
1313use yii \web \Application ;
1414
15+ use function array_key_exists ;
1516use function define ;
1617use function defined ;
1718use function file_exists ;
5455 * container?: array{
5556 * definitions?: array<array-key, DefinitionType>,
5657 * singletons?: array<array-key, DefinitionType>,
57- * }
58+ * },
59+ * params?: array<string, mixed>,
5860 * }
5961 *
6062 * @copyright Copyright (C) 2023 Terabytesoftw.
@@ -97,6 +99,13 @@ final class ServiceMap
9799 */
98100 private array $ componentsDefinitions = [];
99101
102+ /**
103+ * Application params for PHPStan type inference.
104+ *
105+ * @phpstan-var array<string, mixed>
106+ */
107+ private array $ params = [];
108+
100109 /**
101110 * Service definitions map for Yii Application analysis.
102111 *
@@ -133,6 +142,7 @@ public function __construct(string $configPath = '')
133142 $ this ->processBehaviors ($ config );
134143 $ this ->processComponents ($ config );
135144 $ this ->processDefinition ($ config );
145+ $ this ->processParams ($ config );
136146 $ this ->processSingletons ($ config );
137147 }
138148
@@ -237,6 +247,19 @@ public function getComponentDefinitionById(string $id): array
237247 return is_array ($ definition ) ? $ definition : [];
238248 }
239249
250+ /**
251+ * Retrieves the application params map for PHPStan type inference.
252+ *
253+ * Returns the `params` key-value pairs extracted from the Yii Application configuration file, enabling static
254+ * analysis tools to infer precise array shape types for `Yii::$app->params` access.
255+ *
256+ * @return array<string, mixed> Params key-value pairs from configuration.
257+ */
258+ public function getParams (): array
259+ {
260+ return $ this ->params ;
261+ }
262+
240263 /**
241264 * Retrieves the fully qualified class name of a Yii Service by its identifier.
242265 *
@@ -306,6 +329,10 @@ private function loadConfig(string $configPath): array
306329 $ this ->throwErrorWhenConfigFileIsNotArray ($ configPath , 'components ' );
307330 }
308331
332+ if (array_key_exists ('params ' , $ config ) && is_array ($ config ['params ' ]) === false ) {
333+ $ this ->throwErrorWhenConfigFileIsNotArray ($ configPath , 'params ' );
334+ }
335+
309336 if (isset ($ config ['container ' ])) {
310337 if (is_array ($ config ['container ' ]) === false ) {
311338 $ this ->throwErrorWhenConfigFileIsNotArray ($ configPath , 'container ' );
@@ -517,6 +544,23 @@ private function processDefinition(array $config): void
517544 }
518545 }
519546
547+ /**
548+ * Processes application params from the Yii Application configuration array.
549+ *
550+ * Extracts the `params` section and stores it for type inference of `Yii::$app->params` array access.
551+ *
552+ * @param array $config Yii Application configuration array containing params definitions.
553+ *
554+ * @phpstan-import-type ServiceType from ServiceMap
555+ * @phpstan-param ServiceType $config
556+ */
557+ private function processParams (array $ config ): void
558+ {
559+ if ($ config !== []) {
560+ $ this ->params = $ config ['params ' ] ?? [];
561+ }
562+ }
563+
520564 /**
521565 * Processes singleton service definitions from the Yii Application configuration array.
522566 *
@@ -584,7 +628,9 @@ private function throwErrorWhenConfigFileIsNotArray(string ...$args): never
584628 */
585629 private function throwErrorWhenIsNotString (string ...$ args ): never
586630 {
587- throw new RuntimeException (sprintf ("'%s': '%s' must be a 'string', got '%s'. " , ...$ args ));
631+ throw new RuntimeException (
632+ sprintf ("'%s': '%s' must be a 'string', got '%s'. " , ...$ args ),
633+ );
588634 }
589635
590636 /**
@@ -602,6 +648,8 @@ private function throwErrorWhenIsNotString(string ...$args): never
602648 */
603649 private function throwErrorWhenUnsupportedDefinition (string $ id ): never
604650 {
605- throw new RuntimeException (sprintf ("Unsupported definition for '%s'. " , $ id ));
651+ throw new RuntimeException (
652+ sprintf ("Unsupported definition for '%s'. " , $ id ),
653+ );
606654 }
607655}
0 commit comments