1
0
mirror of https://github.com/halaxa/json-machine.git synced 2025-01-16 20:48:17 +01:00

Fix build

This commit is contained in:
Filip Halaxa 2024-11-22 19:26:04 +01:00
parent 05dc2eb0fd
commit 644fe90245
10 changed files with 64 additions and 33 deletions

View File

@ -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)
;

View File

@ -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"}

View File

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

View File

@ -68,8 +68,6 @@ final class Items implements \IteratorAggregate, PositionAware
/**
* @return \Generator
*
* @throws Exception\PathNotFoundException
*/
#[\ReturnTypeWillChange]
public function getIterator()

View File

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

View File

@ -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

View File

@ -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.'

View File

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

16
src/functions.php Normal file
View File

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

View File

@ -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();
}