Completed
Push — develop ( 141adb...8f70d4 )
by Jaap
05:39
created

Filter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 39.13%

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 9
cts 23
cp 0.3913
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A attachDefaults() 0 20 2
A attach() 0 4 1
A filter() 0 6 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\Filter;
17
18
use phpDocumentor\Descriptor\ConstantDescriptor;
19
use phpDocumentor\Descriptor\FunctionDescriptor;
20
use phpDocumentor\Descriptor\InterfaceDescriptor;
21
use phpDocumentor\Descriptor\MethodDescriptor;
22
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
23
use phpDocumentor\Descriptor\PropertyDescriptor;
24
use phpDocumentor\Descriptor\TraitDescriptor;
25
26
/**
27
 * Filter used to manipulate a descriptor after being build.
28
 *
29
 * This class is used during the building of descriptors. It passes the descriptor to each individual sub-filter, which
30
 * may change data in the descriptor or even remove it from the building process by returning null.
31
 */
32
class Filter
33
{
34
    /** @var int default priority for a filter in the series of filters. */
35
    const DEFAULT_PRIORITY = 1000;
36
37
    /** @var ClassFactory */
38
    protected $factory;
39
40
    /**
41
     * Constructs the filter and attaches the factory to it.
42
     */
43 1
    public function __construct(ClassFactory $factory)
44
    {
45 1
        $this->factory = $factory;
46 1
    }
47
48
    public function attachDefaults(ProjectDescriptorBuilder $descriptorBuilder): void
49
    {
50
        $stripOnVisibility = new StripOnVisibility($descriptorBuilder);
51
        $filtersOnAllDescriptors = [
52
            new StripInternal($descriptorBuilder),
53
            new StripIgnore($descriptorBuilder),
54
        ];
55
56
        foreach ($filtersOnAllDescriptors as $filter) {
57
            $this->attach(ConstantDescriptor::class, $filter);
58
            $this->attach(FunctionDescriptor::class, $filter);
59
            $this->attach(InterfaceDescriptor::class, $filter);
60
            $this->attach(TraitDescriptor::class, $filter);
61
            $this->attach(PropertyDescriptor::class, $filter);
62
            $this->attach(MethodDescriptor::class, $filter);
63
        }
64
65
        $this->attach(PropertyDescriptor::class, $stripOnVisibility);
66
        $this->attach(MethodDescriptor::class, $stripOnVisibility);
67
    }
68
69
    /**
70
     * Attaches a filter to a specific FQCN.
71
     */
72 1
    public function attach(string $fqcn, FilterInterface $filter): void
73
    {
74 1
        $this->factory->attachTo($fqcn, $filter);
75 1
    }
76
77
    /**
78
     * Filters the given Descriptor and returns the altered object.
79
     */
80 1
    public function filter(Filterable $descriptor): ?Filterable
81
    {
82 1
        $chain = $this->factory->getChainFor(get_class($descriptor));
83
84 1
        return $chain($descriptor);
85
    }
86
}
87