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

orchestral / sidekick / 14526914458

18 Apr 2025 12:02AM UTC coverage: 97.222% (+0.2%) from 97.059%
14526914458

Pull #18

github

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

4 of 4 new or added lines in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

70 of 72 relevant lines covered (97.22%)

3.18 hits per line

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

91.3
/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 (PHP_VERSION_ID >= 80100) {
14
    if (! \function_exists('Orchestra\Sidekick\enum_name')) {
15
        /**
16
         * Get the proper name from enum.
17
         *
18
         * @api
19
         *
20
         * @param  \BackedEnum|\UnitEnum  $enum
21
         * @return string
22
         */
23
        function enum_name(BackedEnum|UnitEnum $enum): string
24
        {
25
            return Str::title(str_replace('_', ' ', $enum->name));
5✔
26
        }
27
    }
28

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

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

54
if (! \function_exists('Orchestra\Sidekick\once')) {
55
    /**
56
     * Run callback only once.
57
     *
58
     * @api
59
     *
60
     * @param  mixed  $callback
61
     * @return \Closure():mixed
62
     */
63
    function once($callback): Closure
64
    {
65
        $response = new UndefinedValue;
5✔
66

67
        return function () use ($callback, &$response) {
5✔
68
            if ($response instanceof UndefinedValue) {
5✔
69
                $response = value($callback) ?? null;
5✔
70
            }
71

72
            return $response;
5✔
73
        };
5✔
74
    }
75
}
76

77
if (! \function_exists('Orchestra\Sidekick\join_paths')) {
78
    /**
79
     * Join the given paths together.
80
     *
81
     * @param  string|null  $basePath
82
     * @param  string  ...$paths
83
     * @return string
84
     */
85
    function join_paths(?string $basePath, string ...$paths): string
86
    {
87
        foreach ($paths as $index => $path) {
1✔
88
            if (empty($path) && $path !== '0') {
1✔
89
                unset($paths[$index]);
1✔
90
            } else {
91
                $paths[$index] = DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR);
1✔
92
            }
93
        }
94

95
        return $basePath.implode('', $paths);
1✔
96
    }
97
}
98

99
if (! \function_exists('Orchestra\Sidekick\is_symlink')) {
100
    /**
101
     * Determine if path is symlink for both Unix and Windows environment.
102
     *
103
     * @api
104
     *
105
     * @param  string  $path
106
     * @return bool
107
     */
108
    function is_symlink(string $path): bool
109
    {
110
        if (windows_os() && is_dir($path) && readlink($path) !== $path) {
1✔
UNCOV
111
            return true;
×
112
        } elseif (is_link($path)) {
1✔
UNCOV
113
            return true;
×
114
        }
115

116
        return false;
1✔
117
    }
118
}
119

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

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

162
        /** @var string $laravel */
163
        $laravel = transform(
164
            Application::VERSION,
165
            fn (string $version) => match ($version) {
166
                '13.x-dev' => '13.0.0',
167
                default => $version,
168
            }
169
        );
170

171
        if (\is_null($operator)) {
172
            return version_compare($laravel, $version);
173
        }
174

175
        return version_compare($laravel, $version, $operator);
176
    }
177
}
178

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

205
        /** @var string $phpunit */
206
        $phpunit = transform(
207
            Version::id(),
208
            fn (string $version) => match (true) {
209
                str_starts_with($version, '12.2-') => '12.2.0',
210
                default => $version,
211
            }
212
        );
213

214
        if (\is_null($operator)) {
215
            return version_compare($phpunit, $version);
216
        }
217

218
        return version_compare($phpunit, $version, $operator);
219
    }
220
}
221

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

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