2929use function is_object ;
3030use function WyriHaximus \iteratorOrArrayToArray ;
3131
32+ use const PHP_EOL ;
3233use const WyriHaximus \Constants \Boolean \FALSE_ ;
3334use const WyriHaximus \Constants \Numeric \ZERO ;
3435
@@ -82,12 +83,13 @@ private function create(object $object, string $interface): object
8283 }
8384
8485 if ($ this ->registry ->hasByInterface ($ interface )) {
85- return $ this ->registry ->getByInterface ($ interface )->create ();
86+ $ instance = $ this ->registry ->getByInterface ($ interface );
87+ } else {
88+ $ instance = $ this ->registry ->create ($ object , $ interface );
8689 }
8790
88- $ instance = $ this ->registry ->create ($ object , $ interface );
89-
9091 if ($ this ->counterCreate instanceof Counters) {
92+ echo PHP_EOL , __METHOD__ , PHP_EOL ;
9193 $ this ->counterCreate ->counter (new Label ('class ' , $ instance ->class ()), new Label ('interface ' , $ interface ))->incr ();
9294 }
9395
@@ -143,14 +145,19 @@ private function handleNotify(Notify $notify): void
143145 }
144146
145147 if ($ notify ->link () instanceof Link) {
146- $ instance = $ this ->registry ->getByHash ($ notify ->link ()->rootHash ());
147- $ object = $ this ->followChain ($ notify ->link ());
148+ $ object = $ this ->followChain ($ notify ->link ());
148149
149150 if ($ object === null ) {
150151 return ;
151152 }
152153
153- $ instance = new Instance ($ object , $ instance ->interface (), FALSE_ , $ this ->in );
154+ $ interface = $ this ->getInterfaceForOutcome ($ object );
155+
156+ if ($ interface === null ) {
157+ return ;
158+ }
159+
160+ $ instance = new Instance ($ object , $ interface , FALSE_ , $ this ->in );
154161 } else {
155162 $ instance = $ this ->registry ->getByHash ($ notify ->hash ());
156163 $ instance ->reference ($ notify ->objectHash ());
@@ -171,14 +178,19 @@ private function handleCall(Call $call): void
171178 }
172179
173180 if ($ call ->link () instanceof Link) {
174- $ instance = $ this ->registry ->getByHash ($ call ->link ()->rootHash ());
175- $ object = $ this ->followChain ($ call ->link ());
181+ $ object = $ this ->followChain ($ call ->link ());
176182
177183 if ($ object === null ) {
178184 return ;
179185 }
180186
181- $ instance = new Instance ($ object , $ instance ->interface (), FALSE_ , $ this ->in );
187+ $ interface = $ this ->getInterfaceForOutcome ($ object );
188+
189+ if ($ interface === null ) {
190+ return ;
191+ }
192+
193+ $ instance = new Instance ($ object , $ interface , FALSE_ , $ this ->in );
182194 } else {
183195 $ instance = $ this ->registry ->getByHash ($ call ->hash ());
184196 $ instance ->reference ($ call ->objectHash ());
@@ -269,12 +281,36 @@ private function followChain(Link $link): ?object
269281
270282 /** @phpstan-ignore-next-line */
271283 $ result = $ this ->registry ->getByHash ($ link ->hash ())->object ()->{$ link ->method ()}(...$ link ->args ());
284+ $ this ->countHandledChain ($ result );
272285 /** @phpstan-ignore-next-line */
273286 foreach ($ chain as $ link ) {
274287 /** @phpstan-ignore-next-line */
275288 $ result = $ result ->{$ link ->method ()}(...$ link ->args ());
289+ $ this ->countHandledChain ($ result );
276290 }
277291
278292 return $ result ;
279293 }
294+
295+ private function countHandledChain (object $ object ): void
296+ {
297+ $ interface = $ this ->getInterfaceForOutcome ($ object );
298+ if ($ interface === null ) {
299+ return ;
300+ }
301+
302+ if ($ this ->counterCreate instanceof Counters) {
303+ $ this ->counterCreate ->counter (new Label ('class ' , get_class ($ object )), new Label ('interface ' , $ interface ))->incr ();
304+ }
305+
306+ if ($ this ->counterCall instanceof Counters) {
307+ $ this ->counterCall ->counter (new Label ('class ' , get_class ($ object )), new Label ('interface ' , $ interface ))->incr ();
308+ }
309+
310+ if (! ($ this ->counterDestruct instanceof Counters)) {
311+ return ;
312+ }
313+
314+ $ this ->counterDestruct ->counter (new Label ('class ' , get_class ($ object )), new Label ('interface ' , $ interface ))->incr ();
315+ }
280316}
0 commit comments