Skip to content

Commit bd69735

Browse files
author
Jonas Pardon
authored
Merge pull request #16 from JonasPardon/#11-add-support-for-dispatch-sync
2 parents 1b3bbb8 + e680ef1 commit bd69735

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

src/EventVisualizer.php

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,15 @@ private function getDispatchedEvents(CodeParser $codeParser): Collection
155155
$events = [];
156156

157157
$foundFunctionCalls = $codeParser->getFunctionCalls('event');
158-
if (count($foundFunctionCalls) > 0) {
159-
$events = array_merge($events, $foundFunctionCalls);
160-
}
158+
$events = array_merge($events, $foundFunctionCalls);
161159

162160
foreach ($classes as $class) {
163161
foreach ($methods as $method) {
164162
$foundStaticCalls = $codeParser->getStaticCalls($class, $method);
165-
if (count($foundStaticCalls) !== 0) {
166-
$events = array_merge($events, $foundStaticCalls);
167-
}
163+
$events = array_merge($events, $foundStaticCalls);
168164

169165
$foundMethodCalls = $codeParser->getMethodCalls($class, $method);
170-
if (count($foundMethodCalls) !== 0) {
171-
$events = array_merge($events, $foundMethodCalls);
172-
}
166+
$events = array_merge($events, $foundMethodCalls);
173167
}
174168
}
175169

@@ -194,22 +188,19 @@ private function getDispatchedJobs(CodeParser $codeParser): Collection
194188

195189
$jobs = [];
196190

197-
$foundFunctionCalls = $codeParser->getFunctionCalls('dispatch');
198-
if (count($foundFunctionCalls) > 0) {
199-
$jobs = array_merge($jobs, $foundFunctionCalls);
200-
}
191+
$foundFunctionCalls = array_merge(
192+
$codeParser->getFunctionCalls('dispatch'),
193+
$codeParser->getFunctionCalls('dispatch_sync'),
194+
);
195+
$jobs = array_merge($jobs, $foundFunctionCalls);
201196

202197
foreach ($classes as $class) {
203198
foreach ($methods as $method) {
204199
$foundStaticCalls = $codeParser->getStaticCalls($class, $method);
205-
if (count($foundStaticCalls) !== 0) {
206-
$jobs = array_merge($jobs, $foundStaticCalls);
207-
}
200+
$jobs = array_merge($jobs, $foundStaticCalls);
208201

209202
$foundMethodCalls = $codeParser->getMethodCalls($class, $method);
210-
if (count($foundMethodCalls) !== 0) {
211-
$jobs = array_merge($jobs, $foundMethodCalls);
212-
}
203+
$jobs = array_merge($jobs, $foundMethodCalls);
213204
}
214205
}
215206

tests/Unit/CodeParser/FunctionCallsParsingTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,39 @@ public function handle(): void
7777
$this->assertEquals($expectedHelperCall->method, $helperCalls[0]->method);
7878
$this->assertEquals($expectedHelperCall->dispatchedClass, $helperCalls[0]->dispatchedClass);
7979
}
80+
81+
/** @test */
82+
public function it_finds_a_dispatch_sync_helper_call(): void
83+
{
84+
$code = <<<'CODE'
85+
<?php
86+
87+
namespace App\Domain\SomeDomain;
88+
89+
use App\Events\SomeJob;
90+
91+
class SomeClass
92+
{
93+
public function handle(): void
94+
{
95+
dispatch_sync(new SomeJob());
96+
}
97+
}
98+
CODE;
99+
100+
$codeParser = new CodeParser($code);
101+
102+
$helperCalls = $codeParser->getFunctionCalls('dispatch_sync');
103+
104+
$expectedHelperCall = new ResolvedCall(
105+
dispatcherClass: 'none',
106+
dispatchedClass: 'App\Events\SomeJob',
107+
method: 'dispatch_sync',
108+
);
109+
110+
$this->assertCount(1, $helperCalls);
111+
$this->assertEquals($expectedHelperCall->dispatcherClass, $helperCalls[0]->dispatcherClass);
112+
$this->assertEquals($expectedHelperCall->method, $helperCalls[0]->method);
113+
$this->assertEquals($expectedHelperCall->dispatchedClass, $helperCalls[0]->dispatchedClass);
114+
}
80115
}

0 commit comments

Comments
 (0)