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

orchestral / sidekick / 14528015860

18 Apr 2025 01:42AM UTC coverage: 94.737%. Remained the same
14528015860

push

github

crynobone
Merge branch '1.1.x' into 1.2.x

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

72 of 76 relevant lines covered (94.74%)

3.34 hits per line

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

85.19
/src/functions.php
1
<?php
2

3
namespace Orchestra\Sidekick;
4

5
use BackedEnum;
6
use Closure;
7
use Illuminate\Foundation\Application;
8
use Illuminate\Support\Str;
9
use PHPUnit\Runner\Version;
10
use RuntimeException;
11
use UnitEnum;
12

13
if (! \function_exists('Orchestra\Sidekick\enum_name')) {
14
    /**
15
     * Get the proper name from enum.
16
     *
17
     * @api
18
     *
19
     * @param  \BackedEnum|\UnitEnum  $enum
20
     *
21
     * @throws \RuntimeException
22
     */
23
    function enum_name($enum): string
24
    {
25
        if (PHP_VERSION_ID < 80100) {
5✔
26
            throw new RuntimeException(\sprintf('%s requires PHP 8.1 and above', __FUNCTION__));
×
27
        }
28

29
        return Str::title(str_replace('_', ' ', $enum->name));
5✔
30
    }
31
}
32

33
if (! \function_exists('Orchestra\Sidekick\enum_value')) {
34
    /**
35
     * Get the proper name from enum.
36
     *
37
     * @api
38
     *
39
     * @template TValue
40
     * @template TDefault
41
     *
42
     * @param  TValue  $value
43
     * @param  TDefault|callable(TValue): TDefault  $default
44
     * @return ($value is empty ? TDefault : mixed)
45
     *
46
     * @throws \RuntimeException
47
     */
48
    function enum_value(mixed $value, mixed $default = null): mixed
49
    {
50
        if (PHP_VERSION_ID < 80100) {
20✔
51
            throw new RuntimeException(\sprintf('%s requires PHP 8.1 and above', __FUNCTION__));
×
52
        }
53

54
        return match (true) {
55
            $value instanceof BackedEnum => $value->value,
20✔
56
            $value instanceof UnitEnum => $value->name,
16✔
57

58
            default => $value ?? value($default),
20✔
59
        };
60
    }
61
}
62

63
if (! \function_exists('Orchestra\Sidekick\once')) {
64
    /**
65
     * Run callback only once.
66
     *
67
     * @api
68
     *
69
     * @return \Closure():mixed
70
     */
71
    function once($callback): Closure
72
    {
73
        $response = new UndefinedValue;
5✔
74

75
        return function () use ($callback, &$response) {
5✔
76
            if ($response instanceof UndefinedValue) {
5✔
77
                $response = value($callback) ?? null;
5✔
78
            }
79

80
            return $response;
5✔
81
        };
5✔
82
    }
83
}
84

85
if (! \function_exists('Orchestra\Sidekick\join_paths')) {
86
    /**
87
     * Join the given paths together.
88
     *
89
     * @api
90
     */
91
    function join_paths(?string $basePath, string ...$paths): string
92
    {
93
        foreach ($paths as $index => $path) {
1✔
94
            if (empty($path) && $path !== '0') {
1✔
95
                unset($paths[$index]);
1✔
96
            } else {
97
                $paths[$index] = DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR);
1✔
98
            }
99
        }
100

101
        return $basePath.implode('', $paths);
1✔
102
    }
103
}
104

105
if (! \function_exists('Orchestra\Sidekick\is_symlink')) {
106
    /**
107
     * Determine if the path is a symlink for both Unix and Windows environments.
108
     *
109
     * @api
110
     */
111
    function is_symlink(string $path): bool
112
    {
113
        if (windows_os() && is_dir($path) && readlink($path) !== $path) {
1✔
114
            return true;
×
115
        } elseif (is_link($path)) {
1✔
116
            return true;
×
117
        }
118

119
        return false;
1✔
120
    }
121
}
122

123
if (! \function_exists('Orchestra\Sidekick\transform_relative_path')) {
124
    /**
125
     * Transform relative path.
126
     *
127
     * @api
128
     */
129
    function transform_relative_path(string $path, string $workingPath): string
130
    {
131
        return str_starts_with($path, './')
1✔
132
            ? rtrim($workingPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.mb_substr($path, 2)
1✔
133
            : $path;
1✔
134
    }
135
}
136

137
if (! \function_exists('Orchestra\Sidekick\laravel_version_compare')) {
138
    /**
139
     * Laravel version compare.
140
     *
141
     * @api
142
     *
143
     * @template TOperator of string|null
144
     *
145
     * @phpstan-param  TOperator  $operator
146
     *
147
     * @phpstan-return (TOperator is null ? int : bool)
148
     *
149
     * @codeCoverageIgnore
150
     */
151
    function laravel_version_compare(string $version, ?string $operator = null): int|bool
152
    {
153
        if (! class_exists(Application::class)) {
154
            throw new RuntimeException('Unable to verify Laravel Framework version');
155
        }
156

157
        /** @var string $laravel */
158
        $laravel = transform(
159
            Application::VERSION,
160
            fn (string $version) => match ($version) {
161
                '13.x-dev' => '13.0.0',
162
                default => $version,
163
            }
164
        );
165

166
        if (\is_null($operator)) {
167
            return version_compare($laravel, $version);
168
        }
169

170
        return version_compare($laravel, $version, $operator);
171
    }
172
}
173

174
if (! \function_exists('Orchestra\Sidekick\phpunit_version_compare')) {
175
    /**
176
     * PHPUnit version compare.
177
     *
178
     * @api
179
     *
180
     * @template TOperator of string|null
181
     *
182
     * @throws \RuntimeException
183
     *
184
     * @phpstan-param  TOperator  $operator
185
     *
186
     * @phpstan-return (TOperator is null ? int : bool)
187
     *
188
     * @codeCoverageIgnore
189
     */
190
    function phpunit_version_compare(string $version, ?string $operator = null): int|bool
191
    {
192
        if (! class_exists(Version::class)) {
193
            throw new RuntimeException('Unable to verify PHPUnit version');
194
        }
195

196
        /** @var string $phpunit */
197
        $phpunit = transform(
198
            Version::id(),
199
            fn (string $version) => match (true) {
200
                str_starts_with($version, '12.2-') => '12.2.0',
201
                default => $version,
202
            }
203
        );
204

205
        if (\is_null($operator)) {
206
            return version_compare($phpunit, $version);
207
        }
208

209
        return version_compare($phpunit, $version, $operator);
210
    }
211
}
212

213
if (! \function_exists('Orchestra\Sidekick\php_binary')) {
214
    /**
215
     * Determine the PHP Binary.
216
     *
217
     * @api
218
     *
219
     * @codeCoverageIgnore
220
     */
221
    function php_binary(): string
222
    {
223
        return (new PhpExecutableFinder)->find(false) ?: 'php';
224
    }
225
}
226

227
if (! \function_exists('Orchestra\Sidekick\windows_os')) {
228
    /**
229
     * Determine whether the current environment is Windows-based.
230
     *
231
     * @api
232
     *
233
     * @codeCoverageIgnore
234
     */
235
    function windows_os(): bool
236
    {
237
        return PHP_OS_FAMILY === 'Windows';
238
    }
239
}
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