Lines 83.33% 5 / 6
Functions and Methods 66.66% 2 / 3
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ProjectDirectory 83.33% 5 / 6 66.66% 2 / 3 6.17 0.00% 0 / 1
 __construct 100.00% 4 / 4 100.00% 1 / 1 4
 resolveWithin 100.00% 1 / 1 100.00% 1 / 1 1
 __toString 0.00% 0 / 1 0.00% 0 / 1 2
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Config;
6
7use Stewart\Runtime\Exception\ConfigurationException;
8use Stringable;
9
10final readonly class ProjectDirectory implements Stringable
11{
12    public string $value;
13
14    /** @throws ConfigurationException */
15    public function __construct(string $value)
16    {
17        $directory = rtrim($value, '/');
18
19        // Restricted to the project: the result goes to a writer that deletes files.
20        if ($directory === '' || str_starts_with($directory, '/') || \in_array('..', explode('/', $directory), true)) {
21            throw ConfigurationException::valueInvalid($value, 'a path inside the project, such as "generated"');
22        }
23
24        $this->value = $directory;
25    }
26
27    public function resolveWithin(string $projectRoot): string
28    {
29        return $projectRoot . '/' . $this->value;
30    }
31
32    public function __toString(): string
33    {
34        return $this->value;
35    }
36}