Skip to content

Commit

Permalink
Updated dependencies, improved parsing of type references
Browse files Browse the repository at this point in the history
  • Loading branch information
Václav Pelíšek committed Nov 10, 2023
1 parent f328eb0 commit 445f216
Show file tree
Hide file tree
Showing 27 changed files with 399 additions and 636 deletions.
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@
],
"require": {
"php": ">=8.0",
"infinityloop-dev/graphpinator-tokenizer": "^1.1.1",
"infinityloop-dev/graphpinator-common": "^1.1",
"infinityloop-dev/utils": "^2.0",
"nette/utils": "^3.0"
"infinityloop-dev/graphpinator-tokenizer": "^1.2",
"infinityloop-dev/graphpinator-common": "^2.0",
"infinityloop-dev/utils": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
"infection/infection": "^0.26",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": "^10.4",
"infection/infection": "^0.27",
"phpstan/phpstan": "^1.10",
"infinityloop-dev/coding-standard": "^0.2"
},
"scripts": {
Expand Down
865 changes: 328 additions & 537 deletions composer.lock

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache">
<coverage>
<report>
<clover outputFile="./build/phpunit.clover.xml"/>
<html outputDirectory="./build/html" lowUpperBound="50" highLowerBound="90"/>
Expand All @@ -18,4 +15,9 @@
<logging>
<junit outputFile="./build/logfile.xml"/>
</logging>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
2 changes: 0 additions & 2 deletions src/Directive/Directive.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class Directive
{
use \Nette\SmartObject;

public function __construct(
private string $name,
private ?\Graphpinator\Parser\Value\ArgumentValueSet $arguments,
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/DuplicateArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ final class DuplicateArgument extends \Graphpinator\Parser\Exception\ParserError

public function __construct(string $name, \Graphpinator\Common\Location $location)
{
$this->messageArgs = [$name];

parent::__construct($location);
parent::__construct($location, [$name]);
}
}
6 changes: 2 additions & 4 deletions src/Exception/ExpectedError.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

abstract class ExpectedError extends \Graphpinator\Parser\Exception\ParserError
{
final public function __construct(\Graphpinator\Common\Location $location, string $token)
final public function __construct(\Graphpinator\Common\Location $location, \Graphpinator\Tokenizer\TokenType $token)
{
$this->messageArgs[] = $token;

parent::__construct($location);
parent::__construct($location, [$token->value]);
}
}
6 changes: 3 additions & 3 deletions src/Exception/ParserError.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

abstract class ParserError extends \Graphpinator\Exception\GraphpinatorBase
{
public function __construct(\Graphpinator\Common\Location $location)
public function __construct(\Graphpinator\Common\Location $location, array $messageArgs = [])
{
parent::__construct();
parent::__construct($messageArgs);

$this->location = $location;
$this->setLocation($location);

Check warning on line 13 in src/Exception/ParserError.php

View workflow job for this annotation

GitHub Actions / mutation

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function __construct(\Graphpinator\Common\Location $location, array $messageArgs = []) { parent::__construct($messageArgs); - $this->setLocation($location); + } public final function isOutputable() : bool {
}

final public function isOutputable() : bool
Expand Down
2 changes: 0 additions & 2 deletions src/Field/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class Field
{
use \Nette\SmartObject;

public function __construct(
private string $name,
private ?string $alias = null,
Expand Down
2 changes: 0 additions & 2 deletions src/Fragment/Fragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class Fragment
{
use \Nette\SmartObject;

public function __construct(
private string $name,
private \Graphpinator\Parser\TypeRef\NamedTypeRef $typeCond,
Expand Down
2 changes: 0 additions & 2 deletions src/FragmentSpread/InlineFragmentSpread.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class InlineFragmentSpread implements \Graphpinator\Parser\FragmentSpread\FragmentSpread
{
use \Nette\SmartObject;

private \Graphpinator\Parser\Directive\DirectiveSet $directives;

public function __construct(
Expand Down
2 changes: 0 additions & 2 deletions src/FragmentSpread/NamedFragmentSpread.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class NamedFragmentSpread implements \Graphpinator\Parser\FragmentSpread\FragmentSpread
{
use \Nette\SmartObject;

private \Graphpinator\Parser\Directive\DirectiveSet $directives;

public function __construct(
Expand Down
2 changes: 0 additions & 2 deletions src/Operation/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class Operation
{
use \Nette\SmartObject;

private ?\Graphpinator\Parser\Variable\VariableSet $variables;
private ?\Graphpinator\Parser\Directive\DirectiveSet $directives;

Expand Down
2 changes: 0 additions & 2 deletions src/ParsedRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class ParsedRequest
{
use \Nette\SmartObject;

public function __construct(
private \Graphpinator\Parser\Operation\OperationSet $operations,
private \Graphpinator\Parser\Fragment\FragmentSet $fragments,
Expand Down
51 changes: 30 additions & 21 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

final class Parser
{
use \Nette\SmartObject;

private \Graphpinator\Parser\TokenContainer $tokenizer;

/**
Expand Down Expand Up @@ -43,7 +41,7 @@ public function parse(\Graphpinator\Source\Source $source) : ParsedRequest
// query shorthand
case TokenType::CUR_O:
$operation = new \Graphpinator\Parser\Operation\Operation(
\Graphpinator\Tokenizer\OperationType::QUERY,
\Graphpinator\Tokenizer\TokenType::QUERY->value,
null,
new \Graphpinator\Parser\Variable\VariableSet(),
new \Graphpinator\Parser\Directive\DirectiveSet(),
Expand All @@ -57,14 +55,14 @@ public function parse(\Graphpinator\Source\Source $source) : ParsedRequest
break;
case TokenType::NAME:
switch ($this->tokenizer->getCurrent()->getValue()) {
case TokenType::FRAGMENT:
case TokenType::FRAGMENT->value:
$fragment = $this->parseFragmentDefinition();
$fragments[$fragment->getName()] = $fragment;

break;
case TokenType::QUERY:
case TokenType::MUTATION:
case TokenType::SUBSCRIPTION:
case TokenType::QUERY->value:
case TokenType::MUTATION->value:
case TokenType::SUBSCRIPTION->value:
$operationType = $this->tokenizer->getCurrent()->getValue();
$this->tokenizer->getNext();

Expand Down Expand Up @@ -125,12 +123,12 @@ private function parseFragmentDefinition() : \Graphpinator\Parser\Fragment\Fragm
{
$fragmentName = $this->tokenizer->assertNext(TokenType::NAME, \Graphpinator\Parser\Exception\ExpectedFragmentName::class)->getValue();

if ($fragmentName === TokenType::ON) {
if ($fragmentName === TokenType::ON->value) {
throw new \Graphpinator\Parser\Exception\DisallowedFragmentName($this->tokenizer->getPrev()->getLocation());
}

$this->tokenizer->assertNextValue(TokenType::NAME, TokenType::ON, \Graphpinator\Parser\Exception\ExpectedTypeCondition::class);
$typeCond = $this->parseType(true);
$this->tokenizer->assertNextValue(TokenType::NAME, TokenType::ON->value, \Graphpinator\Parser\Exception\ExpectedTypeCondition::class);
$typeCond = $this->parseNamedType();
$directives = $this->parseDirectives();
$this->tokenizer->assertNext(TokenType::CUR_O, \Graphpinator\Parser\Exception\ExpectedSelectionSet::class);

Expand Down Expand Up @@ -276,8 +274,8 @@ private function parseFragmentSpread() : \Graphpinator\Parser\FragmentSpread\Fra
{
switch ($this->tokenizer->getNext()->getType()) {
case TokenType::NAME:
if ($this->tokenizer->getCurrent()->getValue() === TokenType::ON) {
$typeCond = $this->parseType(true);
if ($this->tokenizer->getCurrent()->getValue() === TokenType::ON->value) {
$typeCond = $this->parseNamedType();
$directives = $this->parseDirectives();
$this->tokenizer->assertNext(TokenType::CUR_O, \Graphpinator\Parser\Exception\ExpectedSelectionSet::class);

Expand Down Expand Up @@ -435,9 +433,9 @@ private function parseValue(bool $literalOnly) : \Graphpinator\Parser\Value\Valu
return new \Graphpinator\Parser\Value\VariableRef($this->tokenizer->getCurrent()->getValue());
case TokenType::NAME:
return match ($this->tokenizer->getCurrent()->getValue()) {
TokenType::TRUE => new \Graphpinator\Parser\Value\Literal(true),
TokenType::FALSE => new \Graphpinator\Parser\Value\Literal(false),
TokenType::NULL => new \Graphpinator\Parser\Value\Literal(null),
TokenType::TRUE->value => new \Graphpinator\Parser\Value\Literal(true),
TokenType::FALSE->value => new \Graphpinator\Parser\Value\Literal(false),
TokenType::NULL->value => new \Graphpinator\Parser\Value\Literal(null),
default => new \Graphpinator\Parser\Value\EnumLiteral($this->tokenizer->getCurrent()->getValue()),
};
case TokenType::STRING:
Expand Down Expand Up @@ -477,14 +475,13 @@ private function parseValue(bool $literalOnly) : \Graphpinator\Parser\Value\Valu
}

/**
* Parses type reference.
* Parses type reference with modifiers.
*
* Expects iterator on previous token
* Leaves iterator to last used token - last token in type definition
*
* @param bool $namedOnly
*/
private function parseType(bool $namedOnly) : \Graphpinator\Parser\TypeRef\TypeRef
private function parseType() : \Graphpinator\Parser\TypeRef\TypeRef
{
switch ($this->tokenizer->getNext()->getType()) {
case TokenType::NAME:
Expand All @@ -509,13 +506,25 @@ private function parseType(bool $namedOnly) : \Graphpinator\Parser\TypeRef\TypeR
$type = new \Graphpinator\Parser\TypeRef\NotNullRef($type);
}

if ($namedOnly && !$type instanceof \Graphpinator\Parser\TypeRef\NamedTypeRef) {
return $type;
}

/**
* Parses type reference without modifiers
*
* Expects iterator on previous token
* Leaves iterator to last used token - last token in type definition
*
*/
private function parseNamedType() : \Graphpinator\Parser\TypeRef\NamedTypeRef
{
if ($this->tokenizer->getNext()->getType() !== TokenType::NAME) {
throw new \Graphpinator\Parser\Exception\ExpectedNamedType(
$this->tokenizer->getCurrent()->getLocation(),
$type->print(),
$this->tokenizer->getCurrent()->getType(),
);
}

return $type;
return new \Graphpinator\Parser\TypeRef\NamedTypeRef($this->tokenizer->getCurrent()->getValue());
}
}
16 changes: 8 additions & 8 deletions src/TokenContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Graphpinator\Parser;

use \Graphpinator\Tokenizer\Token;

final class TokenContainer implements \IteratorAggregate
{
use \Nette\SmartObject;

private array $tokens = [];
private int $currentIndex = 0;

Expand All @@ -30,12 +30,12 @@ public function isEmpty() : bool
return \count($this->tokens) === 0;
}

public function getCurrent() : \Graphpinator\Tokenizer\Token
public function getCurrent() : Token
{
return $this->tokens[$this->currentIndex];
}

public function getPrev() : \Graphpinator\Tokenizer\Token
public function getPrev() : Token
{
\assert(\array_key_exists($this->currentIndex - 1, $this->tokens));

Expand All @@ -44,7 +44,7 @@ public function getPrev() : \Graphpinator\Tokenizer\Token
return $this->tokens[$this->currentIndex];
}

public function getNext() : \Graphpinator\Tokenizer\Token
public function getNext() : Token
{
if (!$this->hasNext()) {
throw new \Graphpinator\Parser\Exception\UnexpectedEnd($this->getCurrent()->getLocation());
Expand All @@ -55,7 +55,7 @@ public function getNext() : \Graphpinator\Tokenizer\Token
return $this->tokens[$this->currentIndex];
}

public function peekNext() : \Graphpinator\Tokenizer\Token
public function peekNext() : Token
{
if (!$this->hasNext()) {
throw new \Graphpinator\Parser\Exception\UnexpectedEnd($this->getCurrent()->getLocation());
Expand All @@ -64,7 +64,7 @@ public function peekNext() : \Graphpinator\Tokenizer\Token
return $this->tokens[$this->currentIndex + 1];
}

public function assertNext(string $tokenType, string $exceptionClass) : \Graphpinator\Tokenizer\Token
public function assertNext(\Graphpinator\Tokenizer\TokenType $tokenType, string $exceptionClass) : Token
{
$token = $this->getNext();

Expand All @@ -75,7 +75,7 @@ public function assertNext(string $tokenType, string $exceptionClass) : \Graphpi
throw new $exceptionClass($token->getLocation(), $token->getType());
}

public function assertNextValue(string $tokenType, string $value, string $exceptionClass) : \Graphpinator\Tokenizer\Token
public function assertNextValue(\Graphpinator\Tokenizer\TokenType $tokenType, string $value, string $exceptionClass) : Token
{
$token = $this->getNext();

Expand Down
2 changes: 0 additions & 2 deletions src/TypeRef/ListTypeRef.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class ListTypeRef implements \Graphpinator\Parser\TypeRef\TypeRef
{
use \Nette\SmartObject;

public function __construct(
private TypeRef $innerRef,
)
Expand Down
2 changes: 0 additions & 2 deletions src/TypeRef/NamedTypeRef.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class NamedTypeRef implements \Graphpinator\Parser\TypeRef\TypeRef
{
use \Nette\SmartObject;

public function __construct(
private string $name,
)
Expand Down
2 changes: 0 additions & 2 deletions src/TypeRef/NotNullRef.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class NotNullRef implements \Graphpinator\Parser\TypeRef\TypeRef
{
use \Nette\SmartObject;

public function __construct(
private TypeRef $innerRef,
)
Expand Down
2 changes: 0 additions & 2 deletions src/Value/ArgumentValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class ArgumentValue
{
use \Nette\SmartObject;

public function __construct(
private \Graphpinator\Parser\Value\Value $value,
private string $name,
Expand Down
2 changes: 0 additions & 2 deletions src/Value/EnumLiteral.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class EnumLiteral implements \Graphpinator\Parser\Value\Value
{
use \Nette\SmartObject;

public function __construct(
private string $value,
)
Expand Down
2 changes: 0 additions & 2 deletions src/Value/ListVal.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class ListVal implements \Graphpinator\Parser\Value\Value
{
use \Nette\SmartObject;

public function __construct(
private array $value,
)
Expand Down
2 changes: 0 additions & 2 deletions src/Value/Literal.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class Literal implements \Graphpinator\Parser\Value\Value
{
use \Nette\SmartObject;

public function __construct(
private string|int|float|bool|null $value,
)
Expand Down
Loading

0 comments on commit 445f216

Please sign in to comment.