Skip to content

Commit

Permalink
set the minimum PHP version to 8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Mopolo committed Aug 8, 2023
1 parent 64db80f commit feaf550
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 207 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [ 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2 ]
php: [ 8.2 ]
dependency-version: [ prefer-lowest, prefer-stable ]

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

- name: Run PHPStan
run: php vendor/bin/phpstan analyse --no-progress --no-interaction --no-ansi --memory-limit=-1

- name: Execute tests
run: php vendor/bin/phpunit
31 changes: 16 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " install to setup the dev environment."
@echo " test to perform tests."
@echo " coverage to perform tests with code coverage."
@echo " phpstan to run phpstan"
@echo " infection to run infection"
.DEFAULT_GOAL := help

install:
.PHONY: help
help: ## Show help message
@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)

.PHONY: install
install: ## to setup the dev environment.
composer install

test:
.PHONY: test
test: ## to perform unit tests.
php vendor/bin/phpunit

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

phpstan:
.PHONY: phpstan
phpstan: ## to run PHPStan
php vendor/bin/phpstan analyse

INFECTION_THREADS = $(shell sysctl -n hw.ncpu)

infection:
php vendor/bin/infection --threads=$(INFECTION_THREADS)
.PHONY: infection
infection: ## to run Infection
php vendor/bin/infection --threads=max
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": ">=7.1.0"
"php": "8.2.*"
},
"autoload": {
"psr-4": {
Expand All @@ -31,7 +31,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^7.5 || ^8.5.23 || ^9.0 || ^10.0",
"phpunit/phpunit": "^10.0",
"phpstan/phpstan": "1.10.27"
}
}
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ parameters:
checkMissingIterableValueType: false
paths:
- src
- tests
13 changes: 7 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<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">
<coverage/>
<testsuites>
<testsuite name="Tests suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion src/Exception/InvalidFormatException.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
final class InvalidFormatException extends InvalidArgumentException implements MagicConstantException
{
public function __construct(MagicConstant $magicConstant, string $format)
public function __construct(MagicConstant $magicConstant, string|int $format)
{
parent::__construct(sprintf('The format `%s` does not exist in `%s`', $format, get_class($magicConstant)));
}
Expand Down
3 changes: 0 additions & 3 deletions src/Exception/InvalidValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

use InvalidArgumentException;

/**
* @codeCoverageIgnore
*/
final class InvalidValueException extends InvalidArgumentException implements MagicConstantException
{
/**
Expand Down
97 changes: 27 additions & 70 deletions src/MagicConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@

abstract class MagicConstant
{
/** @var mixed */
protected $value;
protected mixed $value;

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

/**
* @param mixed $value
*/
final public function __construct($value)
final public function __construct(mixed $value)
{
if ($value instanceof self) {
$value = $value->getValue();
Expand All @@ -31,17 +27,13 @@ final public function __construct($value)
$this->setValue($value);
}

/**
* @param string|null $format
* @return mixed
*/
public function getValue(string $format = null)
public function getValue(string|int $format = null): mixed
{
if (empty($format)) {
return $this->value;
}

$values = static::toArray();
$values = self::toArray();

if (!isset($values[$this->getKey()][$format])) {
throw new InvalidFormatException($this, $format);
Expand All @@ -55,7 +47,7 @@ public function getValue(string $format = null)
*/
public function getAllFormats(): array
{
$values = static::toArray();
$values = self::toArray();
$instances = array_map(
function ($value) {
return new static($value);
Expand All @@ -71,24 +63,22 @@ function ($value) {
*/
public function getAllValues(): array
{
$values = static::toArray();
$values = self::toArray();

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

public function getKey(): string
{
return (string)static::search($this->value);
return (string)self::search($this->value);
}

/**
* Returns the current instance format.
*
* @return int|string|null
*/
public function getFormat()
public function getFormat(): int|string|null
{
$values = static::toArray();
$values = self::toArray();

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

/**
* @return string
*/
public function __toString()
public function __toString(): string
{
return (string)$this->value;
}

/**
* @return MagicConstant
*/
public function normalize(): MagicConstant
{
$array = static::toArray();
$array = self::toArray();
$key = $this->getKey();

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

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

/**
* @param mixed $value
*/
protected function setValue($value): void
protected function setValue(mixed $value): void
{
if (!static::isValidValue($value)) {
throw new InvalidValueException(static::class, $value);
Expand All @@ -143,14 +124,13 @@ final public function equals(?MagicConstant $other): bool
}

$ownKey = $this->getKey();
$otherKey = static::search($other->getValue());
$otherKey = self::search($other->getValue());

return $ownKey === $otherKey;
}

/**
* @param mixed[] $values
* @return bool
*/
public function in(array $values): bool
{
Expand All @@ -167,11 +147,7 @@ public function in(array $values): bool
return false;
}

/**
* @param string $format
* @return static
*/
public function toFormat(string $format): self
public function toFormat(string $format): static
{
return new static($this->getValue($format));
}
Expand All @@ -181,18 +157,17 @@ public function toFormat(string $format): self
*/
public static function keys(): array
{
return array_keys(static::toArray());
return array_keys(self::toArray());
}

/**
* @param string|null $pattern
* @return static[]
*/
public static function values(string $pattern = null): array
{
$out = [];

foreach (static::toArray() as $key => $values) {
foreach (self::toArray() as $key => $values) {
if (null === $pattern || preg_match($pattern, $key)) {
$out[$key] = new static(reset($values));
}
Expand Down Expand Up @@ -229,37 +204,25 @@ private static function toArray(): array
return static::$cache[static::class];
}

/**
* @param mixed $value
* @return bool
*/
public static function isValidValue($value): bool
public static function isValidValue(mixed $value): bool
{
return false !== static::search($value);
return false !== self::search($value);
}

/**
* @param mixed $key
* @return bool
*/
public static function isValidKey($key): bool
public static function isValidKey(mixed $key): bool
{
$array = static::toArray();
$array = self::toArray();

return isset($array[$key]);
}

/**
* @param mixed $value
* @return false|string
*/
private static function search($value)
private static function search(mixed $value): string|false
{
/**
* @var string $constant
* @var array $values
*/
foreach (static::toArray() as $constant => $values) {
foreach (self::toArray() as $constant => $values) {
if (in_array($value, $values, true)) {
return $constant;
}
Expand All @@ -268,11 +231,7 @@ private static function search($value)
return false;
}

/**
* @param mixed $value
* @return static|null
*/
public static function tryFrom($value): ?self
public static function tryFrom(mixed $value): ?self
{
try {
return new static($value);
Expand All @@ -282,14 +241,12 @@ public static function tryFrom($value): ?self
}

/**
* @param string $name
* @param array $arguments
* @return static
* @throws InvalidKeyException
*/
public static function __callStatic(string $name, array $arguments = [])
public static function __callStatic(string $name, array $arguments = []): static
{
$array = static::toArray();
$array = self::toArray();

if (!isset($array[$name])) {
throw new InvalidKeyException(static::class, $name);
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/AnyValueMagicConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

final class AnyValueMagicConstant extends MagicConstant
{
protected function setValue($value): void
protected function setValue(mixed $value): void
{
$this->value = $value;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/CustomSetValueMagicConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class CustomSetValueMagicConstant extends MagicConstant
{
protected const A = 'foo';

protected function setValue($value): void
protected function setValue(mixed $value): void
{
parent::setValue(strtolower($value));
}
Expand Down
Loading

0 comments on commit feaf550

Please sign in to comment.