ClassIdentifierRecorder::enterNode()   B
last analyzed

Complexity

Conditions 8
Paths 11

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8.1515

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 32
ccs 13
cts 15
cp 0.8667
crap 8.1515
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\PhpParser\NodeVisitor;
16
17
use Humbug\PhpScoper\PhpParser\Node\FullyQualifiedFactory;
18
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\IdentifierResolver;
19
use Humbug\PhpScoper\PhpParser\UnexpectedParsingScenario;
20
use Humbug\PhpScoper\Symbol\EnrichedReflector;
21
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
22
use PhpParser\Node;
23
use PhpParser\Node\Identifier;
24
use PhpParser\Node\Name\FullyQualified;
25
use PhpParser\Node\Stmt\Class_;
26
use PhpParser\Node\Stmt\Interface_;
27
use PhpParser\NodeVisitorAbstract;
28
29
/**
30
 * Records the user classes which are exposed.
31
 *
32
 * @private
33
 */
34
final class ClassIdentifierRecorder extends NodeVisitorAbstract
35
{
36
    private string $prefix;
37 549
    private IdentifierResolver $identifierResolver;
38
    private SymbolsRegistry $symbolsRegistry;
39
    private EnrichedReflector $enrichedReflector;
40
41
    public function __construct(
42 549
        string $prefix,
43 549
        IdentifierResolver $identifierResolver,
44 549
        SymbolsRegistry $symbolsRegistry,
45
        EnrichedReflector $enrichedReflector
46
    ) {
47
        $this->prefix = $prefix;
48
        $this->identifierResolver = $identifierResolver;
49
        $this->symbolsRegistry = $symbolsRegistry;
50 548
        $this->enrichedReflector = $enrichedReflector;
51
    }
52 548
53 548
    public function enterNode(Node $node): Node
54
    {
55
        if (!($node instanceof Identifier) || !ParentNodeAppender::hasParent($node)) {
56 382
            return $node;
57
        }
58 382
59 320
        $parent = ParentNodeAppender::getParent($node);
60
61
        $isClassOrInterface = $parent instanceof Class_ || $parent instanceof Interface_;
62 292
63
        if (!$isClassOrInterface) {
64
            return $node;
65
        }
66
67 292
        if (null === $parent->name) {
68
            throw UnexpectedParsingScenario::create();
69 292
        }
70
71
        $resolvedName = $this->identifierResolver->resolveIdentifier($node);
72
73
        if (!($resolvedName instanceof FullyQualified)) {
74 292
            throw UnexpectedParsingScenario::create();
75 286
        }
76
77 81
        if ($this->enrichedReflector->isExposedClass((string) $resolvedName)) {
78 81
            $this->symbolsRegistry->recordClass(
79 81
                $resolvedName,
80
                FullyQualifiedFactory::concat($this->prefix, $resolvedName),
81
            );
82
        }
83 292
84
        return $node;
85
    }
86
}
87