Skip to content

Commit

Permalink
bump phpstan level up to 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Garifullin committed Dec 7, 2023
1 parent 658a73e commit 8fda6cb
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ vendor/
bin/
.php-cs-fixer.cache
composer.lock
phpstan.neon
77 changes: 77 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
parameters:
ignoreErrors:
-
message: "#^Method M6Web\\\\Bundle\\\\StatsdBundle\\\\Client\\\\Client\\:\\:addEventToListen\\(\\) has parameter \\$eventConfig with no value type specified in iterable type array\\.$#"
count: 1
path: src/Client/Client.php

-
message: "#^Method M6Web\\\\Bundle\\\\StatsdBundle\\\\Client\\\\Client\\:\\:addTiming\\(\\) has parameter \\$tags with no value type specified in iterable type array\\.$#"
count: 1
path: src/Client/Client.php

-
message: "#^Method M6Web\\\\Bundle\\\\StatsdBundle\\\\Client\\\\Client\\:\\:getListenedEvents\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Client/Client.php

-
message: "#^Method M6Web\\\\Bundle\\\\StatsdBundle\\\\Client\\\\Client\\:\\:mergeTags\\(\\) has parameter \\$config with no value type specified in iterable type array\\.$#"
count: 1
path: src/Client/Client.php

-
message: "#^Method M6Web\\\\Bundle\\\\StatsdBundle\\\\Client\\\\Client\\:\\:mergeTags\\(\\) has parameter \\$event with no type specified\\.$#"
count: 1
path: src/Client/Client.php

-
message: "#^Method M6Web\\\\Bundle\\\\StatsdBundle\\\\Client\\\\Client\\:\\:mergeTags\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Client/Client.php

-
message: "#^Property M6Web\\\\Bundle\\\\StatsdBundle\\\\Client\\\\Client\\:\\:\\$listenedEvents type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Client/Client.php

-
message: "#^Method M6Web\\\\Bundle\\\\StatsdBundle\\\\DataCollector\\\\StatsdDataCollector\\:\\:getClients\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/DataCollector/StatsdDataCollector.php

-
message: "#^Property M6Web\\\\Bundle\\\\StatsdBundle\\\\DataCollector\\\\StatsdDataCollector\\:\\:\\$statsdClients type has no value type specified in iterable type array\\.$#"
count: 1
path: src/DataCollector/StatsdDataCollector.php

-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:children\\(\\)\\.$#"
count: 2
path: src/DependencyInjection/Configuration.php

-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeParentInterface\\:\\:booleanNode\\(\\)\\.$#"
count: 1
path: src/DependencyInjection/Configuration.php

-
message: "#^Method M6Web\\\\Bundle\\\\StatsdBundle\\\\DependencyInjection\\\\M6WebStatsdExtension\\:\\:load\\(\\) has parameter \\$configs with no value type specified in iterable type array\\.$#"
count: 1
path: src/DependencyInjection/M6WebStatsdExtension.php

-
message: "#^Method M6Web\\\\Bundle\\\\StatsdBundle\\\\DependencyInjection\\\\M6WebStatsdExtension\\:\\:loadClient\\(\\) has parameter \\$config with no value type specified in iterable type array\\.$#"
count: 1
path: src/DependencyInjection/M6WebStatsdExtension.php

-
message: "#^Method M6Web\\\\Bundle\\\\StatsdBundle\\\\DependencyInjection\\\\M6WebStatsdExtension\\:\\:loadClient\\(\\) has parameter \\$servers with no value type specified in iterable type array\\.$#"
count: 1
path: src/DependencyInjection/M6WebStatsdExtension.php

-
message: "#^Method M6Web\\\\Bundle\\\\StatsdBundle\\\\Event\\\\ConsoleEvent\\:\\:__call\\(\\) has parameter \\$parameters with no value type specified in iterable type array\\.$#"
count: 1
path: src/Event/ConsoleEvent.php

