HasMakeMapper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 70
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFacadeAccessor() 0 4 1
A make() 0 13 2
A composeMakeParameters() 0 24 5
1
<?php
2
3
namespace Mindtwo\DynamicMutators\Traits;
4
5
use Illuminate\Support\Str;
6
use Mindtwo\DynamicMutators\Exceptions\InvalidParameterException;
7
use Mindtwo\DynamicMutators\Handler\MutationHandler;
8
9
trait HasMakeMapper
10
{
11
    /**
12
     * Get the registered name of the component.
13
     *
14
     * @return string
15
     */
16
    protected static function getFacadeAccessor()
17
    {
18
        return MutationHandler::class;
19
    }
20
21
    /**
22
     * Make new instance.
23
     *
24
     * @param array $arguments
25
     *
26
     * @throws InvalidParameterException
27
     *
28
     * @return object
29
     */
30
    public static function make(array $arguments = [])
31
    {
32
        $class = static::getFacadeAccessor();
33
34
        try {
35
            $reflection = new \ReflectionClass(static::getFacadeAccessor());
36
            $parameters = $reflection->getConstructor()->getParameters();
37
        } catch (\Throwable $error) {
38
            throw new \Exception('Wrong parameter', 0, $error);
39
        }
40
41
        return new $class(...static::composeMakeParameters($parameters, $arguments));
42
    }
43
44
    /**
45
     * Compose make() parameters.
46
     *
47
     * @param array $parameters
48
     * @param array $arguments
49
     *
50
     * @throws InvalidParameterException
51
     *
52
     * @return array
53
     */
54
    protected static function composeMakeParameters(array $parameters, array $arguments = []): array
55
    {
56
        foreach ($parameters as $key=>$parameter) {
57
            $name = $parameter->getName();
58
59
            if (array_key_exists($name, $arguments)) {
60
                $value = $arguments[$name];
61
            } elseif (array_key_exists(Str::snake($name), $arguments)) {
62
                $value = $arguments[Str::snake($name)];
63
            } elseif ($parameter->isDefaultValueAvailable()) {
64
                $value = $parameter->getDefaultValue();
65
            } else {
66
                throw new InvalidParameterException('Invalid parameters for make()!', 0, null, [
67
                    'name'       => $name,
68
                    'accessor'   => static::getFacadeAccessor(),
69
                    'parameters' => $parameters,
70
                    'arguments'  => $arguments,
71
                ]);
72
            }
73
            $result[$parameter->getPosition()] = $value;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
74
        }
75
76
        return $result ?? [];
77
    }
78
}
79