Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 22 additions & 35 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,55 @@ on: [push]
jobs:
cs:
name: Code Style
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
Comment thread
ColonelMoutarde marked this conversation as resolved.
Outdated
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.2
coverage: xdebug
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
run: composer install --no-progress
- name: Run php-cs-fixture
- name: Run php-cs-fixer
env:
PHP_CS_FIXER_FUTURE_MODE: 1
run: bin/php-cs-fixer fix --config=.php-cs-fixer.php --dry-run --no-interaction --diff

phpunit:
name: Unit Tests
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
env:
COMPOSER_MEMORY_LIMIT: "-1"
strategy:
matrix:
include:
# oldest supported versions
- php: 7.2
composer_flags: "--prefer-lowest"
cs_fixer_config: "1.3.*"
symfony_phpunit_remove_return_typehint: 1
- php: 7.3
symfony_version: "4.4.*"
- php: 7.4
symfony_version: "5.1.*"
- php: 7.4
symfony_version: 5.2.*
# most recent versions
- php: 8.0
symfony_version: 5.3.*
- php: 8.0
symfony_version: 5.4.*
- php: 8.0
symfony_version: 6.0.*
- php: 8.1
symfony_version: "6.4.*"
- php: 8.2
symfony_version: "6.4.*"
- php: 8.2
symfony_version: "7.0.*"
- php: 8.2
symfony_version: "7.1.*"
- php: 8.3
symfony_version: "7.1.*"
- php: 8.3
symfony_version: "7.2.*"
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
Expand All @@ -69,25 +62,19 @@ jobs:
coverage: xdebug
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Symfony ${{ matrix.symfony_version }}
run: composer require "symfony/framework-bundle:${{ matrix.symfony_version }}" --no-update
if: matrix.symfony_version != ''
- name: Downgrade php-cs-fixer
run: composer require "m6web/php-cs-fixer-config:${{ matrix.cs_fixer_config }}" --no-update
if: matrix.cs_fixer_config != ''
- name: Install Dependencies
run: composer update --prefer-dist --no-interaction --optimize-autoloader --prefer-stable --no-progress $COMPOSER_FLAGS
env:
COMPOSER_FLAGS: ${{ matrix.composer_flags }}
run: composer update --prefer-dist --no-interaction --optimize-autoloader --prefer-stable --no-progress
- name: Run PHPUnit
run: bin/simple-phpunit
env:
SYMFONY_DEPRECATIONS_HELPER: weak
SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT: ${{ matrix.symfony_phpunit_remove_return_typehint }}
20 changes: 13 additions & 7 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?php

$config = new M6Web\CS\Config\Php71;
$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
;

$config->getFinder()
->in([
__DIR__
]);

return $config;
return (new PhpCsFixer\Config())
->setRules([
Comment thread
ColonelMoutarde marked this conversation as resolved.
Outdated
'@PSR12' => true,
'@PER-CS' => true,
'@PHP81Migration' => true,
'@PHPUnit80Migration' => true,
'array_syntax' => ['syntax' => 'short'],
])
->setFinder($finder)
;
2 changes: 1 addition & 1 deletion Client/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(ServerInterface $server);
/**
* Send metrics data to the configured server
*
* @param array $lines array of string lines to send
* @param array<string> $lines array of string lines to send
*/
public function sendLines(array $lines): void;
}
18 changes: 11 additions & 7 deletions Client/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

