Debian Codegraph @

lib/3rdParty/composer/phpdocumentor/reflection-docblock/src/DocBlock/ExampleFinder.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * This file is part of phpDocumentor.
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 *
11 * @link http://phpdoc.org
12 */
13
14 namespace phpDocumentor\Reflection\DocBlock;
15
16 use phpDocumentor\Reflection\DocBlock\Tags\Example;
17
18 use function array_slice;
19 use function file;
20 use function getcwd;
21 use function implode;
22 use function is_readable;
23 use function rtrim;
24 use function sprintf;
25 use function trim;
26
27 use const DIRECTORY_SEPARATOR;
28
29 /**
30 * Class used to find an example file's location based on a given ExampleDescriptor.
31 */
32 class ExampleFinder
33 {
34 private string $sourceDirectory = '';
35
36 /** @var string[] */
37 private array $exampleDirectories = [];
38
39 /**
40 * Attempts to find the example contents for the given descriptor.
41 */
42 public function find(Example $example): string
43 {
44 $filename = $example->getFilePath();
45
46 $file = $this->getExampleFileContents($filename);
47 if ($file === null) {
48 return sprintf('** File not found : %s **', $filename);
49 }
50
51 return implode('', array_slice($file, $example->getStartingLine() - 1, $example->getLineCount()));
52 }
53
54 /**
55 * Registers the project's root directory where an 'examples' folder can be expected.
56 */
57 public function setSourceDirectory(string $directory = ''): void
58 {
59 $this->sourceDirectory = $directory;
60 }
61
62 /**
63 * Returns the project's root directory where an 'examples' folder can be expected.
64 */
65 public function getSourceDirectory(): string
66 {
67 return $this->sourceDirectory;
68 }
69
70 /**
71 * Registers a series of directories that may contain examples.
72 *
73 * @param string[] $directories
74 */
75 public function setExampleDirectories(array $directories): void
76 {
77 $this->exampleDirectories = $directories;
78 }
79
80 /**
81 * Returns a series of directories that may contain examples.
82 *
83 * @return string[]
84 */
85 public function getExampleDirectories(): array
86 {
87 return $this->exampleDirectories;
88 }
89
90 /**
91 * Attempts to find the requested example file and returns its contents or null if no file was found.
92 *
93 * This method will try several methods in search of the given example file, the first one it encounters is
94 * returned:
95 *
96 * 1. Iterates through all examples folders for the given filename
97 * 2. Checks the source folder for the given filename
98 * 3. Checks the 'examples' folder in the current working directory for examples
99 * 4. Checks the path relative to the current working directory for the given filename
100 *
101 * @return string[] all lines of the example file
102 */
103 private function getExampleFileContents(string $filename): ?array
104 {
105 $normalizedPath = null;
106
107 foreach ($this->exampleDirectories as $directory) {
108 $exampleFileFromConfig = $this->constructExamplePath($directory, $filename);
109 if (is_readable($exampleFileFromConfig)) {
110 $normalizedPath = $exampleFileFromConfig;
111 break;
112 }
113 }
114
115 if ($normalizedPath === null) {
116 if (is_readable($this->getExamplePathFromSource($filename))) {
117 $normalizedPath = $this->getExamplePathFromSource($filename);
118 } elseif (is_readable($this->getExamplePathFromExampleDirectory($filename))) {
119 $normalizedPath = $this->getExamplePathFromExampleDirectory($filename);
120 } elseif (is_readable($filename)) {
121 $normalizedPath = $filename;
122 }
123 }
124
125 $lines = $normalizedPath !== null && is_readable($normalizedPath) ? file($normalizedPath) : false;
126
127 return $lines !== false ? $lines : null;
128 }
129
130 /**
131 * Get example filepath based on the example directory inside your project.
132 */
133 private function getExamplePathFromExampleDirectory(string $file): string
134 {
135 return getcwd() . DIRECTORY_SEPARATOR . 'examples' . DIRECTORY_SEPARATOR . $file;
136 }
137
138 /**
139 * Returns a path to the example file in the given directory..
140 */
141 private function constructExamplePath(string $directory, string $file): string
142 {
143 return rtrim($directory, '\\/') . DIRECTORY_SEPARATOR . $file;
144 }
145
146 /**
147 * Get example filepath based on sourcecode.
148 */
149 private function getExamplePathFromSource(string $file): string
150 {
151 return sprintf(
152 '%s%s%s',
153 trim($this->getSourceDirectory(), '\\/'),
154 DIRECTORY_SEPARATOR,
155 trim($file, '"')
156 );
157 }
158 }