Skip to content

Commit a8b97b4

Browse files
pataarGeniJaho
andauthored
Add support for dispatchSync to DispatchToHelperFunctionsRector rule (#355)
* Add support for dispatchSync -> dispatch_sync to DispatchToHelperFunctionsRector rule * add docs * Add support for dispatchSync -> dispatch_sync to DispatchToHelperFunctionsRector rule * add docs * lint --------- Co-authored-by: Geni Jaho <[email protected]>
1 parent e47da85 commit a8b97b4

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

docs/rector_rules_overview.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,12 @@ Use the event or dispatch helpers instead of the static dispatch method.
593593
-ExampleJob::dispatch($email);
594594
+dispatch(new ExampleJob($email));
595595
```
596+
<br>
597+
598+
```diff
599+
-ExampleJob::dispatchSync($email);
600+
+dispatch_sync(new ExampleJob($email));
601+
```
596602

597603
<br>
598604

src/Rector/StaticCall/DispatchToHelperFunctionsRector.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public function getRuleDefinition(): RuleDefinition
4242
'ExampleJob::dispatch($email);',
4343
'dispatch(new ExampleJob($email));'
4444
),
45+
new CodeSample(
46+
'ExampleJob::dispatchSync($email);',
47+
'dispatch_sync(new ExampleJob($email));'
48+
),
4549
],
4650
);
4751
}
@@ -60,7 +64,7 @@ public function refactor(Node $node): ?Node
6064
return null;
6165
}
6266

63-
if (! $this->isName($node->name, 'dispatch')) {
67+
if (! $this->isName($node->name, 'dispatch') && ! $this->isName($node->name, 'dispatchSync')) {
6468
return null;
6569
}
6670

@@ -76,6 +80,10 @@ public function refactor(Node $node): ?Node
7680
}
7781

7882
if ($this->usesBusDispatchable($classReflection)) {
83+
if ($this->isName($node->name, 'dispatchSync')) {
84+
return $this->createDispatchableCall($node, 'dispatch_sync');
85+
}
86+
7987
return $this->createDispatchableCall($node, 'dispatch');
8088
}
8189

tests/Rector/StaticCall/DispatchToHelperFunctionsRector/Fixture/bus_dispatchable.php.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace RectorLaravel\Tests\Rector\StaticCall\DispatchToHelperFunctionsRector\
55
use RectorLaravel\Tests\Rector\StaticCall\DispatchToHelperFunctionsRector\Source\TestJob;
66

77
TestJob::dispatch('param1', 'param2');
8+
TestJob::dispatchSync('param1', 'param2');
89
-----
910
<?php
1011

@@ -13,3 +14,4 @@ namespace RectorLaravel\Tests\Rector\StaticCall\DispatchToHelperFunctionsRector\
1314
use RectorLaravel\Tests\Rector\StaticCall\DispatchToHelperFunctionsRector\Source\TestJob;
1415

1516
dispatch(new \RectorLaravel\Tests\Rector\StaticCall\DispatchToHelperFunctionsRector\Source\TestJob('param1', 'param2'));
17+
dispatch_sync(new \RectorLaravel\Tests\Rector\StaticCall\DispatchToHelperFunctionsRector\Source\TestJob('param1', 'param2'));

0 commit comments

Comments
 (0)