-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathNormalizer.php
More file actions
68 lines (54 loc) · 1.74 KB
/
Copy pathPathNormalizer.php
File metadata and controls
68 lines (54 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
declare(strict_types=1);
namespace WebProject\Codeception\Module\AiReporter\Report;
use function ltrim;
use function rtrim;
use function str_contains;
use function str_replace;
use function str_starts_with;
use function strlen;
use function strtolower;
final class PathNormalizer
{
private readonly string $normalizedRoot;
private readonly string $normalizedRootLower;
public function __construct(string $projectRoot, private readonly bool $compactPaths)
{
$normalized = str_replace('\\', '/', rtrim($projectRoot, '/\\'));
$this->normalizedRoot = $normalized . '/';
$this->normalizedRootLower = strtolower($this->normalizedRoot);
}
/**
* @phpstan-return ($path is null ? null : string)
*/
public function normalize(?string $path): ?string
{
if (null === $path) {
return null;
}
if ('' === $path) {
return '';
}
$normalized = str_replace('\\', '/', $path);
if (!$this->compactPaths) {
return $normalized;
}
if ($this->startsWithProjectRoot($normalized)) {
return ltrim((string) substr($normalized, strlen($this->normalizedRoot)), '/');
}
return $normalized;
}
public function isVendorPath(?string $path): bool
{
if (null === $path || '' === $path) {
return false;
}
$normalized = strtolower(str_replace('\\', '/', $path));
return str_contains($normalized, '/vendor/');
}
private function startsWithProjectRoot(string $path): bool
{
return str_starts_with($path, $this->normalizedRoot)
|| str_starts_with(strtolower($path), $this->normalizedRootLower);
}
}