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

Directory::create()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
nc 5
nop 2
dl 0
loc 25
ccs 13
cts 14
cp 0.9286
crap 5.009
rs 9.4888
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpvfs\Node;
6
7
use drupol\phpvfs\Utils\Path;
8
9
class Directory extends Vfs implements DirectoryInterface
10
{
11
    /**
12
     * @param string $id
13
     *
14
     * @return null|\drupol\phpvfs\Node\DirectoryInterface|\drupol\phpvfs\Node\FileInterface|\drupol\phpvfs\Node\VfsInterface
15
     */
16 1
    public function containsAttributeId(string $id): ?VfsInterface
17
    {
18
        /** @var \drupol\phpvfs\Node\VfsInterface $child */
19 1
        foreach ($this->children() as $child) {
20 1
            if ($child->getAttribute('id') === $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

20
            if ($child->/** @scrutinizer ignore-call */ getAttribute('id') === $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...
21 1
                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...
22
            }
23
        }
24
25 1
        return null;
26
    }
27
28
    /**
29
     * @param string $id
30
     * @param array $attributes
31
     *
32
     * @throws \Exception
33
     *
34
     * @return \drupol\phpvfs\Node\Directory
35
     */
36 1
    public static function create(string $id, array $attributes = [])
37
    {
38 1
        $path = Path::fromString($id);
39
40 1
        if (\DIRECTORY_SEPARATOR !== $id && false !== \strpos($id, \DIRECTORY_SEPARATOR)) {
41 1
            if ($path->isAbsolute()) {
42 1
                $firstPart = \DIRECTORY_SEPARATOR;
43
            } else {
44
                $firstPart = $path->shift();
45
            }
46
47 1
            $root = self::create($firstPart, $attributes);
48
49 1
            foreach ($path->getIterator() as $pathPart) {
50 1
                $child = new self(['id' => $pathPart]);
51 1
                $root->add($child);
52 1
                $root = $child;
53
            }
54
55 1
            return $root;
56
        }
57
58 1
        $attributes = ['id' => $id] + $attributes;
59
60 1
        return new self($attributes);
61
    }
62
63
    /**
64
     * @param string $id
65
     *
66
     * @throws \Exception
67
     *
68
     * @return \drupol\phptree\Node\NodeInterface|\drupol\phpvfs\Node\DirectoryInterface
69
     */
70 1
    public function mkdir(string $id)
71
    {
72 1
        $dir = self::create($id);
73
74 1
        return $this->add($dir->root());
75
    }
76
}
77