Passed
Pull Request — master (#1383)
by Marcin
22:06
created

DepthExclusionStrategy::shouldSkipClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Exclusion;
6
7
use JMS\Serializer\Context;
8
use JMS\Serializer\Metadata\ClassMetadata;
9
use JMS\Serializer\Metadata\PropertyMetadata;
10
11
/**
12
 * @author Adrien Brault <[email protected]>
13
 */
14
final class DepthExclusionStrategy implements ExclusionStrategyInterface
15
{
16
    public function shouldSkipClass(ClassMetadata $metadata, Context $context): bool
17
    {
18
        return $this->isTooDeep($context);
19 6
    }
20
21 6
    public function shouldSkipProperty(PropertyMetadata $property, Context $context): bool
22
    {
23
        return $this->isTooDeep($context);
24
    }
25
26
    private function isTooDeep(Context $context): bool
27 6
    {
28
        $relativeDepth = 0;
29 6
30
        foreach ($context->getMetadataStack() as $metadata) {
31
            if (!$metadata instanceof PropertyMetadata) {
32 6
                continue;
33
            }
34 6
35 6
            $relativeDepth++;
36
37 6
            if (0 === $metadata->maxDepth && $context->getMetadataStack()->top() === $metadata) {
38
                continue;
39 6
            }
40 6
41 6
            if (null !== $metadata->maxDepth && $relativeDepth > $metadata->maxDepth) {
42 6
                return true;
43 6
            }
44
        }
45 6
46 6
        return false;
47
    }
48
}
49