From 644fe90245352c51a606d4a27cad0be6a5b262b3 Mon Sep 17 00:00:00 2001 From: Filip Halaxa Date: Fri, 22 Nov 2024 19:26:04 +0100 Subject: [PATCH] Fix build --- .php-cs-fixer.dist.php | 2 ++ composer.json | 3 ++- src/FacadeTrait.php | 9 +++++++-- src/Items.php | 2 -- src/ItemsOptions.php | 2 -- src/Parser.php | 25 +++++++------------------ src/RecursiveItems.php | 9 ++++++--- src/ResumableIteratorAggregateProxy.php | 25 ++++++++++++++++++++++--- src/functions.php | 16 ++++++++++++++++ test/JsonMachineTest/ParserTest.php | 4 ++-- 10 files changed, 64 insertions(+), 33 deletions(-) create mode 100644 src/functions.php diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 410916d..b3eba16 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -15,6 +15,8 @@ return $config->setRules([ 'visibility_required' => false, 'php_unit_test_class_requires_covers' => true, 'declare_strict_types' => true, + 'phpdoc_to_comment' => false, // todo remove when we move to GeneratorAggregate + ]) ->setFinder($finder) ; diff --git a/composer.json b/composer.json index 9ed9f28..ce27b67 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,8 @@ }, "autoload": { "psr-4": {"JsonMachine\\": "src/"}, - "exclude-from-classmap": ["src/autoloader.php"] + "exclude-from-classmap": ["src/autoloader.php"], + "files": ["src/functions.php"] }, "autoload-dev": { "psr-4": {"JsonMachineTest\\": "test/JsonMachineTest"} diff --git a/src/FacadeTrait.php b/src/FacadeTrait.php index c6e2b4f..ac50177 100644 --- a/src/FacadeTrait.php +++ b/src/FacadeTrait.php @@ -6,6 +6,7 @@ namespace JsonMachine; use JsonMachine\Exception\InvalidArgumentException; use JsonMachine\JsonDecoder\ExtJsonDecoder; +use LogicException; trait FacadeTrait { @@ -46,11 +47,15 @@ trait FacadeTrait } /** - * @throws Exception\JsonMachineException + * Returns JSON bytes read so far. */ public function getPosition() { - return $this->parser->getPosition(); + if ($this->parser instanceof PositionAware) { + return $this->parser->getPosition(); + } + + throw new LogicException('getPosition() may only be called on PositionAware'); } /** diff --git a/src/Items.php b/src/Items.php index 20cd832..ca183c6 100644 --- a/src/Items.php +++ b/src/Items.php @@ -68,8 +68,6 @@ final class Items implements \IteratorAggregate, PositionAware /** * @return \Generator - * - * @throws Exception\PathNotFoundException */ #[\ReturnTypeWillChange] public function getIterator() diff --git a/src/ItemsOptions.php b/src/ItemsOptions.php index db4698c..e21a86c 100644 --- a/src/ItemsOptions.php +++ b/src/ItemsOptions.php @@ -56,10 +56,8 @@ class ItemsOptions extends \ArrayObject private function opt_pointer($pointer) { if (is_array($pointer)) { - /** @phpstan-ignore expr.resultUnused */ (function (string ...$p) {})(...$pointer); } else { - /** @phpstan-ignore expr.resultUnused */ (function (string $p) {})($pointer); } diff --git a/src/Parser.php b/src/Parser.php index d99d744..e0315d1 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -15,6 +15,7 @@ use JsonMachine\Exception\UnexpectedEndSyntaxErrorException; use JsonMachine\JsonDecoder\ExtJsonDecoder; use JsonMachine\JsonDecoder\ItemDecoder; use JsonMachine\JsonDecoder\StringOnlyDecoder; +use LogicException; use Traversable; class Parser implements \IteratorAggregate, PositionAware @@ -314,18 +315,6 @@ class Parser implements \IteratorAggregate, PositionAware $this->currentPath = null; } - /** - * @return Generator - */ - private function remainingTokens() - { - $iterator = $this->tokensIterator; - while ($iterator->valid()) { - yield $iterator->current(); - $iterator->next(); - } - } - public function ensureIterationComplete(): void { $generator = $this->getIterator(); @@ -429,20 +418,20 @@ class Parser implements \IteratorAggregate, PositionAware { throw new $exception( $msg." '".$token."'", - $this->tokens instanceof PositionAware ? $this->tokens->getPosition() : '' + $this->getPosition() ); } /** * Returns JSON bytes read so far. - * - * @return int - * - * @throws JsonMachineException */ public function getPosition() { - return $this->tokens->getPosition(); + if ($this->tokens instanceof PositionAware) { + return $this->tokens->getPosition(); + } + + throw new LogicException('getPosition() may only be called on PositionAware'); } private static function jsonPointerToPath(string $jsonPointer): array diff --git a/src/RecursiveItems.php b/src/RecursiveItems.php index 9c1381c..bee99df 100644 --- a/src/RecursiveItems.php +++ b/src/RecursiveItems.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace JsonMachine; +use Exception; +use Generator; use Iterator; use IteratorAggregate; use JsonMachine\Exception\InvalidArgumentException; @@ -22,7 +24,7 @@ final class RecursiveItems implements \RecursiveIterator, PositionAware /** @var ItemsOptions */ private $options; - /** @var Iterator */ + /** @var Generator|Iterator */ private $parserIterator; public function __construct(IteratorAggregate $parser, ?ItemsOptions $options = null) @@ -131,7 +133,7 @@ final class RecursiveItems implements \RecursiveIterator, PositionAware public function rewind(): void { - $this->parserIterator = $this->parser->getIterator(); + $this->parserIterator = toIterator($this->parser->getIterator()); $this->parserIterator->rewind(); } @@ -187,8 +189,9 @@ final class RecursiveItems implements \RecursiveIterator, PositionAware public function toArray(): array { try { + /** @throws Exception */ $this->rewind(); - } catch (\Exception $e) { + } catch (Exception $e) { if (false !== strpos($e->getMessage(), 'generator')) { throw new JsonMachineException( 'Method toArray() can only be called before any items in the collection have been accessed.' diff --git a/src/ResumableIteratorAggregateProxy.php b/src/ResumableIteratorAggregateProxy.php index 607068f..dee5f7b 100644 --- a/src/ResumableIteratorAggregateProxy.php +++ b/src/ResumableIteratorAggregateProxy.php @@ -4,26 +4,33 @@ declare(strict_types=1); namespace JsonMachine; +use InvalidArgumentException; use IteratorAggregate; +use LogicException; /** * Allows to resume iteration of the inner IteratorAggregate via foreach, which would be otherwise impossible as * foreach implicitly calls reset(). This Iterator does not pass the reset() call to the inner Iterator thus enabling * to follow up on a previous iteation. */ -class ResumableIteratorAggregateProxy implements IteratorAggregate +class ResumableIteratorAggregateProxy implements IteratorAggregate, PositionAware { /** @var IteratorAggregate */ private $iteratorAggregate; - public function __construct(IteratorAggregate $iteratorAggregate) + public function __construct(\Traversable $iteratorAggregate) { + // todo remove when the whole system moves to GeneratorAggregate + if ( ! $iteratorAggregate instanceof IteratorAggregate) { + throw new InvalidArgumentException('$iteratorAggregate must be an instance of IteratorAggregate'); + } + $this->iteratorAggregate = $iteratorAggregate; } public function getIterator(): \Traversable { - $iterator = $this->iteratorAggregate->getIterator(); + $iterator = toIterator($this->iteratorAggregate->getIterator()); while ($iterator->valid()) { yield $iterator->key() => $iterator->current(); $iterator->next(); @@ -34,4 +41,16 @@ class ResumableIteratorAggregateProxy implements IteratorAggregate { return $this->iteratorAggregate->$name(...$arguments); } + + /** + * Returns JSON bytes read so far. + */ + public function getPosition() + { + if ($this->iteratorAggregate instanceof PositionAware) { + return $this->iteratorAggregate->getPosition(); + } + + throw new LogicException('getPosition() may only be called on PositionAware'); + } } diff --git a/src/functions.php b/src/functions.php new file mode 100644 index 0000000..dd45953 --- /dev/null +++ b/src/functions.php @@ -0,0 +1,16 @@ +getIterator()); + } + + if ($traversable instanceof Iterator) { + return $traversable; + } + + throw new \LogicException('Cannot turn Traversable into Iterator'); +} diff --git a/test/JsonMachineTest/ParserTest.php b/test/JsonMachineTest/ParserTest.php index c11adcd..29666ae 100644 --- a/test/JsonMachineTest/ParserTest.php +++ b/test/JsonMachineTest/ParserTest.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace JsonMachineTest; -use Error; use JsonMachine\Exception\JsonMachineException; use JsonMachine\Exception\PathNotFoundException; use JsonMachine\Exception\SyntaxErrorException; @@ -14,6 +13,7 @@ use JsonMachine\Parser; use JsonMachine\StringChunks; use JsonMachine\Tokens; use JsonMachine\TokensWithDebugging; +use LogicException; use Traversable; /** @@ -558,7 +558,7 @@ class ParserTest extends \PHPUnit_Framework_TestCase { $parser = new Parser(new \ArrayObject()); - $this->expectException(Error::class); + $this->expectException(LogicException::class); $parser->getPosition(); }