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

DepthExclusionStrategy::isTooDeep()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 10
c 2
b 0
f 0
nc 5
nop 1
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 7
rs 8.8333
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