@@ -84,35 +84,6 @@ public static function fromPreparedData(string $class, array $constructorArgumen
8484 return new self ($ class , $ constructorArguments , $ methodsAndProperties );
8585 }
8686
87- /**
88- * @psalm-param array<string, mixed> $config
89- *
90- * @psalm-return array<string, MethodOrPropertyItem>
91- */
92- private static function getMethodsAndPropertiesFromConfig (array $ config ): array
93- {
94- $ methodsAndProperties = [];
95-
96- foreach ($ config as $ key => $ value ) {
97- if ($ key === self ::CONSTRUCTOR ) {
98- continue ;
99- }
100-
101- /**
102- * @infection-ignore-all Explode limit does not affect the result.
103- *
104- * @see \Yiisoft\Definitions\Tests\Unit\Helpers\DefinitionValidatorTest::testIncorrectMethodName()
105- */
106- if (count ($ methodArray = explode ('() ' , $ key , 2 )) === 2 ) {
107- $ methodsAndProperties [$ key ] = [self ::TYPE_METHOD , $ methodArray [0 ], $ value ];
108- } elseif (count ($ propertyArray = explode ('$ ' , $ key )) === 2 ) {
109- $ methodsAndProperties [$ key ] = [self ::TYPE_PROPERTY , $ propertyArray [1 ], $ value ];
110- }
111- }
112-
113- return $ methodsAndProperties ;
114- }
115-
11687 /**
11788 * @psalm-return class-string
11889 */
@@ -173,6 +144,65 @@ public function resolve(ContainerInterface $container): object
173144 return $ object ;
174145 }
175146
147+ /**
148+ * Create a new definition that is merged from this definition and another definition.
149+ *
150+ * @param ArrayDefinition $other Definition to merge with.
151+ *
152+ * @return self New definition that is merged from this definition and another definition.
153+ */
154+ public function merge (self $ other ): self
155+ {
156+ $ new = clone $ this ;
157+ $ new ->class = $ other ->class ;
158+ $ new ->constructorArguments = ArrayDefinitionHelper::mergeArguments ($ this ->constructorArguments , $ other ->constructorArguments );
159+
160+ $ methodsAndProperties = $ this ->methodsAndProperties ;
161+ foreach ($ other ->methodsAndProperties as $ key => $ item ) {
162+ if ($ item [0 ] === self ::TYPE_PROPERTY ) {
163+ $ methodsAndProperties [$ key ] = $ item ;
164+ } elseif ($ item [0 ] === self ::TYPE_METHOD ) {
165+ /** @psalm-suppress MixedArgument */
166+ $ arguments = isset ($ methodsAndProperties [$ key ])
167+ ? ArrayDefinitionHelper::mergeArguments ($ methodsAndProperties [$ key ][2 ], $ item [2 ])
168+ : $ item [2 ];
169+ $ methodsAndProperties [$ key ] = [$ item [0 ], $ item [1 ], $ arguments ];
170+ }
171+ }
172+ $ new ->methodsAndProperties = $ methodsAndProperties ;
173+
174+ return $ new ;
175+ }
176+
177+ /**
178+ * @psalm-param array<string, mixed> $config
179+ *
180+ * @psalm-return array<string, MethodOrPropertyItem>
181+ */
182+ private static function getMethodsAndPropertiesFromConfig (array $ config ): array
183+ {
184+ $ methodsAndProperties = [];
185+
186+ foreach ($ config as $ key => $ value ) {
187+ if ($ key === self ::CONSTRUCTOR ) {
188+ continue ;
189+ }
190+
191+ /**
192+ * @infection-ignore-all Explode limit does not affect the result.
193+ *
194+ * @see \Yiisoft\Definitions\Tests\Unit\Helpers\DefinitionValidatorTest::testIncorrectMethodName()
195+ */
196+ if (count ($ methodArray = explode ('() ' , $ key , 2 )) === 2 ) {
197+ $ methodsAndProperties [$ key ] = [self ::TYPE_METHOD , $ methodArray [0 ], $ value ];
198+ } elseif (count ($ propertyArray = explode ('$ ' , $ key )) === 2 ) {
199+ $ methodsAndProperties [$ key ] = [self ::TYPE_PROPERTY , $ propertyArray [1 ], $ value ];
200+ }
201+ }
202+
203+ return $ methodsAndProperties ;
204+ }
205+
176206 /**
177207 * @param array<string,ParameterDefinition> $dependencies
178208 *
@@ -267,34 +297,4 @@ private function isIntegerIndexed(array $arguments): bool
267297
268298 return $ hasIntegerIndex ;
269299 }
270-
271- /**
272- * Create a new definition that is merged from this definition and another definition.
273- *
274- * @param ArrayDefinition $other Definition to merge with.
275- *
276- * @return self New definition that is merged from this definition and another definition.
277- */
278- public function merge (self $ other ): self
279- {
280- $ new = clone $ this ;
281- $ new ->class = $ other ->class ;
282- $ new ->constructorArguments = ArrayDefinitionHelper::mergeArguments ($ this ->constructorArguments , $ other ->constructorArguments );
283-
284- $ methodsAndProperties = $ this ->methodsAndProperties ;
285- foreach ($ other ->methodsAndProperties as $ key => $ item ) {
286- if ($ item [0 ] === self ::TYPE_PROPERTY ) {
287- $ methodsAndProperties [$ key ] = $ item ;
288- } elseif ($ item [0 ] === self ::TYPE_METHOD ) {
289- /** @psalm-suppress MixedArgument */
290- $ arguments = isset ($ methodsAndProperties [$ key ])
291- ? ArrayDefinitionHelper::mergeArguments ($ methodsAndProperties [$ key ][2 ], $ item [2 ])
292- : $ item [2 ];
293- $ methodsAndProperties [$ key ] = [$ item [0 ], $ item [1 ], $ arguments ];
294- }
295- }
296- $ new ->methodsAndProperties = $ methodsAndProperties ;
297-
298- return $ new ;
299- }
300300}
0 commit comments