Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
18 / 19
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApieFilesystem
94.74% covered (success)
94.74%
18 / 19
50.00% covered (danger)
50.00%
1 / 2
8.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 visit
94.44% covered (success)
94.44%
17 / 18
0.00% covered (danger)
0.00%
0 / 1
7.01
1<?php
2namespace Apie\ApieFileSystem;
3
4use Apie\ApieFileSystem\Virtual\RootFolder;
5use Apie\ApieFileSystem\Virtual\VirtualFileInterface;
6use Apie\ApieFileSystem\Virtual\VirtualFolderInterface;
7
8class ApieFilesystem
9{
10    public function __construct(public readonly RootFolder $rootFolder)
11    {
12    }
13
14    public function visit(string $path): VirtualFolderInterface|VirtualFileInterface|null
15    {
16        $path = ltrim($path, '/');
17        $current = $this->rootFolder;
18        $visited = [$current];
19        $paths = explode('/', $path);
20        while (!empty($paths)) {
21            $segment = array_shift($paths);
22            if ($segment === '.' || $segment === '') {
23                continue;
24            }
25            if ($segment === '..') {
26                $current = array_pop($visited);
27                if (empty($visited)) {
28                    throw new \LogicException('Invalid path: ' . $path);
29                }
30                continue;
31            }
32            $current = $current->getChild($segment);
33            if ($current === null) {
34                return null;
35            }
36            $visited[] = $current;
37        }
38        return $current;
39    }
40}