class Server implements ServerInterface
{
/** @var string */
private $name;
/** @phpstan-ignore property.onlyWritten */
private string $name;

/** @var mixed string format udp://.+ */
private $address;
Expand All @@ -18,20 +18,24 @@ class Server implements ServerInterface
/**
* Server constructor.
*
* @param array<string, mixed> $serverConfig
*
* @throws ServerException
*/
public function __construct(string $serverName, array $serverConfig)
{
if ($this->checkServersConfigurations($serverName, $serverConfig)) {
$this->name = $serverName;
$this->address = $serverConfig['address'];
$this->port = intval($serverConfig['port']);
$this->port = (int) $serverConfig['port'];
}
}

/**
* Init the servers defined in the app configuration
*
* @param array<string, mixed> $serverConfig
*
* @throws ServerException
*/
protected function checkServersConfigurations(string $serverName, array $serverConfig): bool
Expand All @@ -40,11 +44,11 @@ protected function checkServersConfigurations(string $serverName, array $serverC
throw new ServerException('No servers have been configured');
}

if (!isset($serverConfig['address']) || !isset($serverConfig['port'])) {
throw new ServerException($serverName.' : no address or port in the configuration');
if (!isset($serverConfig['address'], $serverConfig['port'])) {
throw new ServerException($serverName . ' : no address or port in the configuration');
}
if (strpos($serverConfig['address'], 'udp://') !== 0) {
throw new ServerException($serverName.' : address should begin with udp://');
if (!str_starts_with($serverConfig['address'], 'udp://')) {
throw new ServerException($serverName . ' : address should begin with udp://');
}

return true;
Expand Down
2 changes: 2 additions & 0 deletions Client/ServerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
interface ServerInterface
{
/**
* @param array<string, mixed> $serverConfig
*
* @throws ServerException
*/
public function __construct(string $serverName, array $serverConfig);
Expand Down
11 changes: 6 additions & 5 deletions Client/UdpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ class UdpClient implements ClientInterface
/** @var int max safe size in bytes of one message to send (max official size is 65507) */
public const MAX_MESSAGE_SIZE = 64000;

/** @var ServerInterface */
protected $server;
protected ServerInterface $server;

/** @var ?bool */
protected $debugEnabled;
protected ?bool $debugEnabled;

public function __construct(ServerInterface $server, ?bool $debugEnabled = false)
{
Expand All @@ -21,6 +19,8 @@ public function __construct(ServerInterface $server, ?bool $debugEnabled = false

/**
* Split metrics to send them group by group
*
* @param array<string> $lines
*/
public function sendLines(array $lines): void
{
Expand All @@ -30,6 +30,7 @@ public function sendLines(array $lines): void
}
}

/** @param array<string> $lines */
protected function writeLines(array $lines): bool
{
if ($resource = @fsockopen($this->server->getAddress(), $this->server->getPort())) {
Expand All @@ -56,7 +57,7 @@ protected function getFormattedLine(string $line): string
{
if ($this->debugEnabled) {
// With debug mode on, we add a carriage return to provide more readable data
return $line."\n";
return $line . "\n";
}

return $line;
Expand Down
30 changes: 13 additions & 17 deletions DataCollector/StatsdDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function reset(): void

public function onKernelResponse(ResponseEvent $event): void
{
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
if (HttpKernelInterface::MAIN_REQUEST === $event->getRequestType()) {
foreach ($this->eventListeners as $serviceId => $eventListener) {
$clientInfo = [
'name' => $serviceId,
Expand All @@ -45,14 +45,12 @@ public function onKernelResponse(ResponseEvent $event): void
$metricHandler = $eventListener->getMetricHandler();

foreach ($metricHandler->getMetrics() as $metric) {
if ($metric instanceof MetricInterface) {
try {
$clientInfo['operations'][] = [
'message' => $metricHandler->getFormattedMetric($metric),
];
$this->data['operations']++;
} catch (MetricException $e) {
}
try {
$clientInfo['operations'][] = [
'message' => $metricHandler->getFormattedMetric($metric),
];
$this->data['operations']++;
} catch (MetricException $e) {
}
}
$this->data['clients'][] = $clientInfo;
Expand All @@ -71,18 +69,16 @@ public function addEventListener(string $serviceId, EventListener $eventListener
/**
* Collect the data
*
* @param Request $request The request object
* @param Response $response The response object
* @param \Throwable $exception A throwable
* @param Request $request The request object
* @param Response $response The response object
* @param \Throwable|null $exception A throwable
*/
public function collect(Request $request, Response $response, \Throwable $exception = null): void
{
}
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void {}

/**
* Return the list of statsd operations
*
* @return array operations list
* @return array<mixed> operations list
*/
public function getClients(): array
{
Expand All @@ -104,7 +100,7 @@ public function getOperations(): int
*
* @return string data collector name
*/
public function getName()
public function getName(): string
{
return 'statsd';
}
Expand Down
11 changes: 4 additions & 7 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ private function addServersSection(ArrayNodeDefinition $rootNode): void
->scalarNode('address')
->isRequired()
->validate()
->ifTrue(function ($v) {return substr($v, 0, 6) !== 'udp://'; })
->ifTrue(function ($v) {
return substr($v, 0, 6) !== 'udp://';
})
->thenInvalid("address parameter should begin with 'udp://'")
->end()
->end()
Expand Down Expand Up @@ -187,13 +189,8 @@ private function getClientsGroupsEvents()
return $eventsNode;
}

private function getRootNode(TreeBuilder $treeBuilder, $name)
private function getRootNode(TreeBuilder $treeBuilder, string $name): ArrayNodeDefinition
{
// BC layer for symfony/config 4.1 and older
if (!\method_exists($treeBuilder, 'getRootNode')) {
return $treeBuilder->root($name);
}

return $treeBuilder->getRootNode();
}
}
5 changes: 3 additions & 2 deletions DependencyInjection/M6WebStatsdPrometheusExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected function setEventListenerAsServiceAndGetServiceId(string $clientName,

protected function getServiceIdFrom(string $alias): string
{
return ($alias === 'default') ? self::CONFIG_ROOT_KEY : self::CONFIG_ROOT_KEY.$alias;
return ($alias === 'default') ? self::CONFIG_ROOT_KEY : self::CONFIG_ROOT_KEY . $alias;
}

/**
Expand All @@ -135,7 +135,7 @@ protected function addEventListenerTagsOnServiceDefinition(Definition $eventList
$eventsGroupConfig['tags'] ?? []
);
// Prefix the metric name.
$metricConfig['name'] = $this->metricsPrefix.$metricConfig['name'];
$metricConfig['name'] = $this->metricsPrefix . $metricConfig['name'];
}
// Set all the metrics config array in the object
// One event can send several metrics. Multiple metrics will be handled in the EventListener.
Expand Down Expand Up @@ -245,6 +245,7 @@ protected function getClientServerDefinition(string $clientName, string $serverN
if (!\array_key_exists($serverName, $this->servers)) {
throw new InvalidConfigurationException(sprintf('M6WebStatsd client %s used server %s which is not defined in the servers section', $clientName, $serverName));
}

// Matched server configurations.
return new Definition(Server::class, [
$serverName,
Expand Down
6 changes: 3 additions & 3 deletions Event/AbstractMonitoringEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

abstract class AbstractMonitoringEvent extends Event implements MonitoringEventInterface
{
/** @var array */
protected $parameters;
/** @var array<string, mixed> */
protected array $parameters;

/**
* AbstractMonitoringEvent constructor.
*
* @param array $parameters parameters can contain metrics values, tags values or/and custom param values
* @param array<string, mixed> $parameters parameters can contain metrics values, tags values or/and custom param values
*
* @see https://github.com/M6Web/StatsdPrometheusBundle/blob/master/Doc/usage.md
*/
Expand Down
2 changes: 1 addition & 1 deletion Event/Console/ConsoleCommandMonitoringEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ConsoleCommandMonitoringEvent extends AbstractMonitoringEvent
{
public static function fromFacade(ConsoleMonitoringEventFacade $facade): ConsoleCommandMonitoringEvent
public static function fromFacade(ConsoleMonitoringEventFacade $facade): self
{
return new self($facade->toMonitoringArray());
}
Expand Down
2 changes: 1 addition & 1 deletion Event/Console/ConsoleErrorMonitoringEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ConsoleErrorMonitoringEvent extends AbstractMonitoringEvent
{
public static function fromFacade(ConsoleMonitoringEventFacade $facade): ConsoleErrorMonitoringEvent
public static function fromFacade(ConsoleMonitoringEventFacade $facade): self
{
return new self($facade->toMonitoringArray());
}
Expand Down
Loading