From 0002aa43428518e4fce3933d7ae1ef14f736ac17 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Thu, 30 Aug 2018 20:25:25 +0300 Subject: [PATCH 1/4] [client] Add typehints to producer interface, its implementations --- bin/pre-commit | 42 +++++++------------ pkg/enqueue/Client/Producer.php | 15 +++---- pkg/enqueue/Client/ProducerInterface.php | 14 +++---- pkg/enqueue/Client/SpoolProducer.php | 25 +++--------- pkg/enqueue/Client/TraceableProducer.php | 52 +++++------------------- 5 files changed, 41 insertions(+), 107 deletions(-) diff --git a/bin/pre-commit b/bin/pre-commit index 7c3936ca3..5706477a6 100755 --- a/bin/pre-commit +++ b/bin/pre-commit @@ -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() @@ -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); } diff --git a/pkg/enqueue/Client/Producer.php b/pkg/enqueue/Client/Producer.php index 2e74df0ec..7a4d18f9e 100644 --- a/pkg/enqueue/Client/Producer.php +++ b/pkg/enqueue/Client/Producer.php @@ -3,6 +3,7 @@ namespace Enqueue\Client; use Enqueue\Client\Extension\PrepareBodyExtension; +use Enqueue\Rpc\Promise; use Enqueue\Rpc\RpcFactory; use Enqueue\Util\UUID; @@ -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); @@ -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); @@ -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( diff --git a/pkg/enqueue/Client/ProducerInterface.php b/pkg/enqueue/Client/ProducerInterface.php index 2fc829eb2..f4207b39f 100644 --- a/pkg/enqueue/Client/ProducerInterface.php +++ b/pkg/enqueue/Client/ProducerInterface.php @@ -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; } diff --git a/pkg/enqueue/Client/SpoolProducer.php b/pkg/enqueue/Client/SpoolProducer.php index c5388770f..1bee97ba1 100644 --- a/pkg/enqueue/Client/SpoolProducer.php +++ b/pkg/enqueue/Client/SpoolProducer.php @@ -2,6 +2,8 @@ namespace Enqueue\Client; +use Enqueue\Rpc\Promise; + class SpoolProducer implements ProducerInterface { /** @@ -19,9 +21,6 @@ class SpoolProducer implements ProducerInterface */ private $commands; - /** - * @param ProducerInterface $realProducer - */ public function __construct(ProducerInterface $realProducer) { $this->realProducer = $realProducer; @@ -30,10 +29,7 @@ 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); @@ -42,26 +38,15 @@ public function sendCommand($command, $message, $needReply = false) $this->commands->enqueue([$command, $message]); } - /** - * {@inheritdoc} - */ - public function sendEvent($topic, $message) + public function sendEvent(string $topic, $message): void { $this->events->enqueue([$topic, $message]); } - /** - * {@inheritdoc} - */ - public function send($topic, $message) - { - $this->sendEvent($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(); diff --git a/pkg/enqueue/Client/TraceableProducer.php b/pkg/enqueue/Client/TraceableProducer.php index cd55a91d6..71af53047 100644 --- a/pkg/enqueue/Client/TraceableProducer.php +++ b/pkg/enqueue/Client/TraceableProducer.php @@ -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); @@ -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) { @@ -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) { @@ -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, From 6e3bef6184d66dcc5ac9b65c525c3b825dc27e3e Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Thu, 30 Aug 2018 20:38:38 +0300 Subject: [PATCH 2/4] fix cs --- .php_cs.php | 1 + pkg/amqp-bunny/Tests/AmqpProducerTest.php | 1 - pkg/amqp-ext/Tests/Functional/AmqpConsumptionUseCasesTest.php | 2 +- pkg/async-event-dispatcher/Tests/Functional/UseCasesTest.php | 1 - pkg/enqueue/Client/ProducerInterface.php | 2 +- 5 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.php_cs.php b/.php_cs.php index 7f7a92b99..39bd30f52 100644 --- a/.php_cs.php +++ b/.php_cs.php @@ -20,6 +20,7 @@ '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') ->setFinder( diff --git a/pkg/amqp-bunny/Tests/AmqpProducerTest.php b/pkg/amqp-bunny/Tests/AmqpProducerTest.php index ddb067858..ef08cad0e 100644 --- a/pkg/amqp-bunny/Tests/AmqpProducerTest.php +++ b/pkg/amqp-bunny/Tests/AmqpProducerTest.php @@ -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; diff --git a/pkg/amqp-ext/Tests/Functional/AmqpConsumptionUseCasesTest.php b/pkg/amqp-ext/Tests/Functional/AmqpConsumptionUseCasesTest.php index 54fccb719..905fb6427 100644 --- a/pkg/amqp-ext/Tests/Functional/AmqpConsumptionUseCasesTest.php +++ b/pkg/amqp-ext/Tests/Functional/AmqpConsumptionUseCasesTest.php @@ -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; diff --git a/pkg/async-event-dispatcher/Tests/Functional/UseCasesTest.php b/pkg/async-event-dispatcher/Tests/Functional/UseCasesTest.php index 1c2768d51..a2926b88c 100644 --- a/pkg/async-event-dispatcher/Tests/Functional/UseCasesTest.php +++ b/pkg/async-event-dispatcher/Tests/Functional/UseCasesTest.php @@ -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; diff --git a/pkg/enqueue/Client/ProducerInterface.php b/pkg/enqueue/Client/ProducerInterface.php index f4207b39f..1c7b056cf 100644 --- a/pkg/enqueue/Client/ProducerInterface.php +++ b/pkg/enqueue/Client/ProducerInterface.php @@ -15,7 +15,7 @@ public function sendEvent(string $topic, $message): void; /** * 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 + * The promise is returned if needReply argument is true. * * @param string|array|Message $message */ From 5ea24ce1780238728301219f2ffb11c61413cd0e Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Thu, 30 Aug 2018 20:56:31 +0300 Subject: [PATCH 3/4] fix php-cs-fixer on travis --- .php_cs.php | 2 +- .travis.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.php_cs.php b/.php_cs.php index 39bd30f52..e28581304 100644 --- a/.php_cs.php +++ b/.php_cs.php @@ -22,7 +22,7 @@ '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$/') diff --git a/.travis.yml b/.travis.yml index 7101e8329..8eddd23c3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 From 91eaeec9050b6e516b2b6512d2a15f1fbc5f213c Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Thu, 30 Aug 2018 21:01:10 +0300 Subject: [PATCH 4/4] fix tests --- pkg/enqueue/Client/SpoolProducer.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/enqueue/Client/SpoolProducer.php b/pkg/enqueue/Client/SpoolProducer.php index 1bee97ba1..8ad0940f5 100644 --- a/pkg/enqueue/Client/SpoolProducer.php +++ b/pkg/enqueue/Client/SpoolProducer.php @@ -36,6 +36,8 @@ public function sendCommand(string $command, $message, bool $needReply = false): } $this->commands->enqueue([$command, $message]); + + return null; } public function sendEvent(string $topic, $message): void