Skip to content

Commit 4052d37

Browse files
authored
Merge pull request #12 from Yproximite/build/phpstan
2 parents 8961629 + a7f1f4e commit 4052d37

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+201
-205
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ jobs:
4141

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

44+
- run: php bin/phpstan
45+
4446
ci:
4547
runs-on: ubuntu-latest
4648
strategy:

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
},
1212
"require-dev": {
1313
"phpspec/phpspec": "^7.2",
14-
"friendsofphp/php-cs-fixer": "^3.8"
14+
"friendsofphp/php-cs-fixer": "^3.8",
15+
"phpstan/phpstan": "^1.6",
16+
"phpstan/phpstan-strict-rules": "^1.2",
17+
"phpstan/phpstan-symfony": "^1.1"
1518
},
1619
"minimum-stability": "beta",
1720
"autoload": {

phpstan-baseline.neon

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: "#^Parameter \\#2 \\$eventName of method Symfony\\\\Contracts\\\\EventDispatcher\\\\EventDispatcherInterface\\:\\:dispatch\\(\\) expects string\\|null, mixed given\\.$#"
5+
count: 1
6+
path: src/Client/Client.php
7+
8+
-
9+
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\\.$#"
10+
count: 1
11+
path: src/DataCollector/ClientRequest.php
12+
13+
-
14+
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeDefinition\\:\\:children\\(\\)\\.$#"
15+
count: 4
16+
path: src/DependencyInjection/Configuration.php
17+
18+
-
19+
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\\.$#"
20+
count: 1
21+
path: src/DependencyInjection/YproximiteInfluxDbPresetExtension.php
22+
23+
-
24+
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\\.$#"
25+
count: 1
26+
path: src/DependencyInjection/YproximiteInfluxDbPresetExtension.php
27+
28+
-
29+
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\\.$#"
30+
count: 1
31+
path: src/DependencyInjection/YproximiteInfluxDbPresetExtension.php

phpstan.neon

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
includes:
2+
- vendor/phpstan/phpstan-strict-rules/rules.neon
3+
- vendor/phpstan/phpstan-symfony/extension.neon
4+
- vendor/phpstan/phpstan-symfony/rules.neon
5+
- phpstan-baseline.neon
6+
7+
parameters:
8+
level: max
9+
10+
paths:
11+
- src
12+
13+
inferPrivatePropertyTypeFromConstructor: true

src/Client/Client.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
use Yproximite\InfluxDbBundle\Events\HttpEvent;
2020
use Yproximite\InfluxDbBundle\Events\UdpEvent;
2121

22-
/**
23-
* Class Client
24-
*/
2522
class Client implements ClientInterface
2623
{
2724
/**
@@ -53,9 +50,9 @@ public function sendPoint(
5350
string $profileName,
5451
string $presetName,
5552
float $value,
56-
\DateTimeInterface $dateTime = null
57-
) {
58-
if (!$dateTime) {
53+
?\DateTimeInterface $dateTime = null
54+
): void {
55+
if (null === $dateTime) {
5956
$dateTime = new \DateTime();
6057
}
6158

@@ -77,13 +74,12 @@ private function buildPoint(PointPresetInterface $preset, float $value, \DateTim
7774
$builder
7875
->setPreset($preset)
7976
->setValue($value)
80-
->setDateTime($dateTime)
81-
;
77+
->setDateTime($dateTime);
8278

8379
return $builder->build();
8480
}
8581

86-
private function sendPointUsingConnection(Point $point, ConnectionInterface $connection)
82+
private function sendPointUsingConnection(Point $point, ConnectionInterface $connection): void
8783
{
8884
$class = $this->getEventClassName($connection);
8985
$event = new $class([$point], Database::PRECISION_SECONDS, $connection->getName());

src/Client/ClientInterface.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44

55
namespace Yproximite\Bundle\InfluxDbPresetBundle\Client;
66

7-
/**
8-
* Interface ClientInterface
9-
*/
107
interface ClientInterface
118
{
129
public function sendPoint(
1310
string $profileName,
1411
string $presetName,
1512
float $value,
16-
\DateTimeInterface $dateTime = null
17-
);
13+
?\DateTimeInterface $dateTime = null
14+
): void;
1815
}

src/Connection/Connection.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Yproximite\Bundle\InfluxDbPresetBundle\Connection;
66

7-
/**
8-
* Class Connection
9-
*/
107
class Connection implements ConnectionInterface
118
{
129
/**

src/Connection/ConnectionFactory.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Yproximite\Bundle\InfluxDbPresetBundle\Connection;
66

7-
/**
8-
* Class ConnectionFactory
9-
*/
107
class ConnectionFactory implements ConnectionFactoryInterface
118
{
129
public function create(): ConnectionInterface

src/Connection/ConnectionFactoryInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace Yproximite\Bundle\InfluxDbPresetBundle\Connection;
66

7-
/**
8-
* Interface ConnectionFactoryInterface
9-
*/
107
interface ConnectionFactoryInterface
118
{
129
public function create(): ConnectionInterface;
1310

11+
/**
12+
* @param array{ name:string, protocol:string, deferred:bool } $config
13+
*/
1414
public function createFromConfig(array $config): ConnectionInterface;
1515
}

src/Connection/ConnectionInterface.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Yproximite\Bundle\InfluxDbPresetBundle\Connection;
66

7-
/**
8-
* Interface ConnectionInterface
9-
*/
107
interface ConnectionInterface
118
{
129
public const PROTOCOL_UDP = 'udp';

src/DataCollector/ClientRequest.php

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111
use Yproximite\Bundle\InfluxDbPresetBundle\Profile\Profile;
1212
use Yproximite\Bundle\InfluxDbPresetBundle\Profile\ProfileInterface;
1313

14-
/**
15-
* Class ClientRequest
16-
*/
17-
class ClientRequest implements \Serializable
14+
class ClientRequest
1815
{
1916
/**
2017
* @var ProfileInterface
@@ -68,9 +65,12 @@ public function getDateTime(): \DateTimeInterface
6865
return $this->dateTime;
6966
}
7067

71-
public function serialize()
68+
/**
69+
* @return array<int, int|string|float|\DateTimeInterface|array<mixed>|null>
70+
*/
71+
public function __serialize(): array
7272
{
73-
return serialize([
73+
return [
7474
$this->value,
7575
$this->dateTime,
7676
$this->profile->getName(),
@@ -81,12 +81,22 @@ public function serialize()
8181
return $this->serializePointPreset($pointPreset);
8282
}, $this->profile->getPointPresets()),
8383
$this->serializePointPreset($this->pointPreset),
84-
]);
84+
];
8585
}
8686

87-
public function unserialize($serialized)
87+
/**
88+
* @param array{
89+
* 0:float,
90+
* 1:\DateTimeInterface,
91+
* 2:string,
92+
* 3:array<string, array{ 0:string, 1:string, 2:bool }>,
93+
* 4:array<string, array{ 0:string, 1:array<string,string>, 2:string, 3:array<string,string> }>,
94+
* 5:array{ 0:string, 1:array<string,string>, 2:string, 3:array<string,string> }
95+
* } $serialized
96+
*/
97+
public function __unserialize(array $serialized): void
8898
{
89-
list($this->value, $this->dateTime, $profileName, $profileConnections, $profilePointPresets, $pointPreset) = unserialize($serialized);
99+
[$this->value, $this->dateTime, $profileName, $profileConnections, $profilePointPresets, $pointPreset] = $serialized;
90100

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

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

100-
foreach ($profilePointPresets as $pointPreset) {
101-
$this->profile->addPointPreset($this->unserializePointPreset($pointPreset));
110+
foreach ($profilePointPresets as $profilePointPreset) {
111+
$this->profile->addPointPreset($this->unserializePointPreset($profilePointPreset));
102112
}
103113
}
104114

115+
/**
116+
* @return array<int, string|array<string,string>>
117+
*/
105118
protected function serializePointPreset(PointPresetInterface $pointPreset): array
106119
{
107120
return [
@@ -112,6 +125,9 @@ protected function serializePointPreset(PointPresetInterface $pointPreset): arra
112125
];
113126
}
114127

128+
/**
129+
* @param array{ 0:string, 1:array<string,string>, 2:string, 3:array<string,string> } $pointPreset
130+
*/
115131
protected function unserializePointPreset(array $pointPreset): PointPresetInterface
116132
{
117133
$newPointPreset = new PointPreset();
@@ -124,6 +140,9 @@ protected function unserializePointPreset(array $pointPreset): PointPresetInterf
124140
return $newPointPreset;
125141
}
126142

143+
/**
144+
* @return array<int, string|bool>
145+
*/
127146
protected function serializeConnection(ConnectionInterface $connection): array
128147
{
129148
return [
@@ -133,6 +152,9 @@ protected function serializeConnection(ConnectionInterface $connection): array
133152
];
134153
}
135154

155+
/**
156+
* @param array{ 0:string, 1:string, 2:bool } $connection
157+
*/
136158
protected function unserializeConnection(array $connection): ConnectionInterface
137159
{
138160
$newConnection = new Connection();

src/DataCollector/InfluxDbPresetDataCollector.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ public function __construct(ProfilePoolInterface $profilePool)
2727
$this->profilePool = $profilePool;
2828
}
2929

30-
public function onClientRequest(ClientRequestEvent $event)
30+
public function onClientRequest(ClientRequestEvent $event): void
3131
{
3232
$profile = $this->profilePool->getProfileByName($event->getProfileName());
3333
$preset = $profile->getPointPresetByName($event->getPresetName());
3434

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

38-
public function collect(Request $request, Response $response, \Throwable $exception = null)
38+
public function collect(Request $request, Response $response, \Throwable $exception = null): void
3939
{
4040
$this->data = [
4141
'requests' => $this->requests,
@@ -45,7 +45,7 @@ public function collect(Request $request, Response $response, \Throwable $except
4545
/**
4646
* Resets this data collector to its initial state.
4747
*/
48-
public function reset()
48+
public function reset(): void
4949
{
5050
$this->data = [];
5151
}
@@ -58,7 +58,7 @@ public function getRequests(): array
5858
return $this->data['requests'];
5959
}
6060

61-
public function getName()
61+
public function getName(): string
6262
{
6363
return 'yproximite.influxdb_preset';
6464
}

src/DependencyInjection/Configuration.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,25 @@
88
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
99
use Symfony\Component\Config\Definition\ConfigurationInterface;
1010

11-
/**
12-
* Class Configuration
13-
*/
1411
class Configuration implements ConfigurationInterface
1512
{
1613
/**
17-
* @var array
14+
* @var array<int, string>
1815
*/
1916
private static $protocols = ['http', 'udp'];
2017

21-
/**
22-
* {@inheritdoc}
23-
*/
24-
public function getConfigTreeBuilder()
18+
public function getConfigTreeBuilder(): TreeBuilder
2519
{
2620
$treeBuilder = new TreeBuilder('yproximite_influx_db_preset');
27-
$rootNode = method_exists($treeBuilder, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('yproximite_influx_db_preset');
21+
$rootNode = $treeBuilder->getRootNode();
2822

2923
$this->addProfilesSection($rootNode);
3024
$this->addExtensionsSection($rootNode);
3125

3226
return $treeBuilder;
3327
}
3428

35-
private function addProfilesSection(NodeDefinition $rootNode)
29+
private function addProfilesSection(NodeDefinition $rootNode): void
3630
{
3731
$profiles = $rootNode
3832
->children()
@@ -51,7 +45,7 @@ private function addProfilesSection(NodeDefinition $rootNode)
5145
;
5246
}
5347

54-
private function addConnectionsSection(NodeDefinition $rootNode)
48+
private function addConnectionsSection(NodeDefinition $rootNode): void
5549
{
5650
$rootNode
5751
->children()
@@ -64,7 +58,7 @@ private function addConnectionsSection(NodeDefinition $rootNode)
6458
->isRequired()
6559
->validate()
6660
->ifTrue(function ($value) {
67-
return !\in_array($value, self::$protocols);
61+
return !\in_array($value, self::$protocols, true);
6862
})
6963
->thenInvalid(
7064
sprintf(
@@ -82,7 +76,7 @@ private function addConnectionsSection(NodeDefinition $rootNode)
8276
;
8377
}
8478

85-
private function addPresetsSection(NodeDefinition $rootNode)
79+
private function addPresetsSection(NodeDefinition $rootNode): void
8680
{
8781
$rootNode
8882
->children()
@@ -104,7 +98,7 @@ private function addPresetsSection(NodeDefinition $rootNode)
10498
;
10599
}
106100

107-
private function addExtensionsSection(NodeDefinition $rootNode)
101+
private function addExtensionsSection(NodeDefinition $rootNode): void
108102
{
109103
$rootNode
110104
->children()

0 commit comments

Comments
 (0)