Completed
Pull Request — master (#14)
by Roman
03:21
created

NodeDispatchingTrait::getVisitor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PeacefulBit\Packet\Visitors;
4
5
use PeacefulBit\Packet\Nodes;
6
7
trait NodeDispatchingTrait
8
{
9
    /**
10
     * @param Nodes\Node $node
11
     * @return mixed
12
     */
13
    public function visit(Nodes\Node $node)
14
    {
15
        $type = get_class($node);
16
        switch ($type) {
17
            case Nodes\ConstantNode::class:
18
                return $this->visitConstantNode($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PeacefulBit\Packet\Nodes\Node> is not a sub-type of object<PeacefulBit\Packet\Nodes\ConstantNode>. It seems like you assume a concrete implementation of the interface PeacefulBit\Packet\Nodes\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
19
            case Nodes\FunctionNode::class:
20
                return $this->visitFunctionNode($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PeacefulBit\Packet\Nodes\Node> is not a sub-type of object<PeacefulBit\Packet\Nodes\FunctionNode>. It seems like you assume a concrete implementation of the interface PeacefulBit\Packet\Nodes\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
21
            case Nodes\InvokeNode::class:
22
                return $this->visitInvokeNode($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PeacefulBit\Packet\Nodes\Node> is not a sub-type of object<PeacefulBit\Packet\Nodes\InvokeNode>. It seems like you assume a concrete implementation of the interface PeacefulBit\Packet\Nodes\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
23
            case Nodes\SequenceNode::class:
24
                return $this->visitSequenceNode($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PeacefulBit\Packet\Nodes\Node> is not a sub-type of object<PeacefulBit\Packet\Nodes\SequenceNode>. It seems like you assume a concrete implementation of the interface PeacefulBit\Packet\Nodes\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
25
            case Nodes\StringNode::class:
26
                return $this->visitStringNode($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PeacefulBit\Packet\Nodes\Node> is not a sub-type of object<PeacefulBit\Packet\Nodes\StringNode>. It seems like you assume a concrete implementation of the interface PeacefulBit\Packet\Nodes\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
27
            case Nodes\SymbolNode::class:
28
                return $this->visitSymbolNode($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PeacefulBit\Packet\Nodes\Node> is not a sub-type of object<PeacefulBit\Packet\Nodes\SymbolNode>. It seems like you assume a concrete implementation of the interface PeacefulBit\Packet\Nodes\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
29
            case Nodes\NativeNode::class:
30
                return $this->visitNativeNode($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PeacefulBit\Packet\Nodes\Node> is not a sub-type of object<PeacefulBit\Packet\Nodes\NativeNode>. It seems like you assume a concrete implementation of the interface PeacefulBit\Packet\Nodes\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
31
            case Nodes\LambdaNode::class:
32
                return $this->visitLambdaNode($node);
0 ignored issues
show
Compatibility introduced by
$node of type object<PeacefulBit\Packet\Nodes\Node> is not a sub-type of object<PeacefulBit\Packet\Nodes\LambdaNode>. It seems like you assume a concrete implementation of the interface PeacefulBit\Packet\Nodes\Node to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
33
            default:
34
                throw new \InvalidArgumentException("Unsupported type of node - $type");
35
        }
36
    }
37
38
    /**
39
     * @param Nodes\Node $node
40
     * @return callable
41
     */
42
    public function getVisitor(Nodes\Node $node)
43
    {
44
        $type = explode('\\', get_class($node));
45
        $class = end($type);
46
        $method = 'visit' . $class;
47
48
        if (method_exists($this, $method)) {
49
            return [$this, $method];
50
        }
51
52
        throw new \InvalidArgumentException("Unsupported type of node - $class");
53
    }
54
55
    /**
56
     * @param Nodes\ConstantNode $node
57
     * @return mixed
58
     */
59
    abstract public function visitConstantNode(Nodes\ConstantNode $node);
60
61
    /**
62
     * @param Nodes\FunctionNode $node
63
     * @return mixed
64
     */
65
    abstract public function visitFunctionNode(Nodes\FunctionNode $node);
66
67
    /**
68
     * @param Nodes\InvokeNode $node
69
     * @return mixed
70
     */
71
    abstract public function visitInvokeNode(Nodes\InvokeNode $node);
72
73
    /**
74
     * @param Nodes\SequenceNode $node
75
     * @return mixed
76
     */
77
    abstract public function visitSequenceNode(Nodes\SequenceNode $node);
78
79
    /**
80
     * @param Nodes\StringNode $node
81
     * @return mixed
82
     */
83
    abstract public function visitStringNode(Nodes\StringNode $node);
84
85
    /**
86
     * @param Nodes\SymbolNode $node
87
     * @return mixed
88
     */
89
    abstract public function visitSymbolNode(Nodes\SymbolNode $node);
90
91
    /**
92
     * @param Nodes\NativeNode $node
93
     * @return mixed
94
     */
95
    abstract public function visitNativeNode(Nodes\NativeNode $node);
96
97
    /**
98
     * @param Nodes\LambdaNode $node
99
     * @return mixed
100
     */
101
    abstract public function visitLambdaNode(Nodes\LambdaNode $node);
102
}
103