Passed
Push — master ( 84c32b...3332dd )
by Pol
02:08
created

Vfs   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 68%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 23
c 3
b 0
f 0
dl 0
loc 77
ccs 17
cts 25
cp 0.68
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 11 2
A add() 0 21 4
A root() 0 9 2
A contains() 0 10 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpvfs\Node;
6
7
use drupol\phptree\Node\AttributeNode;
8
use drupol\phptree\Node\NodeInterface;
9
use drupol\phpvfs\Utils\Path;
10
11
/**
12
 * Class Vfs.
13
 */
14
abstract class Vfs extends AttributeNode implements VfsInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     *
19
     * @throws \Exception
20
     *
21
     * @return \drupol\phpvfs\Node\DirectoryInterface
22
     */
23 1
    public function add(NodeInterface ...$nodes): NodeInterface
24
    {
25
        /** @var \drupol\phpvfs\Node\VfsInterface $node */
26 1
        foreach ($nodes as $node) {
27 1
            if ($this->getAttribute('id') === $node->getAttribute('id')) {
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on drupol\phptree\Node\NodeInterface. It seems like you code against a sub-type of drupol\phptree\Node\NodeInterface such as drupol\phptree\Node\AttributeNode or drupol\phptree\Node\AttributeNodeInterface or drupol\phptree\Node\AttributeNode. ( Ignorable by Annotation )

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

27
            if ($this->getAttribute('id') === $node->/** @scrutinizer ignore-call */ getAttribute('id')) {
Loading history...
28 1
                $this->add($node[0]->setParent(null));
29
30 1
                continue;
31
            }
32
33
            // If the $cwd contains the nodechild.
34 1
            if (null !== $child = $this->contains($node)) {
35
                $child->add($node[0]->setParent(null));
36
37
                continue;
38
            }
39
40 1
            parent::add($node->setParent(null));
41
        }
42
43 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type drupol\phpvfs\Node\Vfs which is incompatible with the documented return type drupol\phpvfs\Node\DirectoryInterface.
Loading history...
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getPath(): Path
50
    {
51
        $paths = [
52
            $this->getAttribute('id'),
53
        ];
54
55
        foreach ($this->getAncestors() as $ancestor) {
56
            \array_unshift($paths, $ancestor->getAttribute('id'));
57
        }
58
59
        return Path::fromString(\str_replace('//', '/', \implode('/', $paths)));
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function root(): VfsInterface
66
    {
67 1
        $root = $this;
68
69 1
        foreach ($this->getAncestors() as $ancestor) {
70 1
            $root = $ancestor;
71
        }
72
73 1
        return $root;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $root could return the type drupol\phptree\Node\NodeInterface which includes types incompatible with the type-hinted return drupol\phpvfs\Node\VfsInterface. Consider adding an additional type-check to rule them out.
Loading history...
74
    }
75
76
    /**
77
     * @param \drupol\phpvfs\Node\VfsInterface $node
78
     *
79
     * @return null|\drupol\phpvfs\Node\VfsInterface
80
     */
81 1
    protected function contains(VfsInterface $node): ?VfsInterface
82
    {
83
        /** @var \drupol\phpvfs\Node\VfsInterface $child */
84 1
        foreach ($this->children() as $child) {
85 1
            if ($node->getAttribute('id') === $child->getAttribute('id')) {
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on ArrayObject. ( Ignorable by Annotation )

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

85
            if ($node->getAttribute('id') === $child->/** @scrutinizer ignore-call */ getAttribute('id')) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
                return $child;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $child returns the type ArrayObject which is incompatible with the type-hinted return drupol\phpvfs\Node\VfsInterface|null.
Loading history...
87
            }
88
        }
89
90 1
        return null;
91
    }
92
}
93