Passed
Push — feature/second-release ( 176745...89fc3a )
by Andrea Marco
02:27
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 UnitEnum $case
25
     * @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...
26
     * @throws InvalidArgumentException
27
     * @throws \ValueError
28
     */
29 9
    public function __invoke(object $case, string $name, array $arguments): mixed
30
    {
31
        try {
32 9
            $value = $case->resolveMetaAttribute($name);
33 5
        } catch (Throwable $e) {
34 5
            return $this->translate($case, $name, $arguments) ?: throw $e;
35
        }
36
37
        return match (true) {
38 4
            ! is_string($value) => $value, /** @phpstan-ignore-next-line argument.type */
39 4
            method_exists($value, '__invoke') => call_user_func_array(App::make($value), $arguments),
40 2
            namespaceExists($value) => App::make($value, $arguments),
41 4
            default => $value,
42
        };
43
    }
44
45
    /**
46
     * Retrieve the translation of the given key.
47
     *
48
     * @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...
49
     * @throws InvalidArgumentException
50
     */
51 5
    protected function translate(UnitEnum $case, string $name, array $arguments): ?string
52
    {
53 5
        $key = Enums::resolveTranslationKey($case, $name);
54
55 5
        if ($key === Lang::get($key)) {
56 1
            return null;
57
        }
58
59 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

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