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

liqueurdetoile / wordpress-bundler / 3842803254

pending completion
3842803254

push

github

Liqueur de Toile
tests: Fix tests

328 of 459 relevant lines covered (71.46%)

8.19 hits per line

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

91.67
/src/Resolver.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Lqdt\WordpressBundler;
5

6
use ComposerLocator;
7
use Symfony\Component\Filesystem\Path;
8

9
/**
10
 * Utility class to transform and resolve paths in current project
11
 *
12
 * @author Liqueur de Toile <contact@liqueurdetoile.com>
13
 * @copyright 2022-present Liqueur de Toile
14
 * @license GPL-3.0-or-later (https://www.gnu.org/licenses/gpl-3.0.html)
15
 */
16
class Resolver
17
{
18
    /**
19
     * Stores base path to resolve from
20
     *
21
     * @var string|null
22
     */
23
    protected static $_rootpath;
24

25
    /**
26
     * Get resolver root path
27
     *
28
     * This root path will be used by resolver to build absolute path.
29
     * If no path is provided as argument, the project root path will be returned.
30
     * If the provided path is relative, it will be made absolute from the root of the project, otherwise it will be normalized and returned
31
     *
32
     * @param  string|null $rootpath Overrides base path if provided
33
     * @throws \Lqdt\WordpressBundler\Exception\InvalidRootPathResolverException If path doesn't match a valid folder
34
     * @return string
35
     */
36
    public static function getRootPath(?string $rootpath = null): string
37
    {
38
        if (empty(self::$_rootpath)) {
48✔
39
            self::$_rootpath = self::normalize(ComposerLocator::getRootPath());
×
40
        }
41

42
        if ($rootpath === null) {
48✔
43
            return self::$_rootpath;
48✔
44
        }
45

46
        $rootpath = Path::makeAbsolute($rootpath, self::$_rootpath);
26✔
47

48
        return self::normalize($rootpath);
26✔
49
    }
50

51
    /**
52
     * Checks that a path is a subpath of another
53
     *
54
     * @param string $path Path to check
55
     * @param string $basepath Path to compare with
56
     * @return bool
57
     */
58
    public static function isSubPath(string $path, string $basepath): bool
59
    {
60
        return Path::isBasePath($basepath, $path);
6✔
61
    }
62

63
    /**
64
     * Makes a path absolute
65
     * If no base path provided, project root path will be used
66
     *
67
     * @param  string      $path     Path
68
     * @param  string|null $basepath Base path to use
69
     * @return string  Absolute path
70
     */
71
    public static function makeAbsolute(string $path, ?string $basepath = null): string
72
    {
73
        return Path::makeAbsolute($path, self::getRootPath($basepath));
47✔
74
    }
75

76
    /**
77
     * Makes a path relative
78
     * If no base path provided, project basepath will be used
79
     *
80
     * @param  string      $path     Path
81
     * @param  string|null $basepath Base path to use
82
     * @return string  Absolute path
83
     */
84
    public static function makeRelative(string $path, ?string $basepath = null): string
85
    {
86
        return Path::makeRelative($path, self::getRootPath($basepath));
19✔
87
    }
88

89
    /**
90
     * Normalize a path
91
     *
92
     * @param  string $path Path to normalize
93
     * @return string Normalized path
94
     */
95
    public static function normalize(string $path): string
96
    {
97
        return Path::normalize($path);
26✔
98
    }
99

100
    /**
101
     * Resolves a path/glob, optionnally from a given base path for relative ones
102
     *
103
     * The input can be either :
104
     * - an absolute path
105
     * - a relative path
106
     *
107
     * Relative paths will be evaluated from the provided base path as second argument or project root if not provided
108
     * Glob patterns can be used as path
109
     *
110
     * If path does not match any file or folder, an empty array will be returned
111
     *
112
     * The key of items are the relative path to basepath and the value is the absolute path
113
     *
114
     * @param  string      $path         Path to resolve
115
     * @param  string|null $basepath     Base path to resolve from
116
     * @return array<string,string> Resolved path(s) or false in case of error
117
     */
118
    public static function resolve(string $path, ?string $basepath = null): array
119
    {
120
        $basepath = self::getRootPath($basepath);
20✔
121

122
        if (!is_dir($basepath)) {
20✔
123
            throw new \RuntimeException('[WordpressBundler] Resolver is unable to locate base folder');
×
124
        }
125

126
        $path = self::makeAbsolute($path, $basepath);
20✔
127

128
        if (is_file($path)) {
20✔
129
            return [self::makeRelative($path, $basepath) => $path];
18✔
130
        }
131

132
        if (is_dir($path)) {
18✔
133
            return [self::makeRelative($path, $basepath) => $path];
15✔
134
        }
135

136
        $paths = glob($path) ?: [];
16✔
137

138
        return self::resolveMany($paths, $basepath);
16✔
139
    }
140

141
    /**
142
     * Resolves an array of paths
143
     *
144
     * @param  array<string> $paths    Patterns to resolve
145
     * @param  string|null   $basepath Base path to resolve from
146
     * @return array<string,string>, string> Resolved path(s) or false in case of error
147
     * @see Resolver::resolve
148
     */
149
    public static function resolveMany(array $paths, ?string $basepath = null): array
150
    {
151
        $ret = [];
16✔
152

153
        foreach ($paths as $path) {
16✔
154
            $ret += self::resolve($path, $basepath);
15✔
155
        }
156

157
        return $ret;
16✔
158
    }
159
}
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