Completed
Push — master ( 42a15b...d491ac )
by Paweł
02:03
created

CommandReflection::fromClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Clearcode\CommandBusConsole;
4
5
class CommandReflection
6
{
7
    /** @var string */
8
    public $commandName;
9
    /** @var string */
10
    public $commandClass;
11
12
    /**
13
     * @param $commandName
14
     * @param $commandClass
15
     */
16
    public function __construct($commandName, $commandClass)
17
    {
18
        $this->commandName = $commandName;
19
        $this->commandClass = $commandClass;
20
    }
21
22
    /**
23
     * @param $className
24
     *
25
     * @return CommandReflection
26
     */
27
    public static function fromClass($className)
28
    {
29
        $reflection = new \ReflectionClass($className);
30
31
        return new self($reflection->getShortName(), $className);
32
    }
33
34
    /**
35
     * @return \ReflectionParameter[]
36
     */
37
    public function parameters()
38
    {
39
        $commandReflection = new \ReflectionClass($this->commandClass);
40
41
        return $commandParameters = $commandReflection->getConstructor() ?
0 ignored issues
show
Unused Code introduced by
$commandParameters is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
42
            $commandReflection->getConstructor()->getParameters() :
43
            [];
44
    }
45
46
    /**
47
     * @param array              $arguments
48
     * @param ArgumentsProcessor $argumentsProcessor
49
     *
50
     * @return object
51
     *
52
     * @throws InvalidCommandArgument
53
     * @throws MissingCommandArgument
54
     */
55
    public function createCommand(array $arguments, ArgumentsProcessor $argumentsProcessor)
56
    {
57
        $classReflection = new \ReflectionClass($this->commandClass);
58
59
        $inputArguments = $argumentsProcessor->process($arguments);
60
61
        $arrayDivide = function (array $array, callable $predicate) {
62
            return [
63
                array_filter($array, function ($value, $key) use ($predicate) { return $predicate($key, $value); }, ARRAY_FILTER_USE_BOTH),
64
                array_filter($array, function ($value, $key) use ($predicate) { return !$predicate($key, $value); }, ARRAY_FILTER_USE_BOTH),
65
            ];
66
        };
67
68
        $isKeyInteger = function ($key) {
69
            return is_integer($key);
70
        };
71
72
        list($orderedArguments, $namedArguments) = $arrayDivide($inputArguments, $isKeyInteger);
73
74
        $constructorArguments = [];
75
76
        foreach ($this->parameters() as $commandArgument) {
77
            $argument = array_key_exists($commandArgument->getName(), $namedArguments) ?
0 ignored issues
show
Bug introduced by
Consider using $commandArgument->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
78
                $namedArguments[$commandArgument->getName()] :
0 ignored issues
show
Bug introduced by
Consider using $commandArgument->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
79
                array_shift($orderedArguments);
80
81
            if (null === $argument) {
82
                throw new MissingCommandArgument(
83
                    sprintf("Missing argument %s for '%s' command.", $commandArgument->getPosition() + 1, $this->commandName)
84
                );
85
            }
86
87
            if (null !== $commandArgument->getClass()) {
88
                $argumentClass = $commandArgument->getClass()->getName();
0 ignored issues
show
Bug introduced by
Consider using $commandArgument->getClass()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
89
90
                if (!$argument instanceof $argumentClass) {
91
                    throw new InvalidCommandArgument(
92
                        sprintf(
93
                            "Invalid argument for '%s' command. Expected parameter %s to be instance of '%s'.",
94
                            $this->commandName,
95
                            $commandArgument->getPosition() + 1,
96
                            $argumentClass
97
                        )
98
                    );
99
                }
100
            }
101
102
            $constructorArguments[$commandArgument->getPosition()] = $argument;
103
        }
104
105
        return $classReflection->newInstanceArgs($constructorArguments);
106
    }
107
}
108