Skip to content

Commit

Permalink
Add PHPStan (#8)
Browse files Browse the repository at this point in the history
* add phpstan

* make phpstan happy
  • Loading branch information
cosmastech authored Jul 9, 2024
1 parent 7433aef commit a016bb7
Show file tree
Hide file tree
Showing 25 changed files with 216 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ jobs:

- name: Style check
run: composer php-cs-fixer-check

- name: Static Analysis
run: composer static-analysis
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"friendsofphp/php-cs-fixer": "^3.59",
"league/statsd": "^2.0.0",
"cosmastech/psr-logger-spy": "^0.0.2",
"datadog/php-datadogstatsd": "^1.6.1"
"datadog/php-datadogstatsd": "^1.6.1",
"phpstan/phpstan": "^1.11"
},
"suggest": {
"datadog/php-datadogstatsd": "For DataDog stats",
Expand All @@ -40,6 +41,9 @@
"scripts": {
"test": "phpunit tests",
"php-cs-fixer": "./vendor/bin/php-cs-fixer fix ./",
"php-cs-fixer-check": "./vendor/bin/php-cs-fixer check ./"
"php-cs-fixer-check": "./vendor/bin/php-cs-fixer check ./",
"static-analysis": [
"@php vendor/bin/phpstan analyse -c phpstan.neon"
]
}
}
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 7
paths:
- src
- tests
7 changes: 5 additions & 2 deletions src/Adapters/Concerns/HasDefaultTagsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