4 changes: 3 additions & 1 deletion phpstan.neon → phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
includes:
- phpstan-baseline.neon
parameters:
level: 5
level: 6
paths:
- src
excludePaths:
Expand Down
8 changes: 5 additions & 3 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getListenedEvents()
* @param string $eventName The event name to listen
* @param array $eventConfig The event handler configuration
*/
public function addEventToListen($eventName, $eventConfig)
public function addEventToListen($eventName, $eventConfig): void
{
$this->listenedEvents[$eventName] = $eventConfig;
}
Expand Down Expand Up @@ -139,6 +139,8 @@ public function handleEvent($event, $name = null)
* @param string $method
*
* @throws Exception
*
* @return mixed
*/
private function getEventValue($event, $method)
{
Expand All @@ -160,7 +162,7 @@ private function getEventValue($event, $method)
*
* @throws Exception
*/
private function addTiming($event, $timingMethod, $node, $tags = [])
private function addTiming($event, $timingMethod, $node, $tags = []): void
{
$timing = $this->getEventValue($event, $timingMethod);
if ($timing > 0) {
Expand Down Expand Up @@ -200,7 +202,7 @@ private function replaceConfigPlaceholder($event, $eventName, $string)
*
* @return array of tags
*/
private function mergeTags($event, $config)
private function mergeTags($event, $config): array
{
$configTags = isset($config['tags']) ? $config['tags'] : [];

Expand Down
19 changes: 12 additions & 7 deletions src/DataCollector/StatsdDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
class StatsdDataCollector extends DataCollector
{
/**
* @var array
*/
private $statsdClients;

/**
Expand All @@ -29,7 +32,7 @@ public function __construct()
/**
* Reset the data collector to initial state
*/
public function reset()
public function reset(): void
{
$this->statsdClients = [];
$this->data = [
Expand All @@ -43,7 +46,7 @@ public function reset()
*
* @param Event $event The received event
*/
public function onKernelResponse($event)
public function onKernelResponse($event): void
{
if ($event instanceof KernelEvent && HttpKernelInterface::MASTER_REQUEST == $event->getRequestType()) {
foreach ($this->statsdClients as $clientName => $client) {
Expand Down Expand Up @@ -76,17 +79,19 @@ public function onKernelResponse($event)
* @param string $clientAlias The client alias
* @param object $statsdClient A statsd client instance
*/
public function addStatsdClient($clientAlias, $statsdClient)
public function addStatsdClient($clientAlias, $statsdClient): void
{
$this->statsdClients[$clientAlias] = $statsdClient;
}

/**
* Collect the data
*
* @param Request $request The request object
* @param Response $response The response object
* @param \Throwable $exception An exception
* @param Request $request The request object
* @param Response $response The response object
* @param \Throwable|null $exception An exception
*
* @return void
*/
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
Expand All @@ -97,7 +102,7 @@ public function collect(Request $request, Response $response, \Throwable $except
*
* @return array operations list
*/
public function getClients()
public function getClients(): array
{
return $this->data['clients'];
}
Expand Down
19 changes: 16 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace M6Web\Bundle\StatsdBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -15,9 +16,13 @@
*/
class Configuration implements ConfigurationInterface
{
/**
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('m6_statsd');
/** @var ArrayNodeDefinition $rootNode */
$rootNode = $treeBuilder->getRootNode();

$this->addServersSection($rootNode);
Expand All @@ -27,7 +32,10 @@ public function getConfigTreeBuilder()
return $treeBuilder;
}

private function addServersSection($rootNode)
/**
* @param ArrayNodeDefinition $rootNode
*/
private function addServersSection($rootNode): void
{
$rootNode
->children()
Expand All @@ -53,7 +61,10 @@ function ($v) {
->end();
}

private function addClientsSection($rootNode)
/**
* @param ArrayNodeDefinition $rootNode
*/
private function addClientsSection($rootNode): void
{
$rootNode
->children()
Expand Down Expand Up @@ -97,8 +108,10 @@ private function addClientsSection($rootNode)

/**
* addDefaultEventSection
*
* @param ArrayNodeDefinition $rootNode
*/
private function addDefaultEventSection($rootNode)
private function addDefaultEventSection($rootNode): void
{
$rootNode
->children()
Expand Down
8 changes: 8 additions & 0 deletions src/DependencyInjection/M6WebStatsdExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
*/
class M6WebStatsdExtension extends Extension
{
/**
* @param array $configs
* @param ContainerBuilder $container
*
* @return void
*
* @throws \Exception
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
Expand Down
2 changes: 2 additions & 0 deletions src/Event/ConsoleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ final public function __construct(BaseConsoleEvent $originalEvent, $startTime =
*
* @param string $name
* @param array $parameters
*
* @return mixed
*/
public function __call($name, $parameters)
{
Expand Down
8 changes: 4 additions & 4 deletions src/Listener/ConsoleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ public function __construct()
/**
* Define event dispatch
*/
public function setEventDispatcher(EventDispatcherInterface $ev)
public function setEventDispatcher(EventDispatcherInterface $ev): void
{
$this->eventDispatcher = $ev;
}

public function onCommand(BaseConsoleEvent $e)
public function onCommand(BaseConsoleEvent $e): void
{
$this->startTime = microtime(true);

$this->dispatch($e, ConsoleEvent::COMMAND);
}

public function onTerminate(ConsoleTerminateEvent $e)
public function onTerminate(ConsoleTerminateEvent $e): void
{
// For non-0 exit command, fire an ERROR event
if ($e->getExitCode() != 0) {
Expand All @@ -56,7 +56,7 @@ public function onTerminate(ConsoleTerminateEvent $e)
$this->dispatch($e, ConsoleEvent::TERMINATE);
}

public function onException(BaseConsoleEvent $e)
public function onException(BaseConsoleEvent $e): void
{
$this->dispatch($e, ConsoleEvent::EXCEPTION);
}
Expand Down
15 changes: 9 additions & 6 deletions src/Statsd/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/
class Listener
{
/** @var Client */
protected $statsdClient;

/** @var EventDispatcherInterface */
protected $eventDispatcher;

/**
Expand All @@ -34,7 +37,7 @@ public function __construct(Client $statsdClient, EventDispatcherInterface $even
/**
* onKernelException
*/
public function onKernelException(ExceptionEvent $event)
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();

Expand All @@ -55,7 +58,7 @@ public function onKernelException(ExceptionEvent $event)
*
* @return void
*/
public function onKernelTerminate(TerminateEvent $event)
public function onKernelTerminate(TerminateEvent $event): void
{
$this->statsdClient->send();
}
Expand All @@ -67,7 +70,7 @@ public function onKernelTerminate(TerminateEvent $event)
*
* @return void
*/
public function onConsoleTerminate(ConsoleTerminateEvent $event)
public function onConsoleTerminate(ConsoleTerminateEvent $event): void
{
$this->statsdClient->send();
}
Expand All @@ -76,7 +79,7 @@ public function onConsoleTerminate(ConsoleTerminateEvent $event)
* method called if base_collectors = true in config to dispatch base events
* (you still have to catch them)
*/
public function dispatchBaseEvents(TerminateEvent $event)
public function dispatchBaseEvents(TerminateEvent $event): void
{
$this->dispatchMemory();
$this->dispatchRequestTime($event);
Expand All @@ -85,7 +88,7 @@ public function dispatchBaseEvents(TerminateEvent $event)
/**
* dispatchMemory dispatch a memory event
*/
private function dispatchMemory()
private function dispatchMemory(): void
{
$memory = memory_get_peak_usage(true);
$memory = ($memory > 1024 ? intval($memory / 1024) : 0);
Expand All @@ -101,7 +104,7 @@ private function dispatchMemory()
* This time is a "fake" one, because some actions are performed before the initialization of the request
* It is ~100ms smaller than the real kernel time.
*/
private function dispatchRequestTime(TerminateEvent $event)
private function dispatchRequestTime(TerminateEvent $event): void
{
$request = $event->getRequest();
$startTime = $request->server->get('REQUEST_TIME_FLOAT', $request->server->get('REQUEST_TIME'));
Expand Down
4 changes: 3 additions & 1 deletion src/Statsd/MonitorableEventInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ interface MonitorableEventInterface
{
/**
* the measured value
*
* @return mixed
*/
public function getValue();

/**
* array of tags [key => value]
*
* @return array
* @return array|mixed
*/
public function getTags();
}
Loading

0 comments on commit 8fda6cb

Please sign in to comment.