AssemblerFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 27

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 92
ccs 0
cts 42
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 27

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 4 1
A get() 0 11 3
A registerFallback() 0 4 1
A createDefault() 0 37 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Descriptor\Builder;
17
18
use phpDocumentor\Reflection\DocBlock\ExampleFinder;
19
20
use phpDocumentor\Descriptor\Builder\Reflector\ArgumentAssembler;
21
use phpDocumentor\Descriptor\Builder\Reflector\ClassAssembler;
22
use phpDocumentor\Descriptor\Builder\Reflector\ConstantAssembler;
23
use phpDocumentor\Descriptor\Builder\Reflector\FileAssembler;
24
use phpDocumentor\Descriptor\Builder\Reflector\FunctionAssembler;
25
use phpDocumentor\Descriptor\Builder\Reflector\InterfaceAssembler;
26
use phpDocumentor\Descriptor\Builder\Reflector\MethodAssembler;
27
use phpDocumentor\Descriptor\Builder\Reflector\NamespaceAssembler;
28
use phpDocumentor\Descriptor\Builder\Reflector\PropertyAssembler;
29
use phpDocumentor\Descriptor\Builder\Reflector\Tags\AuthorAssembler;
30
use phpDocumentor\Descriptor\Builder\Reflector\Tags\DeprecatedAssembler;
31
use phpDocumentor\Descriptor\Builder\Reflector\Tags\ExampleAssembler;
32
use phpDocumentor\Descriptor\Builder\Reflector\Tags\GenericTagAssembler;
33
use phpDocumentor\Descriptor\Builder\Reflector\Tags\LinkAssembler;
34
use phpDocumentor\Descriptor\Builder\Reflector\Tags\MethodAssembler as MethodTagAssembler;
35
use phpDocumentor\Descriptor\Builder\Reflector\Tags\ParamAssembler;
36
use phpDocumentor\Descriptor\Builder\Reflector\Tags\PropertyAssembler as PropertyTagAssembler;
37
use phpDocumentor\Descriptor\Builder\Reflector\Tags\ReturnAssembler;
38
use phpDocumentor\Descriptor\Builder\Reflector\Tags\SeeAssembler;
39
use phpDocumentor\Descriptor\Builder\Reflector\Tags\SinceAssembler;
40
use phpDocumentor\Descriptor\Builder\Reflector\Tags\ThrowsAssembler;
41
use phpDocumentor\Descriptor\Builder\Reflector\Tags\UsesAssembler;
42
use phpDocumentor\Descriptor\Builder\Reflector\Tags\VarAssembler;
43
use phpDocumentor\Descriptor\Builder\Reflector\Tags\VersionAssembler;
44
use phpDocumentor\Descriptor\Builder\Reflector\TraitAssembler;
45
use phpDocumentor\Reflection\DocBlock\Tag;
46
use phpDocumentor\Reflection\DocBlock\Tags;
47
use phpDocumentor\Reflection\DocBlock\Tags\Author;
48
use phpDocumentor\Reflection\DocBlock\Tags\Deprecated;
49
use phpDocumentor\Reflection\DocBlock\Tags\Example;
50
use phpDocumentor\Reflection\DocBlock\Tags\Link;
51
use phpDocumentor\Reflection\DocBlock\Tags\Param;
52
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
53
use phpDocumentor\Reflection\DocBlock\Tags\See;
54
use phpDocumentor\Reflection\DocBlock\Tags\Since;
55
use phpDocumentor\Reflection\DocBlock\Tags\Throws;
56
use phpDocumentor\Reflection\DocBlock\Tags\Uses;
57
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
58
use phpDocumentor\Reflection\DocBlock\Tags\Version;
59
use phpDocumentor\Reflection\Php\Argument;
60
use phpDocumentor\Reflection\Php\Class_;
61
use phpDocumentor\Reflection\Php\Constant;
62
use phpDocumentor\Reflection\Php\File;
63
use phpDocumentor\Reflection\Php\Function_;
64
use phpDocumentor\Reflection\Php\Interface_;
65
use phpDocumentor\Reflection\Php\Method;
66
use phpDocumentor\Reflection\Php\Namespace_;
67
use phpDocumentor\Reflection\Php\Property;
68
use phpDocumentor\Reflection\Php\Trait_;
69
70
/**
71
 * Attempts to retrieve an Assembler for the provided criteria.
72
 */
