Issues (22)

src/Node/FilesystemNode.php (5 issues)

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 FilesystemNode.
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 30
    public function __construct(
23
        array $attributes = [],
24
        ?int $capacity = 0
25
    ) {
26 30
        $time = \time();
27
28
        $attributes = [
29 30
            'uid' => \function_exists('posix_getuid') ? \posix_getuid() : 0,
30 30
            'gid' => \function_exists('posix_getgid') ? \posix_getgid() : 0,
31 30
            'atime' => $time,
32 30
            'mtime' => $time,
33 30
            'ctime' => $time,
34 30
        ] + $attributes;
35
36 30
        parent::__construct($attributes, $capacity);
37 30
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @throws \Exception
43
     *
44
     * @return \drupol\phpvfs\Node\DirectoryInterface
45
     */
46 24
    public function add(NodeInterface ...$nodes): NodeInterface
47
    {
48 24
        foreach ($nodes as $node) {
49 24
            if (!($node instanceof FilesystemNodeInterface)) {
50 1
                throw new \Exception('Invalid filesystem node type.');
51
            }
52
53 24
            $node = $node->root();
54
55 24
            if ($this->getAttribute('id') === $node->getAttribute('id')) {
56 12
                $this->add($node[0]->setParent(null));
57
58 12
                continue;
59
            }
60
61
            // If the $cwd contains the nodechild.
62 24
            if (null !== $child = $this->contains($node)) {
63 3
                if (0 !== $node->degree()) {
64 3
                    $child->add($node[0]->setParent(null));
65
                }
66
67 3
                continue;
68
            }
69
70 24
            parent::add($node->setParent(null));
71
        }
72
73 24
        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...
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 16
    public function getPath(): Path
80
    {
81
        $paths = [
82 16
            $this->getAttribute('id'),
83
        ];
84
85 16
        foreach ($this->getAncestors() as $ancestor) {
86 15
            \array_unshift($paths, $ancestor->getAttribute('id'));
0 ignored issues
show
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

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

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