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

orchestral / sidekick / 14527419607

18 Apr 2025 12:47AM UTC coverage: 94.737% (-2.3%) from 97.059%
14527419607

Pull #18

github

web-flow
Merge 09d1527c5 into e7297e328
Pull Request #18: Add `Orchestra\Sidekick\enum_name()` and `Orchestra\Sidekick\enum_value()` functions

6 of 8 new or added lines in 1 file covered. (75.0%)

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
     * @return string
21
     *
22
     * @throws \RuntimeException
23
     */
24
    function enum_name($enum): string
25
    {
26
        if (PHP_VERSION_ID < 80100) {
5✔
NEW
27
            throw new RuntimeException(\sprintf('%s requires PHP 8.1 and above', __FUNCTION__));
×
28
        }
29

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

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

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

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

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

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

82
            return $response;
5✔
83
        };
5✔
84
    }
85
}
86

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

105
        return $basePath.implode('', $paths);
1✔
106
    }
107
}
108

109
if (! \function_exists('Orchestra\Sidekick\is_symlink')) {
110
    /**
111
     * Determine if path is symlink for both Unix and Windows environment.
112
     *
113
     * @api
114
     *
115
     * @param  string  $path
116
     * @return bool
117
     */
118
    function is_symlink(string $path): bool
119
    {
120
        if (windows_os() && is_dir($path) && readlink($path) !== $path) {
1✔
121
            return true;
×
122
        } elseif (is_link($path)) {
1✔
123
            return true;
×
124
        }
125

126
        return false;
1✔
127
    }
128
}
129

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

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

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

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

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

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

215
        /** @var string $phpunit */
216
        $phpunit = transform(
217
            Version::id(),
218
            fn (string $version) => match (true) {
219
                str_starts_with($version, '12.2-') => '12.2.0',
220
                default => $version,
221
            }
222
        );
223

224
        if (\is_null($operator)) {
225
            return version_compare($phpunit, $version);
226
        }
227

228
        return version_compare($phpunit, $version, $operator);
229
    }
230
}
231

232
if (! \function_exists('Orchestra\Sidekick\php_binary')) {
233
    /**
234
     * Determine the PHP Binary.
235
     *
236
     * @api
237
     *
238
     * @return string
239
     *
240
     * @codeCoverageIgnore
241
     */
242
    function php_binary(): string
243
    {
244
        return (new PhpExecutableFinder)->find(false) ?: 'php';
245
    }
246
}
247

248
if (! \function_exists('Orchestra\Sidekick\windows_os')) {
249
    /**
250
     * Determine whether the current environment is Windows based.
251
     *
252
     * @api
253
     *
254
     * @return bool
255
     *
256
     * @codeCoverageIgnore
257
     */
258
    function windows_os(): bool
259
    {
260
        return PHP_OS_FAMILY === 'Windows';
261
    }
262
}
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