Completed
Pull Request — master (#1099)
by
unknown
27:16 queued 12:16
created

ExpressionLanguageExclusionStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Exclusion;
6
7
use JMS\Serializer\Context;
8
use JMS\Serializer\Expression\CompilableExpressionEvaluatorInterface;
9
use JMS\Serializer\Expression\Expression;
10
use JMS\Serializer\Expression\ExpressionEvaluatorInterface;
11
use JMS\Serializer\Metadata\ClassMetadata;
12
use JMS\Serializer\Metadata\PropertyMetadata;
13
use JMS\Serializer\SerializationContext;
14
15
/**
16
 * Exposes an exclusion strategy based on the Symfony's expression language.
17
 * This is not a standard exclusion strategy and can not be used in user applications.
18
 *
19
 * @internal
20
 *
21
 * @author Asmir Mustafic <[email protected]>
22
 */
23
final class ExpressionLanguageExclusionStrategy
24
{
25
    /**
26
     * @var ExpressionEvaluatorInterface
27 27
     */
28
    private $expressionEvaluator;
29 27
30 27
    public function __construct(ExpressionEvaluatorInterface $expressionEvaluator)
31
    {
32
        $this->expressionEvaluator = $expressionEvaluator;
33
    }
34
35 27
    /**
36
     * {@inheritDoc}
37 27
     */
38 26
    public function shouldSkipProperty(PropertyMetadata $property, Context $navigatorContext): bool
39
    {
40
        if (null === $property->excludeIf) {
41
            return false;
42 22
        }
43 22
44
        $variables = [
45 22
            'context' => $navigatorContext,
46 22
            'property_metadata' => $property,
47
        ];
48 1
        if ($navigatorContext instanceof SerializationContext) {
49
            $variables['object'] = $navigatorContext->getObject();
50
        } else {
51 22
            $variables['object'] = null;
52
        }
53
54
        if (($property->excludeIf instanceof Expression) && ($this->expressionEvaluator instanceof CompilableExpressionEvaluatorInterface)) {
0 ignored issues
show
introduced by
$property->excludeIf is never a sub-type of JMS\Serializer\Expression\Expression.
Loading history...
55
            return $this->expressionEvaluator->evaluateParsed($property->excludeIf, $variables);
56
        }
57
58
        return $this->expressionEvaluator->evaluate($property->excludeIf, $variables);
59
    }
60
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function shouldSkipClass(ClassMetadata $class, Context $navigatorContext): bool
66
    {
67
        if (null === $class->excludeIf) {
68
            return false;
69
        }
70
71
        $variables = [
72
            'context' => $navigatorContext,
73
            'property_metadata' => $class,
74
        ];
75
        if ($navigatorContext instanceof SerializationContext) {
76
            $variables['object'] = $navigatorContext->getObject();
77
        } else {
78
            $variables['object'] = null;
79
        }
80
81
        if (($class->excludeIf instanceof Expression) && ($this->expressionEvaluator instanceof CompilableExpressionEvaluatorInterface)) {
0 ignored issues
show
introduced by
$class->excludeIf is never a sub-type of JMS\Serializer\Expression\Expression.
Loading history...
82
            return $this->expressionEvaluator->evaluateParsed($class->excludeIf, $variables);
83
        }
84
85
        return $this->expressionEvaluator->evaluate($class->excludeIf, $variables);
0 ignored issues
show
Bug introduced by
$class->excludeIf of type boolean is incompatible with the type string expected by parameter $expression of JMS\Serializer\Expressio...orInterface::evaluate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
        return $this->expressionEvaluator->evaluate(/** @scrutinizer ignore-type */ $class->excludeIf, $variables);
Loading history...
86
    }
87
}
88