• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 11474462544

23 Oct 2024 06:39AM UTC coverage: 81.448% (-0.7%) from 82.194%
11474462544

Pull #513

github

web-flow
Merge 4e432caf1 into 219bda667
Pull Request #513: feat(console): add ability to publish vendor files

52 of 140 new or added lines in 6 files covered. (37.14%)

6 existing lines in 3 files now uncovered.

6884 of 8452 relevant lines covered (81.45%)

41.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.45
/src/Tempest/Support/src/PathHelper.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Support;
6

7
use Exception;
8
use Tempest\Core\Composer;
9
use Tempest\Core\Kernel;
10
use function Tempest\get;
11

12
final readonly class PathHelper
13
{
NEW
14
    public static function root(string ...$paths): string
×
15
    {
NEW
16
        return static::make(get(Kernel::class)->root, ...$paths);
×
17
    }
18

19
    private static function prapareStringForNamespace(string $path, string $root = ''): StringHelper
3✔
20
    {
21
        $normalized = str($path)
3✔
22
            ->replaceStart($root, '')
3✔
23
            ->replaceStart('/', '')
3✔
24
            ->replace(['/', '//'], '\\');
3✔
25

26
        // If the path is a to a PHP file, we exclude the file name. Otherwise,
27
        // it's a path to a directory, which should be included in the namespace.
28
        if ($normalized->endsWith('.php')) {
3✔
29
            return $normalized->beforeLast(['/', '\\']);
3✔
30
        }
31

32
        return $normalized;
2✔
33
    }
34

35
    public static function toNamespace(string $path, string $root = ''): string
1✔
36
    {
37
        $path = static::prapareStringForNamespace($path, $root)->replaceEnd('\\', '');
1✔
38

39
        return arr(explode('\\', (string) $path))
1✔
40
            ->map(fn (string $segment) => (string) str($segment)->pascal())
1✔
41
            ->implode('\\')
1✔
42
            ->toString();
1✔
43
    }
44

45
    public static function toRegisteredNamespace(string $path): string
2✔
46
    {
47
        $composer = get(Composer::class);
2✔
48
        $kernel = get(Kernel::class);
2✔
49

50
        $relativePath = static::prapareStringForNamespace($path, $kernel->root)
2✔
51
            ->replaceEnd('\\', '')
2✔
52
            ->replace('\\', '/')
2✔
53
            ->finish('/');
2✔
54

55
        foreach ($composer->namespaces as $namespace) {
2✔
56
            if ($relativePath->startsWith($namespace->path)) {
2✔
57
                return (string) $relativePath
1✔
58
                    ->replace($namespace->path, $namespace->namespace)
1✔
59
                    ->replace(['/', '//'], '\\')
1✔
60
                    ->replaceEnd('.php', '')
1✔
61
                    ->replaceEnd('\\', '');
1✔
62
            }
63
        }
64

65
        throw new Exception(sprintf('No registered namespace matches the specified path [%s].', $path));
1✔
66
    }
67

68
    public static function make(string ...$paths): string
302✔
69
    {
70
        // Split paths items on forward and backward slashes
71
        $parts = array_reduce($paths, fn (array $carry, string $part) => [...$carry, ...explode('/', $part)], []);
302✔
72
        $parts = array_reduce($parts, fn (array $carry, string $part) => [...$carry, ...explode('\\', $part)], []);
302✔
73

74
        // Trim forward and backward slashes
75
        $parts = array_map(fn (string $part) => trim($part, '/\\'), $parts);
302✔
76
        $parts = array_filter($parts);
302✔
77

78
        // Glue parts together
79
        $path = implode(DIRECTORY_SEPARATOR, $parts);
302✔
80

81
        // Add DIRECTORY_SEPARATOR if first entry starts with forward- or backward slash
82
        $firstEntry = $paths[0];
302✔
83
        if (str_starts_with($firstEntry, '/') || str_starts_with($firstEntry, '\\')) {
302✔
84
            $path = DIRECTORY_SEPARATOR . $path;
296✔
85
        }
86

87
        // Add DIRECTORY_SEPARATOR if last entry ends with forward- or backward slash
88
        $lastEntry = $paths[count($paths) - 1];
302✔
89
        if ((count($paths) > 1 || strlen($lastEntry) > 1) && (str_ends_with($lastEntry, '/') || str_ends_with($lastEntry, '\\'))) {
302✔
90
            $path .= DIRECTORY_SEPARATOR;
294✔
91
        }
92

93
        return $path;
302✔
94
    }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc