Passed
Push — feature/second-release ( 3d3b5e...855c58 )
by Andrea Marco
14:24
created

Enums::resolveTranslationKey()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 5
rs 10
ccs 2
cts 2
cp 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LaravelEnum;
6
7
use Cerbero\Enum\Enums as BaseEnums;
8
use Cerbero\LaravelEnum\Actions\OnCall;
9
use Closure;
10
use Generator;
11
use Illuminate\Support\Arr;
12
use Illuminate\Support\Facades\App;
13
use Illuminate\Support\Str;
14
use UnitEnum;
0 ignored issues
show
Bug introduced by
The type UnitEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
/**
17
 * The enums manager.
18
 */
19
class Enums extends BaseEnums
20
{
21
    /**
22
     * The logic to resolve the translation key.
23
     *
24
     * @var ?Closure(UnitEnum $case, string $method): string
25
     */
26
    protected static ?Closure $translateFrom = null;
27
28
    /**
29 1
     * The glob paths to find enums in.
30
     *
31 1
     * @var string[]
32
     */
33
    protected static array $paths = ['app/Enums'];
34
35
    /**
36
     * Set the logic to resolve the translation key.
37 4
     *
38
     * @param callable(UnitEnum $case, string $method): string $callback
39 4
     */
40 1
    public static function translateFrom(callable $callback): void
41 4
    {
42
        static::$translateFrom = $callback(...);
43
    }
44
45
    /**
46
     * Retrieve the translation key for the given case.
47
     */
48
    public static function resolveTranslationKey(UnitEnum $case, ?string $method = null): string
49 7
    {
50
        return static::$translateFrom
51 7
            ? (static::$translateFrom)($case, (string) $method)
52
            : sprintf('enums.%s.%s%s', $case::class, $case->name, $method ? ".{$method}" : '');
53
    }
54
55
    /**
56
     * Set the glob paths to find all the application enums.
57
     */
58
    public static function paths(string ...$paths): void
59
    {
60
        static::$paths = $paths;
61
    }
62
63
    /**
64
     * Yield the namespaces of all the application enums.
65
     *
66
     * @return Generator<int, class-string>
67
     */
68
    public static function namespaces(): Generator
69
    {
70
        $composer = json_decode((string) file_get_contents(App::basePath('composer.json')), true);
71
        /** @var array<string, string> */
72
        $psr4 = Arr::get((array) $composer, 'autoload.psr-4', []);
73
74
        foreach (static::$paths as $relativePath) {
75
            $glob = App::basePath(trim($relativePath, '/')) . '/*.php';
76
77
            foreach (glob($glob) ?: [] as $path) {
78
                foreach ($psr4 as $namespace => $relative) {
79
                    $absolute = Str::finish(App::basePath($relative), '/');
80
81
                    if (str_starts_with($path, $absolute)) {
82
                        $enum = str_replace([$absolute, '/', '.php'], [$namespace, '\\', ''], $path);
83
84
                        if (enum_exists($enum)) {
0 ignored issues
show
Bug introduced by
The function enum_exists was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
                        if (/** @scrutinizer ignore-call */ enum_exists($enum)) {
Loading history...
85
                            yield $enum;
86
                        }
87
                    }
88
                }
89
            }
90
        }
91
    }
92
93
    /**
94
     * Handle the call to an inaccessible case method.
95
     *
96
     * @param array<array-key, mixed> $arguments
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array-key, mixed> at position 2 could not be parsed: Unknown type name 'array-key' at position 2 in array<array-key, mixed>.
Loading history...
97
     */
98
    public static function handleCall(object $case, string $name, array $arguments): mixed
99
    {
100
        return (static::$onCall ?: new OnCall())($case, $name, $arguments);
101
    }
102
}
103