Skip to content

Commit

Permalink
Merge pull request #12 from Yproximite/build/phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
RomulusED69 authored May 12, 2022
2 parents 8961629 + a7f1f4e commit 4052d37
Show file tree
Hide file tree
Showing 41 changed files with 201 additions and 205 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:

- run: php bin/phpspec run -f dot -n

- run: php bin/phpstan

ci:
runs-on: ubuntu-latest
strategy:
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
},
"require-dev": {
"phpspec/phpspec": "^7.2",
"friendsofphp/php-cs-fixer": "^3.8"
"friendsofphp/php-cs-fixer": "^3.8",
"phpstan/phpstan": "^1.6",
"phpstan/phpstan-strict-rules": "^1.2",
"phpstan/phpstan-symfony": "^1.1"
},
"minimum-stability": "beta",
"autoload": {
Expand Down
31 changes: 31 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#2 \\$eventName of method Symfony\\\\Contracts\\\\EventDispatcher\\\\EventDispatcherInterface\\:\\:dispatch\\(\\) expects string\\|null, mixed given\\.$#"
count: 1
path: src/Client/Client.php

-
message: "#^Parameter \\#1 \\$callback of function array_map expects \\(callable\\(mixed\\)\\: mixed\\)\\|null, Closure\\(Yproximite\\\\Bundle\\\\InfluxDbPresetBundle\\\\Point\\\\PointPreset\\)\\: array\\<int, array\\<string, string\\>\\|string\\> given\\.$#"
count: 1
path: src/DataCollector/ClientRequest.php

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

-
message: "#^Parameter \\#1 \\$config of method Yproximite\\\\Bundle\\\\InfluxDbPresetBundle\\\\DependencyInjection\\\\YproximiteInfluxDbPresetExtension\\:\\:registerEvents\\(\\) expects array\\{profiles\\: array\\<string, array\\{presets\\?\\: array\\<string, array\\{measurement\\: string, tags\\: array\\<string, string\\>, fields\\: array\\<string, string\\>\\}\\>\\}\\>\\}, array given\\.$#"
count: 1
path: src/DependencyInjection/YproximiteInfluxDbPresetExtension.php

-
message: "#^Parameter \\#1 \\$config of method Yproximite\\\\Bundle\\\\InfluxDbPresetBundle\\\\DependencyInjection\\\\YproximiteInfluxDbPresetExtension\\:\\:registerExtensions\\(\\) expects array\\{default_profile_name\\: string, extensions\\: array\\<string, array\\{enabled\\: bool, profile_name\\?\\: string, preset_name\\: string\\}\\>\\}, array given\\.$#"
count: 1
path: src/DependencyInjection/YproximiteInfluxDbPresetExtension.php

-
message: "#^Parameter \\#1 \\$config of method Yproximite\\\\Bundle\\\\InfluxDbPresetBundle\\\\DependencyInjection\\\\YproximiteInfluxDbPresetExtension\\:\\:registerProfiles\\(\\) expects array\\{profiles\\: array\\<string, array\\{presets\\: array\\<string, array\\{measurement\\: string, tags\\: array\\<string, string\\>, fields\\: array\\<string, string\\>\\}\\>, connections\\: array\\<string, array\\{protocol\\: string, deferred\\: bool\\}\\>\\}\\>\\}, array given\\.$#"
count: 1
path: src/DependencyInjection/YproximiteInfluxDbPresetExtension.php
13 changes: 13 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-symfony/rules.neon
- phpstan-baseline.neon

parameters:
level: max

paths:
- src

inferPrivatePropertyTypeFromConstructor: true
14 changes: 5 additions & 9 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
use Yproximite\InfluxDbBundle\Events\HttpEvent;
use Yproximite\InfluxDbBundle\Events\UdpEvent;

/**
* Class Client
*/
class Client implements ClientInterface
{
/**
Expand Down Expand Up @@ -53,9 +50,9 @@ public function sendPoint(
string $profileName,
string $presetName,
float $value,
\DateTimeInterface $dateTime = null
) {
if (!$dateTime) {
?\DateTimeInterface $dateTime = null
): void {
if (null === $dateTime) {
$dateTime = new \DateTime();
}

Expand All @@ -77,13 +74,12 @@ private function buildPoint(PointPresetInterface $preset, float $value, \DateTim
$builder
->setPreset($preset)
->setValue($value)
->setDateTime($dateTime)
;
->setDateTime($dateTime);

return $builder->build();
}

private function sendPointUsingConnection(Point $point, ConnectionInterface $connection)
private function sendPointUsingConnection(Point $point, ConnectionInterface $connection): void
{
$class = $this->getEventClassName($connection);
$event = new $class([$point], Database::PRECISION_SECONDS, $connection->getName());
Expand Down
7 changes: 2 additions & 5 deletions src/Client/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@

namespace Yproximite\Bundle\InfluxDbPresetBundle\Client;

/**
* Interface ClientInterface
*/
interface ClientInterface
{
public function sendPoint(
string $profileName,
string $presetName,
float $value,
\DateTimeInterface $dateTime = null
);
?\DateTimeInterface $dateTime = null
): void;
}
3 changes: 0 additions & 3 deletions src/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Yproximite\Bundle\InfluxDbPresetBundle\Connection;

/**
* Class Connection
*/
class Connection implements ConnectionInterface
{
/**
Expand Down
3 changes: 0 additions & 3 deletions src/Connection/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Yproximite\Bundle\InfluxDbPresetBundle\Connection;

/**
* Class ConnectionFactory
*/
class ConnectionFactory implements ConnectionFactoryInterface
{
public function create(): ConnectionInterface
Expand Down
6 changes: 3 additions & 3 deletions src/Connection/ConnectionFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Yproximite\Bundle\InfluxDbPresetBundle\Connection;

/**
* Interface ConnectionFactoryInterface
*/
interface ConnectionFactoryInterface
{
public function create(): ConnectionInterface;

/**
* @param array{ name:string, protocol:string, deferred:bool } $config
*/
public function createFromConfig(array $config): ConnectionInterface;
}
3 changes: 0 additions & 3 deletions src/Connection/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

namespace Yproximite\Bundle\InfluxDbPresetBundle\Connection;

/**
* Interface ConnectionInterface
*/
interface ConnectionInterface
{
public const PROTOCOL_UDP = 'udp';
Expand Down
44 changes: 33 additions & 11 deletions src/DataCollector/ClientRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
use Yproximite\Bundle\InfluxDbPresetBundle\Profile\Profile;
use Yproximite\Bundle\InfluxDbPresetBundle\Profile\ProfileInterface;

/**
* Class ClientRequest
*/
class ClientRequest implements \Serializable
class ClientRequest
{
/**
* @var ProfileInterface
Expand Down Expand Up @@ -68,9 +65,12 @@ public function getDateTime(): \DateTimeInterface
return $this->dateTime;
}

public function serialize()
/**
* @return array<int, int|string|float|\DateTimeInterface|array<mixed>|null>
*/
public function __serialize(): array
{
return serialize([
return [
$this->value,
$this->dateTime,
$this->profile->getName(),
Expand All @@ -81,12 +81,22 @@ public function serialize()
return $this->serializePointPreset($pointPreset);
}, $this->profile->getPointPresets()),
$this->serializePointPreset($this->pointPreset),
]);
];
}

public function unserialize($serialized)
/**
* @param array{
* 0:float,
* 1:\DateTimeInterface,
* 2:string,
* 3:array<string, array{ 0:string, 1:string, 2:bool }>,
* 4:array<string, array{ 0:string, 1:array<string,string>, 2:string, 3:array<string,string> }>,
* 5:array{ 0:string, 1:array<string,string>, 2:string, 3:array<string,string> }
* } $serialized
*/
public function __unserialize(array $serialized): void
{
list($this->value, $this->dateTime, $profileName, $profileConnections, $profilePointPresets, $pointPreset) = unserialize($serialized);
[$this->value, $this->dateTime, $profileName, $profileConnections, $profilePointPresets, $pointPreset] = $serialized;

$this->pointPreset = $this->unserializePointPreset($pointPreset);

Expand All @@ -97,11 +107,14 @@ public function unserialize($serialized)
$this->profile->addConnection($this->unserializeConnection($connection));
}

foreach ($profilePointPresets as $pointPreset) {
$this->profile->addPointPreset($this->unserializePointPreset($pointPreset));
foreach ($profilePointPresets as $profilePointPreset) {
$this->profile->addPointPreset($this->unserializePointPreset($profilePointPreset));
}
}

/**
* @return array<int, string|array<string,string>>
*/
protected function serializePointPreset(PointPresetInterface $pointPreset): array
{
return [
Expand All @@ -112,6 +125,9 @@ protected function serializePointPreset(PointPresetInterface $pointPreset): arra
];
}

/**
* @param array{ 0:string, 1:array<string,string>, 2:string, 3:array<string,string> } $pointPreset
*/
protected function unserializePointPreset(array $pointPreset): PointPresetInterface
{
$newPointPreset = new PointPreset();
Expand All @@ -124,6 +140,9 @@ protected function unserializePointPreset(array $pointPreset): PointPresetInterf
return $newPointPreset;
}

/**
* @return array<int, string|bool>
*/
protected function serializeConnection(ConnectionInterface $connection): array
{
return [
Expand All @@ -133,6 +152,9 @@ protected function serializeConnection(ConnectionInterface $connection): array
];
}

/**
* @param array{ 0:string, 1:string, 2:bool } $connection
*/
protected function unserializeConnection(array $connection): ConnectionInterface
{
$newConnection = new Connection();
Expand Down
8 changes: 4 additions & 4 deletions src/DataCollector/InfluxDbPresetDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public function __construct(ProfilePoolInterface $profilePool)
$this->profilePool = $profilePool;
}

public function onClientRequest(ClientRequestEvent $event)
public function onClientRequest(ClientRequestEvent $event): void
{
$profile = $this->profilePool->getProfileByName($event->getProfileName());
$preset = $profile->getPointPresetByName($event->getPresetName());

$this->requests[] = new ClientRequest($profile, $preset, $event->getValue(), $event->getDateTime());
}

public function collect(Request $request, Response $response, \Throwable $exception = null)
public function collect(Request $request, Response $response, \Throwable $exception = null): void
{
$this->data = [
'requests' => $this->requests,
Expand All @@ -45,7 +45,7 @@ public function collect(Request $request, Response $response, \Throwable $except
/**
* Resets this data collector to its initial state.
*/
public function reset()
public function reset(): void
{
$this->data = [];
}
Expand All @@ -58,7 +58,7 @@ public function getRequests(): array
return $this->data['requests'];
}

public function getName()
public function getName(): string
{
return 'yproximite.influxdb_preset';
}
Expand Down
22 changes: 8 additions & 14 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,25 @@
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* Class Configuration
*/
class Configuration implements ConfigurationInterface
{
/**
* @var array
* @var array<int, string>
*/
private static $protocols = ['http', 'udp'];

/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('yproximite_influx_db_preset');
$rootNode = method_exists($treeBuilder, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('yproximite_influx_db_preset');
$rootNode = $treeBuilder->getRootNode();

$this->addProfilesSection($rootNode);
$this->addExtensionsSection($rootNode);

return $treeBuilder;
}

private function addProfilesSection(NodeDefinition $rootNode)
private function addProfilesSection(NodeDefinition $rootNode): void
{
$profiles = $rootNode
->children()
Expand All @@ -51,7 +45,7 @@ private function addProfilesSection(NodeDefinition $rootNode)
;
}

private function addConnectionsSection(NodeDefinition $rootNode)
private function addConnectionsSection(NodeDefinition $rootNode): void
{
$rootNode
->children()
Expand All @@ -64,7 +58,7 @@ private function addConnectionsSection(NodeDefinition $rootNode)
->isRequired()
->validate()
->ifTrue(function ($value) {
return !\in_array($value, self::$protocols);
return !\in_array($value, self::$protocols, true);
})
->thenInvalid(
sprintf(
Expand All @@ -82,7 +76,7 @@ private function addConnectionsSection(NodeDefinition $rootNode)
;
}

private function addPresetsSection(NodeDefinition $rootNode)
private function addPresetsSection(NodeDefinition $rootNode): void
{
$rootNode
->children()
Expand All @@ -104,7 +98,7 @@ private function addPresetsSection(NodeDefinition $rootNode)
;
}

private function addExtensionsSection(NodeDefinition $rootNode)
private function addExtensionsSection(NodeDefinition $rootNode): void
{
$rootNode
->children()
Expand Down
Loading

0 comments on commit 4052d37

Please sign in to comment.