Passed
Push — develop ( 9b8c4e...79e7ae )
by Andrea Marco
03:34 queued 14s
created

OnCall::translate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 2
b 0
f 0
nc 3
nop 3
dl 0
loc 16
ccs 8
cts 8
cp 1
crap 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cerbero\LaravelEnum\Actions;
6
7
use Cerbero\LaravelEnum\Enums;
8
use Illuminate\Support\Facades\App;
9
use Illuminate\Support\Facades\Lang;
10
use InvalidArgumentException;
11
use Throwable;
12
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...
13
14
use function Cerbero\LaravelEnum\namespaceExists;
15
16
/**
17
 * The logic to handle an inaccessible case method call.
18
 */
19
class OnCall
20
{
21
    /**
22
     * Handle the call to an inaccessible case method.
23
     *
24
     * @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...
25
     * @throws InvalidArgumentException
26
     * @throws \ValueError
27
     */
28 9
    public function __invoke(UnitEnum $case, string $name, array $arguments): mixed
29
    {
30
        try {
31 9
            $value = $case->resolveMetaAttribute($name);
32 5
        } catch (Throwable $e) {
33 5
            return $this->translate($case, $name, $arguments) ?? throw $e;
34
        }
35
36
        return match (true) {
37 4
            ! is_string($value) => $value, /** @phpstan-ignore-next-line argument.type */
38 4
            method_exists($value, '__invoke') => call_user_func_array(App::make($value), $arguments),
39 2
            namespaceExists($value) => App::make($value, $arguments),
40 4
            default => $value,
41
        };
42
    }
43
44
    /**
45
     * Retrieve the translation of the given key.
46
     *
47
     * @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...
48
     * @throws InvalidArgumentException
49
     */
50 5
    protected function translate(UnitEnum $case, string $name, array $arguments): ?string
51
    {
52 5
        $key = Enums::resolveTranslationKey($case, $name);
53
54 5
        if (Lang::get($key) === $key) {
55 1
            return null;
56
        }
57
58 4
        if ($arguments && array_is_list($arguments)) {
0 ignored issues
show
Bug introduced by
The function array_is_list 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

58
        if ($arguments && /** @scrutinizer ignore-call */ array_is_list($arguments)) {
Loading history...
Bug Best Practice introduced by
The expression $arguments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
59 1
            $method = sprintf('%s::%s->%s()', $case::class, $case->name, $name);
60
61 1
            throw new InvalidArgumentException("The method {$method} must be called with its named arguments");
62
        }
63
64
        /** @var string */
65 3
        return Lang::get($key, $arguments);
66
    }
67
}
68