Completed
Push — master ( e6850e...e5139b )
by Pol
02:35 queued 21s
created

Cd   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 36
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A exec() 0 26 5
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace drupol\phpvfs\Command;
6
7
use drupol\phpvfs\Filesystem\FilesystemInterface;
8
use drupol\phpvfs\Node\DirectoryInterface;
9
use drupol\phpvfs\Utils\Path;
10
11
class Cd
12
{
13
    /**
14
     * @param \drupol\phpvfs\Filesystem\FilesystemInterface $vfs
15
     * @param string $id
16
     *
17
     * @throws \Exception
18
     *
19
     * @return \drupol\phpvfs\Node\DirectoryInterface
20
     */
21 2
    public static function exec(FilesystemInterface $vfs, string $id): DirectoryInterface
22
    {
23 2
        $path = Path::fromString($id);
24
25
        /** @var \drupol\phpvfs\Node\DirectoryInterface $cwd */
26 2
        $cwd = $path->isAbsolute() ?
27 2
            $vfs->getCwd()->root() :
28 2
            $vfs->getCwd();
29
30 2
        if ($path->isRoot()) {
31 1
            $vfs->setCwd($cwd);
32
33 1
            return $cwd;
34
        }
35
36 2
        foreach ($path->getIterator() as $pathPart) {
37 2
            if (null !== $child = $cwd->containsAttributeId($pathPart)) {
38 1
                $cwd = $child;
39
            } else {
40 1
                throw new \Exception('Unknown directory.');
41
            }
42
        }
43
44 1
        $vfs->setCwd($cwd);
45
46 1
        return $cwd;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $cwd could return the type drupol\phpvfs\Node\FilesystemNodeInterface which includes types incompatible with the type-hinted return drupol\phpvfs\Node\DirectoryInterface. Consider adding an additional type-check to rule them out.
Loading history...
47
    }
48
}
49