Passed
Push — master ( b8f1e8...ad6bc5 )
by SignpostMarv
06:16
created

TraitRememberDaftObject::ThrowIfNotType()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 4
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
* Base daft objects.
4
*
5
* @author SignpostMarv
6
*/
7
declare(strict_types=1);
8
9
namespace SignpostMarv\DaftObject;
10
11
trait TraitRememberDaftObject
12
{
13 114
    public function RememberDaftObject(DefinesOwnIdPropertiesInterface $object) : void
14
    {
15 114
        if ($object instanceof DaftNestedWriteableObject) {
16 112
            $this->RememberDaftObjectWriteableTyped($object);
17
        } else {
18 2
            static::ThrowIfNotType($object, DaftNestedWriteableObject::class, 1, __FUNCTION__);
19
        }
20 112
    }
21
22
    /**
23
    * @return array<int, DaftNestedObject>
24
    */
25
    abstract public function RecallDaftNestedObjectFullTree(
26
        int $relativeDepthLimit = null
27
    ) : array;
28
29
    abstract public function CountDaftNestedObjectFullTree(int $relativeDepthLimit = null) : int;
30
31
    /**
32
    * @param DaftObject|string $object
33
    */
34 2
    protected static function ThrowIfNotType(
35
        $object,
36
        string $type,
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

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

36
        /** @scrutinizer ignore-unused */ string $type,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
        int $argument,
38
        string $function
39
    ) : void {
40 2
        if ( ! is_a($object, DaftNestedWriteableObject::class, is_string($object))) {
41 2
            throw new DaftObjectRepositoryTypeByClassMethodAndTypeException(
42 2
                $argument,
43 2
                static::class,
44 2
                $function,
45 2
                DaftNestedWriteableObject::class,
46 2
                is_string($object) ? $object : get_class($object)
47
            );
48
        }
49
    }
50
51 112
    private function RememberDaftObjectWriteableTyped(DaftNestedWriteableObject $object) : void
52
    {
53 112
        $left = $object->GetIntNestedLeft();
54 112
        $right = $object->GetIntNestedRight();
55 112
        $level = $object->GetIntNestedLevel();
56
57 112
        if (0 === $left && 0 === $right && 0 === $level) {
58 110
            $fullTreeCount = $this->CountDaftNestedObjectFullTree();
59
60 110
            if ($fullTreeCount > AbstractArrayBackedDaftNestedObject::COUNT_EXPECT_NON_EMPTY) {
61 84
                $tree = $this->RecallDaftNestedObjectFullTree();
62
63
                /**
64
                * @var DaftNestedWriteableObject
65
                */
66 84
                $end = end($tree);
67
68 84
                $left = $end->GetIntNestedRight() + 1;
69
            } else {
70 110
                $left = $fullTreeCount + $fullTreeCount;
71
            }
72
73 110
            $object->SetIntNestedLeft($left);
74 110
            $object->SetIntNestedRight($left + 1);
75
        }
76
77 112
        parent::RememberDaftObject($object);
78 112
    }
79
}
80