Skip to content

[client] Add typehints to producer interface, its implementations #521

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Merged
merged 4 commits into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .php_cs.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
'phpdoc_order' => true,
'psr4' => true,
'strict_param' => true,
'native_function_invocation' => false,
))
->setCacheFile(getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__.'/var/.php_cs.cache')
->setCacheFile(getenv('TRAVIS') ? getenv('HOME') . '/php-cs-fixer/.php-cs-fixer' : __DIR__.'/var/.php_cs.cache')
->setFinder(
PhpCsFixer\Finder::create()
->name('/\.php$/')
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ matrix:
cache:
directories:
- $HOME/.composer/cache
- $HOME/.php-cs-fixer
- $HOME/php-cs-fixer

before_install:
- echo "extension = mongodb.so" >> $HOME/.phpenv/versions/$(phpenv version-name)/etc/php.ini
Expand Down
42 changes: 14 additions & 28 deletions bin/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,17 @@ function runPhpCsFixer()
}

$filesWithErrors = array();
foreach (getFilesToFix() as $file) {
$output = '';
$returnCode = null;

exec(sprintf(
'%s %s fix %s --dry-run --config=.php_cs.php',
$phpBin,
$phpCsFixerBin,
$projectRootDir.'/'.$file
), $output, $returnCode);

if ($returnCode) {
$output = '';

exec(sprintf(
'%s %s fix %s',
$phpBin,
$phpCsFixerBin,
$projectRootDir.'/'.$file
), $output);
$output = '';
$returnCode = null;

$filesWithErrors[] = $file;
}
}
exec(sprintf(
'%s %s fix --config=.php_cs.php --no-interaction -v --path-mode=intersection -- %s',
$phpBin,
$phpCsFixerBin,
implode(' ', getFilesToFix())
), $output, $returnCode);

return $filesWithErrors;
return $returnCode ? $output : false;
}

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

foreach ($phpCSFixerErrors as $error) {
echo $error . PHP_EOL;
}
echo array_walk_recursive($phpStanErrors, function($item) {
echo $item.PHP_EOL;
}) . PHP_EOL;
echo PHP_EOL;

exit(1);
}
Expand Down
1 change: 0 additions & 1 deletion pkg/amqp-bunny/Tests/AmqpProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Enqueue\AmqpBunny\Tests;

use Bunny\Channel;
use Bunny\Message;
use Enqueue\AmqpBunny\AmqpContext;
use Enqueue\AmqpBunny\AmqpProducer;
use Enqueue\AmqpTools\DelayStrategy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Enqueue\Consumption\Extension\ReplyExtension;
use Enqueue\Consumption\QueueConsumer;
use Enqueue\Consumption\Result;
use Enqueue\Test\RabbitmqAmqpExtension;
use Enqueue\Test\RabbitManagementExtensionTrait;
use Enqueue\Test\RabbitmqAmqpExtension;
use Interop\Queue\PsrContext;
use Interop\Queue\PsrMessage;
use Interop\Queue\PsrProcessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Interop\Queue\PsrProcessor;
use Interop\Queue\PsrQueue;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
Expand Down
15 changes: 5 additions & 10 deletions pkg/enqueue/Client/Producer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Enqueue\Client;

use Enqueue\Client\Extension\PrepareBodyExtension;
use Enqueue\Rpc\Promise;
use Enqueue\Rpc\RpcFactory;
use Enqueue\Util\UUID;

Expand Down Expand Up @@ -37,7 +38,7 @@ public function __construct(
;
}

