Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
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
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
fail-fast: false
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3']
php-versions: ['8.1', '8.2', '8.3', '8.4']

steps:
- name: Checkout
Expand All @@ -25,7 +25,7 @@ jobs:
run: composer install --prefer-dist --no-progress --no-suggest --no-interaction

- name: phpcs
run: vendor/bin/php-cs-fixer fix --diff --dry-run -v
run: PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix --diff --dry-run -v

- name: psalm
run: vendor/bin/psalm --show-info=false
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ stats.log

# php-cs-fixer
.php-cs-fixer.cache
/.phpunit.cache/test-results
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
}
],
"require": {
"php": ">= 7.3 || ^8"
"php": "^8.1"
},
"require-dev": {
"phpunit/phpunit": "^9",
"vimeo/psalm": "^4.6",
"friendsofphp/php-cs-fixer": "^3.0"
"phpunit/phpunit": "^10.5",
"vimeo/psalm": "^6.12",
"friendsofphp/php-cs-fixer": "^3.75"
},
"autoload": {
"psr-4": {
Expand Down
33 changes: 22 additions & 11 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="tests/bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="statsd-php Test Suite">
<directory>tests/unit</directory>
</testsuite>
</testsuites>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
bootstrap="tests/bootstrap.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnPhpunitDeprecations="true"
>
<testsuites>
<testsuite name="statsd-php Test Suite">
<directory>tests/unit</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
5 changes: 5 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,10 @@
<RawObjectIteration errorLevel="info" />

<InvalidStringClass errorLevel="info" />
<UnusedClass errorLevel="info" />
<MissingOverrideAttribute errorLevel="info" />
<ClassMustBeFinal errorLevel="suppress"/>
<PossiblyUnusedMethod errorLevel="info" />
<PossiblyUnusedReturnValue errorLevel="info" />
</issueHandlers>
</psalm>
8 changes: 4 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function timing(string $key, float $value, float $sampleRate = 1.0, array
*/
public function startTiming(string $key, array $tags = []): void
{
$timingKey = $key . md5(json_encode($tags));
$timingKey = $key . md5((string)json_encode($tags));
$this->timings[$timingKey] = gettimeofday(true);
}

Expand All @@ -145,7 +145,7 @@ public function startTiming(string $key, array $tags = []): void
public function endTiming(string $key, float $sampleRate = 1.0, array $tags = []): ?float
{
$end = gettimeofday(true);
$timingKey = $key . md5(json_encode($tags));
$timingKey = $key . md5((string)json_encode($tags));

if (isset($this->timings[$timingKey])) {
$timing = ($end - $this->timings[$timingKey]) * 1000;
Expand Down Expand Up @@ -266,10 +266,10 @@ private function send(string $key, $value, string $type, float $sampleRate, arra
$key = $this->namespace . '.' . $key;
}

$message = $key . ':' . $value . '|' . $type;
$message = $key . ':' . ((string)$value) . '|' . $type;

if ($sampleRate < 1) {
$sampledData = $message . '|@' . $sampleRate;
$sampledData = $message . '|@' . ((string)$sampleRate);
} else {
$sampledData = $message;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Connection/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(string $filePath, string $mode = "a+")

private function open(): void
{
$this->handle = @fopen($this->filePath, $this->mode);
$this->handle = @fopen($this->filePath, $this->mode) ?: null;
}

public function send(string $message): void
Expand Down
2 changes: 1 addition & 1 deletion src/Connection/InMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class InMemory implements Connection
/**
* @var string[]
*/
private $messages = [];
private array $messages = [];

public function send(string $message): void
{
Expand Down
30 changes: 15 additions & 15 deletions src/Connection/InetSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,28 @@ abstract class InetSocket implements Connection

/**
* host name
*
* @var string
*/
protected $host;
protected string $host;
Comment thread
annadamm-check24 marked this conversation as resolved.
Outdated

/**
* port number
*
* @var int
*/
protected $port;
protected int $port;

/**
* Socket timeout
*
* @var int
*/
private $timeout;
private int $timeout;

/**
* Persistent connection
*
* @var bool
*/
private $persistent = false;
private bool $persistent;

/**
* @var int
* @var int<1, max>
*/
private $maxPayloadSize;
private int $maxPayloadSize;

/**
* instantiates the Connection object and a real connection to statsd
Expand All @@ -64,11 +56,19 @@ public function __construct(
$this->host = $host;
$this->port = $port;
$this->persistent = $persistent;
$this->maxPayloadSize = $mtu -
$maxPayloadSize = $mtu -
self::IP_HEADER_SIZE -
$this->getProtocolHeaderSize() -
strlen(self::LINE_DELIMITER);

if ($maxPayloadSize <= 0) {
throw new \InvalidArgumentException(
'The maximum payload size must be greater than 0. Please check the MTU value.'
);
}

$this->maxPayloadSize = $maxPayloadSize;

if ($timeout === null) {
$this->timeout = (int) ini_get('default_socket_timeout');
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Connection/TcpSocketException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TcpSocketException extends \RuntimeException
{
public function __construct(string $host, int $port, string $message, \Exception $previous = null)
public function __construct(string $host, int $port, string $message, ?\Exception $previous = null)
{
parent::__construct(
sprintf('Couldn\'t connect to host "%s:%d": %s', $host, $port, $message),
Expand Down
4 changes: 2 additions & 2 deletions src/Connection/UdpSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ protected function connect(string $host, int $port, int $timeout, bool $persiste
$url = 'udp://' . $host;

if ($persistent) {
$this->socket = @pfsockopen($url, $port, $errorNumber, $errorMessage, $timeout);
$this->socket = @pfsockopen($url, $port, $errorNumber, $errorMessage, $timeout) ?: null;
} else {
$this->socket = @fsockopen($url, $port, $errorNumber, $errorMessage, $timeout);
$this->socket = @fsockopen($url, $port, $errorNumber, $errorMessage, $timeout) ?: null;
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/StatsdAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
*/
trait StatsdAwareTrait
{
/**
* @var Client
*/
protected $statsd;
protected Client $statsd;
Comment thread
annadamm-check24 marked this conversation as resolved.
Outdated

/**
* Sets the StatsD client.
Expand Down
60 changes: 21 additions & 39 deletions tests/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Domnikl\Test\Statsd;

use Domnikl\Statsd\Client as Client;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

class ClientTest extends TestCase
Expand Down Expand Up @@ -58,18 +60,16 @@ public function testCountWithFloatValue()
);
}

public function sampleRateData()
public static function sampleRateData()
{
return [
[0.9, 1, '0.9'],
[0.9, 0.5, '0.5'],
];
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
#[DataProvider('sampleRateData')]
#[Group('sampling')]
public function testCountWithSamplingRate(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', $globalSampleRate);
Expand All @@ -82,10 +82,8 @@ public function testCountWithSamplingRate(float $globalSampleRate, float $sample
);
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
#[DataProvider('sampleRateData')]
#[Group('sampling')]
public function testCountWithSamplingRateAndTags(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', $globalSampleRate);
Expand All @@ -107,10 +105,8 @@ public function testIncrement()
);
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
#[DataProvider('sampleRateData')]
#[Group('sampling')]
public function testIncrementWithSamplingRate(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', $globalSampleRate);
Expand All @@ -123,10 +119,8 @@ public function testIncrementWithSamplingRate(float $globalSampleRate, float $sa
);
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
#[DataProvider('sampleRateData')]
#[Group('sampling')]
public function testIncrementWithSamplingRateAndTags(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', $globalSampleRate);
Expand All @@ -148,10 +142,8 @@ public function testDecrement()
);
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
#[DataProvider('sampleRateData')]
#[Group('sampling')]
public function testDecrementWithSamplingRate(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', $globalSampleRate);
Expand All @@ -164,10 +156,8 @@ public function testDecrementWithSamplingRate(float $globalSampleRate, float $sa
);
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
#[DataProvider('sampleRateData')]
#[Group('sampling')]
public function testDecrementWithSamplingRateAndTags(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', $globalSampleRate);
Expand All @@ -190,10 +180,8 @@ public function testCanMeasureTimingWithClosure()
}


/**
* @dataProvider sampleRateData
* @group sampling
*/
#[DataProvider('sampleRateData')]
#[Group('sampling')]
public function testTimingWithSamplingRate(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', $globalSampleRate);
Expand Down Expand Up @@ -232,10 +220,8 @@ public function testEndTimingReturnsTiming()
$this->assertGreaterThanOrEqual($sleep / 1000, $this->client->endTiming($key));
}

/**
* @dataProvider sampleRateData
* @group sampling
*/
#[DataProvider('sampleRateData')]
#[Group('sampling')]
public function testStartEndTimingWithSamplingRate(float $globalSampleRate, float $sampleRate, string $expectedSampleRate)
{
$client = new Client($this->connection, 'test', $globalSampleRate);
Expand Down Expand Up @@ -265,9 +251,7 @@ public function testTimeClosure()
);
}

/**
* @group memory
*/
#[Group('memory')]
public function testMemory()
{
$this->client->memory('foo.bar');
Expand All @@ -277,9 +261,7 @@ public function testMemory()
);
}

/**
* @group memory
*/
#[Group('memory')]
public function testMemoryProfile()
{
$this->client->startMemoryProfile('foo.bar');
Expand Down
Loading