Completed
Push — main ( fffb13...d8afdd )
by Laurent
139:07 queued 124:29
created

FamilyLog::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Domain\Model\Common\Entities;
6
7
use Domain\Model\Common\VO\NameField;
8
9
class FamilyLog
10
{
11
    /**
12
     * @var string
13
     */
14
    private $name;
15
16
    /**
17
     * @var string|null
18
     */
19
    private $parent = null;
20
21
    /**
22
     * @var FamilyLog[]|null
23
     */
24
    private $children;
25
    /**
26
     * @var string
27
     */
28
    private $slug;
29
30
    /**
31
     * @var string
32
     */
33
    private $path;
34
35
    /**
36
     * FamilyLog constructor.
37
     *
38
     * @param NameField      $name
39
     * @param FamilyLog|null $parent
40
     */
41
    public function __construct(NameField $name, ?FamilyLog $parent = null)
42
    {
43
        $this->name = $name->getValue();
44
        $this->path = $name->slugify();
45
        $this->slug = $name->slugify();
46
        if (null !== $parent) {
47
            $this->parent = $parent;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parent of type object<Domain\Model\Common\Entities\FamilyLog> is incompatible with the declared type string|null of property $parent.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
48
            $this->parent->addChild($this);
49
            $this->path = $parent->slug().':'.$name->slugify();
50
            if (null !== $this->parent->parent) {
51
                $this->path = $this->parent->parent->slug().':'.$this->parent->slug().':'.$name->slugify();
0 ignored issues
show
Bug introduced by
The method slug cannot be called on $this->parent->parent (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
52
            }
53
        }
54
    }
55
56
    /**
57
     * @param NameField      $name
58
     * @param FamilyLog|null $parent
59
     *
60
     * @return FamilyLog
61
     */
62
    public static function create(NameField $name, ?FamilyLog $parent = null): self
63
    {
64
        return new self($name, $parent);
65
    }
66
67
    final public function parent(): FamilyLog
68
    {
69
        return $this->parent;
70
    }
71
72
    final public function path(): string
73
    {
74
        return $this->path;
75
    }
76
77
    final public function slug(): string
78
    {
79
        return $this->slug;
80
    }
81
82
    final public function parseTree(): array
83
    {
84
        $arrayChildren = [];
85
        foreach ($this->children as $child) {
0 ignored issues
show
Bug introduced by
The expression $this->children of type array<integer,object<Dom...tities\FamilyLog>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
86
            if (null !== $this->hasChildren($child)) {
87
                $arrayChildren[$child->name] = $this->hasChildren($child);
88
            } else {
89
                $arrayChildren[] = $child->name;
90
            }
91
        }
92
93
        return [$this->name => $arrayChildren];
94
    }
95
96
    /**
97
     * @param FamilyLog $familyLog
98
     *
99
     * @return array|null
100
     */
101
    private function hasChildren(FamilyLog $familyLog): ?array
102
    {
103
        if (null !== $familyLog->children) {
104
            return array_map(static function ($child) {
105
                return $child->name;
106
            }, $familyLog->children);
107
        }
108
109
        return null;
110
    }
111
112
    private function addChild(FamilyLog $child): void
113
    {
114
        $this->children[] = $child;
115
    }
116
}
117