Passed
Push — master ( a3a054...93611a )
by Pol
02:16
created

FilesystemNode   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
eloc 26
c 0
b 0
f 0
dl 0
loc 83
ccs 27
cts 28
cp 0.9643
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 27 5
A getPath() 0 11 2
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 FilesystemNode extends AttributeNode implements FilesystemNodeInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     *
19
     * @throws \Exception
20
     *
21
     * @return \drupol\phpvfs\Node\DirectoryInterface
22
     */
23 15
    public function add(NodeInterface ...$nodes): NodeInterface
24
    {
25
        /** @var \drupol\phpvfs\Node\FilesystemNodeInterface $node */
26 15
        foreach ($nodes as $node) {
27 15
            $node = $node->root();
0 ignored issues
show
Bug introduced by
The method root() 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\phpvfs\Node\FilesystemNode or drupol\phpvfs\Node\FilesystemNodeInterface or drupol\phpvfs\Node\FilesystemNode or drupol\phpvfs\Node\FilesystemNode. ( Ignorable by Annotation )

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

27
            /** @scrutinizer ignore-call */ 
28
            $node = $node->root();
Loading history...
28
29 15
            if (!($node instanceof FilesystemNodeInterface)) {
30
                throw new \Exception('Invalid filesystem node type.');
31
            }
32
33 15
            if ($this->getAttribute('id') === $node->getAttribute('id')) {
34 7
                $this->add($node[0]->setParent(null));
35
36 7
                continue;
37
            }
38
39
            // If the $cwd contains the nodechild.
40 15
            if (null !== $child = $this->contains($node)) {
41 2
                $child->add($node[0]->setParent(null));
42
43 2
                continue;
44
            }
45
46 15
            parent::add($node->setParent(null));
47
        }
48
49 15
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type drupol\phpvfs\Node\FilesystemNode which is incompatible with the documented return type drupol\phpvfs\Node\DirectoryInterface.
Loading history...
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function getPath(): Path
56
    {
57
        $paths = [
58 1
            $this->getAttribute('id'),
59
        ];
60
61 1
        foreach ($this->getAncestors() as $ancestor) {
62 1
            \array_unshift($paths, $ancestor->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

62
            \array_unshift($paths, $ancestor->/** @scrutinizer ignore-call */ getAttribute('id'));
Loading history...
63
        }
64
65 1
        return Path::fromString(\str_replace('//', '/', \implode('/', $paths)));
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 16
    public function root(): FilesystemNodeInterface
72
    {
73 16
        $root = $this;
74
75 16
        foreach ($this->getAncestors() as $ancestor) {
76 13
            $root = $ancestor;
77
        }
78
79 16
        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\FilesystemNodeInterface. Consider adding an additional type-check to rule them out.
Loading history...
80
    }
81
82
    /**
83
     * @param \drupol\phpvfs\Node\FilesystemNodeInterface $node
84
     *
85
     * @return null|\drupol\phpvfs\Node\FilesystemNodeInterface
86
     */
87 15
    protected function contains(FilesystemNodeInterface $node): ?FilesystemNodeInterface
88
    {
89
        /** @var \drupol\phpvfs\Node\FilesystemNodeInterface $child */
90 15
        foreach ($this->children() as $child) {
91 3
            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

91
            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...
92 3
                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\FilesystemNodeInterface|null.
Loading history...
93
            }
94
        }
95
96 15
        return null;
97
    }
98
}
99