Skip to content

Commit

Permalink
Merge pull request #2 from WyriHaximus/implement-package
Browse files Browse the repository at this point in the history
Implement Package
  • Loading branch information
WyriHaximus committed Dec 22, 2020
2 parents 71fecf3 + fecdf0b commit 34a3909
Show file tree
Hide file tree
Showing 16 changed files with 1,177 additions and 84 deletions.
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -10,10 +10,10 @@
],
"require": {
"php": "^7.4",
"wyrihaximus/metrics": "^1.0"
"wyrihaximus/metrics": "^1.0.2"
},
"require-dev": {
"wyrihaximus/test-utilities": "^2.5 || ^3.0"
"wyrihaximus/test-utilities": "^3.3.2"
},
"config": {
"platform": {
Expand Down
26 changes: 13 additions & 13 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 6 additions & 67 deletions infection.json.dist
Expand Up @@ -10,72 +10,11 @@
},
"mutators": {
"@default": true,
"Increment": {
"ignore": [
"WyriHaximus\\Metrics\\InMemory\\Summary::cleanUpBuckets"
]
},
"For_": {
"ignore": [
"WyriHaximus\\Metrics\\InMemory\\Summary::cleanUpBuckets"
]
},
"FunctionCallRemoval": {
"ignore": [
"WyriHaximus\\Metrics\\InMemory\\Registry\\Summaries::summary",
"WyriHaximus\\Metrics\\InMemory\\Summary::cleanUpBuckets"
]
},
"LessThan": {
"ignore": [
"WyriHaximus\\Metrics\\InMemory\\Summary::cleanUpBuckets"
]
},
"LessThanOrEqualTo": {
"ignore": [
"WyriHaximus\\Metrics\\InMemory\\Summary::observe"
]
},
"LessThanOrEqualToNegotiation": {
"ignore": [
"WyriHaximus\\Metrics\\InMemory\\Summary::observe"
]
},
"LessThanNegotiation": {
"ignore": [
"WyriHaximus\\Metrics\\InMemory\\Summary::cleanUpBuckets"
]
},
"MethodCallRemoval": {
"ignore": [
"WyriHaximus\\Metrics\\InMemory\\Registry\\Summaries::summary",
"WyriHaximus\\Metrics\\InMemory\\Summary::observe"
]
},
"Minus": {
"ignore": [
"WyriHaximus\\Metrics\\InMemory\\Summary::cleanUpBuckets"
]
},
"NotIdentical": {
"ignore": [
"WyriHaximus\\Metrics\\InMemory\\Summary::observe"
]
},
"PublicVisibility": {
"ignore": [
"WyriHaximus\\Metrics\\Factory::createWithConfiguration"
]
},
"UnwrapArrayKeys": {
"ignore": [
"WyriHaximus\\Metrics\\InMemory\\Summary::cleanUpBuckets"
]
},
"UnwrapArrayValues": {
"ignore": [
"WyriHaximus\\Metrics\\InMemory\\Registry\\Summaries::summaries"
]
}
"ArrayItemRemoval": false,
"InstanceOf_": false,
"MethodCallRemoval": false,
"UnwrapArrayMap": false,
"Yield_": false,
"YieldValue": false
}
}
2 changes: 0 additions & 2 deletions phpstan.neon
@@ -1,6 +1,4 @@
parameters:
ignoreErrors:
- '#Parameter \#1 \$it of function iterator_to_array expects Traversable, iterable<WyriHaximus\\Metrics\\Counter|Histogram|Bucket|Label|Gauge|Summary> given.#'
ergebnis:
classesAllowedToBeExtended:
- Exception
Expand Down
110 changes: 110 additions & 0 deletions src/Counter.php
@@ -0,0 +1,110 @@
<?php

declare(strict_types=1);

namespace WyriHaximus\Metrics\LazyRegistry;

use InvalidArgumentException;
use WyriHaximus\Metrics\Counter as CounterInterface;
use WyriHaximus\Metrics\Label;

use function func_get_args;

use const WyriHaximus\Constants\Numeric\ONE;
use const WyriHaximus\Constants\Numeric\ZERO;

final class Counter implements CounterInterface
{
private ?CounterInterface $counter = null;

private string $name;
private string $description;
/** @var array<Label> */
private array $labels;

/** @var array<string|array<mixed>> */
private array $queue = [];

public function __construct(string $name, string $description, Label ...$labels)
{
$this->name = $name;
$this->description = $description;
$this->labels = $labels;
}

public function name(): string
{
return $this->name;
}

public function description(): string
{
return $this->description;
}

public function count(): int
{
if ($this->counter instanceof CounterInterface) {
return $this->counter->count();
}

return ZERO;
}

/**
* @return array<Label>
*/
public function labels(): array
{
return $this->labels;
}

public function incr(): void
{
if ($this->counter instanceof CounterInterface) {
$this->counter->incr();

return;
}

$this->queue[] = [__FUNCTION__, func_get_args()];
}

public function incrBy(int $incr): void
{
if ($this->counter instanceof CounterInterface) {
$this->counter->incrBy($incr);

return;
}

$this->queue[] = [__FUNCTION__, func_get_args()];
}

public function incrTo(int $count): void
{
if ($this->counter instanceof CounterInterface) {
$this->counter->incrTo($count);

return;
}

$this->queue[] = [__FUNCTION__, func_get_args()];
}

public function register(CounterInterface $counter): void
{
if ($this->counter instanceof CounterInterface) {
throw new InvalidArgumentException();
}

$this->counter = $counter;

foreach ($this->queue as $call) {
/** @psalm-suppress PossiblyInvalidMethodCall */
$this->counter->{$call[ZERO]}(...$call[ONE]); /** @phpstan-ignore-line */
}

$this->queue = [];
}
}

0 comments on commit 34a3909

Please sign in to comment.