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

StripInternal::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
ccs 9
cts 9
cp 1
crap 3
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\ProjectDescriptor\Settings;
20
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
21
use Zend\Filter\AbstractFilter;
22
23
/**
24
 * Filters a Descriptor when the @internal inline tag, or normal tag, is used.
25
 *
26
 * When a Descriptor's description contains the inline tag @internal then the description of that tag should be
27
 * included only when the visibility allows INTERNAL information. Otherwise it needs to be removed.
28
 *
29
 * Similarly, whenever the normal @internal tag is used should this filter return null if the visibility does not allow
30
 * INTERNAL information. This will remove this descriptor from the project.
31
 *
32
 * @link http://www.phpdoc.org/docs/latest/for-users/phpdoc/tags/internal.html
33
 */
34
class StripInternal implements FilterInterface
35
{
36
    /** @var ProjectDescriptorBuilder $builder */
37
    protected $builder;
38
39
    /**
40
     * Initializes this filter with an instance of the builder to retrieve the latest ProjectDescriptor from.
41
     */
42 1
    public function __construct(ProjectDescriptorBuilder $builder)
43
    {
44 1
        $this->builder = $builder;
45 1
    }
46
47
    /**
48
     * If the ProjectDescriptor's settings allow internal tags then return the Descriptor, otherwise null to filter it.
49
     *
50
     * @param DescriptorAbstract $value
51
     *
52
     * @return DescriptorAbstract|null
53
     */
54 6
    public function __invoke(?Filterable $value) : ?Filterable
55
    {
56 6
        $isInternalAllowed = $this->builder->getProjectDescriptor()->isVisibilityAllowed(Settings::VISIBILITY_INTERNAL);
57 6
        if ($isInternalAllowed) {
58 3
            $value->setDescription(preg_replace('/\{@internal\s(.+?)\}\}/', '$1', $value->getDescription()));
59
60 3
            return $value;
61
        }
62
63
        // remove inline @internal tags
64 3
        $value->setDescription(preg_replace('/\{@internal\s(.+?)\}\}/', '', $value->getDescription()));
65
66
        // if internal elements are not allowed; filter this element
67 3
        if ($value->getTags()->get('internal')) {
68 1
            return null;
69
        }
70
71 2
        return $value;
72
    }
73
}
74