Completed
Push — develop ( 9193e7...62056c )
by Jaap
12:45 queued 02:43
created

Compiler/Pass/ExampleTagsEnricher.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Compiler\Pass;
13
14
use phpDocumentor\Descriptor\DescriptorAbstract;
15
use phpDocumentor\Descriptor\Example\Finder;
16
use phpDocumentor\Compiler\CompilerPassInterface;
17
use phpDocumentor\Descriptor\ProjectDescriptor;
18
use phpDocumentor\Descriptor\Builder\Reflector\Tags\ExampleAssembler;
19
use phpDocumentor\Reflection\DocBlock\ExampleFinder;
20
use phpDocumentor\Reflection\DocBlock\Tags\Example;
21
22
/**
23
 * This index builder collects all examples from tags and inserts them into the example index.
24
 */
25
class ExampleTagsEnricher implements CompilerPassInterface
26
{
27
    const COMPILER_PRIORITY = 9002;
28
29
    /** @var ExampleAssembler */
30
    private $exampleAssembler;
31
32
    /**
33
     * Initializes this compiler pass with its dependencies.
34
     *
35
     * @param ExampleFinder $finder Finds examples in several directories.
36
     */
37
    public function __construct(ExampleFinder $finder)
38
    {
39
        $this->exampleAssembler = new ExampleAssembler($finder);
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45
    public function getDescription()
46
    {
47
        return 'Enriches inline example tags with their sources';
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53
    public function execute(ProjectDescriptor $project)
54
    {
55
        $elements = $project->getIndexes()->get('elements');
56
57
        /** @var DescriptorAbstract $element */
58
        foreach ($elements as $element) {
59
            $element->setDescription($this->replaceInlineExamples($element));
60
        }
61
    }
62
63
    /**
64
     * Replaces the example tags in the description with the contents of the found example.
65
     *
66
     * @param DescriptorAbstract $element
67
     *
68
     * @return string
69
     */
70
    protected function replaceInlineExamples(DescriptorAbstract $element)
71
    {
72
        $description = $element->getDescription();
73
        $matches     = array();
74
75
        if (! $description
76
            || ! preg_match_all('/\{@example\s(.+?)\}/', $description, $matches)
77
            || count($matches[0]) < 1
78
        ) {
79
            return $description;
80
        }
81
82
        $matched = array();
83
        foreach ($matches[0] as $index => $match) {
84
            if (isset($matched[$match])) {
85
                continue;
86
            }
87
88
            $matched[$match] = true;
89
            $exampleReflector = Example::create($matches[1][$index]);
90
91
            $example = $this->exampleAssembler->create($exampleReflector);
0 ignored issues
show
It seems like $exampleReflector defined by \phpDocumentor\Reflectio...te($matches[1][$index]) on line 89 can be null; however, phpDocumentor\Descriptor...mpleAssembler::create() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
92
93
            $replacement = '`'.$example->getExample().'`';
94
            if ($example->getDescription()) {
95
                $replacement = '*' . $example->getDescription() . '*' . $replacement;
96
            }
97
98
            $description = str_replace($match, $replacement, $description);
99
        }
100
101
        return $description;
102
    }
103
}
104