Skip to content

Commit b39ac9d

Browse files
committed
set the minimum PHP version to 8.2
1 parent 94b95c6 commit b39ac9d

12 files changed

+130
-207
lines changed

.github/workflows/run-tests.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
fail-fast: true
1010
matrix:
11-
php: [ 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2 ]
11+
php: [ 8.2 ]
1212
dependency-version: [ prefer-lowest, prefer-stable ]
1313

1414
name: P${{ matrix.php }} - ${{ matrix.dependency-version }}
@@ -33,5 +33,8 @@ jobs:
3333
- name: Install dependencies
3434
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
3535

36+
- name: Run PHPStan
37+
run: php vendor/bin/phpstan analyse --no-progress --no-interaction --no-ansi --memory-limit=-1
38+
3639
- name: Execute tests
3740
run: php vendor/bin/phpunit

Makefile

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
help:
2-
@echo "Please use \`make <target>' where <target> is one of"
3-
@echo " install to setup the dev environment."
4-
@echo " test to perform tests."
5-
@echo " coverage to perform tests with code coverage."
6-
@echo " phpstan to run phpstan"
7-
@echo " infection to run infection"
1+
.DEFAULT_GOAL := help
82

9-
install:
3+
.PHONY: help
4+
help: ## Show help message
5+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
6+
7+
.PHONY: install
8+
install: ## to setup the dev environment.
109
composer install
1110

12-
test:
11+
.PHONY: test
12+
test: ## to perform unit tests.
1313
php vendor/bin/phpunit
1414

15-
coverage:
15+
.PHONY: coverage
16+
coverage: ## to perform unit tests with code coverage.
1617
php -d xdebug.mode=coverage vendor/bin/phpunit --coverage-text
1718

18-
phpstan:
19+
.PHONY: phpstan
20+
phpstan: ## to run PHPStan
1921
php vendor/bin/phpstan analyse
2022

21-
INFECTION_THREADS = $(shell sysctl -n hw.ncpu)
22-
23-
infection:
24-
php vendor/bin/infection --threads=$(INFECTION_THREADS)
23+
.PHONY: infection
24+
infection: ## to run Infection
25+
php vendor/bin/infection --threads=max

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
}
1919
],
2020
"require": {
21-
"php": ">=7.1.0"
21+
"php": "8.2.*"
2222
},
2323
"autoload": {
2424
"psr-4": {
@@ -31,7 +31,7 @@
3131
}
3232
},
3333
"require-dev": {
34-
"phpunit/phpunit": "^7.5 || ^8.5.23 || ^9.0 || ^10.0",
34+
"phpunit/phpunit": "^10.0",
3535
"phpstan/phpstan": "1.10.27"
3636
}
3737
}

phpstan.neon.dist

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ parameters:
33
checkMissingIterableValueType: false
44
paths:
55
- src
6+
- tests

phpunit.xml.dist

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3-
<coverage processUncoveredFiles="true">
4-
<include>
5-
<directory suffix=".php">src/</directory>
6-
</include>
7-
</coverage>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<coverage/>
84
<testsuites>
95
<testsuite name="Tests suite">
106
<directory>tests</directory>
117
</testsuite>
128
</testsuites>
9+
<source>
10+
<include>
11+
<directory suffix=".php">src/</directory>
12+
</include>
13+
</source>
1314
</phpunit>

