diff --git a/Classes/Collector/AbstractCollector.php b/Classes/Collector/AbstractCollector.php index 25736e2..d55c2c7 100644 --- a/Classes/Collector/AbstractCollector.php +++ b/Classes/Collector/AbstractCollector.php @@ -1,4 +1,5 @@ '/metrics', 'basicAuth' => [] ]; diff --git a/Classes/Renderer.php b/Classes/Renderer.php index ea2e852..d9591dd 100644 --- a/Classes/Renderer.php +++ b/Classes/Renderer.php @@ -1,4 +1,5 @@ getName() . " {$sampleCollection->getType()}"; foreach ($sampleCollection->getSamples() as $sample) { - $lines[] = $this->renderSample($sampleCollection, $sample); + $lines[] = $this->renderSample($sample); } $lines[] = ''; } @@ -49,11 +50,10 @@ public function render(array $sampleCollections): string } /** - * @param SampleCollection $sampleCollection * @param Sample $sample * @return string */ - private function renderSample(SampleCollection $sampleCollection, Sample $sample): string + private function renderSample(Sample $sample): string { $labelStatements = []; foreach ($sample->getLabels() as $labelName => $labelValue) { diff --git a/Classes/Sample.php b/Classes/Sample.php index a7d20aa..0c179cd 100644 --- a/Classes/Sample.php +++ b/Classes/Sample.php @@ -1,4 +1,5 @@ operation = $operation; $this->value = $value; diff --git a/Classes/Storage/GaugeUpdate.php b/Classes/Storage/GaugeUpdate.php index f0f47bf..3b6aea0 100644 --- a/Classes/Storage/GaugeUpdate.php +++ b/Classes/Storage/GaugeUpdate.php @@ -15,19 +15,19 @@ class GaugeUpdate * * @var string */ - private $operation; + private string $operation; /** * A positive number * - * @var int|double|float + * @var int|float */ private $value; /** * @var array */ - private $labels; + private array $labels; /** * @param string $operation @@ -40,7 +40,7 @@ public function __construct(string $operation, $value, array $labels) throw new \InvalidArgumentException(sprintf('gauge update: invalid operation type "%s"', $operation), 1574257299); } if (!is_numeric($value)) { - throw new \InvalidArgumentException(sprintf('invalid value for gauge update, must be a number'), 1574257303); + throw new \InvalidArgumentException('invalid value for gauge update, must be a number', 1574257303); } $this->operation = $operation; $this->value = $value; diff --git a/Classes/Storage/InMemoryStorage.php b/Classes/Storage/InMemoryStorage.php index 63c5758..6ab071d 100644 --- a/Classes/Storage/InMemoryStorage.php +++ b/Classes/Storage/InMemoryStorage.php @@ -25,15 +25,16 @@ class InMemoryStorage extends AbstractStorage /** * @var array */ - private $countersData = []; + private array $countersData = []; /** * @var array */ - private $gaugesData = []; + private array $gaugesData = []; /** * @return SampleCollection[] + * @throws \Exception */ public function collect(): array { @@ -79,6 +80,7 @@ public function registerCollector(AbstractCollector $collector): void * @param Counter $counter * @param CounterUpdate $update * @return void + * @throws \Exception */ public function updateCounter(Counter $counter, CounterUpdate $update): void { @@ -102,6 +104,7 @@ public function updateCounter(Counter $counter, CounterUpdate $update): void * @param Gauge $gauge * @param GaugeUpdate $update * @return void + * @throws \Exception */ public function updateGauge(Gauge $gauge, GaugeUpdate $update): void { @@ -130,6 +133,7 @@ public function updateGauge(Gauge $gauge, GaugeUpdate $update): void /** * @param array $collectorsData * @return SampleCollection[] + * @throws \Exception */ private function prepareCollections(array $collectorsData): array { diff --git a/Classes/Storage/RedisStorage.php b/Classes/Storage/RedisStorage.php index aeecb2c..0211cad 100644 --- a/Classes/Storage/RedisStorage.php +++ b/Classes/Storage/RedisStorage.php @@ -31,57 +31,57 @@ class RedisStorage extends AbstractStorage /** * @var Counter[] */ - protected $counters; + protected array $counters = []; /** * @var Gauge[] */ - protected $gauges; + protected array $gauges = []; /** * @var Predis\Client */ - protected $redis; + protected Predis\Client $redis; /** * @var string */ - protected $hostname = '127.0.0.1'; + protected string $hostname = '127.0.0.1'; /** - * @var integer + * @var int */ - protected $port = 6379; + protected int $port = 6379; /** * @var array */ - protected $sentinels = []; + protected array $sentinels = []; /** * @var string */ - protected $service = 'mymaster'; + protected string $service = 'mymaster'; /** - * @var integer + * @var int */ - protected $database = 0; + protected int $database = 0; /** * @var string */ - protected $password = ''; + protected string $password = ''; /** * @var string */ - protected $keyPrefix = 'flownative_prometheus'; + protected string $keyPrefix = 'flownative_prometheus'; /** - * @var boolean + * @var bool */ - protected $ignoreConnectionErrors = false; + protected bool $ignoreConnectionErrors = false; /** * @param array $options @@ -321,13 +321,4 @@ private function getRedisClient(): Predis\Client } return new Predis\Client($connectionParameters, $options); } - - /** - * @param array $data - * @return string - */ - private function toMetricKey(array $data): string - { - return implode(':', [$this->keyPrefix, $data['type'], $data['name']]); - } } diff --git a/Tests/Unit/CollectorRegistryTest.php b/Tests/Unit/CollectorRegistryTest.php index e74f628..f348eb0 100644 --- a/Tests/Unit/CollectorRegistryTest.php +++ b/Tests/Unit/CollectorRegistryTest.php @@ -56,6 +56,7 @@ public function registerManyRegistersCollectorsDefinedInArray(array $collectorCo /** * @test * @throws InvalidCollectorTypeException + * @throws \Exception */ public function registerAlsoRegistersCollectorAtStorage(): void { @@ -97,7 +98,7 @@ public function getSampleCollectionsCollectsSamplesFromStorage(): void $registry->register('flownative_prometheus_test_calls_total', Counter::TYPE, 'a test call counter', ['tests', 'counter']); $counterA = $registry->getCounter('flownative_prometheus_test_calls_total'); - $counterA->inc(1); + $counterA->inc(); $sampleCollections = $registry->collect(); diff --git a/Tests/Unit/CounterTest.php b/Tests/Unit/CounterTest.php index 840fa9c..7ac26bc 100644 --- a/Tests/Unit/CounterTest.php +++ b/Tests/Unit/CounterTest.php @@ -45,6 +45,7 @@ public function getIdentifierReturnsGeneratedString(): void /** * @test + * @throws \Exception */ public function incIncreasesCounterByOne(): void { @@ -77,6 +78,7 @@ public function increaseValues(): array * @param $firstValue * @param $secondValue * @param $expectedResult + * @throws \Exception */ public function incIncreasesCounterByGivenValue($firstValue, $secondValue, $expectedResult): void { @@ -94,6 +96,7 @@ public function incIncreasesCounterByGivenValue($firstValue, $secondValue, $expe /** * @test + * @throws \Exception */ public function incSupportsLabels(): void { diff --git a/Tests/Unit/DefaultCollectorRegistryTest.php b/Tests/Unit/DefaultCollectorRegistryTest.php index c6a17d4..92720af 100644 --- a/Tests/Unit/DefaultCollectorRegistryTest.php +++ b/Tests/Unit/DefaultCollectorRegistryTest.php @@ -8,7 +8,6 @@ * (c) Flownative GmbH - www.flownative.com */ -use Flownative\Prometheus\Collector\Counter; use Flownative\Prometheus\DefaultCollectorRegistry; use Flownative\Prometheus\Exception\InvalidCollectorTypeException; use Flownative\Prometheus\Storage\InMemoryStorage; @@ -35,7 +34,6 @@ public function collectorsDefinedInSettingsAreRegisteredAutomatically(): void $registry->initializeObject(); $counter = $registry->getCounter('flownative_test_hits_total'); - self::assertInstanceOf(Counter::class, $counter); self::assertSame('flownative_test_hits_total', $counter->getName()); } } diff --git a/Tests/Unit/GaugeTest.php b/Tests/Unit/GaugeTest.php index 66a5789..a2cbcd5 100644 --- a/Tests/Unit/GaugeTest.php +++ b/Tests/Unit/GaugeTest.php @@ -45,6 +45,7 @@ public function getIdentifierReturnsGeneratedString(): void /** * @test + * @throws \Exception */ public function incIncreasesGaugeByOne(): void { @@ -78,6 +79,7 @@ public function increaseValues(): array * @param $firstValue * @param $secondValue * @param $expectedResult + * @throws \Exception */ public function incIncreasesGaugeByGivenValue($firstValue, $secondValue, $expectedResult): void { @@ -95,6 +97,7 @@ public function incIncreasesGaugeByGivenValue($firstValue, $secondValue, $expect /** * @test + * @throws \Exception */ public function decDecreasesGaugeByOne(): void { @@ -112,6 +115,7 @@ public function decDecreasesGaugeByOne(): void /** * @test + * @throws \Exception */ public function setSetsTheGaugesValue(): void { @@ -129,6 +133,7 @@ public function setSetsTheGaugesValue(): void /** * @test + * @throws \Exception */ public function setToCurrentTimeSetsValueToCurrentUnixTimestamp(): void { @@ -150,6 +155,7 @@ public function setToCurrentTimeSetsValueToCurrentUnixTimestamp(): void /** * @test + * @throws \Exception */ public function incSupportsLabels(): void { diff --git a/Tests/Unit/Http/DummyRequestHandler.php b/Tests/Unit/Http/DummyRequestHandler.php index 8a6390c..b31f5cd 100644 --- a/Tests/Unit/Http/DummyRequestHandler.php +++ b/Tests/Unit/Http/DummyRequestHandler.php @@ -18,7 +18,7 @@ class DummyRequestHandler implements RequestHandlerInterface /** * @var bool */ - protected $handleCalled = false; + protected bool $handleCalled = false; /** * @param ServerRequestInterface $request diff --git a/Tests/Unit/Http/MetricsExporterComponentTest.php b/Tests/Unit/Http/MetricsExporterComponentTest.php index 36e489a..dd63a98 100644 --- a/Tests/Unit/Http/MetricsExporterComponentTest.php +++ b/Tests/Unit/Http/MetricsExporterComponentTest.php @@ -13,9 +13,9 @@ use Flownative\Prometheus\Exception\InvalidCollectorTypeException; use Flownative\Prometheus\Http\MetricsExporterMiddleware; use Flownative\Prometheus\Storage\InMemoryStorage; -use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\ServerRequest; use GuzzleHttp\Psr7\Uri; +use Neos\Flow\Security\Exception\AccessDeniedException; use Neos\Flow\Tests\UnitTestCase; class MetricsExporterMiddlewareTest extends UnitTestCase @@ -25,6 +25,7 @@ class MetricsExporterMiddlewareTest extends UnitTestCase */ public function setUp(): void { + parent::setUp(); putenv('FLOWNATIVE_PROMETHEUS_ENABLE=true'); } @@ -49,6 +50,7 @@ public function middlewareIgnoresRequestsWithNonMatchingPath(): void /** * @test * @throws InvalidCollectorTypeException + * @throws AccessDeniedException */ public function middlewareRendersMetrics(): void { @@ -77,6 +79,7 @@ public function middlewareRendersMetrics(): void /** * @test + * @throws AccessDeniedException */ public function middlewareRendersCommentIfNoCollectorsAreRegistered(): void { @@ -117,6 +120,7 @@ public function middlewareRendersCommentIfNoMetricsExist(): void /** * @test + * @throws AccessDeniedException */ public function telemetryPathIsConfigurable(): void { @@ -137,6 +141,7 @@ public function telemetryPathIsConfigurable(): void /** * @test + * @throws AccessDeniedException */ public function middlewareRequiresHttpBasicAuthIfConfigured(): void { @@ -160,6 +165,7 @@ public function middlewareRequiresHttpBasicAuthIfConfigured(): void /** * @test + * @throws AccessDeniedException */ public function middlewareAcceptsCorrectHttpBasicAuthIfConfigured(): void { diff --git a/Tests/Unit/Storage/AbstractStorageTest.php b/Tests/Unit/Storage/AbstractStorageTest.php index f74ffc6..547bf27 100644 --- a/Tests/Unit/Storage/AbstractStorageTest.php +++ b/Tests/Unit/Storage/AbstractStorageTest.php @@ -22,7 +22,7 @@ abstract class AbstractStorageTest extends UnitTestCase /** * @var StorageInterface */ - protected $storage; + protected StorageInterface $storage; /** * @test @@ -116,6 +116,7 @@ public function updateCounterSortsSamplesByLabels(): void /** * @test + * @throws \Exception */ public function updateCounterResetsCounterIfSetOperationIsSpecified(): void { diff --git a/Tests/Unit/Storage/RedisStorageTest.php b/Tests/Unit/Storage/RedisStorageTest.php index 8df498b..40134ff 100644 --- a/Tests/Unit/Storage/RedisStorageTest.php +++ b/Tests/Unit/Storage/RedisStorageTest.php @@ -21,9 +21,9 @@ public function setUp(): void { parent::setUp(); $this->storage = new RedisStorage([ - 'hostname' => getenv('REDIS_HOST') ?? '127.0.0.1', - 'port' => getenv('REDIS_PORT') ?? '6379', - 'password' => getenv('REDIS_PASSWORD') ?? '', + 'hostname' => getenv('REDIS_HOST') ?: '127.0.0.1', + 'port' => getenv('REDIS_PORT') ?: '6379', + 'password' => getenv('REDIS_PASSWORD') ?: '', ]); $this->storage->flush();