public function sendEvent($topic, $message)
public function sendEvent(string $topic, $message): void
{
if (false == $message instanceof Message) {
$message = new Message($message);
Expand All @@ -54,7 +55,7 @@ public function sendEvent($topic, $message)
$this->doSend($message);
}

public function sendCommand($command, $message, $needReply = false)
public function sendCommand(string $command, $message, bool $needReply = false): ?Promise
{
if (false == $message instanceof Message) {
$message = new Message($message);
Expand Down Expand Up @@ -92,17 +93,11 @@ public function sendCommand($command, $message, $needReply = false)

return $promise;
}
}

/**
* {@inheritdoc}
*/
public function send($topic, $message)
{
$this->sendEvent($topic, $message);
return null;
}

private function doSend(Message $message)
private function doSend(Message $message): void
{
if (false === is_string($message->getBody())) {
throw new \LogicException(sprintf(
Expand Down
14 changes: 7 additions & 7 deletions pkg/enqueue/Client/ProducerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
interface ProducerInterface
{
/**
* @param string $topic
* The message could be pretty much everything as long as you have a client extension that transforms a body to string on onPreSendEvent.
*
* @param string|array|Message $message
*/
public function sendEvent($topic, $message);
public function sendEvent(string $topic, $message): void;

/**
* @param string $command
* @param string|array|Message $message
* @param bool $needReply
* The message could be pretty much everything as long as you have a client extension that transforms a body to string on onPreSendCommand.
* The promise is returned if needReply argument is true.
*
* @return Promise|null the promise is returned if needReply argument is true
* @param string|array|Message $message
*/
public function sendCommand($command, $message, $needReply = false);
public function sendCommand(string $command, $message, bool $needReply = false): ?Promise;
}
27 changes: 7 additions & 20 deletions pkg/enqueue/Client/SpoolProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Enqueue\Client;

use Enqueue\Rpc\Promise;

class SpoolProducer implements ProducerInterface
{
/**
Expand All @@ -19,9 +21,6 @@ class SpoolProducer implements ProducerInterface
*/
private $commands;

/**
* @param ProducerInterface $realProducer
*/
public function __construct(ProducerInterface $realProducer)
{
$this->realProducer = $realProducer;
Expand All @@ -30,38 +29,26 @@ public function __construct(ProducerInterface $realProducer)
$this->commands = new \SplQueue();
}

/**
* {@inheritdoc}
*/
public function sendCommand($command, $message, $needReply = false)
public function sendCommand(string $command, $message, bool $needReply = false): ?Promise
{
if ($needReply) {
return $this->realProducer->sendCommand($command, $message, $needReply);
}

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

/**
* {@inheritdoc}
*/
public function sendEvent($topic, $message)
{
$this->events->enqueue([$topic, $message]);
return null;
}

/**
* {@inheritdoc}
*/
public function send($topic, $message)
public function sendEvent(string $topic, $message): void
{
$this->sendEvent($topic, $message);
$this->events->enqueue([$topic, $message]);
}

/**
* When it is called it sends all previously queued messages.
*/
public function flush()
public function flush(): void
{
while (false == $this->events->isEmpty()) {
list($topic, $message) = $this->events->dequeue();
Expand Down
52 changes: 10 additions & 42 deletions pkg/enqueue/Client/TraceableProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,33 @@

namespace Enqueue\Client;

use Enqueue\Rpc\Promise;

class TraceableProducer implements ProducerInterface
{
/**
* @var array
*/
protected $traces = [];

/**
* @var ProducerInterface
*/
private $producer;

/**
* @param ProducerInterface $producer
*/
public function __construct(ProducerInterface $producer)
{
$this->producer = $producer;
}

/**
* {@inheritdoc}
*/
public function sendEvent($topic, $message)
public function sendEvent(string $topic, $message): void
{
$this->producer->sendEvent($topic, $message);

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

/**
* {@inheritdoc}
*/
public function sendCommand($command, $message, $needReply = false)
public function sendCommand(string $command, $message, bool $needReply = false): ?Promise
{
$result = $this->producer->sendCommand($command, $message, $needReply);

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

/**
* {@inheritdoc}
*/
public function send($topic, $message)
{
$this->sendEvent($topic, $message);
}

/**
* @param string $topic
*
* @return array
*/
public function getTopicTraces($topic)
public function getTopicTraces(string $topic): array
{
$topicTraces = [];
foreach ($this->traces as $trace) {
Expand All @@ -68,12 +49,7 @@ public function getTopicTraces($topic)
return $topicTraces;
}

/**
* @param string $command
*
* @return array
*/
public function getCommandTraces($command)
public function getCommandTraces(string $command): array
{
$commandTraces = [];
foreach ($this->traces as $trace) {
Expand All @@ -85,25 +61,17 @@ public function getCommandTraces($command)
return $commandTraces;
}

/**
* @return array
*/
public function getTraces()
public function getTraces(): array
{
return $this->traces;
}

public function clearTraces()
public function clearTraces(): void
{
$this->traces = [];
}

/**
* @param string|null $topic
* @param string|null $command
* @param mixed $message
*/
private function collectTrace($topic, $command, $message)
private function collectTrace(string $topic = null, string $command = null, $message): void
{
$trace = [
'topic' => $topic,
Expand Down