src/Exception/InvalidFormatException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
final class InvalidFormatException extends InvalidArgumentException implements MagicConstantException
1313
{
14-
public function __construct(MagicConstant $magicConstant, string $format)
14+
public function __construct(MagicConstant $magicConstant, string|int $format)
1515
{
1616
parent::__construct(sprintf('The format `%s` does not exist in `%s`', $format, get_class($magicConstant)));
1717
}

src/Exception/InvalidValueException.php

-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
use InvalidArgumentException;
77

8-
/**
9-
* @codeCoverageIgnore
10-
*/
118
final class InvalidValueException extends InvalidArgumentException implements MagicConstantException
129
{
1310
/**

src/MagicConstant.php

+27-70
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@
1313

1414
abstract class MagicConstant
1515
{
16-
/** @var mixed */
17-
protected $value;
16+
protected mixed $value;
1817

19-
/** @var array */
20-
protected static $cache = [];
18+
/** @var array<class-string<static>, array<mixed>> */
19+
protected static array $cache = [];
2120

22-
/**
23-
* @param mixed $value
24-
*/
25-
final public function __construct($value)
21+
final public function __construct(mixed $value)
2622
{
2723
if ($value instanceof self) {
2824
$value = $value->getValue();
@@ -31,17 +27,13 @@ final public function __construct($value)
3127
$this->setValue($value);
3228
}
3329

34-
/**
35-
* @param string|null $format
36-
* @return mixed
37-
*/
38-
public function getValue(string $format = null)
30+
public function getValue(string|int $format = null): mixed
3931
{
4032
if (empty($format)) {
4133
return $this->value;
4234
}
4335

44-
$values = static::toArray();
36+
$values = self::toArray();
4537

4638
if (!isset($values[$this->getKey()][$format])) {
4739
throw new InvalidFormatException($this, $format);
@@ -55,7 +47,7 @@ public function getValue(string $format = null)
5547
*/
5648
public function getAllFormats(): array
5749
{
58-
$values = static::toArray();
50+
$values = self::toArray();
5951
$instances = array_map(
6052
function ($value) {
6153
return new static($value);
@@ -71,24 +63,22 @@ function ($value) {
7163
*/
7264
public function getAllValues(): array
7365
{
74-
$values = static::toArray();
66+
$values = self::toArray();
7567

7668
return array_values($values[$this->getKey()]);
7769
}
7870

7971
public function getKey(): string
8072
{
81-
return (string)static::search($this->value);
73+
return (string)self::search($this->value);
8274
}
8375

8476
/**
8577
* Returns the current instance format.
86-
*
87-
* @return int|string|null
8878
*/
89-
public function getFormat()
79+
public function getFormat(): int|string|null
9080
{
91-
$values = static::toArray();
81+
$values = self::toArray();
9282

9383
foreach ($values[$this->getKey()] as $format => $value) {
9484
if ($value === $this->value) {
@@ -99,31 +89,22 @@ public function getFormat()
9989
return null;
10090
}
10191

102-
/**
103-
* @return string
104-
*/
105-
public function __toString()
92+
public function __toString(): string
10693
{
10794
return (string)$this->value;
10895
}
10996

110-
/**
111-
* @return MagicConstant
112-
*/
11397
public function normalize(): MagicConstant
11498
{
115-
$array = static::toArray();
99+
$array = self::toArray();
116100
$key = $this->getKey();
117101

118102
$values = array_values($array[$key]);
119103

120104
return new static($values[0]);
121105
}
122106

123-
/**
124-
* @param mixed $value
125-
*/
126-
protected function setValue($value): void
107+
protected function setValue(mixed $value): void
127108
{
128109
if (!static::isValidValue($value)) {
129110
throw new InvalidValueException(static::class, $value);
@@ -143,14 +124,13 @@ final public function equals(?MagicConstant $other): bool
143124
}
144125

145126
$ownKey = $this->getKey();
146-
$otherKey = static::search($other->getValue());
127+
$otherKey = self::search($other->getValue());
147128

148129
return $ownKey === $otherKey;
149130
}
150131

151132
/**
152133
* @param mixed[] $values
153-
* @return bool
154134
*/
155135
public function in(array $values): bool
156136
{
@@ -167,11 +147,7 @@ public function in(array $values): bool
167147
return false;
168148
}
169149

170-
/**
171-
* @param string $format
172-
* @return static
173-
*/
174-
public function toFormat(string $format): self
150+
public function toFormat(string $format): static
175151
{
176152
return new static($this->getValue($format));
177153
}
@@ -181,18 +157,17 @@ public function toFormat(string $format): self
181157
*/
182158
public static function keys(): array
183159
{
184-
return array_keys(static::toArray());
160+
return array_keys(self::toArray());
185161
}
186162

187163
/**
188-
* @param string|null $pattern
189164
* @return static[]
190165
*/
191166
public static function values(string $pattern = null): array
192167
{
193168
$out = [];
194169

195-
foreach (static::toArray() as $key => $values) {
170+
foreach (self::toArray() as $key => $values) {
196171
if (null === $pattern || preg_match($pattern, $key)) {
197172
$out[$key] = new static(reset($values));
198173
}
@@ -229,37 +204,25 @@ private static function toArray(): array
229204
return static::$cache[static::class];
230205
}
231206

232-
/**
233-
* @param mixed $value
234-
* @return bool
235-
*/
236-
public static function isValidValue($value): bool
207+
public static function isValidValue(mixed $value): bool
237208
{
238-
return false !== static::search($value);
209+
return false !== self::search($value);
239210
}
240211

241-
/**
242-
* @param mixed $key
243-
* @return bool
244-
*/
245-
public static function isValidKey($key): bool
212+
public static function isValidKey(mixed $key): bool
246213
{
247-
$array = static::toArray();
214+
$array = self::toArray();
248215

249216
return isset($array[$key]);
250217
}
251218

252-
/**
253-
* @param mixed $value
254-
* @return false|string
255-
*/
256-
private static function search($value)
219+
private static function search(mixed $value): string|false
257220
{
258221
/**
259222
* @var string $constant
260223
* @var array $values
261224
*/
262-
foreach (static::toArray() as $constant => $values) {
225+
foreach (self::toArray() as $constant => $values) {
263226
if (in_array($value, $values, true)) {
264227
return $constant;
265228
}
@@ -268,11 +231,7 @@ private static function search($value)
268231
return false;
269232
}
270233

271-
/**
272-
* @param mixed $value
273-
* @return static|null
274-
*/
275-
public static function tryFrom($value): ?self
234+
public static function tryFrom(mixed $value): ?self
276235
{
277236
try {
278237
return new static($value);
@@ -282,14 +241,12 @@ public static function tryFrom($value): ?self
282241
}
283242

284243
/**
285-
* @param string $name
286244
* @param array $arguments
287-
* @return static
288245
* @throws InvalidKeyException
289246
*/
290-
public static function __callStatic(string $name, array $arguments = [])
247+
public static function __callStatic(string $name, array $arguments = []): static
291248
{
292-
$array = static::toArray();
249+
$array = self::toArray();
293250

294251
if (!isset($array[$name])) {
295252
throw new InvalidKeyException(static::class, $name);

tests/Fixture/AnyValueMagicConstant.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
final class AnyValueMagicConstant extends MagicConstant
1010
{
11-
protected function setValue($value): void
11+
protected function setValue(mixed $value): void
1212
{
1313
$this->value = $value;
1414
}

tests/Fixture/CustomSetValueMagicConstant.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class CustomSetValueMagicConstant extends MagicConstant
99
{
1010
protected const A = 'foo';
1111

12-
protected function setValue($value): void
12+
protected function setValue(mixed $value): void
1313
{
1414
parent::setValue(strtolower($value));
1515
}

0 commit comments

Comments
 (0)