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 | 4 | public function __construct(ExampleFinder $finder) |
|
38 | { |
||
39 | 4 | $this->exampleAssembler = new ExampleAssembler($finder); |
|
40 | 4 | } |
|
41 | |||
42 | /** |
||
43 | * {@inheritDoc} |
||
44 | */ |
||
45 | 1 | public function getDescription() |
|
46 | { |
||
47 | 1 | return 'Enriches inline example tags with their sources'; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritDoc} |
||
52 | */ |
||
53 | 4 | public function execute(ProjectDescriptor $project) |
|
54 | { |
||
55 | 4 | $elements = $project->getIndexes()->get('elements'); |
|
56 | |||
57 | /** @var DescriptorAbstract $element */ |
||
58 | 4 | foreach ($elements as $element) { |
|
59 | 4 | $element->setDescription($this->replaceInlineExamples($element)); |
|
60 | } |
||
61 | 4 | } |
|
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 | 4 | protected function replaceInlineExamples(DescriptorAbstract $element) |
|
71 | { |
||
72 | 4 | $description = $element->getDescription(); |
|
73 | 4 | $matches = array(); |
|
74 | |||
75 | 4 | if (! $description |
|
76 | 4 | || ! preg_match_all('/\{@example\s(.+?)\}/', $description, $matches) |
|
77 | 4 | || count($matches[0]) < 1 |
|
78 | ) { |
||
79 | 1 | return $description; |
|
80 | } |
||
81 | |||
82 | 3 | $matched = array(); |
|
83 | 3 | foreach ($matches[0] as $index => $match) { |
|
84 | 3 | if (isset($matched[$match])) { |
|
85 | 1 | continue; |
|
86 | } |
||
87 | |||
88 | 3 | $matched[$match] = true; |
|
89 | 3 | $exampleReflector = Example::create($matches[1][$index]); |
|
90 | |||
91 | 3 | $example = $this->exampleAssembler->create($exampleReflector); |
|
0 ignored issues
–
show
|
|||
92 | |||
93 | 3 | $replacement = '`'.$example->getExample().'`'; |
|
94 | 3 | if ($example->getDescription()) { |
|
95 | 1 | $replacement = '*' . $example->getDescription() . '*' . $replacement; |
|
96 | } |
||
97 | |||
98 | 3 | $description = str_replace($match, $replacement, $description); |
|
99 | } |
||
100 | |||
101 | 3 | return $description; |
|
102 | } |
||
103 | } |
||
104 |
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: