Skip to content

Commit 9af07c2

Browse files
author
alessandro
committed
Improve annotations and code quality
1 parent e794a2d commit 9af07c2

10 files changed

+43
-19
lines changed

src/AggregationRule.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
use Palicao\PhpRedisTimeSeries\Exception\InvalidAggregationException;
77

8-
class AggregationRule
8+
/** @psalm-immutable */
9+
final class AggregationRule
910
{
1011
public const AGG_AVG = 'AVG';
1112
public const AGG_SUM = 'SUM';

src/DateTimeUtils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use DateTimeInterface;
88
use Palicao\PhpRedisTimeSeries\Exception\TimestampParsingException;
99

10-
class DateTimeUtils
10+
final class DateTimeUtils
1111
{
1212
public static function dateTimeFromTimestampWithMs(int $timestamp) : DateTimeInterface
1313
{

src/Exception/InvalidAggregationException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
use RuntimeException;
77

8-
class InvalidAggregationException extends RuntimeException
8+
final class InvalidAggregationException extends RuntimeException
99
{
1010
}

src/Exception/InvalidFilterOperationException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
use InvalidArgumentException;
77

8-
class InvalidFilterOperationException extends InvalidArgumentException
8+
final class InvalidFilterOperationException extends InvalidArgumentException
99
{
1010
}

src/Exception/RedisClientException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
use RuntimeException;
77

8-
class RedisClientException extends RuntimeException
8+
final class RedisClientException extends RuntimeException
99
{
1010
}

src/Exception/TimestampParsingException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
use InvalidArgumentException;
77

8-
class TimestampParsingException extends InvalidArgumentException
8+
final class TimestampParsingException extends InvalidArgumentException
99
{
1010
}

src/Filter.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use Palicao\PhpRedisTimeSeries\Exception\InvalidFilterOperationException;
77

8-
class Filter
8+
final class Filter
99
{
1010
public const OP_EQUALS = 0;
1111
public const OP_NOT_EQUALS = 1;
@@ -23,7 +23,10 @@ class Filter
2323
self::OP_NOT_IN
2424
];
2525

26-
/** @var array */
26+
/**
27+
* @var array
28+
* @psalm-var array<array{string, int, string|array|null}>
29+
*/
2730
private $filters = [];
2831

2932
public function __construct(string $label, string $value)
@@ -69,9 +72,11 @@ public function toRedisParams(): array
6972
foreach ($this->filters as $filter) {
7073
switch ($filter[1]) {
7174
case self::OP_EQUALS:
75+
assert(is_string($filter[2]));
7276
$params[] = $filter[0] . '=' . $filter[2];
7377
break;
7478
case self::OP_NOT_EQUALS:
79+
assert(is_string($filter[2]));
7580
$params[] = $filter[0] . '!=' . $filter[2];
7681
break;
7782
case self::OP_EXISTS:
@@ -81,9 +86,11 @@ public function toRedisParams(): array
8186
$params[] = $filter[0] . '!=';
8287
break;
8388
case self::OP_IN:
89+
assert(is_array($filter[2]));
8490
$params[] = $filter[0] . '=(' . implode(',', $filter[2]) . ')';
8591
break;
8692
case self::OP_NOT_IN:
93+
assert(is_array($filter[2]));
8794
$params[] = $filter[0] . '!=(' . implode(',', $filter[2]) . ')';
8895
break;
8996
}

src/Label.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
namespace Palicao\PhpRedisTimeSeries;
55

6-
class Label
6+
/** @psalm-immutable */
7+
final class Label
78
{
89
/** @var string */
910
private $key;

src/Metadata.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
use DateTimeInterface;
77

8-
class Metadata
8+
/** @psalm-immutable */
9+
final class Metadata
910
{
1011
/** @var DateTimeInterface */
1112
private $lastTimestamp;
@@ -45,8 +46,7 @@ public function __construct(
4546
array $labels = [],
4647
?string $sourceKey = null,
4748
array $rules = []
48-
)
49-
{
49+
) {
5050
$this->lastTimestamp = $lastTimestamp;
5151
$this->retentionTime = $retentionTime;
5252
$this->chunkCount = $chunkCount;
@@ -56,6 +56,16 @@ public function __construct(
5656
$this->rules = $rules;
5757
}
5858

59+
/**
60+
* @param int $lastTimestamp
61+
* @param int $retentionTime
62+
* @param int $chunkCount
63+
* @param int $maxSamplesPerChunk
64+
* @param Label[] $labels
65+
* @param string|null $sourceKey
66+
* @param AggregationRule[] $rules
67+
* @return static
68+
*/
5969
public static function fromRedis(
6070
int $lastTimestamp,
6171
int $retentionTime = 0,

src/Sample.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55

66
use DateTimeInterface;
77

8+
/** @psalm-immutable */
89
class Sample
910
{
1011
/** @var string */
11-
private $key;
12+
protected $key;
1213

1314
/** @var float */
14-
private $value;
15+
protected $value;
1516

1617
/** @var DateTimeInterface|null */
17-
private $dateTime;
18+
protected $dateTime;
1819

1920
public function __construct(string $key, float $value, ?DateTimeInterface $dateTime = null)
2021
{
@@ -44,18 +45,22 @@ public function getDateTime(): ?DateTimeInterface
4445
}
4546

4647
/**
47-
* @return int|string
48+
* @return string
49+
* @psalm-external-mutation-free
4850
*/
49-
public function getTimestampWithMs()
51+
public function getTimestampWithMs(): string
5052
{
5153
if ($this->dateTime === null) {
5254
return '*';
5355
}
54-
return DateTimeUtils::timestampWithMsFromDateTime($this->dateTime);
56+
return (string)DateTimeUtils::timestampWithMsFromDateTime($this->dateTime);
5557
}
5658

59+
/**
60+
* @return string[]
61+
*/
5762
public function toRedisParams(): array
5863
{
59-
return [$this->getKey(), $this->getTimestampWithMs(), $this->getValue()];
64+
return [$this->getKey(), $this->getTimestampWithMs(), (string) $this->getValue()];
6065
}
6166
}

0 commit comments

Comments
 (0)