Skip to content

Commit c6e2fec

Browse files
authored
PHP 8 & Symfony 6 compatibility (#85)
1 parent 07e5252 commit c6e2fec

20 files changed

Lines changed: 195 additions & 229 deletions

File tree

.coke

Lines changed: 0 additions & 10 deletions
This file was deleted.

.github/workflows/tests.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@ jobs:
1111
- 7.2
1212
- 7.3
1313
- 7.4
14+
- 8.0
15+
- 8.1
1416
symfony-version:
15-
- 4.4.0
16-
- ^5.0
17+
- 4.4.*
18+
- 5.3.*
19+
- 5.4.*
1720
include:
1821
- php-version: 7.1
19-
symfony-version: 4.4.0
22+
symfony-version: 4.4.*
23+
- php-version: 8.1
24+
symfony-version: 6.0.*
2025

2126
steps:
2227
- uses: actions/checkout@v2
@@ -33,6 +38,6 @@ jobs:
3338
- name: Install dependencies
3439
run: composer update --prefer-dist --no-interaction
3540
- name: Code style check
36-
run: bin/coke
41+
run: bin/php-cs-fixer fix --dry-run --using-cache=no --verbose
3742
- name: Unit tests
3843
run: bin/atoum

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ app/logs/*
44
build/
55
vendor/
66
bin/
7+
.php-cs-fixer.cache
78
composer.lock

.php-cs-fixer.dist.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(
5+
[
6+
__DIR__.'/src',
7+
]
8+
);
9+
10+
$config = new M6Web\CS\Config\BedrockStreaming();
11+
$config->setFinder($finder);
12+
13+
return $config;

composer.json

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,21 @@
1717
"sort-packages": true
1818
},
1919
"require": {
20-
"php": "^7.1.3",
21-
"symfony/config": "^4.4 || ^5.0",
22-
"symfony/dependency-injection": "^4.4 || ^5.0",
20+
"php": "^7.1.3 || ^8.0",
21+
"symfony/config": "^4.4 || ^5.0 || ^6.0",
22+
"symfony/dependency-injection": "^4.4 || ^5.0 || ^6.0",
2323
"symfony/event-dispatcher-contracts": "^1.1 || ^2.1",
24-
"symfony/http-kernel": "^4.4 || ^5.0",
25-
"symfony/property-access": "^4.4 || ^5.0",
26-
"symfony/validator": "^4.4 || ^5.0",
27-
"symfony/yaml": "^4.4 || ^5.0",
24+
"symfony/http-kernel": "^4.4 || ^5.0 || ^6.0",
25+
"symfony/property-access": "^4.4 || ^5.0 || ^6.0",
26+
"symfony/validator": "^4.4 || ^5.0 || ^6.0",
27+
"symfony/yaml": "^4.4 || ^5.0 || ^6.0",
2828
"m6web/statsd": "^1.3"
2929
},
3030
"require-dev": {
31-
"atoum/atoum": "^2.8 || ^3.0",
32-
"m6web/coke": "~1.2",
33-
"m6web/symfony2-coding-standard": "~1.2",
34-
"symfony/console": "^4.4 || ^5.0",
35-
"symfony/http-foundation": "^4.4 || ^5.0"
31+
"atoum/atoum": "^3.4 || ^4.0",
32+
"m6web/php-cs-fixer-config": "^2.0",
33+
"symfony/console": "^4.4 || ^5.0 || ^6.0",
34+
"symfony/http-foundation": "^4.4 || ^5.0 || ^6.0"
3635
},
3736
"autoload": {
3837
"psr-4": {

src/Client/Client.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88

99
/**
1010
* Class that extends base statsd client, to handle auto-increment from event dispatcher notifications
11-
*
1211
*/
1312
class Client extends BaseClient
1413
{
1514
protected $listenedEvents = [];
1615

17-
/**
18-
* @var PropertyAccessInterface
19-
*/
16+
/** @var PropertyAccessInterface */
2017
protected $propertyAccessor;
2118

2219
protected $toSendLimit;
@@ -58,7 +55,6 @@ public function setToSendLimit($toSendLimit)
5855

5956
/**
6057
* set the property accessor used in replaceConfigPlaceholder
61-
* @param PropertyAccess\PropertyAccessorInterface $propertyAccessor
6258
*
6359
* @return $this
6460
*/
@@ -76,6 +72,7 @@ public function setPropertyAccessor(PropertyAccess\PropertyAccessorInterface $pr
7672
* @param string $name the event name
7773
*
7874
* @throws Exception
75+
*
7976
* @return void
8077
*/
8178
public function handleEvent($event, $name = null)
@@ -89,17 +86,16 @@ public function handleEvent($event, $name = null)
8986
return;
9087
}
9188

92-
$config = $this->listenedEvents[$name];
89+
$config = $this->listenedEvents[$name];
9390
$immediateSend = false;
94-
$tags = $this->mergeTags($event, $config);
91+
$tags = $this->mergeTags($event, $config);
9592

9693
// replace placeholders in tags
97-
$tags = array_map(function($tag) use ($event, $name) {
94+
$tags = array_map(function ($tag) use ($event, $name) {
9895
return $this->replaceConfigPlaceholder($event, $name, $tag);
9996
}, $tags);
10097

10198
foreach ($config as $conf => $confValue) {
102-
10399
// increment
104100
if ('increment' === $conf) {
105101
$this->increment($this->replaceConfigPlaceholder($event, $name, $confValue), 1, $tags);
@@ -123,7 +119,7 @@ public function handleEvent($event, $name = null)
123119
} elseif ('tags' === $conf) {
124120
// nothing
125121
} else {
126-
throw new Exception("configuration : ".$conf." not handled by the StatsdBundle or its value is in a wrong format.");
122+
throw new Exception('configuration : '.$conf.' not handled by the StatsdBundle or its value is in a wrong format.');
127123
}
128124
}
129125

@@ -143,25 +139,26 @@ public function handleEvent($event, $name = null)
143139
* @param string $method
144140
*
145141
* @return mixed
142+
*
146143
* @throws Exception
147144
*/
148145
private function getEventValue($event, $method)
149146
{
150147
if (!method_exists($event, $method)) {
151-
throw new Exception("The event class ".get_class($event)." must have a ".$method." method in order to mesure value");
148+
throw new Exception('The event class '.get_class($event).' must have a '.$method.' method in order to mesure value');
152149
}
153150

154-
return call_user_func(array($event,$method));
151+
return call_user_func([$event, $method]);
155152
}
156153

157154
/**
158155
* Factorisation of the timing method
159156
* find the value timed
160157
*
161-
* @param object $event Event
158+
* @param object $event Event
162159
* @param string $timingMethod Callable method in the event
163-
* @param string $node Node
164-
* @param array $tags Tags key => value for influxDb
160+
* @param string $node Node
161+
* @param array $tags Tags key => value for influxDb
165162
*
166163
* @throws Exception
167164
*/
@@ -176,9 +173,9 @@ private function addTiming($event, $timingMethod, $node, $tags = [])
176173
/**
177174
* Replaces a string with a method name
178175
*
179-
* @param EventInterface $event An event
176+
* @param EventInterface $event An event
180177
* @param string $eventName The name of the event
181-
* @param string $string The node in which the replacing will happen
178+
* @param string $string The node in which the replacing will happen
182179
*
183180
* @return string
184181
*/

src/DataCollector/StatsdDataCollector.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace M6Web\Bundle\StatsdBundle\DataCollector;
44

5-
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
6-
use Symfony\Component\HttpFoundation\Response;
75
use Symfony\Component\HttpFoundation\Request;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
88
use Symfony\Component\HttpKernel\HttpKernelInterface;
99

1010
/**
@@ -44,8 +44,8 @@ public function onKernelResponse($event)
4444
if (HttpKernelInterface::MASTER_REQUEST == $event->getRequestType()) {
4545
foreach ($this->statsdClients as $clientName => $client) {
4646
$clientInfo = [
47-
'name' => $clientName,
48-
'operations' => []
47+
'name' => $clientName,
48+
'operations' => [],
4949
];
5050
foreach ($client->getToSend() as $operation) {
5151
if ($operation) {
@@ -54,10 +54,10 @@ public function onKernelResponse($event)
5454

5555
$clientInfo['operations'][] = [
5656
'server' => $operation['server'],
57-
'node' => $message->getNode(),
58-
'value' => $message->getValue(),
57+
'node' => $message->getNode(),
58+
'value' => $message->getValue(),
5959
'sample' => $message->getSampleRate(),
60-
'unit' => $message->getUnit()
60+
'unit' => $message->getUnit(),
6161
];
6262
}
6363
}
@@ -101,7 +101,7 @@ public function getClients()
101101
/**
102102
* Return the number of statsd operations
103103
*
104-
* @return integer the number of operations
104+
* @return int the number of operations
105105
*/
106106
public function getOperations()
107107
{

src/DependencyInjection/M6WebStatsdExtension.php

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace M6Web\Bundle\StatsdBundle\DependencyInjection;
44

5+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6+
use Symfony\Component\Config\FileLocator;
57
use Symfony\Component\DependencyInjection\ContainerBuilder;
6-
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
78
use Symfony\Component\DependencyInjection\ContainerInterface;
89
use Symfony\Component\DependencyInjection\Definition;
9-
use Symfony\Component\DependencyInjection\Reference;
10-
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1110
use Symfony\Component\DependencyInjection\Loader;
12-
use Symfony\Component\Config\FileLocator;
11+
use Symfony\Component\DependencyInjection\Reference;
12+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1313

1414
/**
1515
* This is the class that loads and manages your bundle configuration
@@ -24,9 +24,9 @@ class M6WebStatsdExtension extends Extension
2424
public function load(array $configs, ContainerBuilder $container)
2525
{
2626
$configuration = new Configuration();
27-
$config = $this->processConfiguration($configuration, $configs);
28-
$servers = isset($config['servers']) ? $config['servers'] : [];
29-
$clients = isset($config['clients']) ? $config['clients'] : [];
27+
$config = $this->processConfiguration($configuration, $configs);
28+
$servers = isset($config['servers']) ? $config['servers'] : [];
29+
$clients = isset($config['clients']) ? $config['clients'] : [];
3030

3131
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
3232
$loader->load('services.yml');
@@ -49,15 +49,15 @@ public function load(array $configs, ContainerBuilder $container)
4949
'data_collector',
5050
[
5151
'template' => '@M6WebStatsd/Collector/statsd.html.twig',
52-
'id' => 'statsd'
52+
'id' => 'statsd',
5353
]
5454
);
5555

5656
$definition->addTag(
5757
'kernel.event_listener',
5858
[
5959
'event' => 'kernel.response',
60-
'method' => 'onKernelResponse'
60+
'method' => 'onKernelResponse',
6161
]
6262
);
6363

@@ -98,15 +98,16 @@ public function load(array $configs, ContainerBuilder $container)
9898
* @param string $alias Alias of the client
9999
* @param array $config Base config of the client
100100
* @param array $servers List of available servers as describe in the config file
101-
* @param boolean $baseEvents Register base events
101+
* @param bool $baseEvents Register base events
102102
*
103103
* @throws InvalidConfigurationException
104+
*
104105
* @return string the service name
105106
*/
106107
protected function loadClient($container, $alias, array $config, array $servers, $baseEvents)
107108
{
108-
$usedServers = [];
109-
$events = $config['events'];
109+
$usedServers = [];
110+
$events = $config['events'];
110111
$matchedServers = [];
111112

112113
if ($config['servers'][0] == 'all') {
@@ -115,7 +116,6 @@ protected function loadClient($container, $alias, array $config, array $servers,
115116
} else {
116117
// Use only declared servers
117118
foreach ($config['servers'] as $serverAlias) {
118-
119119
// Named server
120120
if (array_key_exists($serverAlias, $servers)) {
121121
$matchedServers[] = $serverAlias;
@@ -127,7 +127,7 @@ protected function loadClient($container, $alias, array $config, array $servers,
127127
foreach (array_keys($servers) as $key) {
128128
if (fnmatch($serverAlias, $key)) {
129129
$matchedServers[] = $key;
130-
$found = true;
130+
$found = true;
131131
}
132132
}
133133

@@ -146,12 +146,12 @@ protected function loadClient($container, $alias, array $config, array $servers,
146146
foreach ($matchedServers as $serverAlias) {
147147
$usedServers[] = [
148148
'address' => $servers[$serverAlias]['address'],
149-
'port' => $servers[$serverAlias]['port']
149+
'port' => $servers[$serverAlias]['port'],
150150
];
151151
}
152152

153153
// Add the statsd client configured
154-
$serviceId = ($alias == 'default') ? 'm6_statsd' : 'm6_statsd.'.$alias;
154+
$serviceId = ($alias == 'default') ? 'm6_statsd' : 'm6_statsd.'.$alias;
155155
$definition = new Definition('M6Web\Bundle\StatsdBundle\Client\Client');
156156
$definition->setPublic(true);
157157
$definition->addArgument($usedServers);
@@ -176,26 +176,26 @@ protected function loadClient($container, $alias, array $config, array $servers,
176176
$definition->addArgument(new Reference($serviceId));
177177
$definition->addArgument(new Reference('event_dispatcher'));
178178
$definition->addTag('kernel.event_listener', [
179-
'event' => 'kernel.terminate',
180-
'method' => 'onKernelTerminate',
181-
'priority' => -100
179+
'event' => 'kernel.terminate',
180+
'method' => 'onKernelTerminate',
181+
'priority' => -100,
182182
]);
183183
$definition->addTag('kernel.event_listener', [
184-
'event' => 'console.terminate',
185-
'method' => 'onConsoleTerminate',
186-
'priority' => -100
184+
'event' => 'console.terminate',
185+
'method' => 'onConsoleTerminate',
186+
'priority' => -100,
187187
]);
188188

189189
if ($baseEvents) {
190190
$definition->addTag('kernel.event_listener', [
191191
'event' => 'kernel.terminate',
192192
'method' => 'dispatchBaseEvents',
193-
'priority' => 0
193+
'priority' => 0,
194194
]);
195195
$definition->addTag('kernel.event_listener', [
196196
'event' => 'kernel.exception',
197197
'method' => 'onKernelException',
198-
'priority' => 0
198+
'priority' => 0,
199199
]);
200200
}
201201
$container->setDefinition($serviceListenerId, $definition);
@@ -208,10 +208,8 @@ protected function loadClient($container, $alias, array $config, array $servers,
208208
*
209209
* trick allowing bypassing the Bundle::getContainerExtension check on getAlias
210210
* not very clean, to investigate
211-
*
212-
* @return string
213211
*/
214-
public function getAlias()
212+
public function getAlias(): string
215213
{
216214
return 'm6_statsd';
217215
}

0 commit comments

Comments
 (0)