Skip to content

Commit

Permalink
refactor(makefile): add makefile with phpstan and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudgirard committed Mar 5, 2024
1 parent dca73da commit dda0a0a
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 47 deletions.
17 changes: 6 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ clean-vendor:
rm -rf ${SOURCE_DIR}/vendor

.PHONY: phpstan
phpstan: composer-install
phpstan: phpstan-cache-clear
$(call printSection,PHPSTAN)
${BIN_DIR}/phpstan analyse -c phpstan.neon --memory-limit=1G
${BIN_DIR}/phpstan.phar analyse --memory-limit=1G

.PHONY: phpstan-cache-clear
phpstan-cache-clear:
${BIN_DIR}/phpstan.phar clear-result-cache

composer-install:
$(call printSection,COMPOSER INSTALL)
Expand All @@ -52,12 +56,3 @@ cs-fix: composer-install
cs-ci: composer-install
$(call printSection,PHPCS)
${BIN_DIR}/php-cs-fixer fix --dry-run --using-cache=no --verbose

.PHONY: phpstan-cache-clear
phpstan-cache-clear:
${BIN_DIR}/phpstan.phar clear-result-cache

.PHONY: phpstan
phpstan: phpstan-cache-clear
$(call printSection,PHPSTAN)
${BIN_DIR}/phpstan.phar analyse --memory-limit=1G
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
"require" : {
"php": ">=8.1",
"ext-amqp": "^2.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
"symfony/framework-bundle": "^6.4 || ^7.0",
"symfony/http-kernel": "^6.4 || ^7.0",
"symfony/yaml": "^6.4 || ^7.0",
"symfony/dependency-injection": "^5.4 || ^6.4 || ^7.0",
"symfony/framework-bundle": "^5.4 || ^6.4 || ^7.0",
"symfony/http-kernel": "^5.4 || ^6.4 || ^7.0",
"symfony/yaml": "^5.4 || 6.4 || ^7.0",
"twig/twig": "^2.13 || ^3.0"
},
"require-dev" : {
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ parameters:
- 'src'
excludePaths:
- 'src/AmqpBundle/Tests'
- 'src/AmqpBundle/Sandbox'
- 'src/AmqpBundle/DependencyInjection'

level: 8
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
treatPhpDocTypesAsCertain: false

ignoreErrors:
- '#Unable to resolve the template type T in call to method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\)#'
8 changes: 5 additions & 3 deletions src/AmqpBundle/Amqp/AbstractAmqp.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ protected function notifyEvent(string $command, array $arguments, mixed $return,
* @param string $name Method name
* @param array $arguments Method arguments
*/
protected function call(object $object, string $name, array $arguments = [])
protected function call(object $object, string $name, array $arguments = []): mixed
{
$start = microtime(true);

$ret = \call_user_func_array([$object, $name], $arguments);
/** @var callable $callable */
$callable = [$object, $name];
$ret = \call_user_func_array($callable, $arguments);

$this->notifyEvent($name, $arguments, $ret, microtime(true) - $start);

Expand All @@ -63,7 +65,7 @@ protected function call(object $object, string $name, array $arguments = [])
* Set an event dispatcher to notify amqp command.
*
* @param EventDispatcherInterface $eventDispatcher The eventDispatcher object, which implement the notify method
* @param string $eventClass The event class used to create an event and send it to the event dispatcher
* @param class-string $eventClass The event class used to create an event and send it to the event dispatcher
*
* @throws \Exception
*/
Expand Down
7 changes: 4 additions & 3 deletions src/AmqpBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('m6_web_amqp');
/** @var ArrayNodeDefinition $rootNode */
$rootNode = $treeBuilder->getRootNode();

$rootNode
Expand Down Expand Up @@ -55,7 +56,7 @@ protected function addConnections(ArrayNodeDefinition $node): void
->arrayNode('connections')
->useAttributeAsKey('key')
->canBeUnset()
->prototype('array')
->arrayPrototype()
->children()
->scalarNode('class')->defaultValue('%m6_web_amqp.connection.class%')->end()
->scalarNode('host')->defaultValue('localhost')->end()
Expand All @@ -82,7 +83,7 @@ protected function addProducers(ArrayNodeDefinition $node): void
->arrayNode('producers')
->canBeUnset()
->useAttributeAsKey('key')
->prototype('array')
->arrayPrototype()
->children()
->scalarNode('class')->defaultValue('%m6_web_amqp.producer.class%')->end()
->scalarNode('connection')->defaultValue('default')->end()
Expand Down Expand Up @@ -125,7 +126,7 @@ protected function addConsumers(ArrayNodeDefinition $node): void
->arrayNode('consumers')
->canBeUnset()
->useAttributeAsKey('key')
->prototype('array')
->arrayPrototype()
->children()
->scalarNode('class')->defaultValue('%m6_web_amqp.consumer.class%')->end()
->scalarNode('connection')->defaultValue('default')->end()
Expand Down
6 changes: 3 additions & 3 deletions src/AmqpBundle/Event/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Command extends Event implements DispatcherInterface
protected string $command;
protected array $arguments;

protected $return;
protected mixed $return;

/**
* {@inheritDoc}
Expand Down Expand Up @@ -58,7 +58,7 @@ public function getArguments(): array
*
* @param mixed $v value
*/
public function setReturn($v): self
public function setReturn(mixed $v): self
{
$this->return = $v;

Expand All @@ -68,7 +68,7 @@ public function setReturn($v): self
/**
* get the return value.
*/
public function getReturn()
public function getReturn(): mixed
{
return $this->return;
}
Expand Down
17 changes: 13 additions & 4 deletions src/AmqpBundle/Event/DispatcherInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,35 @@ interface DispatcherInterface
*
* @param string $command The sqs command
*/
public function setCommand(string $command);
public function setCommand(string $command): self;

/**
* set execution time.
*
* @param float $v temps
*/
public function setExecutionTime(float $v);
public function setExecutionTime(float $v): self;

/**
* set the arguments.
*
* @param array $v argus
*/
public function setArguments(array $v);
public function setArguments(array $v): self;

/**
* set the return value.
*
* @param mixed $v value
*/
public function setReturn(mixed $v);
public function setReturn(mixed $v): self;

public function getCommand(): string;

public function getArguments(): array;

public function getReturn(): mixed;

public function getExecutionTime(): float;

}
8 changes: 7 additions & 1 deletion src/AmqpBundle/Factory/ConsumerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public function get(string $class, \AMQPConnection $connexion, array $exchangeOp
$queue->bind($exchangeOptions['name']);
}

return new $class($queue, $queueOptions);
$consumer = new $class($queue, $queueOptions);

if (!$consumer instanceof Consumer) {
throw new \InvalidArgumentException("$class must be an instance of Consumer");
}

return $consumer;
}
}
2 changes: 2 additions & 0 deletions src/AmqpBundle/Factory/ProducerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function get(string $class, \AMQPConnection $connexion, array $exchangeOp
}

// Open a new channel
/** @var \AMQPChannel $channel */
$channel = new $this->channelClass($connexion);
$exchange = $this->createExchange($this->exchangeClass, $channel, $exchangeOptions);

Expand All @@ -94,6 +95,7 @@ public function get(string $class, \AMQPConnection $connexion, array $exchangeOp
}

// Create the producer
/** @var Producer */
return new $class($exchange, $exchangeOptions);
}
}
4 changes: 0 additions & 4 deletions src/AmqpBundle/Sandbox/NullChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@
*/
class NullChannel extends \AMQPChannel
{
public function __construct(\AMQPConnection $amqp_connection)
{
// noop
}
}
11 changes: 1 addition & 10 deletions src/AmqpBundle/Sandbox/NullExchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@
*/
class NullExchange extends \AMQPExchange
{
/**
* {@inheritdoc}
*/
public function __construct(\AMQPChannel $amqp_channel)
{
// noop
}

/**
* {@inheritdoc}
*/
Expand All @@ -32,8 +24,7 @@ public function publish(
/**
* {@inheritdoc}
*/
public function declareExchange()
public function declareExchange(): void
{
return true;
}
}
2 changes: 1 addition & 1 deletion src/AmqpBundle/Sandbox/NullQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class NullQueue extends \AMQPQueue
/**
* {@inheritdoc}
*/
public function __construct(\AMQPChannel $channel)
public function __construct()
{
$this->envelopes = new \SplQueue();
}
Expand Down
10 changes: 7 additions & 3 deletions src/AmqpBundle/Tests/Units/Amqp/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,13 @@ public function testGetMessageCurrentCount(): void
public function testConsumerWithNullQueue(): void
{
$this
->if($connection = new NullConnection())
->and($channel = new NullChannel($connection))
->and($queue = new NullQueue($channel))
->if($connection = new NullConnection())
->exception(
static fn() => new NullChannel($connection)
);

$this
->if($queue = new NullQueue())
->and($consumer = new Base($queue, []))
->then
->variable($consumer->getMessage())->isNull()
Expand Down

0 comments on commit dda0a0a

Please sign in to comment.