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

orchestral / sidekick / 14527191037

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

Pull #18

github

web-flow
Merge 12a2847b1 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%)

1 existing line in 1 file now uncovered.

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
    function enum_value(mixed $value, mixed $default = null): mixed
48
    {
49
        if (PHP_VERSION_ID < 80100) {
20✔
NEW
50
            throw new RuntimeException(\sprintf('%s requires PHP 8.1 and above', __FUNCTION__));
×
51
        }
52

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

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

62
if (! \function_exists('Orchestra\Sidekick\once')) {
63
    /**
64
     * Run callback only once.
65
     *
66
     * @api
67
     *
68
     * @param  mixed  $callback
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
     * @param  string|null  $basePath
90
     * @param  string  ...$paths
91
     * @return string
92
     */
93
    function join_paths(?string $basePath, string ...$paths): string
94
    {
95
        foreach ($paths as $index => $path) {
1✔
96
            if (empty($path) && $path !== '0') {
1✔
97
                unset($paths[$index]);
1✔
98
            } else {
99
                $paths[$index] = DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR);
1✔
100
            }
101
        }
102

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

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

124
        return false;
1✔
125
    }
126
}
127

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

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

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

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

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

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

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

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

226
        return version_compare($phpunit, $version, $operator);
227
    }
228
}
229

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

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