trait HasDefaultTagsTrait
{
/**
* @var array<mixed, mixed>
*/
protected array $defaultTags = [];

/**
Expand All @@ -23,8 +26,8 @@ public function setDefaultTags(array $defaultTags = []): void
}

/**
* @param array<string, mixed> $tags
* @return array<string, mixed>
* @param array<mixed, mixed> $tags
* @return array<mixed, mixed>
*/
protected function mergeTags(array $tags): array
{
Expand Down
4 changes: 4 additions & 0 deletions src/Adapters/Concerns/TagNormalizerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public function setTagNormalizer(TagNormalizer $tagNormalizer): void
$this->tagNormalizer = $tagNormalizer;
}

/**
* @param array<mixed, mixed> $tags
* @return array<mixed, mixed>
*/
protected function normalizeTags(array $tags): array
{
return $this->tagNormalizer->normalize($tags);
Expand Down
6 changes: 5 additions & 1 deletion src/Adapters/Datadog/DatadogStatsDClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class DatadogStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAw
use HasDefaultTagsTrait;
use TagNormalizerAwareTrait;

/**
* @param DogStatsd $datadogClient
* @param array<mixed, mixed> $defaultTags
*/
public function __construct(protected readonly DogStatsd $datadogClient, array $defaultTags = [])
{
$this->tagNormalizer = new NoopTagNormalizer();
Expand Down Expand Up @@ -90,7 +94,7 @@ public function decrement(array|string $stats, float $sampleRate = 1.0, array $t
);
}

public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, $tags = null): void
public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, array $tags = null): void
{
$this->datadogClient->updateStats(
$stats,
Expand Down
15 changes: 14 additions & 1 deletion src/Adapters/InMemory/InMemoryClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class InMemoryClientAdapter implements StatsDClientAdapter, TagNormalizerAware
protected InMemoryStatsRecord $stats;
protected readonly ClockInterface $clock;

/**
* @param ClockInterface $clock
* @param array<mixed, mixed> $defaultTags
*/
public function __construct(ClockInterface $clock = new Clock(), array $defaultTags = [])
{
$this->clock = $clock;
Expand All @@ -34,6 +38,9 @@ public function __construct(ClockInterface $clock = new Clock(), array $defaultT
$this->setDefaultTags($defaultTags);
}

/**
* @inheritDoc
*/
public function timing(string $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
{
$this->stats->timing[] = new InMemoryTimingRecord(
Expand All @@ -45,6 +52,9 @@ public function timing(string $stat, float $durationMs, float $sampleRate = 1.0,
);
}

/**
* @inheritDoc
*/
public function gauge(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void
{
$this->stats->gauge[] = new InMemoryGaugeRecord(
Expand Down Expand Up @@ -103,7 +113,10 @@ public function decrement(array|string $stats, float $sampleRate = 1.0, array $t
$this->updateStats($stats, $value, $sampleRate, $tags);
}

public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, $tags = null): void
/**
* @inheritDoc
*/
public function updateStats(array|string $stats, int $delta = 1, float $sampleRate = 1.0, array $tags = []): void
{
$stats = (array) $stats;
$now = $this->clock->now();
Expand Down
7 changes: 7 additions & 0 deletions src/Adapters/InMemory/Models/InMemoryCountRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

class InMemoryCountRecord
{
/**
* @param string $stat
* @param int $count
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @param DateTimeImmutable $recordedAt
*/
public function __construct(
public string $stat,
public int $count,
Expand Down
7 changes: 7 additions & 0 deletions src/Adapters/InMemory/Models/InMemoryDistributionRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

readonly class InMemoryDistributionRecord
{
/**
* @param string $stat
* @param float $value
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @param DateTimeImmutable $recordedAt
*/
public function __construct(
public string $stat,
public float $value,
Expand Down
7 changes: 7 additions & 0 deletions src/Adapters/InMemory/Models/InMemoryGaugeRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

readonly class InMemoryGaugeRecord
{
/**
* @param string $stat
* @param float $value
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @param DateTimeImmutable $recordedAt
*/
public function __construct(
public string $stat,
public float $value,
Expand Down
7 changes: 7 additions & 0 deletions src/Adapters/InMemory/Models/InMemoryHistogramRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

readonly class InMemoryHistogramRecord
{
/**
* @param string $stat
* @param float $value
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @param DateTimeImmutable $recordedAt
*/
public function __construct(
public string $stat,
public float $value,
Expand Down
9 changes: 8 additions & 1 deletion src/Adapters/InMemory/Models/InMemorySetRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@

readonly class InMemorySetRecord
{
/**
* @param string $stat
* @param float|string $value
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @param DateTimeImmutable $recordedAt
*/
public function __construct(
public string $stat,
public float $value,
public float|string $value,
public float $sampleRate,
public array $tags,
public DateTimeImmutable $recordedAt,
Expand Down
7 changes: 7 additions & 0 deletions src/Adapters/InMemory/Models/InMemoryTimingRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

readonly class InMemoryTimingRecord
{
/**
* @param string $stat
* @param float $durationMilliseconds
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @param DateTimeImmutable $recordedAt
*/
public function __construct(
public string $stat,
public float $durationMilliseconds,
Expand Down
30 changes: 28 additions & 2 deletions src/Adapters/League/LeagueStatsDClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class LeagueStatsDClientAdapter implements StatsDClientAdapter, TagNormalizerAwa
*/
protected Closure $unavailableStatHandler;

/**
* @param LeagueStatsDClientInterface $leagueStatsDClient
* @param SampleRateSendDeciderInterface $sampleRateSendDecider
* @param array<mixed, mixed> $defaultTags
* @param TagNormalizer $tagNormalizer
*/
public function __construct(
protected readonly LeagueStatsDClientInterface $leagueStatsDClient,
protected readonly SampleRateSendDeciderInterface $sampleRateSendDecider = new SampleRateSendDecider(),
Expand All @@ -37,14 +43,21 @@ public function __construct(
}

/**
* @param array<string, mixed> $config
* @param string $instanceName
* @param SampleRateSendDeciderInterface|null $sampleRateSendDecider
* @param array<mixed, mixed> $defaultTags
* @return self
*
* @throws ConfigurationException
*/
public static function fromConfig(
array $config,
string $instanceName = 'default',
?SampleRateSendDeciderInterface $sampleRateSendDecider = null,
array $defaultTags = []
): static {
): self {
/** @var Client $instance */
$instance = Client::instance($instanceName);
$instance->configure($config);

Expand All @@ -66,6 +79,13 @@ public function setUnavailableStatHandler(Closure $closure): self
return $this;
}

/**
* @param string $stat
* @param float $value
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @return void
*/
protected function handleUnavailableStat(
string $stat,
float $value,
Expand All @@ -85,6 +105,12 @@ protected function getUnavailableStatHandler(): Closure


/**
* @param string $stat
* @param float $durationMs
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @return void
*
* @throws ConnectionException
*/
public function timing(string $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void
Expand Down Expand Up @@ -171,7 +197,7 @@ public function decrement(array|string $stats, float $sampleRate = 1.0, array $t
/**
* @throws ConnectionException
*/
public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, $tags = null): void
public function updateStats(array|string $stats, int $delta = 1, float $sampleRate = 1.0, array $tags = []): void
{
$this->increment(
$stats,
Expand Down
62 changes: 59 additions & 3 deletions src/Adapters/StatsDClientAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,88 @@

interface StatsDClientAdapter
{
/**
* @param string $stat
* @param float $durationMs
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @return void
*/
public function timing(string $stat, float $durationMs, float $sampleRate = 1.0, array $tags = []): void;

/**
* @param string $stat
* @param float $value
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @return void
*/
public function gauge(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void;

/**
* @param string $stat
* @param float $value
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @return void
*/
public function histogram(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void;

/**
* @param string $stat
* @param float $value
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @return void
*/
public function distribution(string $stat, float $value, float $sampleRate = 1.0, array $tags = []): void;

/**
* @param string $stat
* @param float|string $value
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @return void
*/
public function set(string $stat, float|string $value, float $sampleRate = 1.0, array $tags = []): void;

/**
* @param array<int, string>|string $stats
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @param int $value
* @return void
*/
public function increment(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void;

/**
* @param array<int, string>|string $stats
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @param int $value
* @return void
*/
public function decrement(array|string $stats, float $sampleRate = 1.0, array $tags = [], int $value = 1): void;

public function updateStats(array|string $stats, int $delta = 1, $sampleRate = 1.0, $tags = null): void;
/**
* @param array<int, string>|string $stats
* @param int $delta
* @param float $sampleRate
* @param array<mixed, mixed> $tags
* @return void
*/
public function updateStats(array|string $stats, int $delta = 1, float $sampleRate = 1.0, array $tags = []): void;

public function getClient(): mixed;

/**
* @param array<string, mixed> $tags
* @param array<mixed, mixed> $defaultTags
* @return void
*/
public function setDefaultTags(array $defaultTags = []): void;

/**
* @return array<string, mixed>
* @return array<mixed, mixed>
*/
public function getDefaultTags(): array;
}
Loading

0 comments on commit a016bb7

Please sign in to comment.