73
class AssemblerFactory
74
{
75
    /** @var AssemblerMatcher[] */
76
    protected $assemblers = [];
77
78
    /** @var AssemblerMatcher[] */
79
    protected $fallbackAssemblers = [];
80
81
    /**
82
     * Registers an assembler instance to this factory.
83
     *
84
     * @param callable           $matcher   A callback function accepting the criteria as only parameter and which must
85
     *     return a boolean.
86
     * @param AssemblerInterface $assembler An instance of the Assembler that will be returned if the callback returns
87
     *     true with the provided criteria.
88
     */
89
    public function register(callable $matcher, AssemblerInterface $assembler): void
90
    {
91
        $this->assemblers[] = new AssemblerMatcher($matcher, $assembler);
92
    }
93
94
    /**
95
     * Registers an assembler instance to this factory that is to be executed after all other assemblers have been
96
     * checked.
97
     *
98
     * @param callable           $matcher   A callback function accepting the criteria as only parameter and which must
99
     *     return a boolean.
100
     * @param AssemblerInterface $assembler An instance of the Assembler that will be returned if the callback returns
101
     *     true with the provided criteria.
102
     */
103
    public function registerFallback(callable $matcher, AssemblerInterface $assembler): void
104
    {
105
        $this->fallbackAssemblers[] = new AssemblerMatcher($matcher, $assembler);
106
    }
107
108
    /**
109
     * Retrieves a matching Assembler based on the provided criteria or null if none was found.
110
     *
111
     * @param mixed $criteria
112
     *
113
     * @return AssemblerInterface|null
114
     */
115
    public function get($criteria)
116
    {
117
        /** @var AssemblerMatcher $candidate */
118
        foreach (array_merge($this->assemblers, $this->fallbackAssemblers) as $candidate) {
119
            if ($candidate->match($criteria) === true) {
120
                return $candidate->getAssembler();
121
            }
122
        }
123
124
        return null;
125
    }
126
127
    public static function createDefault(ExampleFinder $exampleFinder): self
128
    {
129
        $factory = new AssemblerFactory();
130
        $argumentAssembler = new ArgumentAssembler();
131
132
        $factory->register(Matcher::forType(File::class), new FileAssembler());
133
        $factory->register(Matcher::forType(Constant::class), new ConstantAssembler());
134
        $factory->register(Matcher::forType(Trait_::class), new TraitAssembler());
135
        $factory->register(Matcher::forType(Class_::class), new ClassAssembler());
136
        $factory->register(Matcher::forType(Interface_::class), new InterfaceAssembler());
137
        $factory->register(Matcher::forType(Property::class), new PropertyAssembler());
138
        $factory->register(Matcher::forType(Argument::class), $argumentAssembler);
139
        $factory->register(Matcher::forType(Method::class), new MethodAssembler($argumentAssembler));
140
        $factory->register(Matcher::forType(Function_::class), new FunctionAssembler($argumentAssembler));
141
        $factory->register(Matcher::forType(Namespace_::class), new NamespaceAssembler());
142
143
        $factory->register(Matcher::forType(Author::class), new AuthorAssembler());
144
        $factory->register(Matcher::forType(Deprecated::class), new DeprecatedAssembler());
145
        $factory->register(Matcher::forType(Example::class), new ExampleAssembler($exampleFinder));
146
        $factory->register(Matcher::forType(Link::class), new LinkAssembler());
147
        $factory->register(Matcher::forType(Tags\Method::class), new MethodTagAssembler());
148
        $factory->register(Matcher::forType(Tags\Property::class), new PropertyTagAssembler());
149
        $factory->register(Matcher::forType(Tags\PropertyRead::class), new PropertyTagAssembler());
150
        $factory->register(Matcher::forType(Tags\PropertyWrite::class), new PropertyTagAssembler());
151
        $factory->register(Matcher::forType(Var_::class), new VarAssembler());
152
        $factory->register(Matcher::forType(Param::class), new ParamAssembler());
153
        $factory->register(Matcher::forType(Throws::class), new ThrowsAssembler());
154
        $factory->register(Matcher::forType(Return_::class), new ReturnAssembler());
155
        $factory->register(Matcher::forType(Uses::class), new UsesAssembler());
156
        $factory->register(Matcher::forType(See::class), new SeeAssembler());
157
        $factory->register(Matcher::forType(Since::class), new SinceAssembler());
158
        $factory->register(Matcher::forType(Version::class), new VersionAssembler());
159
160
        $factory->registerFallback(Matcher::forType(Tag::class), new GenericTagAssembler());
161
162
        return $factory;
163
    }
164
}
165