Completed
Pull Request — master (#85)
by Kévin
18:16 queued 14:06
created

NewOp::compile()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 8.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 23
ccs 7
cts 14
cp 0.5
crap 8.125
rs 8.5906
1
<?php
2
/**
3
 * PHP Smart Analysis project 2015-2016
4
 *
5
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
6
 */
7
8
namespace PHPSA\Compiler\Expression\Operators;
9
10
use PhpParser\Node;
11
use PHPSA\CompiledExpression;
12
use PHPSA\Compiler\Expression\AbstractExpressionCompiler;
13
use PHPSA\Context;
14
use PHPSA\Compiler\Expression;
15
16
class NewOp extends AbstractExpressionCompiler
17
{
18
    protected $name = 'PhpParser\Node\Expr\New_';
19
20
    /**
21
     * @param \PhpParser\Node\Expr\New_ $expr
22
     * @param Context $context
23
     * @return CompiledExpression
24
     */
25 1
    protected function compile($expr, Context $context)
26
    {
27 1
        if ($expr->class instanceof Node\Name) {
28 1
            $name = $expr->class->parts[0];
29
30 1
            $arguments = [];
31
32 1
            if (count($expr->args) > 0) {
33
                foreach ($expr->args as $argument) {
34
                    $arguments[] = $context->getExpressionCompiler()->compile($argument->value);
35
                }
36
            } else {
37 1
                if (class_exists($name, true)) {
38 1
                    return new CompiledExpression(CompiledExpression::OBJECT, new $name());
39
                }
40
            }
41
42
            return new CompiledExpression(CompiledExpression::OBJECT);
43
        }
44
45
        $context->debug('Unknown how to pass new');
46
        return new CompiledExpression();
47
    }
48
}
49