Completed
Pull Request — master (#35)
by Saif Eddin
02:55 queued 38s
created

Dispatcher   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 191
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 96.36%

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 3
dl 0
loc 191
ccs 53
cts 55
cp 0.9636
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A p() 0 15 2
A __call() 0 4 1
A getClass() 0 13 3
A setLastMethod() 0 4 1
A getLastMethod() 0 4 1
B dynamicConstruct() 0 25 6
A getPrecedenceMap() 0 4 1
A issetPrecedenceMap() 0 4 1
A convert() 0 8 1
A getMetadata() 0 4 1
1
<?php
2
3
namespace PhpToZephir\Converter;
4
5
use PhpToZephir\Logger;
6
use PhpToZephir\ClassCollector;
7
8
/**
9
 * @method string pExpr_Assign(\PhpParser\Node\Expr\Assign $node)
10
 * @method string pImplode(array $nodes, string $glue = '')
11
 * @method string pVarOrNewExpr(\PhpParser\Node $node)
12
 * @method string pCommaSeparated(array $nodes)
13
 * @method string pInfixOp(string $type, \PhpParser\Node $leftNode, string $operatorString, \PhpParser\Node $rightNode)
14
 * @method string pPrec(\PhpParser\Node $node, integer $parentPrecedence, integer $parentAssociativity, integer $childPosition)
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
15
 * @method string pExpr_Ternary(\PhpParser\Node\Expr\Ternary $node, boolean $returnAsArray = false)
16
 * @method string pStmts(array $nodes, boolean $indent = true)
17
 * @method string pModifiers(integer $modifiers)
18
 * @method string|array pExpr_Array(\PhpParser\Node\Expr\Array_ $node, boolean $returnAsArray = false)
19
 * @method string pExpr_ArrayDimFetch(\PhpParser\Node\Expr\ArrayDimFetch $node, $returnAsArray = false)
20
 * @method string pObjectProperty($node)
21
 * @method string pPrefixOp(string $type, string $operatorString, \PhpParser\Node $node)
22
 * @method string pEncapsList(array $encapsList, string $quote)
23
 * @method string pPostfixOp(string $type, \PhpParser\Node $node, string $operatorString)
24
 * @method string pStmt_ElseIf(\PhpParser\Node\Stmt\ElseIf_ $node)
25
 * @method string pStmt_If(\PhpParser\Node\Stmt\If_ $node)
26
 */
27
class Dispatcher
28
{
29
    /**
30
     * @var string
31
     */
32
    const noIndentToken = '_NO_INDENT_852452555255254554';
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected NOINDENTTOKEN).
Loading history...
33
    /**
34
     * @var array
35
     */
36
    private $precedenceMap = array();
37
    /**
38
     * @var array
39
     */
40
    private $classes = array();
41
    /**
42
     * @var PrinterCollection
43
     */
44
    private $printerCollection = null;
45
    /**
46
     * @var Logger
47
     */
48
    private $logger = null;
49
    /**
50
     * @var string
51
     */
52
    private $lastMethod = null;
53
    /**
54
     * @var ClassMetadata
55
     */
56
    private $metadata = null;
57
    /**
58
     * @var ClassCollector
59
     */
60
    private $classCollector = null;
61
62
    /**
63
     * @param PrinterCollection $printerCollection
64
     * @param array             $precedenceMap
65
     */
66 1
    public function __construct(PrinterCollection $printerCollection, array $precedenceMap)
67
    {
68 1
        $this->printerCollection = $printerCollection;
69 1
        $this->precedenceMap = $precedenceMap;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
70 1
    }
71
72
    /**
73
     * Pretty prints a node.
74
     *
75
     * @param \PhpParser\Node $node Node to be pretty printed
76
     *
77
     * @return string Pretty printed node
78
     */
79 81
    public function p()
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 3 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
80
    {
81 81
        $args = func_get_args();
82 81
        $node = $args[0];
83
84 81
        if (null === $node) {
85
            return;
86
        }
87
88 81
        $this->logger->trace('p'.$node->getType(), $node, $this->getMetadata()->getFullQualifiedNameClass());
89
90 81
        $class = $this->getClass('p'.$node->getType());
91
92 81
        return call_user_func_array(array($class, "convert"), $args);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal convert does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
93
    }
94
95
    /**
96
     * @param string $method
97
     * @param array  $arguments
98
     *
99
     * @return string
100
     */
101 81
    public function __call($method, $arguments)
102
    {
103 81
        return call_user_func_array(array($this->getClass($method), 'convert'), $arguments);
104
    }
105
106
    /**
107
     * @param string $type
108
     *
109
     * @throws \Exception
110
     *
111
     * @return object
112
     */
113 81
    private function getClass($type)
114
    {
115 81
        if (isset($this->classes[$type]) === false) {
116 25
            if ($this->printerCollection->offsetExists($type) === false) {
117
                throw new \Exception(sprintf('Printer "%s" does not exist', $type));
118
            }
119 25
            $className = $this->printerCollection->offsetGet($type);
120
121 25
            $this->classes[$type] = $this->dynamicConstruct($className);
122 25
        }
123
124 81
        return $this->classes[$type];
125
    }
126
127
    /**
128
     * @param string $value
129
     */
130 79
    public function setLastMethod($value)
131
    {
132 79
        $this->lastMethod = $value;
133 79
    }
134
135
    /**
136
     * @return string
137
     */
138 6
    public function getLastMethod()
139
    {
140 6
        return $this->lastMethod;
141
    }
142
143
    /**
144
     * @param string $className
145
     *
146
     * @return object
147
     */
148 25
    private function dynamicConstruct($className)
149
    {
150 25
        $reflectionClass = new \ReflectionClass($className);
151
152 25
        if ($reflectionClass->getConstructor() === null) {
153 5
            return new $className();
154
        }
155 25
        $dependencies = array();
156
157 25
        foreach ($reflectionClass->getConstructor()->getParameters() as $nmb => $param) {
158 25
            $name = $param->getClass()->name;
159
160 25
            if ($name === 'PhpToZephir\Converter\Dispatcher') {
161 25
                $dependencies[] = $this;
162 25
            } elseif ($name === 'PhpToZephir\Logger') {
163 25
                $dependencies[] = $this->logger;
164 25
            } elseif ($name === 'PhpToZephir\ClassCollector') {
165 1
                $dependencies[] = $this->classCollector;
166 1
            } else {
167 5
                $dependencies[] = $this->dynamicConstruct($name);
168
            }
169 25
        }
170
171 25
        return $reflectionClass->newInstanceArgs($dependencies);
172
    }
173
174
    /**
175
     * @param string $type
176
     *
177
     * @return array
178
     */
179 64
    public function getPrecedenceMap($type)
180
    {
181 64
        return $this->precedenceMap[$type];
182
    }
183
184
    /**
185
     * @param string $type
186
     *
187
     * @return bool
188
     */
189 64
    public function issetPrecedenceMap($type)
190
    {
191 64
        return isset($this->precedenceMap[$type]);
192
    }
193
194
    /**
195
     * Pretty prints an array of statements.
196
     *
197
     * @param \PhpParser\Node[] $stmts Array of statements
198
     *
199
     * @return string Pretty printed statements
200
     */
201 81
    public function convert(array $stmts, ClassMetadata $metadata, ClassCollector $classCollector, Logger $logger)
202
    {
203 81
        $this->metadata = $metadata;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
204 81
        $this->classCollector = $classCollector;
205 81
        $this->logger = $logger;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
206
207 81
        return ltrim(str_replace("\n".self::noIndentToken, "\n", $this->pStmts($stmts, false)));
208
    }
209
210
    /**
211
     * @return ClassMetadata
212
     */
213 81
    public function getMetadata()
214
    {
215 81
        return $this->metadata;
216
    }
217
}
218