Skip to content

Commit

Permalink
Merge pull request #66 from thecodingmachine/6.0
Browse files Browse the repository at this point in the history
Add SF 6 support
  • Loading branch information
nguyenk authored Jun 26, 2023
2 parents 1a7d828 + 671f5d1 commit 770d340
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 65 deletions.
17 changes: 3 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,13 @@ env:
matrix:
fast_finish: true
include:
# Test the latest stable release
- php: 7.2
- php: 8.0
env: COVERAGE=true PHPUNIT_FLAGS="-v --coverage-text"

# Test LTS versions.
- php: 8.0
env: DEPENDENCIES="symfony/lts:^4"

# Latest commit to master
- php: 8.0
env: STABILITY="dev"
- php: 8.1
env: DEPENDENCIES="symfony/^6"

allow_failures:
# Minimum supported dependencies with the latest and oldest PHP version
- php: 8.0
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors"
- php: 7.2
- php: 8.1
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors"
# Dev-master is allowed to fail.
- env: STABILITY="dev"
Expand Down
23 changes: 14 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
],
"require" : {
"php" : ">=7.2",
"thecodingmachine/graphqlite" : "^5.0",
"symfony/validator": "^4.2 | ^5 | ^6" ,
"doctrine/annotations": "^1.6"
"thecodingmachine/graphqlite" : "^6.0",
"symfony/validator": "^6" ,
"doctrine/annotations": "^1.13"
},
"require-dev": {
"phpunit/phpunit": "^8.4.1",
"phpunit/phpunit": "^9.6.5",
"mouf/picotainer": "^1.1",
"phpstan/phpstan": "^0.12.14",
"phpstan/phpstan": "^1.8",
"php-coveralls/php-coveralls": "^2.1.0",
"symfony/translation": "^4 | ^5 | ^6",
"doctrine/coding-standard": "^9.0"
"symfony/translation": "^6",
"doctrine/coding-standard": "^11.1"
},
"scripts": {
"phpstan": "phpstan analyse src/ -c phpstan.neon --level=7 --no-progress",
Expand All @@ -48,9 +48,14 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.0.x-dev"
"dev-master": "6.0.x-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
parameters:
ignoreErrors:

excludePaths:
- vendor
- cache
- .phpstan-cache

#includes:
# - vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
8 changes: 2 additions & 6 deletions src/Annotations/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class Assertion implements ParameterAnnotationInterface
/** @var Constraint[] */
private $constraint;

/**
* @param array<string, mixed> $values
*/
/** @param array<string, mixed> $values */
public function __construct(array $values)
{
if (! isset($values['for'])) {
Expand All @@ -50,9 +48,7 @@ public function getTarget(): string
return $this->for;
}

/**
* @return Constraint[]
*/
/** @return Constraint[] */
public function getConstraint(): array
{
return $this->constraint;
Expand Down
13 changes: 2 additions & 11 deletions src/ConstraintViolationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ConstraintViolationException extends Exception implements GraphQLException
public function __construct(ConstraintViolationInterface $violation)
{
parent::__construct((string) $violation->getMessage(), 400);

$this->violation = $violation;
}

Expand All @@ -27,24 +28,14 @@ public function isClientSafe(): bool
return true;
}

/**
* Returns string describing a category of the error.
*
* Value "graphql" is reserved for errors produced by query parsing or validation, do not use it.
*/
public function getCategory(): string
{
return 'Validate';
}

/**
* Returns the "extensions" object attached to the GraphQL error.
*
* @return array<string, mixed>
*/
public function getExtensions(): array
{
$extensions = [];
$extensions = ['category' => 'Validate'];
$code = $this->violation->getCode();
if (! empty($code)) {
$extensions['code'] = $code;
Expand Down
2 changes: 1 addition & 1 deletion src/Mappers/Parameters/AssertParameterMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(ConstraintValidatorFactoryInterface $constraintValid
$this->translator = $translator;
}

public function mapParameter(ReflectionParameter $refParameter, DocBlock $docBlock, ?Type $paramTagType, ParameterAnnotations $parameterAnnotations, ParameterHandlerInterface $next): ParameterInterface
public function mapParameter(ReflectionParameter $refParameter, DocBlock $docBlock, Type|null $paramTagType, ParameterAnnotations $parameterAnnotations, ParameterHandlerInterface $next): ParameterInterface
{
/** @var Assertion[] $assertionAnnotations */
$assertionAnnotations = $parameterAnnotations->getAnnotationsByType(Assertion::class);
Expand Down
21 changes: 6 additions & 15 deletions src/Mappers/Parameters/ParameterValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\Context\ExecutionContext;
Expand All @@ -29,9 +30,7 @@ class ParameterValidator implements InputTypeParameterInterface
/** @var TranslatorInterface */
private $translator;

/**
* @param Constraint[] $constraints
*/
/** @param Constraint[] $constraints */
public function __construct(InputTypeParameterInterface $parameter, string $parameterName, array $constraints, ConstraintValidatorFactoryInterface $constraintValidatorFactory, ValidatorInterface $validator, TranslatorInterface $translator)
{
$this->parameter = $parameter;
Expand All @@ -42,13 +41,8 @@ public function __construct(InputTypeParameterInterface $parameter, string $para
$this->translator = $translator;
}

/**
* @param array<string, mixed> $args
* @param mixed $context
*
* @return mixed
*/
public function resolve(?object $source, array $args, $context, ResolveInfo $info)
/** @param array<string, mixed> $args */
public function resolve(object|null $source, array $args, mixed $context, ResolveInfo $info): mixed
{
$value = $this->parameter->resolve($source, $args, $context, $info);

Expand All @@ -69,7 +63,7 @@ public function resolve(?object $source, array $args, $context, ResolveInfo $inf
return $value;
}

public function getType(): InputType
public function getType(): InputType&Type
{
return $this->parameter->getType();
}
Expand All @@ -79,10 +73,7 @@ public function hasDefaultValue(): bool
return $this->parameter->hasDefaultValue();
}

/**
* @return mixed
*/
public function getDefaultValue()
public function getDefaultValue(): mixed
{
return $this->parameter->getDefaultValue();
}
Expand Down
9 changes: 3 additions & 6 deletions src/ValidationFailedException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ class ValidationFailedException extends InvalidArgumentException implements Grap
/** @var ConstraintViolationException[] */
private $exceptions = [];

/**
* @param ConstraintViolationListInterface<ConstraintViolationInterface> $constraintViolationList
*/
/** @param ConstraintViolationListInterface<ConstraintViolationInterface> $constraintViolationList */
public function __construct(ConstraintViolationListInterface $constraintViolationList)
{
parent::__construct('Validation failed:', 400);

foreach ($constraintViolationList as $constraintViolation) {
$this->add($constraintViolation);
}
Expand All @@ -33,9 +32,7 @@ private function add(ConstraintViolationInterface $violation): void
$this->message .= "\n" . $violation->getMessage();
}

/**
* @return (ClientAware&Throwable)[]
*/
/** @return (ClientAware&Throwable)[] */
public function getExceptions(): array
{
return $this->exceptions;
Expand Down
5 changes: 2 additions & 3 deletions tests/ConstraintValidationExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ public function testException()
$exception = new ConstraintViolationException(new ConstraintViolation('foo', 'foo {bar}', ['bar' => 'baz'], null, null, 'invalidValue', null, 'myCode'));
$this->assertSame(400, $exception->getCode());
$this->assertTrue($exception->isClientSafe());
$this->assertSame('Validate', $exception->getCategory());
$this->assertSame(['code' => 'myCode'], $exception->getExtensions());
$this->assertSame(['category' => 'Validate', 'code' => 'myCode'], $exception->getExtensions());

$exception = new ConstraintViolationException(new ConstraintViolation('foo', 'foo {bar}', ['bar' => 'baz'], null, null, 'invalidValue'));
$this->assertSame([], $exception->getExtensions());
$this->assertSame(['category' => 'Validate'], $exception->getExtensions());
}
}

0 comments on commit 770d340

Please sign in to comment.