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

tito10047 / type-safe-id-bundle / 23183366968

17 Mar 2026 07:32AM UTC coverage: 91.88% (+13.3%) from 78.621%
23183366968

Pull #2

github

tito10047
rename command
Pull Request #2: V2

139 of 154 new or added lines in 4 files covered. (90.26%)

1 existing line in 1 file now uncovered.

215 of 234 relevant lines covered (91.88%)

31.46 hits per line

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

66.67
/src/Util/PathUtil.php
1
<?php
2

3
namespace Tito10047\TypeSafeIdBundle\Util;
4

5
use Composer\Autoload\ClassLoader;
6

7
class PathUtil
8
{
9
    public static function pathToNamespace(string $path): string
10
    {
11
        // This is now less used, but we keep it for backward compatibility if needed
12
        // Simple heuristic: assume src/ is at the root and skip it
13
        $namespace = preg_replace('/^src\//', '', $path);
30✔
14
        $namespace = str_replace('/', '\\', $namespace);
30✔
15
        return trim($namespace, '\\');
30✔
16
    }
17

18
    public static function namespaceToPath(string $namespace): string
19
    {
20
        $loader = self::getClassLoader();
60✔
21
        if (!$loader) {
60✔
22
            // Fallback for environments without Composer ClassLoader (e.g. simple tests)
23
            // Heuristic: App\ -> src/
NEW
24
            $path = str_replace('App\\', 'src/', $namespace);
×
NEW
25
            return str_replace('\\', '/', rtrim($path, '/'));
×
26
        }
27

28
        $namespaceWithBackslash = trim($namespace, '\\') . '\\';
60✔
29
        $prefixes = $loader->getPrefixesPsr4();
60✔
30
        
31
        // Sort prefixes by length descending to find the most specific match
32
        uksort($prefixes, function(string $a, string $b) {
60✔
33
            return strlen($b) <=> strlen($a);
24✔
34
        });
60✔
35

36
        foreach ($prefixes as $prefix => $paths) {
60✔
37
            if (str_starts_with($namespaceWithBackslash, $prefix)) {
60✔
38
                $relativePath = substr($namespaceWithBackslash, strlen($prefix));
54✔
39
                $path = rtrim($paths[0], '/') . '/' . str_replace('\\', '/', $relativePath);
54✔
40
                return rtrim($path, '/');
54✔
41
            }
42
        }
43

44
        // Fallback if not found in PSR-4 prefixes
45
        return str_replace('\\', '/', rtrim($namespace, '\\'));
6✔
46
    }
47

48
    public static function getClassLoader(): ?ClassLoader
49
    {
50
        foreach (spl_autoload_functions() as $autoloader) {
60✔
51
            if (!is_array($autoloader)) {
60✔
NEW
52
                continue;
×
53
            }
54

55
            if ($autoloader[0] instanceof ClassLoader) {
60✔
56
                return $autoloader[0];
60✔
57
            }
58

NEW
59
            if (method_exists($autoloader[0], 'getClassLoader')) {
×
NEW
60
                $loader = $autoloader[0]->getClassLoader();
×
NEW
61
                if (is_array($loader) && isset($loader[0]) && $loader[0] instanceof ClassLoader) {
×
NEW
62
                    return $loader[0];
×
63
                }
NEW
64
                if ($loader instanceof ClassLoader) {
×
NEW
65
                    return $loader;
×
66
                }
67
            }
68
        }
69

NEW
70
        return null;
×
71
    }
72
}
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

© 2026 Coveralls, Inc