Passed
Push — master ( d14431...7ebb33 )
by Pol
02:16
created

Filesystem::doImport()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 14
ccs 0
cts 12
cp 0
crap 12
rs 10
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpvfs\Importer;
6
7
use drupol\phptree\Importer\ImporterInterface;
8
use drupol\phptree\Node\AttributeNode;
9
use drupol\phptree\Node\NodeInterface;
10
11
/**
12
 * Class Filesystem.
13
 */
14
class Filesystem implements ImporterInterface
15
{
16
    /**
17
     * Import data into a node.
18
     *
19
     * @param mixed $data
20
     *   The data to import
21
     *
22
     * @throws \Exception
23
     *
24
     * @return NodeInterface
25
     *   The new node
26
     */
27
    public function import($data): NodeInterface
28
    {
29
        if (!\is_string($data)) {
30
            throw new \Exception('Must be a string.');
31
        }
32
33
        if (!\file_exists($data)) {
34
            throw new \Exception('The directory doesn\'t exist.');
35
        }
36
37
        return $this->arrayToTree($this->doImport($data));
38
    }
39
40
    /**
41
     * Create a node.
42
     *
43
     * @param mixed $data
44
     *   The arguments
45
     *
46
     * @return \drupol\phptree\Node\NodeInterface
47
     *   The node
48
     */
49
    protected function createNode($data): NodeInterface
50
    {
51
        if (\is_file($data)) {
52
            return (new AttributeNode())
53
                ->setAttribute('id', $data)
54
                ->setAttribute('label', $data);
55
        }
56
57
        if (\is_dir($data)) {
58
            return (new AttributeNode())
59
                ->setAttribute('label', $data)
60
                ->setAttribute('shape', 'rectangle');
61
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 57 is false. This is incompatible with the type-hinted return drupol\phptree\Node\NodeInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
62
    }
63
64
    /**
65
     * Convert an array into a tree.
66
     *
67
     * @param array $data
68
     *
69
     * @return \drupol\phptree\Node\NodeInterface
70
     *   The tree
71
     */
72
    private function arrayToTree(array $data): NodeInterface
73
    {
74
        $data += [
75
            'children' => [],
76
        ];
77
78
        $node = $this->createNode($data['name']);
79
80
        foreach ($data['children'] as $key => $child) {
81
            $node->add($this->arrayToTree($child));
82
        }
83
84
        return $node;
85
    }
86
87
    /**
88
     * @param string $directory
89
     *
90
     * @return array
91
     */
92
    private function doImport(string $directory): array
93
    {
94
        $return = [
95
            'name' => $directory,
96
            'children' => [],
97
        ];
98
99
        if (false !== $items = \glob($directory . '/*')) {
100
            foreach ($items as $item) {
101
                $return['children'][] = $this->doImport($item);
102
            }
103
        }
104
105
        return $return;
106
    }
107
}
108