Skip to content

Commit 2730f1d

Browse files
authored
Merge pull request #521 from php-enqueue/client-producer-add-typehints
[client] Add typehints to producer interface, its implementations
2 parents f28f464 + 3d4f7a4 commit 2730f1d

File tree

10 files changed

+47
-112
lines changed

10 files changed

+47
-112
lines changed

.php_cs.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
'phpdoc_order' => true,
2121
'psr4' => true,
2222
'strict_param' => true,
23+
'native_function_invocation' => false,
2324
))
24-
->setCacheFile(getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__.'/var/.php_cs.cache')
25+
->setCacheFile(getenv('TRAVIS') ? getenv('HOME') . '/php-cs-fixer/.php-cs-fixer' : __DIR__.'/var/.php_cs.cache')
2526
->setFinder(
2627
PhpCsFixer\Finder::create()
2728
->name('/\.php$/')

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ matrix:
3838
cache:
3939
directories:
4040
- $HOME/.composer/cache
41-
- $HOME/.php-cs-fixer
41+
- $HOME/php-cs-fixer
4242

4343
before_install:
4444
- echo "extension = mongodb.so" >> $HOME/.phpenv/versions/$(phpenv version-name)/etc/php.ini

bin/pre-commit

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,17 @@ function runPhpCsFixer()
103103
}
104104

105105
$filesWithErrors = array();
106-
foreach (getFilesToFix() as $file) {
107-
$output = '';
108-
$returnCode = null;
109-
110-
exec(sprintf(
111-
'%s %s fix %s --dry-run --config=.php_cs.php',
112-
$phpBin,
113-
$phpCsFixerBin,
114-
$projectRootDir.'/'.$file
115-
), $output, $returnCode);
116-
117-
if ($returnCode) {
118-
$output = '';
119-
120-
exec(sprintf(
121-
'%s %s fix %s',
122-
$phpBin,
123-
$phpCsFixerBin,
124-
$projectRootDir.'/'.$file
125-
), $output);
106+
$output = '';
107+
$returnCode = null;
126108

127-
$filesWithErrors[] = $file;
128-
}
129-
}
109+
exec(sprintf(
110+
'%s %s fix --config=.php_cs.php --no-interaction -v --path-mode=intersection -- %s',
111+
$phpBin,
112+
$phpCsFixerBin,
113+
implode(' ', getFilesToFix())
114+
), $output, $returnCode);
130115

131-
return $filesWithErrors;
116+
return $returnCode ? $output : false;
132117
}
133118

134119
function runPhpstan()
@@ -162,11 +147,12 @@ $phpCSFixerErrors = runPhpCsFixer();
162147
if ($phpCSFixerErrors) {
163148
echo "Incorrect coding standards were detected and fixed." . PHP_EOL;
164149
echo "Please stash changes and run commit again." . PHP_EOL;
165-
echo "List of changed files:" . PHP_EOL;
150+
echo "Output:" . PHP_EOL . PHP_EOL;
166151

167-
foreach ($phpCSFixerErrors as $error) {
168-
echo $error . PHP_EOL;
169-
}
152+
echo array_walk_recursive($phpStanErrors, function($item) {
153+
echo $item.PHP_EOL;
154+
}) . PHP_EOL;
155+
echo PHP_EOL;
170156

171157
exit(1);
172158
}

pkg/amqp-bunny/Tests/AmqpProducerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Enqueue\AmqpBunny\Tests;
44

55
use Bunny\Channel;
6-
use Bunny\Message;
76
use Enqueue\AmqpBunny\AmqpContext;
87
use Enqueue\AmqpBunny\AmqpProducer;
98
use Enqueue\AmqpTools\DelayStrategy;

