|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Validator\Constraints; |
|
6
|
|
|
|
|
7
|
|
|
use Overblog\GraphQLBundle\Validator\ValidationNode; |
|
8
|
|
|
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
|
10
|
|
|
use Symfony\Component\Validator\Constraint; |
|
11
|
|
|
use Symfony\Component\Validator\Constraints\Expression; |
|
12
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
|
13
|
|
|
|
|
14
|
|
|
class ExpressionValidator extends \Symfony\Component\Validator\Constraints\ExpressionValidator |
|
15
|
|
|
{ |
|
16
|
|
|
private $expressionLanguage; |
|
17
|
|
|
|
|
18
|
9 |
|
public function __construct(ExpressionLanguage $expressionLanguage) |
|
19
|
|
|
{ |
|
20
|
9 |
|
$this->expressionLanguage = $expressionLanguage; |
|
21
|
9 |
|
if (Kernel::VERSION_ID >= 40400) { |
|
22
|
9 |
|
parent::__construct($expressionLanguage); |
|
23
|
|
|
} else { |
|
24
|
|
|
parent::__construct(null, $expressionLanguage); |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
9 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
9 |
|
public function validate($value, Constraint $constraint): void |
|
32
|
|
|
{ |
|
33
|
9 |
|
if (!$constraint instanceof Expression) { |
|
34
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
9 |
|
$variables = $constraint->values; |
|
38
|
9 |
|
$variables['value'] = $value; |
|
39
|
|
|
|
|
40
|
9 |
|
$object = $this->context->getObject(); |
|
41
|
|
|
|
|
42
|
9 |
|
$variables['this'] = $object; |
|
43
|
|
|
|
|
44
|
9 |
|
if ($object instanceof ValidationNode) { |
|
45
|
9 |
|
$variables['parentValue'] = $object->getResolverArg('value'); |
|
46
|
9 |
|
$variables['context'] = $object->getResolverArg('context'); |
|
47
|
9 |
|
$variables['args'] = $object->getResolverArg('args'); |
|
48
|
9 |
|
$variables['info'] = $object->getResolverArg('info'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
9 |
|
if (!$this->expressionLanguage->evaluate($constraint->expression, $variables)) { |
|
52
|
4 |
|
$this->context->buildViolation($constraint->message) |
|
53
|
4 |
|
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING)) |
|
54
|
4 |
|
->setCode(Expression::EXPRESSION_FAILED_ERROR) |
|
55
|
4 |
|
->addViolation(); |
|
56
|
|
|
} |
|
57
|
9 |
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.