1 | <?php |
||
12 | class SwitchChoose implements Processor |
||
13 | { |
||
14 | use Processor\Implementation, |
||
15 | Helper\GetByReference |
||
16 | { |
||
17 | Helper\GetByReference::withSourceAndTarget insteadof Processor\Implementation; |
||
18 | } |
||
19 | |||
20 | /** |
||
21 | * Apply processing to a single node |
||
22 | * |
||
23 | * @param Node $node |
||
24 | */ |
||
25 | 7 | public function applyToNode(Node $node) |
|
26 | { |
||
27 | 7 | $instruction = $node->getInstruction($this); |
|
28 | |||
29 | // for bc |
||
30 | 7 | if (is_string($instruction)) { |
|
31 | 2 | $cases = $node->getResult(); |
|
32 | |||
33 | 2 | $result = $this->choose($instruction, $cases); |
|
|
|||
34 | 2 | return $node->setResult($result); |
|
35 | } |
||
36 | |||
37 | 5 | if (is_array($instruction) || is_object($instruction)) { |
|
38 | 5 | $instruction = (object)$instruction; |
|
39 | } |
||
40 | |||
41 | 5 | if (!isset($instruction->on) || !isset($instruction->options)) { |
|
42 | return; |
||
43 | } |
||
44 | |||
45 | 5 | $instruction->options = (array)$instruction->options; |
|
46 | |||
47 | 5 | $result = isset($instruction->default) ? $instruction->default : null; |
|
48 | |||
49 | 5 | if (isset($instruction->options[$instruction->on])) { |
|
50 | 3 | $result = $instruction->options[$instruction->on]; |
|
51 | } |
||
52 | |||
53 | 5 | $node->setResult($result); |
|
54 | 5 | } |
|
55 | |||
56 | /** |
||
57 | * Choose on of the cases |
||
58 | * |
||
59 | * @param string $ref Property name of source |
||
60 | * @param object $cases |
||
61 | * @return mixed |
||
62 | */ |
||
63 | 2 | protected function choose($ref, $cases) |
|
68 | } |
||
69 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.