Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
halaxa committed Nov 22, 2024
1 parent 05dc2eb commit 644fe90
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
'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)
;
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
9 changes: 7 additions & 2 deletions src/FacadeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use JsonMachine\Exception\InvalidArgumentException;
use JsonMachine\JsonDecoder\ExtJsonDecoder;
use LogicException;

trait FacadeTrait
{
Expand Down Expand Up @@ -46,11 +47,15 @@ private static function createParser(iterable $bytesIterator, ItemsOptions $opti
}

/**
* @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');
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public static function fromIterable($iterable, array $options = []): self

/**
* @return \Generator
*
* @throws Exception\PathNotFoundException
*/
#[\ReturnTypeWillChange]
public function getIterator()
Expand Down
2 changes: 0 additions & 2 deletions src/ItemsOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ private function validateOptions(array $options)
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);
}

Expand Down
25 changes: 7 additions & 18 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use JsonMachine\JsonDecoder\ExtJsonDecoder;
use JsonMachine\JsonDecoder\ItemDecoder;
use JsonMachine\JsonDecoder\StringOnlyDecoder;
use LogicException;
use Traversable;

class Parser implements \IteratorAggregate, PositionAware
Expand Down Expand Up @@ -314,18 +315,6 @@ private function createGenerator(): Generator
$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();
Expand Down Expand Up @@ -429,20 +418,20 @@ private function error($msg, $token, $exception = SyntaxErrorException::class)
{
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
Expand Down
9 changes: 6 additions & 3 deletions src/RecursiveItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace JsonMachine;

use Exception;
use Generator;
use Iterator;
use IteratorAggregate;
use JsonMachine\Exception\InvalidArgumentException;
Expand All @@ -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)
Expand Down Expand Up @@ -131,7 +133,7 @@ public function valid(): bool

public function rewind(): void
{
$this->parserIterator = $this->parser->getIterator();
$this->parserIterator = toIterator($this->parser->getIterator());
$this->parserIterator->rewind();
}

Expand Down Expand Up @@ -187,8 +189,9 @@ public function advanceToKey($key)
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.'
Expand Down
25 changes: 22 additions & 3 deletions src/ResumableIteratorAggregateProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,33 @@

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();
Expand All @@ -34,4 +41,16 @@ public function __call($name, $arguments)
{
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');
}
}
16 changes: 16 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

function toIterator(Traversable $traversable): Iterator
{
if ($traversable instanceof IteratorAggregate) {
return toIterator($traversable->getIterator());
}

if ($traversable instanceof Iterator) {
return $traversable;
}

throw new \LogicException('Cannot turn Traversable into Iterator');
}
4 changes: 2 additions & 2 deletions test/JsonMachineTest/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace JsonMachineTest;

use Error;
use JsonMachine\Exception\JsonMachineException;
use JsonMachine\Exception\PathNotFoundException;
use JsonMachine\Exception\SyntaxErrorException;
Expand All @@ -14,6 +13,7 @@
use JsonMachine\StringChunks;
use JsonMachine\Tokens;
use JsonMachine\TokensWithDebugging;
use LogicException;
use Traversable;

/**
Expand Down Expand Up @@ -558,7 +558,7 @@ public function testGetPositionThrowsIfTokensDoNotSupportGetPosition()
{
$parser = new Parser(new \ArrayObject());

$this->expectException(Error::class);
$this->expectException(LogicException::class);
$parser->getPosition();
}

Expand Down

0 comments on commit 644fe90

Please sign in to comment.