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

FilesystemNode::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
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
     * FilesystemNode constructor.
18
     *
19
     * @param array $attributes
20
     * @param null|int $capacity
21
     */
22 19
    public function __construct(
23
        array $attributes = [],
24
        ?int $capacity = 0
25
    ) {
26 19
        $time = \time();
27
28
        $attributes = [
29 19
            'uid' => \function_exists('posix_getuid') ? \posix_getuid() : 0,
30 19
            'gid' => \function_exists('posix_getgid') ? \posix_getgid() : 0,
31 19
            'atime' => $time,
32 19
            'mtime' => $time,
33 19
            'ctime' => $time,
34 19
        ] + $attributes;
35
36 19
        parent::__construct($attributes, $capacity);
37 19
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @throws \Exception
43
     *
44
     * @return \drupol\phpvfs\Node\DirectoryInterface
45
     */
46 15
    public function add(NodeInterface ...$nodes): NodeInterface
47
    {
48
        /** @var \drupol\phpvfs\Node\FilesystemNodeInterface $node */
49 15
        foreach ($nodes as $node) {
50 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

50
            /** @scrutinizer ignore-call */ 
51
            $node = $node->root();
Loading history...
51
52 15
            if (!($node instanceof FilesystemNodeInterface)) {
53
                throw new \Exception('Invalid filesystem node type.');
54
            }
55
56 15
            if ($this->getAttribute('id') === $node->getAttribute('id')) {
57 7
                $this->add($node[0]->setParent(null));
58
59 7
                continue;
60
            }
61
62
            // If the $cwd contains the nodechild.
63 15
            if (null !== $child = $this->contains($node)) {
64 2
                $child->add($node[0]->setParent(null));
65
66 2
                continue;
67
            }
68
69 15
            parent::add($node->setParent(null));
70
        }
71
72 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...
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 1
    public function getPath(): Path
79
    {
80
        $paths = [
81 1
            $this->getAttribute('id'),
82
        ];
83
84 1
        foreach ($this->getAncestors() as $ancestor) {
85 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

85
            \array_unshift($paths, $ancestor->/** @scrutinizer ignore-call */ getAttribute('id'));
Loading history...
86
        }
87
88 1
        return Path::fromString(\str_replace('//', '/', \implode('/', $paths)));
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 16
    public function root(): FilesystemNodeInterface
95
    {
96 16
        $root = $this;
97
98 16
        foreach ($this->getAncestors() as $ancestor) {
99 13
            $root = $ancestor;
100
        }
101
102 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...
103
    }
104
105
    /**
106
     * @param \drupol\phpvfs\Node\FilesystemNodeInterface $node
107
     *
108
     * @return null|\drupol\phpvfs\Node\FilesystemNodeInterface
109
     */
110 15
    protected function contains(FilesystemNodeInterface $node): ?FilesystemNodeInterface
111
    {
112
        /** @var \drupol\phpvfs\Node\FilesystemNodeInterface $child */
113 15
        foreach ($this->children() as $child) {
114 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

114
            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...
115 2
                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...
116
            }
117
        }
118
119 15
        return null;
120
    }
121
}
122