Skip to content

Commit

Permalink
Leverage PHP 8.1 features
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Jun 14, 2023
1 parent 7cb1d90 commit 3a0d6fe
Show file tree
Hide file tree
Showing 23 changed files with 70 additions and 123 deletions.
2 changes: 1 addition & 1 deletion src/Decoders/ConfigurableDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class ConfigurableDecoder
*
* @param Config $config
*/
public function __construct(private Config $config)
public function __construct(private readonly Config $config)
{
}

Expand Down
30 changes: 15 additions & 15 deletions src/Decoders/DecodedValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@
*/
final class DecodedValue
{
/**
* Instantiate the class.
*
* @param mixed $value
*/
private function __construct(
public bool $succeeded,
public mixed $value = null,
public ?string $error = null,
public ?int $code = null,
public ?Throwable $exception = null,
public ?string $json = null,
) {
}

/**
* Retrieve a successfully decoded value
*
Expand All @@ -47,4 +32,19 @@ public static function failed(Throwable $e, string $json): self
{
return new self(false, null, $e->getMessage(), $e->getCode(), $e, $json);
}

/**
* Instantiate the class.
*
* @param mixed $value
*/
private function __construct(
public readonly bool $succeeded,
public mixed $value = null,
public readonly ?string $error = null,
public readonly ?int $code = null,
public readonly ?Throwable $exception = null,
public readonly ?string $json = null,
) {
}
}
2 changes: 1 addition & 1 deletion src/Decoders/JsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class JsonDecoder extends AbstractDecoder
* @param bool $decodesToArray
* @param int<1, max> $depth
*/
public function __construct(private bool $decodesToArray = true, private int $depth = 512)
public function __construct(private readonly bool $decodesToArray = true, private readonly int $depth = 512)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Decoders/SimdjsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class SimdjsonDecoder extends AbstractDecoder
* @param bool $decodesToArray
* @param int $depth
*/
public function __construct(private bool $decodesToArray = true, private int $depth = 512)
public function __construct(private readonly bool $decodesToArray = true, private readonly int $depth = 512)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/DecodingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class DecodingException extends Exception implements JsonParserException
*
* @param DecodedValue $decoded
*/
public function __construct(public DecodedValue $decoded)
public function __construct(public readonly DecodedValue $decoded)
{
parent::__construct('Decoding error: ' . $decoded->error, (int) $decoded->code);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/IntersectingPointersException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class IntersectingPointersException extends Exception implements JsonParserExcep
* @param Pointer $pointer1
* @param Pointer $pointer2
*/
public function __construct(public Pointer $pointer1, public Pointer $pointer2)
public function __construct(public readonly Pointer $pointer1, public readonly Pointer $pointer2)
{
parent::__construct("The pointers [$pointer1] and [$pointer2] are intersecting");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidPointerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class InvalidPointerException extends Exception implements JsonParserExcep
*
* @param string $pointer
*/
public function __construct(public string $pointer)
public function __construct(public readonly string $pointer)
{
parent::__construct("The string [$pointer] is not a valid JSON pointer");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/SyntaxException.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class SyntaxException extends Exception implements JsonParserException
*
* @param string $value
*/
public function __construct(public string $value)
public function __construct(public readonly string $value)
{
parent::__construct("Syntax error: unexpected '$value'");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/UnsupportedSourceException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class UnsupportedSourceException extends Exception implements JsonParserEx
*
* @param mixed $source
*/
public function __construct(public mixed $source)
public function __construct(public readonly mixed $source)
{
parent::__construct('Unable to load JSON from the provided source');
}
Expand Down
24 changes: 12 additions & 12 deletions src/JsonParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,43 @@ final class JsonParser implements IteratorAggregate
*
* @var Config
*/
private Config $config;
private readonly Config $config;

/**
* The lexer.
*
* @var Lexer
*/
private Lexer $lexer;
private readonly Lexer $lexer;

/**
* The parser.
*
* @var Parser
*/
private Parser $parser;
private readonly Parser $parser;

/**
* Instantiate the class.
* Instantiate the class statically
*
* @param mixed $source
* @return self
*/
public function __construct(mixed $source)
public static function parse(mixed $source): self
{
$this->config = new Config();
$this->lexer = new Lexer(new AnySource($source, $this->config));
$this->parser = new Parser($this->lexer->getIterator(), $this->config);
return new self($source);
}

/**
* Instantiate the class statically
* Instantiate the class.
*
* @param mixed $source
* @return self
*/
public static function parse(mixed $source): self
public function __construct(mixed $source)
{
return new self($source);
$this->config = new Config();
$this->lexer = new Lexer(new AnySource($source, $this->config));
$this->parser = new Parser($this->lexer->getIterator(), $this->config);
}

/**
Expand Down
49 changes: 7 additions & 42 deletions src/Pointers/Pointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,14 @@ final class Pointer implements Stringable
*
* @var string[]
*/
private array $referenceTokens;
public readonly array $referenceTokens;

/**
* The pointer depth.
*
* @var int
*/
private int $depth;

/**
* The pointer callback.
*
* @var Closure
*/
private ?Closure $callback;
public readonly int $depth;

/**
* Whether the pointer was found.
Expand All @@ -52,11 +45,13 @@ final class Pointer implements Stringable
* @param bool $isLazy
* @param Closure|null $callback
*/
public function __construct(private string $pointer, private bool $isLazy = false, Closure $callback = null)
{
public function __construct(
private readonly string $pointer,
public readonly bool $isLazy = false,
private readonly ?Closure $callback = null,
) {
$this->referenceTokens = $this->toReferenceTokens();
$this->depth = count($this->referenceTokens);
$this->callback = $callback;
}

/**
Expand All @@ -76,36 +71,6 @@ private function toReferenceTokens(): array
return array_slice($referenceTokens, 1);
}

/**
* Determine whether the pointer is lazy
*
* @return bool
*/
public function isLazy(): bool
{
return $this->isLazy;
}

/**
* Retrieve the reference tokens
*
* @return string[]
*/
public function referenceTokens(): array
{
return $this->referenceTokens;
}

/**
* Retrieve the JSON pointer depth
*
* @return int
*/
public function depth(): int
{
return $this->depth;
}

/**
* Call the pointer callback
*
Expand Down
2 changes: 1 addition & 1 deletion src/Pointers/Pointers.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function matchTree(Tree $tree): Pointer
$originalTree = $tree->original();

foreach ($this->pointers as $pointer) {
if ($pointer->referenceTokens() == $originalTree) {
if ($pointer->referenceTokens == $originalTree) {
return $this->matching = $pointer;
}

Expand Down
16 changes: 5 additions & 11 deletions src/Sources/Source.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
*/
abstract class Source implements IteratorAggregate
{
/**
* The configuration.
*
* @var Config
*/
protected Config $config;

/**
* The cached size of the JSON source.
*
Expand Down Expand Up @@ -57,14 +50,15 @@ abstract public function matches(): bool;
abstract protected function calculateSize(): ?int;

/**
* Enforce the factory method to instantiate the class.
* Instantiate the class.
*
* @param mixed $source
* @param Config|null $config
*/
final public function __construct(protected mixed $source, Config $config = null)
{
$this->config = $config ?: new Config();
final public function __construct(

Check failure on line 58 in src/Sources/Source.php

View workflow job for this annotation

GitHub Actions / Static analysis

PHPDoc tag @param for parameter $config with type Cerbero\JsonParser\ValueObjects\Config|null is not subtype of native type Cerbero\JsonParser\ValueObjects\Config.
protected readonly mixed $source,
protected readonly Config $config = new Config(),

Check failure on line 60 in src/Sources/Source.php

View workflow job for this annotation

GitHub Actions / Static analysis

PHPDoc type for property Cerbero\JsonParser\Sources\Source::$config with type Cerbero\JsonParser\ValueObjects\Config|null is not subtype of native type Cerbero\JsonParser\ValueObjects\Config.
) {
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Tokens/Comma.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class Comma extends Token
*/
public function mutateState(State $state): void
{
$state->expectsKey = $state->tree()->inObject();
$state->expectsKey = $state->tree->inObject();
$state->expectedToken = $state->expectsKey ? Tokens::SCALAR_STRING : Tokens::VALUE_ANY;
}
}
8 changes: 3 additions & 5 deletions src/Tokens/CompoundBegin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ final class CompoundBegin extends Token
*/
public function mutateState(State $state): void
{
$tree = $state->tree();

if ($this->shouldLazyLoad = $this->shouldLazyLoad && $tree->depth() >= 0) {
$state->expectedToken = $tree->inObject() ? Tokens::AFTER_OBJECT_VALUE : Tokens::AFTER_ARRAY_VALUE;
if ($this->shouldLazyLoad = $this->shouldLazyLoad && $state->tree->depth() >= 0) {
$state->expectedToken = $state->tree->inObject() ? Tokens::AFTER_OBJECT_VALUE : Tokens::AFTER_ARRAY_VALUE;
return;
}

$state->expectsKey = $beginsObject = $this->value == '{';
$state->expectedToken = $beginsObject ? Tokens::AFTER_OBJECT_BEGIN : Tokens::AFTER_ARRAY_BEGIN;
$tree->deepen($beginsObject);
$state->tree->deepen($beginsObject);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Tokens/CompoundEnd.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ final class CompoundEnd extends Token
*/
public function mutateState(State $state): void
{
$state->tree()->emerge();
$state->tree->emerge();

$state->expectedToken = $state->tree()->inObject() ? Tokens::AFTER_OBJECT_VALUE : Tokens::AFTER_ARRAY_VALUE;
$state->expectedToken = $state->tree->inObject() ? Tokens::AFTER_OBJECT_VALUE : Tokens::AFTER_ARRAY_VALUE;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Tokens/Constant.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class Constant extends Token
*/
public function mutateState(State $state): void
{
$state->expectedToken = $state->tree()->inObject() ? Tokens::AFTER_OBJECT_VALUE : Tokens::AFTER_ARRAY_VALUE;
$state->expectedToken = $state->tree->inObject() ? Tokens::AFTER_OBJECT_VALUE : Tokens::AFTER_ARRAY_VALUE;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Tokens/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class Lexer implements IteratorAggregate
*
* @var Progress
*/
private Progress $progress;
private readonly Progress $progress;

/**
* The current position.
Expand All @@ -39,7 +39,7 @@ final class Lexer implements IteratorAggregate
*
* @param Source $source
*/
public function __construct(private Source $source)
public function __construct(private readonly Source $source)
{
$this->progress = new Progress();
}
Expand Down
8 changes: 4 additions & 4 deletions src/Tokens/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class Parser implements IteratorAggregate
*
* @var ConfigurableDecoder
*/
private ConfigurableDecoder $decoder;
private readonly ConfigurableDecoder $decoder;

/**
* Whether the parser is fast-forwarding.
Expand All @@ -40,7 +40,7 @@ final class Parser implements IteratorAggregate
* @param Generator<int, Token> $tokens
* @param Config $config
*/
public function __construct(private Generator $tokens, private Config $config)
public function __construct(private readonly Generator $tokens, private readonly Config $config)
{
$this->decoder = new ConfigurableDecoder($config);
}
Expand All @@ -63,13 +63,13 @@ public function getIterator(): Traversable

$state->mutateByToken($token);

if (!$token->endsChunk() || $state->tree()->isDeep()) {
if (!$token->endsChunk() || $state->tree->isDeep()) {
continue;
}

if ($state->hasBuffer()) {
/** @var string|int $key */
$key = $this->decoder->decode($state->key());
$key = $this->decoder->decode($state->tree->currentKey());
$value = $this->decoder->decode($state->value());

yield $key => $state->callPointer($value, $key);
Expand Down

0 comments on commit 3a0d6fe

Please sign in to comment.