Skip to content

Commit

Permalink
Add & run phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Dolbeau committed Dec 21, 2017
1 parent 67a7570 commit 1bb9b85
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 51 deletions.
35 changes: 23 additions & 12 deletions .travis.yml
Expand Up @@ -13,29 +13,40 @@ env:
global:
- LIBRABBITMQ_VERSION=v0.8.0
- PHPAMQP_VERSION=v1.9.3
- MODE="test" # Allowed values: [test, syntax, phpstan]

matrix:
allow_failures:
- php: nightly

include:
# Test with default configuration
- { php: 7.1 }
- { php: 7.2 }
- { php: nightly }
# Test against librabbitmq cutting-edge
- { php: 7.1, env: LIBRABBITMQ_VERSION=master }
- { php: 7.2, env: LIBRABBITMQ_VERSION=master }
- { php: nightly, env: LIBRABBITMQ_VERSION=master }
# Test with lowest dependencies
- { php: 7.1, env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable' }
- { php: nightly, env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable' }
# Test with lowest & beta dependencies
- { php: 7.2, env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable' }
- { php: 7.2, env: DEPENDENCIES=beta }
# Other modes
- { php: 7.2, env: MODE=syntax }
- { php: 7.2, env: MODE=phpstan }

fast_finish: true

install:
- sudo ./tests/bin/install_rabbitmq-c.sh
- ./tests/bin/install_php-amqp.sh
- composer selfupdate
- composer update --prefer-source -n $COMPOSER_FLAGS
- sudo env ./tests/bin/prepare_rabbit.sh
before_install:
- if [ "$DEPENDENCIES" == "beta" ]; then composer config minimum-stability beta; fi;

script: vendor/bin/phpunit
install:
- if [[ "$MODE" != "syntax" ]]; then sudo ./tests/bin/install_rabbitmq-c.sh; fi;
- if [[ "$MODE" != "syntax" ]]; then ./tests/bin/install_php-amqp.sh; fi;
- if [[ "$MODE" == "syntax" ]]; then composer require --dev --no-update friendsofphp/php-cs-fixer; fi;
- if [[ "$MODE" == "phpstan" ]]; then composer require --dev --no-update phpstan/phpstan; fi;
- composer update --prefer-dist --no-progress --no-suggest $COMPOSER_FLAGS --ansi
- if [[ "$MODE" == "test" ]]; then sudo env "PATH=$PATH" ./tests/bin/prepare_rabbit.sh; fi;

script:
- if [[ "$MODE" == "syntax" ]]; then ./vendor/bin/php-cs-fixer fix --dry-run --diff --no-interaction -v; fi;
- if [[ "$MODE" == "phpstan" ]]; then ./vendor/bin/phpstan analyze -c phpstan.neon -l max src/; fi;
- if [[ "$MODE" == "test" ]]; then ./vendor/bin/phpunit; fi;
25 changes: 25 additions & 0 deletions phpstan.neon
@@ -0,0 +1,25 @@
parameters:
excludes_analyse:
- %currentWorkingDirectory%/tests
- %currentWorkingDirectory%/vendor

ignoreErrors:
- '#^Method .* should return bool|null but empty return statement found.#'
- '#^Call to an undefined method Doctrine\\Common\\Persistence\\ObjectManager::isOpen\(\).$#'
- '#^Function newrelic_[a-z_]* not found.#'

checkNullables: false
# Cause:
# ------ ----------------------------------------------------------------------------------------------------------------------------------------------------
# Line src/Swarrot/Consumer.php
# ------ ----------------------------------------------------------------------------------------------------------------------------------------------------
# 80 Parameter #1 $message of method Swarrot\Processor\ProcessorInterface::process() expects Swarrot\Broker\Message, Swarrot\Broker\Message|null given.
# ------ ----------------------------------------------------------------------------------------------------------------------------------------------------

checkMaybeUndefinedVariables: false
# Cause:
# ------ --------------------------------------------------------------
# Line src/Swarrot/Processor/InstantRetry/InstantRetryProcessor.php
# ------ --------------------------------------------------------------
# 48 Variable $e might not be defined.
# ------ --------------------------------------------------------------
4 changes: 2 additions & 2 deletions src/Swarrot/Broker/MessageProvider/InteropMessageProvider.php
Expand Up @@ -31,7 +31,7 @@ final class InteropMessageProvider implements MessageProviderInterface
private $consumedMessages = [];

/**
* @var float|int
* @var int
*/
private $waitTimeout;

Expand All @@ -43,7 +43,7 @@ final class InteropMessageProvider implements MessageProviderInterface
public function __construct(PsrContext $context, $queueName, $waitTimeout = 1000 /* 1sec */)
{
$this->context = $context;
$this->waitTimeout = $waitTimeout;
$this->waitTimeout = (int) $waitTimeout;

$this->queue = $context->createQueue($queueName);
$this->consumer = $context->createConsumer($this->queue);
Expand Down
Expand Up @@ -42,7 +42,7 @@ private function getAckHandler()
return function ($deliveryTag, $multiple) {
//remove acked from pending list
if ($multiple) {
for ($tag = 0; $tag <= $multiple; ++$tag ) {
for ($tag = 0; $tag <= $multiple; ++$tag) {
unset($this->pendingMessages[$tag]);
}
} else {
Expand Down
16 changes: 8 additions & 8 deletions src/Swarrot/Consumer.php
Expand Up @@ -2,14 +2,15 @@

namespace Swarrot;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Swarrot\Broker\MessageProvider\MessageProviderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Swarrot\Processor\ProcessorInterface;
use Swarrot\Processor\ConfigurableInterface;
use Swarrot\Processor\InitializableInterface;
use Swarrot\Processor\TerminableInterface;
use Swarrot\Processor\SleepyInterface;
use Psr\Log\LoggerInterface;

class Consumer
{
Expand Down Expand Up @@ -44,7 +45,7 @@ public function __construct(MessageProviderInterface $messageProvider, Processor
$this->messageProvider = $messageProvider;
$this->processor = $processor;
$this->optionsResolver = $optionsResolver ?: new OptionsResolver();
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/**
Expand All @@ -54,12 +55,11 @@ public function __construct(MessageProviderInterface $messageProvider, Processor
*/
public function consume(array $options = array())
{
if (null !== $this->logger) {
$this->logger->debug(sprintf(
'Start consuming queue %s.',
$this->messageProvider->getQueueName()
));
}
$this->logger->debug(sprintf(
'Start consuming queue %s.',
$this->messageProvider->getQueueName()
));

$this->optionsResolver->setDefaults(array(
'poll_interval' => 50000,
'queue' => $this->messageProvider->getQueueName(),
Expand Down
7 changes: 4 additions & 3 deletions src/Swarrot/Processor/Ack/AckProcessor.php
Expand Up @@ -7,6 +7,7 @@
use Swarrot\Broker\MessageProvider\MessageProviderInterface;
use Swarrot\Broker\Message;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\OptionsResolver\OptionsResolver;

class AckProcessor implements ConfigurableInterface
Expand Down Expand Up @@ -35,7 +36,7 @@ public function __construct(ProcessorInterface $processor, MessageProviderInterf
{
$this->processor = $processor;
$this->messageProvider = $messageProvider;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/**
Expand All @@ -47,7 +48,7 @@ public function process(Message $message, array $options)
$return = $this->processor->process($message, $options);
$this->messageProvider->ack($message);

$this->logger and $this->logger->info(
$this->logger->info(
'[Ack] Message #'.$message->getId().' has been correctly ack\'ed',
[
'swarrot_processor' => 'ack',
Expand Down Expand Up @@ -85,7 +86,7 @@ private function handleException($exception, Message $message, array $options)
$requeue = isset($options['requeue_on_error']) ? (bool) $options['requeue_on_error'] : false;
$this->messageProvider->nack($message, $requeue);

$this->logger and $this->logger->error(
$this->logger->error(
sprintf(
'[Ack] An exception occurred. Message #%d has been %s.',
$message->getId(),
Expand Down
Expand Up @@ -2,9 +2,10 @@

namespace Swarrot\Processor\ExceptionCatcher;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Swarrot\Broker\Message;
use Swarrot\Processor\ProcessorInterface;
use Psr\Log\LoggerInterface;

class ExceptionCatcherProcessor implements ProcessorInterface
{
Expand All @@ -21,7 +22,7 @@ class ExceptionCatcherProcessor implements ProcessorInterface
public function __construct(ProcessorInterface $processor, LoggerInterface $logger = null)
{
$this->processor = $processor;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/**
Expand All @@ -47,7 +48,7 @@ public function process(Message $message, array $options)
*/
private function handleException($exception, Message $message, array $options)
{
$this->logger and $this->logger->error(
$this->logger->error(
'[ExceptionCatcher] An exception occurred. This exception has been caught.',
[
'swarrot_processor' => 'exception',
Expand Down
7 changes: 4 additions & 3 deletions src/Swarrot/Processor/InstantRetry/InstantRetryProcessor.php
Expand Up @@ -2,11 +2,12 @@

namespace Swarrot\Processor\InstantRetry;

use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use Swarrot\Broker\Message;
use Swarrot\Processor\ConfigurableInterface;
use Swarrot\Processor\ProcessorInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class InstantRetryProcessor implements ConfigurableInterface
Expand All @@ -24,7 +25,7 @@ class InstantRetryProcessor implements ConfigurableInterface
public function __construct(ProcessorInterface $processor, LoggerInterface $logger = null)
{
$this->processor = $processor;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/**
Expand Down Expand Up @@ -66,7 +67,7 @@ public function setDefaultOptions(OptionsResolver $resolver)
*/
private function handleException($exception, Message $message, array $options)
{
$this->logger and $this->logException(
$this->logException(
$exception,
sprintf(
'[InstantRetry] An exception occurred. Message #%d will be processed again in %d ms',
Expand Down
Expand Up @@ -5,6 +5,7 @@
use Swarrot\Processor\ProcessorInterface;
use Swarrot\Broker\Message;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Swarrot\Processor\InitializableInterface;
use Swarrot\Processor\ConfigurableInterface;
Expand Down Expand Up @@ -34,7 +35,7 @@ class MaxExecutionTimeProcessor implements ConfigurableInterface, InitializableI
public function __construct(ProcessorInterface $processor, LoggerInterface $logger = null)
{
$this->processor = $processor;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/**
Expand Down Expand Up @@ -90,7 +91,7 @@ public function process(Message $message, array $options)
protected function isTimeExceeded(array $options)
{
if (microtime(true) - $this->startTime > $options['max_execution_time']) {
$this->logger and $this->logger->info(
$this->logger->info(
sprintf(
'[MaxExecutionTime] Max execution time have been reached (%d)',
$options['max_execution_time']
Expand Down
5 changes: 3 additions & 2 deletions src/Swarrot/Processor/MaxMessages/MaxMessagesProcessor.php
Expand Up @@ -6,6 +6,7 @@
use Swarrot\Processor\ConfigurableInterface;
use Swarrot\Broker\Message;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MaxMessagesProcessor implements ConfigurableInterface
Expand All @@ -32,7 +33,7 @@ class MaxMessagesProcessor implements ConfigurableInterface
public function __construct(ProcessorInterface $processor, LoggerInterface $logger = null)
{
$this->processor = $processor;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/**
Expand All @@ -43,7 +44,7 @@ public function process(Message $message, array $options)
$return = $this->processor->process($message, $options);

if (++$this->messagesProcessed >= $options['max_messages']) {
$this->logger and $this->logger->info(
$this->logger->info(
sprintf('[MaxMessages] Max messages have been reached (%d)', $options['max_messages']),
[
'swarrot_processor' => 'max_messages',
Expand Down
5 changes: 3 additions & 2 deletions src/Swarrot/Processor/MemoryLimit/MemoryLimitProcessor.php
Expand Up @@ -3,6 +3,7 @@
namespace Swarrot\Processor\MemoryLimit;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Swarrot\Broker\Message;
use Swarrot\Processor\ConfigurableInterface;
use Swarrot\Processor\ProcessorInterface;
Expand All @@ -27,7 +28,7 @@ class MemoryLimitProcessor implements ConfigurableInterface
public function __construct(ProcessorInterface $processor, LoggerInterface $logger = null)
{
$this->processor = $processor;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/**
Expand All @@ -38,7 +39,7 @@ public function process(Message $message, array $options)
$return = $this->processor->process($message, $options);

if (null !== $options['memory_limit'] && memory_get_usage() >= $options['memory_limit'] * 1024 * 1024) {
$this->logger and $this->logger->info(
$this->logger->info(
sprintf('[MemoryLimit] Memory limit has been reached (%d MB)', $options['memory_limit']),
[
'swarrot_processor' => 'memory_limit',
Expand Down
9 changes: 5 additions & 4 deletions src/Swarrot/Processor/RPC/RpcClientProcessor.php
Expand Up @@ -3,6 +3,7 @@
namespace Swarrot\Processor\RPC;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Swarrot\Broker\Message;
use Swarrot\Processor\ProcessorInterface;
Expand All @@ -26,7 +27,7 @@ class RpcClientProcessor implements ProcessorInterface, ConfigurableInterface, S
/** @var LoggerInterface */
private $logger;

/** @var ProcessorInterface */
/** @var ProcessorInterface|null */
private $processor;

/** @var bool */
Expand All @@ -35,7 +36,7 @@ class RpcClientProcessor implements ProcessorInterface, ConfigurableInterface, S
public function __construct(ProcessorInterface $processor = null, LoggerInterface $logger = null)
{
$this->processor = $processor;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/** {@inheritdoc} */
Expand All @@ -54,11 +55,11 @@ public function process(Message $message, array $options)

$result = null;

$this->logger and $this->logger->info('Message received from the RPC Server ; terminating consumer', ['correlation_id' => $properties['correlation_id']]);
$this->logger->info('Message received from the RPC Server ; terminating consumer', ['correlation_id' => $properties['correlation_id']]);
$this->awoken = true;

if (null !== $this->processor) {
$this->logger and $this->logger->info('Sending message to sub-processor');
$this->logger->info('Sending message to sub-processor');
$result = $this->processor->process($message, $options);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Swarrot/Processor/RPC/RpcServerProcessor.php
Expand Up @@ -3,6 +3,7 @@
namespace Swarrot\Processor\RPC;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Swarrot\Broker\Message;
use Swarrot\Broker\MessagePublisher\MessagePublisherInterface;
use Swarrot\Processor\ProcessorInterface;
Expand All @@ -27,7 +28,7 @@ public function __construct(ProcessorInterface $processor, MessagePublisherInter
{
$this->processor = $processor;
$this->publisher = $publisher;
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
}

/** {@inheritdoc} */
Expand All @@ -41,7 +42,7 @@ public function process(Message $message, array $options)
return $result;
}

$this->logger and $this->logger->info(sprintf('sending a new message to the "%s" queue with the id "%s"', $properties['reply_to'], $properties['correlation_id']), ['swarrot_processor' => 'rpc']);
$this->logger->info(sprintf('sending a new message to the "%s" queue with the id "%s"', $properties['reply_to'], $properties['correlation_id']), ['swarrot_processor' => 'rpc']);

$message = new Message((string) $result, ['correlation_id' => $properties['correlation_id']]);
$this->publisher->publish($message, $properties['reply_to']);
Expand Down

0 comments on commit 1bb9b85

Please sign in to comment.