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

orchestral / sidekick / 14635294164

24 Apr 2025 06:51AM UTC coverage: 94.444% (-1.9%) from 96.296%
14635294164

push

github

crynobone
wip

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

85 of 90 relevant lines covered (94.44%)

3.09 hits per line

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

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

3
namespace Orchestra\Sidekick;
4

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

12
if (! \function_exists('Orchestra\Sidekick\enum_name')) {
13
    /**
14
     * Get the proper name from enum.
15
     *
16
     * @api
17
     *
18
     * @throws \RuntimeException
19
     */
20
    function enum_name(BackedEnum|UnitEnum $enum): string
21
    {
22
        return mb_convert_case(str_replace('_', ' ', $enum->name), MB_CASE_TITLE, 'UTF-8');
5✔
23
    }
24
}
25

26
if (! \function_exists('Orchestra\Sidekick\enum_value')) {
27
    /**
28
     * Get the proper name from enum.
29
     *
30
     * @api
31
     *
32
     * @template TValue
33
     * @template TDefault
34
     *
35
     * @param  TValue  $value
36
     * @param  TDefault|callable(TValue): TDefault  $default
37
     * @return ($value is empty ? TDefault : mixed)
38
     *
39
     * @throws \RuntimeException
40
     */
41
    function enum_value(mixed $value, mixed $default = null): mixed
42
    {
43
        return match (true) {
44
            $value instanceof BackedEnum => $value->value,
20✔
45
            $value instanceof UnitEnum => $value->name,
16✔
46

47
            default => $value ?? value($default),
20✔
48
        };
49
    }
50
}
51

52
if (! \function_exists('Orchestra\Sidekick\join_paths')) {
53
    /**
54
     * Join the given paths together.
55
     *
56
     * @api
57
     */
58
    function join_paths(?string $basePath, string ...$paths): string
59
    {
60
        foreach ($paths as $index => $path) {
1✔
61
            if (empty($path) && $path !== '0') {
1✔
62
                unset($paths[$index]);
1✔
63
            } else {
64
                $paths[$index] = DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR);
1✔
65
            }
66
        }
67

68
        return $basePath.implode('', $paths);
1✔
69
    }
70
}
71

72
if (! \function_exists('Orchestra\Sidekick\once')) {
73
    /**
74
     * Run callback only once.
75
     *
76
     * @api
77
     *
78
     * @param  mixed  $callback
79
     * @return \Closure():mixed
80
     */
81
    function once($callback): Closure
82
    {
83
        $response = new UndefinedValue;
5✔
84

85
        return function () use ($callback, &$response) {
5✔
86
            if ($response instanceof UndefinedValue) {
5✔
87
                $response = value($callback) ?? null;
5✔
88
            }
89

90
            return $response;
5✔
91
        };
5✔
92
    }
93
}
94

95
if (! \function_exists('Orchestra\Sidekick\is_safe_callable')) {
96
    /**
97
     * Determine if the value is a callable and not a string matching an available function name.
98
     *
99
     * @api
100
     */
101
    function is_safe_callable(mixed $value): bool
102
    {
103
        if ($value instanceof Closure) {
6✔
104
            return true;
1✔
105
        }
106

107
        if (! \is_callable($value)) {
5✔
108
            return false;
2✔
109
        }
110

111
        if (\is_array($value)) {
3✔
112
            return \count($value) === 2 && array_is_list($value) && method_exists(...$value);
1✔
113
        }
114

115
        return ! \is_string($value);
2✔
116
    }
117
}
118

119
if (! \function_exists('Orchestra\Sidekick\is_symlink')) {
120
    /**
121
     * Determine if the path is a symlink for both Unix and Windows environments.
122
     *
123
     * @api
124
     */
125
    function is_symlink(string $path): bool
126
    {
127
        if (windows_os() && is_dir($path) && readlink($path) !== $path) {
1✔
128
            return true;
×
129
        } elseif (is_link($path)) {
1✔
130
            return true;
×
131
        }
132

133
        return false;
1✔
134
    }
135
}
136

137
if (! \function_exists('Orchestra\Sidekick\transform_relative_path')) {
138
    /**
139
     * Transform relative path.
140
     *
141
     * @api
142
     */
143
    function transform_relative_path(string $path, string $workingPath): string
144
    {
145
        return str_starts_with($path, './')
1✔
146
            ? rtrim($workingPath, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.mb_substr($path, 2)
1✔
147
            : $path;
1✔
148
    }
149
}
150

151
if (! \function_exists('Orchestra\Sidekick\laravel_version_compare')) {
152
    /**
153
     * Laravel version compare.
154
     *
155
     * @api
156
     *
157
     * @template TOperator of string|null
158
     *
159
     * @phpstan-param  TOperator  $operator
160
     *
161
     * @phpstan-return (TOperator is null ? int : bool)
162
     *
163
     * @codeCoverageIgnore
164
     */
165
    function laravel_version_compare(string $version, ?string $operator = null): int|bool
166
    {
167
        if (! class_exists(Application::class)) {
168
            throw new RuntimeException('Unable to verify Laravel Framework version');
169
        }
170

171
        /** @var string $laravel */
172
        $laravel = transform(
173
            Application::VERSION,
174
            fn (string $version) => match ($version) {
175
                '13.x-dev' => '13.0.0',
176
                default => $version,
177
            }
178
        );
179

180
        if (\is_null($operator)) {
181
            return version_compare($laravel, $version);
182
        }
183

184
        return version_compare($laravel, $version, $operator);
185
    }
186
}
187

188
if (! \function_exists('Orchestra\Sidekick\phpunit_version_compare')) {
189
    /**
190
     * PHPUnit version compare.
191
     *
192
     * @api
193
     *
194
     * @template TOperator of string|null
195
     *
196
     * @throws \RuntimeException
197
     *
198
     * @phpstan-param  TOperator  $operator
199
     *
200
     * @phpstan-return (TOperator is null ? int : bool)
201
     *
202
     * @codeCoverageIgnore
203
     */
204
    function phpunit_version_compare(string $version, ?string $operator = null): int|bool
205
    {
206
        if (! class_exists(Version::class)) {
207
            throw new RuntimeException('Unable to verify PHPUnit version');
208
        }
209

210
        /** @var string $phpunit */
211
        $phpunit = transform(
212
            Version::id(),
213
            fn (string $version) => match (true) {
214
                str_starts_with($version, '12.2-') => '12.2.0',
215
                default => $version,
216
            }
217
        );
218

219
        if (\is_null($operator)) {
220
            return version_compare($phpunit, $version);
221
        }
222

223
        return version_compare($phpunit, $version, $operator);
224
    }
225
}
226

227
if (! \function_exists('Orchestra\Sidekick\php_binary')) {
228
    /**
229
     * Determine the PHP Binary.
230
     *
231
     * @api
232
     *
233
     * @codeCoverageIgnore
234
     */
235
    function php_binary(): string
236
    {
237
        return (new PhpExecutableFinder)->find(false) ?: 'php';
238
    }
239
}
240

241
if (! \function_exists('Orchestra\Sidekick\windows_os')) {
242
    /**
243
     * Determine whether the current environment is Windows-based.
244
     *
245
     * @api
246
     *
247
     * @codeCoverageIgnore
248
     */
249
    function windows_os(): bool
250
    {
251
        return PHP_OS_FAMILY === 'Windows';
252
    }
253
}
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