pkg/amqp-ext/Tests/Functional/AmqpConsumptionUseCasesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use Enqueue\Consumption\Extension\ReplyExtension;
1010
use Enqueue\Consumption\QueueConsumer;
1111
use Enqueue\Consumption\Result;
12-
use Enqueue\Test\RabbitmqAmqpExtension;
1312
use Enqueue\Test\RabbitManagementExtensionTrait;
13+
use Enqueue\Test\RabbitmqAmqpExtension;
1414
use Interop\Queue\PsrContext;
1515
use Interop\Queue\PsrMessage;
1616
use Interop\Queue\PsrProcessor;

pkg/async-event-dispatcher/Tests/Functional/UseCasesTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Interop\Queue\PsrProcessor;
1313
use Interop\Queue\PsrQueue;
1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\EventDispatcher\Event;
1615
use Symfony\Component\EventDispatcher\EventDispatcher;
1716
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1817
use Symfony\Component\EventDispatcher\GenericEvent;

pkg/enqueue/Client/Producer.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Enqueue\Client;
44

55
use Enqueue\Client\Extension\PrepareBodyExtension;
6+
use Enqueue\Rpc\Promise;
67
use Enqueue\Rpc\RpcFactory;
78
use Enqueue\Util\UUID;
89

@@ -37,7 +38,7 @@ public function __construct(
3738
;
3839
}
3940

