Completed
Push — master ( 454e04...38876e )
by Marco
21s queued 11s
created

NodeToReflection::__invoke()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.0035
c 0
b 0
f 0
ccs 15
cts 15
cp 1
cc 8
nc 6
nop 5
crap 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BetterReflection\SourceLocator\Ast\Strategy;
6
7
use PhpParser\Node;
8
use Roave\BetterReflection\Reflection\Exception\InvalidConstantNode;
9
use Roave\BetterReflection\Reflection\Reflection;
10
use Roave\BetterReflection\Reflection\ReflectionClass;
11
use Roave\BetterReflection\Reflection\ReflectionConstant;
12
use Roave\BetterReflection\Reflection\ReflectionFunction;
13
use Roave\BetterReflection\Reflector\Reflector;
14
use Roave\BetterReflection\SourceLocator\Located\LocatedSource;
15
16
/**
17
 * @internal
18
 */
19
class NodeToReflection implements AstConversionStrategy
20
{
21
    /**
22
     * Take an AST node in some located source (potentially in a namespace) and
23 26
     * convert it to a Reflection
24
     */
25
    public function __invoke(
26
        Reflector $reflector,
27
        Node $node,
28
        LocatedSource $locatedSource,
29 26
        ?Node\Stmt\Namespace_ $namespace,
30 24
        ?int $positionInNode = null
31 24
    ) : ?Reflection {
32 24
        if ($node instanceof Node\Stmt\ClassLike) {
33 24
            return ReflectionClass::createFromNode(
34 24
                $reflector,
35
                $node,
36
                $locatedSource,
37
                $namespace
38 2
            );
39 2
        }
40 2
41
        if ($node instanceof Node\Stmt\ClassMethod
42 1
            || $node instanceof Node\Stmt\Function_
43 1
            || $node instanceof Node\Expr\Closure
44 1
        ) {
45 1
            return ReflectionFunction::createFromNode(
46 1
                $reflector,
47
                $node,
48
                $locatedSource,
49
                $namespace
50 1
            );
51
        }
52
53
        if ($node instanceof Node\Stmt\Const_) {
54
            return ReflectionConstant::createFromNode($reflector, $node, $locatedSource, $namespace, $positionInNode);
55
        }
56
57
        if ($node instanceof Node\Expr\FuncCall) {
58
            try {
59
                return ReflectionConstant::createFromNode($reflector, $node, $locatedSource);
60
            } catch (InvalidConstantNode $e) {
61
                // Ignore
62
            }
63
        }
64
65
        return null;
66
    }
67
}
68