Passed
Push — master ( bd5d77...4b6223 )
by Thorsten
02:02
created

FromArrayTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 48
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D fromArray() 0 45 10
1
<?php
2
/**
3
 * This file is part of the daikon-cqrs/message-bus project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Daikon\MessageBus;
12
13
trait FromArrayTrait
14
{
15
    public static function fromArray(array $arrayState): MessageInterface
16
    {
17
        $classReflection = new \ReflectionClass(static::class);
18
        $classes = [ $classReflection ];
19
        $parent = $classReflection;
20
        while ($parent = $parent->getParentClass()) {
21
            $classes[] = $parent;
22
        }
23
        $valueFactories = [];
24
        foreach ($classes as $curClass) {
25
            $classProps = $curClass->getProperties();
26
            foreach ($classProps as $prop) {
27
                $propName = $prop->getName();
28
                $docBlock = $prop->getDocComment();
29
                if (!preg_match("/@MessageBus::deserialize\((.*)\)/", $docBlock, $matches)) {
30
                    continue;
31
                }
32
                $factoryMethod = $matches[1];
33
                if ($factoryMethod === '$ctor') {
34
                    $valueFactories[$propName] = function ($value) use ($valueImplementor) {
0 ignored issues
show
Bug introduced by
The variable $valueImplementor does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
35
                        return new $valueImplementor($value);
36
                    };
37
                } else {
38
                    $valueFactories[$propName] = $factoryMethod;
39
                }
40
            }
41
        }
42
        $ctorReflection = $classReflection->getMethod("__construct");
43
        $ctorArgs = [];
44
        foreach ($ctorReflection->getParameters() as $argumentReflection) {
45
            $argName = $argumentReflection->getName();
46
            if (isset($arrayState[$argName])) {
47
                if (isset($valueFactories[$argName])) {
48
                    $ctorArgs[] = call_user_func($valueFactories[$argName], $arrayState[$argName]);
49
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
50
                    // missing factory annoation, throw exception or ignore?
51
                }
52
            } elseif ($argumentReflection->allowsNull()) {
53
                $ctorArgs[] = null;
54
            } else {
55
                throw new \Exception("Missing required value for array-key: $argName while constructing from array");
56
            }
57
        }
58
        return new static(...$ctorArgs);
0 ignored issues
show
Unused Code introduced by
The call to FromArrayTrait::__construct() has too many arguments starting with $ctorArgs.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
59
    }
60
}
61