Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8.0 syntax #39

Merged
merged 5 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
matrix:
php-version:
- 7.2
- 8.0

steps:
- name: Checkout
Expand Down Expand Up @@ -54,7 +54,7 @@ jobs:
strategy:
matrix:
php-version:
- 7.2
- 8.0

steps:
- name: Checkout
Expand Down Expand Up @@ -91,10 +91,10 @@ jobs:
strategy:
matrix:
php-version:
- 7.2
- 7.3
- 7.4
- 8.0
- 8.1
- 8.2
- 8.3

steps:
- name: Checkout
Expand Down Expand Up @@ -131,7 +131,7 @@ jobs:
strategy:
matrix:
php-version:
- 7.2
- 8.0

steps:
- name: Checkout commit
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"sort-packages": true
},
"require": {
"php": "^7.2 || ^8.0",
"php": "^8.0",
"beberlei/assert": "^3.2"
},
"require-dev": {
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ parameters:
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
- '~Method [a-zA-Z0-9\\_]+::from[a-zA-Z0-9_]*\(\) has no return typehint specified.~'
- '~Method [a-zA-Z0-9\\_]+OperationConstraint::[a-zA-Z0-9_]*\(\) has no return typehint specified.~'
- '~Method [a-zA-Z0-9\\_]+OperationConstraint::assert\(\) should return bool but return statement is missing.~'
- '~Method [a-zA-Z0-9\\_]+OperationConstraintParser::buildConstraint\(\) should return [a-zA-Z0-9\\_]+OperationConstraint but return statement is missing.~'

includes:
Expand Down
8 changes: 3 additions & 5 deletions src/Comparison/Constraint/CompositeConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ class CompositeConstraint implements Constraint
public const OPERATOR_AND = 'AND';
public const OPERATOR_OR = 'OR';

/** @var string */
protected $operator;
protected string $operator;

/** @var Constraint[] */
protected $constraints;
protected array $constraints;

final public function __construct(string $operator, Constraint $constraint, Constraint ...$constraints)
{
Expand Down Expand Up @@ -60,7 +58,7 @@ public function assert(Version $version): bool
protected function assertAnd(Version $version): bool
{
foreach ($this->constraints as $constraint) {
if (! $constraint->assert($version)) {
if (!$constraint->assert($version)) {
return false;
}
}
Expand Down
51 changes: 20 additions & 31 deletions src/Comparison/Constraint/OperationConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ class OperationConstraint implements Constraint
public const OPERATOR_LT = '<';
public const OPERATOR_LTE = '<=';

/** @var string */
protected $operator;
protected string $operator;

/** @var Version */
protected $operand;
protected Version $operand;

final public function __construct(string $operator, Version $operand)
{
Expand All @@ -31,41 +29,37 @@ final public function __construct(string $operator, Version $operand)
$this->operand = $operand;
}

public static function equalTo(Version $operand)
public static function equalTo(Version $operand): static
{
return new static(self::OPERATOR_EQ, $operand);
}

public static function notEqualTo(Version $operand)
public static function notEqualTo(Version $operand): static
{
return new static(self::OPERATOR_NEQ, $operand);
}

public static function greaterThan(Version $operand)
public static function greaterThan(Version $operand): static
{
return new static(self::OPERATOR_GT, $operand);
}

public static function greaterOrEqualTo(Version $operand)
public static function greaterOrEqualTo(Version $operand): static
{
return new static(self::OPERATOR_GTE, $operand);
}

public static function lessThan(Version $operand)
public static function lessThan(Version $operand): static
{
return new static(self::OPERATOR_LT, $operand);
}

public static function lessOrEqualTo(Version $operand)
public static function lessOrEqualTo(Version $operand): static
{
return new static(self::OPERATOR_LTE, $operand);
}

/**
* @param string $constraintString
* @return OperationConstraint|CompositeConstraint
*/
public static function fromString(string $constraintString)
public static function fromString(string $constraintString): CompositeConstraint|OperationConstraint
{
static $parser = null;

Expand All @@ -88,31 +82,26 @@ public function getOperand(): Version

public function assert(Version $version): bool
{
switch ($this->operator) {
case self::OPERATOR_EQ:
return $version->isEqualTo($this->operand);
case self::OPERATOR_NEQ:
return !$version->isEqualTo($this->operand);
case self::OPERATOR_GT:
return $version->isGreaterThan($this->operand);
case self::OPERATOR_GTE:
return $version->isGreaterOrEqualTo($this->operand);
case self::OPERATOR_LT:
return $version->isLessThan($this->operand);
case self::OPERATOR_LTE:
return $version->isLessOrEqualTo($this->operand);
}
return match ($this->operator) {
self::OPERATOR_EQ => $version->isEqualTo($this->operand),
self::OPERATOR_NEQ => !$version->isEqualTo($this->operand),
self::OPERATOR_GT => $version->isGreaterThan($this->operand),
self::OPERATOR_GTE => $version->isGreaterOrEqualTo($this->operand),
self::OPERATOR_LT => $version->isLessThan($this->operand),
self::OPERATOR_LTE => $version->isLessOrEqualTo($this->operand),
default => throw InvalidOperationConstraint::unsupportedOperator($this->operator),
};
}

protected function validateOperator(string $operator): void
{
static $validOperators = null;

if (null === $validOperators) {
if ($validOperators === null) {
$validOperators = (new ReflectionClass($this))->getConstants();
}

if (! in_array($operator, $validOperators, true)) {
if (!in_array($operator, $validOperators, true)) {
throw InvalidOperationConstraint::unsupportedOperator($operator);
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/Comparison/Constraint/OperationConstraintParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,24 @@ class OperationConstraintParser
{
public const OPERATOR_OR = '||';

/** @var string */
protected $constraintString;
protected string $constraintString;

/** @var array */
protected $constraintParts = [];
protected array $constraintParts = [];

/**
* @param string $constraintString
* @return OperationConstraint|CompositeConstraint
*/
public function parse(string $constraintString)
{
$constraintString = trim($constraintString);

if ('' === $constraintString) {
if ($constraintString === '') {
throw InvalidConstraintString::empty();
}

$this->constraintString = $constraintString;

if (! $this->isMultiPartConstraint()) {
if (!$this->isMultiPartConstraint()) {
return $this->buildConstraint($this->constraintString);
}

Expand All @@ -43,7 +40,7 @@ public function parse(string $constraintString)

protected function isMultiPartConstraint(): bool
{
return (false !== strpos($this->constraintString, ' '));
return str_contains($this->constraintString, ' ');
}

protected function splitConstraintParts(): void
Expand All @@ -65,7 +62,7 @@ protected function buildConstraint(string $constraintPart): OperationConstraint
$operator ?: OperationConstraint::OPERATOR_EQ,
Version::fromString($operandString)
);
} catch (VersionException $ex) {
} catch (VersionException) {
$this->error();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Comparison/Exception/InvalidCompositeConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use InvalidArgumentException;
use Version\Exception\VersionException;

class InvalidCompositeConstraint extends InvalidArgumentException implements VersionException
class InvalidCompositeConstraint extends InvalidArgumentException implements VersionComparisonException
{
public static function unsupportedOperator(string $operator): self
{
Expand Down
6 changes: 3 additions & 3 deletions src/Comparison/Exception/InvalidConstraintString.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use InvalidArgumentException;
use Version\Exception\VersionException;

class InvalidConstraintString extends InvalidArgumentException implements VersionException
class InvalidConstraintString extends InvalidArgumentException implements VersionComparisonException
{
public static function empty(): self
{
return new self('Comparision constraint string must not be empty');
return new self('Comparison constraint string must not be empty');
}

public static function notParsable(string $constraintString): self
{
return new self(sprintf(
"Comparision constraint string: '%s' is not valid and cannot be parsed",
"Comparison constraint string: '%s' is not valid and cannot be parsed",
$constraintString
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Comparison/Exception/InvalidOperationConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use InvalidArgumentException;
use Version\Exception\VersionException;

class InvalidOperationConstraint extends InvalidArgumentException implements VersionException
class InvalidOperationConstraint extends InvalidArgumentException implements VersionComparisonException
{
public static function unsupportedOperator(string $operator): self
{
Expand Down
4 changes: 2 additions & 2 deletions src/Comparison/Exception/VersionComparisonException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Version\Comparison\Exception;

use Throwable;
use Version\Exception\VersionException;

interface VersionComparisonException extends Throwable
interface VersionComparisonException extends VersionException
{
}
3 changes: 1 addition & 2 deletions src/Exception/InvalidVersionString.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

class InvalidVersionString extends InvalidArgumentException implements VersionException
{
/** @var string */
protected $versionString;
protected string $versionString;

public static function notParsable(string $versionString): self
{
Expand Down
9 changes: 4 additions & 5 deletions src/Extension/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ abstract class Extension
{
protected const IDENTIFIERS_SEPARATOR = '.';

/** @var array */
private $identifiers;
private array $identifiers;

final protected function __construct(array $identifiers)
{
Expand All @@ -25,17 +24,17 @@ final protected function __construct(array $identifiers)
*/
abstract protected function validate(array $identifiers): void;

public static function from(string $identifier, string ...$identifiers)
public static function from(string $identifier, string ...$identifiers): static
{
return new static(func_get_args());
}

public static function fromArray(array $identifiers)
public static function fromArray(array $identifiers): static
{
return new static($identifiers);
}

public static function fromString(string $extension)
public static function fromString(string $extension): static
{
return new static(explode(self::IDENTIFIERS_SEPARATOR, trim($extension)));
}
Expand Down
Loading
Loading