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

StripOnVisibility::toVisibility()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 13
ccs 0
cts 8
cp 0
crap 20
rs 9.8333
c 0
b 0
f 0
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\DescriptorAbstract;
19
use phpDocumentor\Descriptor\Interfaces\VisibilityInterface;
20
use phpDocumentor\Descriptor\ProjectDescriptor\Settings;
21
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
22
23
/**
24
 * Strips any Descriptor if their visibility is allowed according to the ProjectDescriptorBuilder.
25
 */
26
class StripOnVisibility implements FilterInterface
27
{
28
    /** @var ProjectDescriptorBuilder $builder */
29
    protected $builder;
30
31
    /**
32
     * Initializes this filter with an instance of the builder to retrieve the latest ProjectDescriptor from.
33
     */
34 1
    public function __construct(ProjectDescriptorBuilder $builder)
35
    {
36 1
        $this->builder = $builder;
37 1
    }
38
39
    /**
40
     * Filter Descriptor with based on visibility.
41
     *
42
     * @param DescriptorAbstract $value
43
     *
44
     * @return DescriptorAbstract|null
45
     */
46 3
    public function __invoke(?Filterable $value) : ?Filterable
47
    {
48 3
        if ($value instanceof VisibilityInterface
49 2
            && !$this->builder->getProjectDescriptor()->isVisibilityAllowed(
50 3
                $this->toVisibility($value->getVisibility())
51
            )
52
        ) {
53 1
            return null;
54
        }
55
56 2
        return $value;
57
    }
58
59
    private function toVisibility(string $visibility) : int
60
    {
61
        switch ($visibility) {
62
            case 'public':
63
                return Settings::VISIBILITY_PUBLIC;
64
            case 'protected':
65
                return Settings::VISIBILITY_PROTECTED;
66
            case 'private':
67
                return Settings::VISIBILITY_PRIVATE;
68
        }
69
70
        throw new \InvalidArgumentException($visibility . ' is not a valid visibility');
71
    }
72
}
73