Skip to content

Commit a812d59

Browse files
[DependencyInjection] Include return type in AppReference shape
1 parent 1edda89 commit a812d59

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

DependencyInjection/Compiler/PhpConfigReferenceDumpPass.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ public static function config(array $config): array
6060

6161
private const WHEN_ENV_APP_TEMPLATE = <<<'PHPDOC'
6262
63-
* "when@{ENV}"?: array{
64-
* imports?: ImportsConfig,
65-
* parameters?: ParametersConfig,
66-
* services?: ServicesConfig,{SHAPE}
67-
* },
63+
* "when@{ENV}"?: array{
64+
* imports?: ImportsConfig,
65+
* parameters?: ParametersConfig,
66+
* services?: ServicesConfig,{SHAPE}
67+
* },
6868
PHPDOC;
6969

7070
private const ROUTES_TYPES_TEMPLATE = <<<'PHPDOC'
@@ -121,23 +121,24 @@ public function process(ContainerBuilder $container): void
121121

122122
$r = new \ReflectionClass(AppReference::class);
123123

124-
if (false === $i = strpos($phpdoc = $r->getDocComment(), "\n */")) {
124+
if (false === $i = strpos($phpdoc = $r->getDocComment(), "\n * @psalm-type ConfigType = ")) {
125125
throw new \LogicException(\sprintf('Cannot insert config shape in "%s".', AppReference::class));
126126
}
127127
$appTypes = substr_replace($phpdoc, $appTypes, $i, 0);
128128

129-
if (false === $i = strpos($phpdoc = $r->getMethod('config')->getDocComment(), "\n * ...<string, ExtensionType|array{")) {
129+
if (false === $i = strrpos($phpdoc = $appTypes, "\n * ...<string, ExtensionType|array{")) {
130130
throw new \LogicException(\sprintf('Cannot insert config shape in "%s".', AppReference::class));
131131
}
132-
$appParam = substr_replace($phpdoc, $this->getShapeForExtensions($anyEnvExtensions, $container), $i, 0);
133-
$i += \strlen($appParam) - \strlen($phpdoc);
132+
$appTypes = substr_replace($phpdoc, $this->getShapeForExtensions($anyEnvExtensions, $container), $i, 0);
133+
$i += \strlen($appTypes) - \strlen($phpdoc);
134134

135135
foreach ($extensionsPerEnv as $env => $extensions) {
136-
$appParam = substr_replace($appParam, strtr(self::WHEN_ENV_APP_TEMPLATE, [
136+
$appTypes = substr_replace($appTypes, strtr(self::WHEN_ENV_APP_TEMPLATE, [
137137
'{ENV}' => $env,
138138
'{SHAPE}' => $this->getShapeForExtensions($extensions, $container, ' '),
139139
]), $i, 0);
140140
}
141+
$appParam = $r->getMethod('config')->getDocComment();
141142

142143
$r = new \ReflectionClass(RoutesReference::class);
143144

@@ -191,7 +192,7 @@ private function getShapeForExtensions(array $extensions, ContainerBuilder $cont
191192
foreach ($extensions as $extension) {
192193
if ($this->getConfiguration($extension, $container)) {
193194
$type = $this->camelCase($extension->getAlias()).'Config';
194-
$shape .= \sprintf("\n * %s%s?: %s,", $indent, $extension->getAlias(), $type);
195+
$shape .= \sprintf("\n * %s%s?: %s,", $indent, $extension->getAlias(), $type);
195196
}
196197
}
197198

Tests/Fixtures/reference.php

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -130,40 +130,43 @@
130130
* count?: int,
131131
* },
132132
* }
133+
* @psalm-type ConfigType = array{
134+
* imports?: ImportsConfig,
135+
* parameters?: ParametersConfig,
136+
* services?: ServicesConfig,
137+
* test?: TestConfig,
138+
* "when@dev"?: array{
139+
* imports?: ImportsConfig,
140+
* parameters?: ParametersConfig,
141+
* services?: ServicesConfig,
142+
* test?: TestConfig,
143+
* },
144+
* "when@prod"?: array{
145+
* imports?: ImportsConfig,
146+
* parameters?: ParametersConfig,
147+
* services?: ServicesConfig,
148+
* test?: TestConfig,
149+
* },
150+
* "when@test"?: array{
151+
* imports?: ImportsConfig,
152+
* parameters?: ParametersConfig,
153+
* services?: ServicesConfig,
154+
* test?: TestConfig,
155+
* },
156+
* ...<string, ExtensionType|array{ // extra keys must follow the when@%env% pattern or match an extension alias
157+
* imports?: ImportsConfig,
158+
* parameters?: ParametersConfig,
159+
* services?: ServicesConfig,
160+
* ...<string, ExtensionType>,
161+
* }>
162+
* }
133163
*/
134164
final class App extends AppReference
135165
{
136166
/**
137-
* @param array{
138-
* imports?: ImportsConfig,
139-
* parameters?: ParametersConfig,
140-
* services?: ServicesConfig,
141-
* test?: TestConfig,
142-
* "when@dev"?: array{
143-
* imports?: ImportsConfig,
144-
* parameters?: ParametersConfig,
145-
* services?: ServicesConfig,
146-
* test?: TestConfig,
147-
* },
148-
* "when@prod"?: array{
149-
* imports?: ImportsConfig,
150-
* parameters?: ParametersConfig,
151-
* services?: ServicesConfig,
152-
* test?: TestConfig,
153-
* },
154-
* "when@test"?: array{
155-
* imports?: ImportsConfig,
156-
* parameters?: ParametersConfig,
157-
* services?: ServicesConfig,
158-
* test?: TestConfig,
159-
* },
160-
* ...<string, ExtensionType|array{ // extra keys must follow the when@%env% pattern or match an extension alias
161-
* imports?: ImportsConfig,
162-
* parameters?: ParametersConfig,
163-
* services?: ServicesConfig,
164-
* ...<string, ExtensionType>,
165-
* }>
166-
* } $config
167+
* @param ConfigType $config
168+
*
169+
* @psalm-return ConfigType
167170
*/
168171
public static function config(array $config): array
169172
{

0 commit comments

Comments
 (0)