Passed
Push — 1.5 ( c7c5a4...fe8795 )
by Luis
06:06
created

DigraphBuilder::externalDefinitionsResolver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.1
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Configuration;
9
10
use PhUml\Graphviz\Builders\ClassGraphBuilder;
11
use PhUml\Graphviz\Builders\EdgesBuilder;
12
use PhUml\Graphviz\Builders\InterfaceGraphBuilder;
13
use PhUml\Graphviz\Builders\NoAssociationsBuilder;
14
use PhUml\Graphviz\DigraphPrinter;
15
use PhUml\Graphviz\Styles\DefaultDigraphStyle;
16
use PhUml\Graphviz\Styles\DigraphStyle;
17
use PhUml\Graphviz\Styles\NonEmptyBlocksStyle;
18
use PhUml\Parser\Code\ExternalAssociationsResolver;
19
use PhUml\Parser\Code\ExternalDefinitionsResolver;
20
use PhUml\Parser\Code\ParserBuilder;
21
use PhUml\Parser\Code\PhpParser;
22
use PhUml\Parser\CodeFinder;
23
use PhUml\Parser\CodeParser;
24
use PhUml\Parser\NonRecursiveCodeFinder;
25
use PhUml\Processors\GraphvizProcessor;
26
use PhUml\Templates\TemplateEngine;
27
28
class DigraphBuilder
29
{
30
    /** @var DigraphConfiguration */
31
    protected $configuration;
32
33
    /** @var ParserBuilder */
34
    protected $parserBuilder;
35
36 33
    public function __construct()
37
    {
38 33
        $this->parserBuilder = new ParserBuilder();
39 33
    }
40
41 33
    public function codeFinder(): CodeFinder
42
    {
43 33
        return $this->configuration->searchRecursively() ? new CodeFinder() : new NonRecursiveCodeFinder();
44
    }
45
46 30
    protected function digraphProcessor(): GraphvizProcessor
47
    {
48 30
        $associationsBuilder = $this->configuration->extractAssociations() ? new EdgesBuilder() : new NoAssociationsBuilder();
49 30
        return new GraphvizProcessor(
50 30
            new ClassGraphBuilder($associationsBuilder),
51 30
            new InterfaceGraphBuilder(),
52 30
            new DigraphPrinter(new TemplateEngine(), $this->digraphStyle())
53
        );
54
    }
55
56 30
    protected function digraphStyle(): DigraphStyle
57
    {
58 30
        if ($this->configuration->hideEmptyBlocks()) {
59 6
            return new NonEmptyBlocksStyle($this->configuration->theme());
60
        }
61 24
        return new DefaultDigraphStyle($this->configuration->theme());
62
    }
63
64 30
    protected function codeParser(): CodeParser
65
    {
66 30
        return new CodeParser($this->tokenParser(), $this->externalDefinitionsResolver());
67
    }
68
69 30
    protected function tokenParser(): PhpParser
70
    {
71 30
        $this->configureAttributes();
72 30
        $this->configureMethods();
73 30
        $this->configureFilters();
74 30
        return $this->parserBuilder->build();
75
    }
76
77 30
    private function configureAttributes(): void
78
    {
79 30
        if ($this->configuration->hideAttributes()) {
80 9
            $this->parserBuilder->excludeAttributes();
81
        }
82 30
    }
83
84 30
    private function configureMethods(): void
85
    {
86 30
        if ($this->configuration->hideMethods()) {
87 9
            $this->parserBuilder->excludeMethods();
88
        }
89 30
    }
90
91 30
    private function configureFilters(): void
92
    {
93 30
        if ($this->configuration->hidePrivate()) {
94 6
            $this->parserBuilder->excludePrivateMembers();
95
        }
96 30
        if ($this->configuration->hideProtected()) {
97 6
            $this->parserBuilder->excludeProtectedMembers();
98
        }
99 30
    }
100
101 30
    private function externalDefinitionsResolver(): ExternalDefinitionsResolver
102
    {
103 30
        if ($this->configuration->extractAssociations()) {
104 24
            return new ExternalAssociationsResolver();
105
        }
106 6
        return new ExternalDefinitionsResolver();
107
    }
108
}
109