Skip to content

Commit

Permalink
Wrap string being parsed in helper object
Browse files Browse the repository at this point in the history
* Wrap string being parsed in helper object
* Drop PHP 7 support
* PHP 8.1+ updates
  • Loading branch information
gapple authored Oct 2, 2024
1 parent 0447404 commit 6bb34fc
Show file tree
Hide file tree
Showing 21 changed files with 442 additions and 302 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
php-versions: ['8.1', '8.2', '8.3']
experimental: [false]
env:
code-coverage-version: '8.3' # Most recent stable PHP version.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"phpmd": "phpmd src,tests ansi phpmd.ruleset.xml"
},
"require": {
"php": "^7.3 || ^8.0",
"php": "^8.1",
"ext-json": "*"
},
"require-dev": {
Expand Down
9 changes: 1 addition & 8 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ parameters:
paths:
- src
- tests

treatPhpDocTypesAsCertain: false
universalObjectCratesClasses:
- gapple\StructuredFields\Dictionary
- gapple\StructuredFields\Parameters

featureToggles:
readOnlyByPhpDoc: true

ignoreErrors:
-
message: '#Unreachable statement - code above always terminates.#'
path: src/TupleTrait.php
9 changes: 1 addition & 8 deletions src/Bytes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@

class Bytes
{
/**
* @var string
* @readonly
*/
private $value;

public function __construct(string $value)
public function __construct(private readonly string $value)
{
$this->value = $value;
}

public function __toString(): string
Expand Down
9 changes: 1 addition & 8 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@

class Date
{
/**
* @var int
* @readonly
*/
private $value;

public function __construct(int $value)
public function __construct(private readonly int $value)
{
$this->value = $value;
}

public function toInt(): int
Expand Down
8 changes: 4 additions & 4 deletions src/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class Dictionary implements \IteratorAggregate
/**
* @var array<string, TupleInterface|array{mixed, object}>
*/
protected $value = [];
protected array $value = [];

/**
* @param array<mixed> $array
* @return Dictionary
*/
public static function fromArray(array $array): Dictionary
public static function fromArray(array $array): self
{
$dictionary = new self();

Expand All @@ -40,7 +40,7 @@ public static function fromArray(array $array): Dictionary
* @param string $name
* @return TupleInterface|array{mixed, object}|null
*/
public function __get(string $name)
public function __get(string $name): mixed
{
return $this->value[$name] ?? null;
}
Expand All @@ -50,7 +50,7 @@ public function __get(string $name)
* @param TupleInterface|array{mixed, object} $value
* @return void
*/
public function __set(string $name, $value)
public function __set(string $name, mixed $value): void
{
$this->value[$name] = $value;
}
Expand Down
9 changes: 1 addition & 8 deletions src/DisplayString.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@

class DisplayString
{
/**
* @var string
* @readonly
*/
private $value;

public function __construct(string $value)
public function __construct(private readonly string $value)
{
$this->value = $value;
}

public function __toString(): string
Expand Down
14 changes: 5 additions & 9 deletions src/InnerList.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ public function __construct(array $value, ?object $parameters = null)
array_walk($value, [$this, 'validateItemType']);

$this->value = $value;

if (is_null($parameters)) {
$this->parameters = new Parameters();
} else {
$this->parameters = $parameters;
}
$this->parameters = $parameters ?? new Parameters();
}

/**
Expand All @@ -37,8 +32,9 @@ public static function fromArray(array $array): InnerList
array_walk($array, function (&$item) {
if (!$item instanceof TupleInterface) {
$item = new Item($item);
} elseif ($item instanceof InnerList) {
throw new \InvalidArgumentException('InnerList objects cannot be nested');
}
self::validateItemType($item);
});

/** @var TupleInterface[] $array */
Expand All @@ -49,7 +45,7 @@ public static function fromArray(array $array): InnerList
* @param TupleInterface|array{mixed, object} $value
* @return void
*/
private static function validateItemType($value): void
private static function validateItemType(mixed $value): void
{
if (is_object($value)) {
if (!($value instanceof TupleInterface)) {
Expand All @@ -61,7 +57,7 @@ private static function validateItemType($value): void
throw new \InvalidArgumentException('InnerList objects cannot be nested');
}
} elseif (is_array($value)) {
if (count($value) != 2) { // @phpstan-ignore-line
if (count($value) != 2) {
throw new \InvalidArgumentException();
}
} else {
Expand Down
9 changes: 2 additions & 7 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ class Item implements TupleInterface
* @param mixed $value
* @param object|null $parameters
*/
public function __construct($value, ?object $parameters = null)
public function __construct(mixed $value, ?object $parameters = null)
{
$this->value = $value;

if (is_null($parameters)) {
$this->parameters = new Parameters();
} else {
$this->parameters = $parameters;
}
$this->parameters = $parameters ?? new Parameters();
}
}
29 changes: 14 additions & 15 deletions src/OuterList.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OuterList implements \IteratorAggregate, \ArrayAccess
*
* @var array<TupleInterface|array{mixed, object}>
*/
public $value;
public array $value;

/**
* @param array<TupleInterface|array{mixed, object}> $value
Expand All @@ -35,26 +35,25 @@ public function __construct(array $value = [])
*/
public static function fromArray(array $array): OuterList
{
$list = new self();
foreach ($array as $value) {
if (!$value instanceof TupleInterface) {
if (is_array($value)) {
$value = InnerList::fromArray($value);
array_walk($array, function (&$item) {
if (!$item instanceof TupleInterface) {
if (is_array($item)) {
$item = InnerList::fromArray($item);
} else {
$value = new Item($value);
$item = new Item($item);
}
}
$list[] = $value;
}
});

return $list;
/** @var TupleInterface[] $array */
return new self($array);
}

/**
* @param TupleInterface|array{mixed, object} $value
* @return void
*/
private static function validateItemType($value): void
private static function validateItemType(mixed $value): void
{
if (is_object($value)) {
if (!($value instanceof TupleInterface)) {
Expand All @@ -63,7 +62,7 @@ private static function validateItemType($value): void
);
}
} elseif (is_array($value)) {
if (count($value) != 2) { // @phpstan-ignore-line
if (count($value) != 2) {
throw new \InvalidArgumentException();
}
} else {
Expand All @@ -87,10 +86,10 @@ public function offsetExists($offset): bool

/**
* @param int $offset
* @return TupleInterface|array{mixed, object}|null
* @return mixed
* @phpstan-return TupleInterface|array{mixed, object}|null
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
public function offsetGet($offset): mixed
{
return $this->value[$offset] ?? null;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class Parameters implements \IteratorAggregate
/**
* @var array<string, mixed>
*/
protected $value = [];
protected array $value = [];

/**
* @param array<mixed> $array
* @param array<string, mixed> $array
* @return Parameters
*/
public static function fromArray(array $array): Parameters
public static function fromArray(array $array): self
{
$parameters = new self();
$parameters->value = $array;
Expand All @@ -30,7 +30,7 @@ public static function fromArray(array $array): Parameters
* @param string $name
* @return mixed|null
*/
public function __get(string $name)
public function __get(string $name): mixed
{
return $this->value[$name] ?? null;
}
Expand All @@ -40,7 +40,7 @@ public function __get(string $name)
* @param mixed $value
* @return void
*/
public function __set(string $name, $value)
public function __set(string $name, mixed $value): void
{
$this->value[$name] = $value;
}
Expand Down
Loading

0 comments on commit 6bb34fc

Please sign in to comment.