From 407493d491d3c37ebaa845666d718a7c1ced8e04 Mon Sep 17 00:00:00 2001 From: Jonas Pardon Date: Tue, 10 Jan 2023 18:39:03 +0100 Subject: [PATCH 1/3] Add a code parsing test --- .../CodeParser/FunctionCallsParsingTest.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/Unit/CodeParser/FunctionCallsParsingTest.php b/tests/Unit/CodeParser/FunctionCallsParsingTest.php index 6c683e3..83010ed 100644 --- a/tests/Unit/CodeParser/FunctionCallsParsingTest.php +++ b/tests/Unit/CodeParser/FunctionCallsParsingTest.php @@ -77,4 +77,39 @@ public function handle(): void $this->assertEquals($expectedHelperCall->method, $helperCalls[0]->method); $this->assertEquals($expectedHelperCall->dispatchedClass, $helperCalls[0]->dispatchedClass); } + + /** @test */ + public function it_finds_a_dispatch_sync_helper_call(): void + { + $code = <<<'CODE' + getFunctionCalls('dispatch_sync'); + + $expectedHelperCall = new ResolvedCall( + dispatcherClass: 'none', + dispatchedClass: 'App\Events\SomeJob', + method: 'dispatch_sync', + ); + + $this->assertCount(1, $helperCalls); + $this->assertEquals($expectedHelperCall->dispatcherClass, $helperCalls[0]->dispatcherClass); + $this->assertEquals($expectedHelperCall->method, $helperCalls[0]->method); + $this->assertEquals($expectedHelperCall->dispatchedClass, $helperCalls[0]->dispatchedClass); + } } From 7da6ed96bba1264168e0ebe26d74a3379d23012c Mon Sep 17 00:00:00 2001 From: Jonas Pardon Date: Tue, 10 Jan 2023 18:40:57 +0100 Subject: [PATCH 2/3] Look for dispatch_sync in the visualization --- src/EventVisualizer.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/EventVisualizer.php b/src/EventVisualizer.php index 83a31a1..1891f0f 100644 --- a/src/EventVisualizer.php +++ b/src/EventVisualizer.php @@ -194,10 +194,11 @@ private function getDispatchedJobs(CodeParser $codeParser): Collection $jobs = []; - $foundFunctionCalls = $codeParser->getFunctionCalls('dispatch'); - if (count($foundFunctionCalls) > 0) { - $jobs = array_merge($jobs, $foundFunctionCalls); - } + $foundFunctionCalls = array_merge( + $codeParser->getFunctionCalls('dispatch'), + $codeParser->getFunctionCalls('dispatch_sync'), + ); + $jobs = array_merge($jobs, $foundFunctionCalls); foreach ($classes as $class) { foreach ($methods as $method) { From e680ef13915a743009e9cddcac8cac4b9f3dab7c Mon Sep 17 00:00:00 2001 From: Jonas Pardon Date: Tue, 10 Jan 2023 18:42:01 +0100 Subject: [PATCH 3/3] Get rid of useless conditionals --- src/EventVisualizer.php | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/EventVisualizer.php b/src/EventVisualizer.php index 1891f0f..3a81290 100644 --- a/src/EventVisualizer.php +++ b/src/EventVisualizer.php @@ -155,21 +155,15 @@ private function getDispatchedEvents(CodeParser $codeParser): Collection $events = []; $foundFunctionCalls = $codeParser->getFunctionCalls('event'); - if (count($foundFunctionCalls) > 0) { - $events = array_merge($events, $foundFunctionCalls); - } + $events = array_merge($events, $foundFunctionCalls); foreach ($classes as $class) { foreach ($methods as $method) { $foundStaticCalls = $codeParser->getStaticCalls($class, $method); - if (count($foundStaticCalls) !== 0) { - $events = array_merge($events, $foundStaticCalls); - } + $events = array_merge($events, $foundStaticCalls); $foundMethodCalls = $codeParser->getMethodCalls($class, $method); - if (count($foundMethodCalls) !== 0) { - $events = array_merge($events, $foundMethodCalls); - } + $events = array_merge($events, $foundMethodCalls); } } @@ -203,14 +197,10 @@ private function getDispatchedJobs(CodeParser $codeParser): Collection foreach ($classes as $class) { foreach ($methods as $method) { $foundStaticCalls = $codeParser->getStaticCalls($class, $method); - if (count($foundStaticCalls) !== 0) { - $jobs = array_merge($jobs, $foundStaticCalls); - } + $jobs = array_merge($jobs, $foundStaticCalls); $foundMethodCalls = $codeParser->getMethodCalls($class, $method); - if (count($foundMethodCalls) !== 0) { - $jobs = array_merge($jobs, $foundMethodCalls); - } + $jobs = array_merge($jobs, $foundMethodCalls); } }