40-
public function sendEvent($topic, $message)
41+
public function sendEvent(string $topic, $message): void
4142
{
4243
if (false == $message instanceof Message) {
4344
$message = new Message($message);
@@ -54,7 +55,7 @@ public function sendEvent($topic, $message)
5455
$this->doSend($message);
5556
}
5657

57-
public function sendCommand($command, $message, $needReply = false)
58+
public function sendCommand(string $command, $message, bool $needReply = false): ?Promise
5859
{
5960
if (false == $message instanceof Message) {
6061
$message = new Message($message);
@@ -92,17 +93,11 @@ public function sendCommand($command, $message, $needReply = false)
9293

9394
return $promise;
9495
}
95-
}
9696

97-
/**
98-
* {@inheritdoc}
99-
*/
100-
public function send($topic, $message)
101-
{
102-
$this->sendEvent($topic, $message);
97+
return null;
10398
}
10499

105-
private function doSend(Message $message)
100+
private function doSend(Message $message): void
106101
{
107102
if (false === is_string($message->getBody())) {
108103
throw new \LogicException(sprintf(

pkg/enqueue/Client/ProducerInterface.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
interface ProducerInterface
88
{
99
/**
10-
* @param string $topic
10+
* The message could be pretty much everything as long as you have a client extension that transforms a body to string on onPreSendEvent.
11+
*
1112
* @param string|array|Message $message
1213
*/
13-
public function sendEvent($topic, $message);
14+
public function sendEvent(string $topic, $message): void;
1415

1516
/**
16-
* @param string $command
17-
* @param string|array|Message $message
18-
* @param bool $needReply
17+
* The message could be pretty much everything as long as you have a client extension that transforms a body to string on onPreSendCommand.
18+
* The promise is returned if needReply argument is true.
1919
*
20-
* @return Promise|null the promise is returned if needReply argument is true
20+
* @param string|array|Message $message
2121
*/
22-
public function sendCommand($command, $message, $needReply = false);
22+
public function sendCommand(string $command, $message, bool $needReply = false): ?Promise;
2323
}

pkg/enqueue/Client/SpoolProducer.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Enqueue\Client;
44

5+
use Enqueue\Rpc\Promise;
6+
57
class SpoolProducer implements ProducerInterface
68
{
79
/**
@@ -19,9 +21,6 @@ class SpoolProducer implements ProducerInterface
1921
*/
2022
private $commands;
2123

22-
/**
23-
* @param ProducerInterface $realProducer
24-
*/
2524
public function __construct(ProducerInterface $realProducer)
2625
{
2726
$this->realProducer = $realProducer;
@@ -30,38 +29,26 @@ public function __construct(ProducerInterface $realProducer)
3029
$this->commands = new \SplQueue();
3130
}
3231

33-
/**
34-
* {@inheritdoc}
35-
*/
36-
public function sendCommand($command, $message, $needReply = false)
32+
public function sendCommand(string $command, $message, bool $needReply = false): ?Promise
3733
{
3834
if ($needReply) {
3935
return $this->realProducer->sendCommand($command, $message, $needReply);
4036
}
4137

4238
$this->commands->enqueue([$command, $message]);
43-
}
4439

45-
/**
46-
* {@inheritdoc}
47-
*/
48-
public function sendEvent($topic, $message)
49-
{
50-
$this->events->enqueue([$topic, $message]);
40+
return null;
5141
}
5242

53-
/**
54-
* {@inheritdoc}
55-
*/
56-
public function send($topic, $message)
43+
public function sendEvent(string $topic, $message): void
5744
{
58-
$this->sendEvent($topic, $message);
45+
$this->events->enqueue([$topic, $message]);
5946
}
6047

6148
/**
6249
* When it is called it sends all previously queued messages.
6350
*/
64-
public function flush()
51+
public function flush(): void
6552
{
6653
while (false == $this->events->isEmpty()) {
6754
list($topic, $message) = $this->events->dequeue();

pkg/enqueue/Client/TraceableProducer.php

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,33 @@
22

33
namespace Enqueue\Client;
44

5+
use Enqueue\Rpc\Promise;
6+
57
class TraceableProducer implements ProducerInterface
68
{
79
/**
810
* @var array
911
*/
1012
protected $traces = [];
13+
1114
/**
1215
* @var ProducerInterface
1316
*/
1417
private $producer;
1518

16-
/**
17-
* @param ProducerInterface $producer
18-
*/
1919
public function __construct(ProducerInterface $producer)
2020
{
2121
$this->producer = $producer;
2222
}
2323

24-
/**
25-
* {@inheritdoc}
26-
*/
27-
public function sendEvent($topic, $message)
24+
public function sendEvent(string $topic, $message): void
2825
{
2926
$this->producer->sendEvent($topic, $message);
3027

3128
$this->collectTrace($topic, null, $message);
3229
}
3330

34-
/**
35-
* {@inheritdoc}
36-
*/
37-
public function sendCommand($command, $message, $needReply = false)
31+
public function sendCommand(string $command, $message, bool $needReply = false): ?Promise
3832
{
3933
$result = $this->producer->sendCommand($command, $message, $needReply);
4034

@@ -43,20 +37,7 @@ public function sendCommand($command, $message, $needReply = false)
4337
return $result;
4438
}
4539

46-
/**
47-
* {@inheritdoc}
48-
*/
49-
public function send($topic, $message)
50-
{
51-
$this->sendEvent($topic, $message);
52-
}
53-
54-
/**
55-
* @param string $topic
56-
*
57-
* @return array
58-
*/
59-
public function getTopicTraces($topic)
40+
public function getTopicTraces(string $topic): array
6041
{
6142
$topicTraces = [];
6243
foreach ($this->traces as $trace) {
@@ -68,12 +49,7 @@ public function getTopicTraces($topic)
6849
return $topicTraces;
6950
}
7051

71-
/**
72-
* @param string $command
73-
*
74-
* @return array
75-
*/
76-
public function getCommandTraces($command)
52+
public function getCommandTraces(string $command): array
7753
{
7854
$commandTraces = [];
7955
foreach ($this->traces as $trace) {
@@ -85,25 +61,17 @@ public function getCommandTraces($command)
8561
return $commandTraces;
8662
}
8763

88-
/**
89-
* @return array
90-
*/
91-
public function getTraces()
64+
public function getTraces(): array
9265
{
9366
return $this->traces;
9467
}
9568

96-
public function clearTraces()
69+
public function clearTraces(): void
9770
{
9871
$this->traces = [];
9972
}
10073

101-
/**
102-
* @param string|null $topic
103-
* @param string|null $command
104-
* @param mixed $message
105-
*/
106-
private function collectTrace($topic, $command, $message)
74+
private function collectTrace(string $topic = null, string $command = null, $message): void
10775
{
10876
$trace = [
10977
'topic' => $topic,

0 commit comments

Comments
 (0)