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
|
|||
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 |
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: