1 | <?php |
||||
2 | /** |
||||
3 | * @copyright Copyright (c) 2017 Julius Härtl <[email protected]> |
||||
4 | * @author Julius Härtl <[email protected]> |
||||
5 | * @license GNU AGPL version 3 or any later version |
||||
6 | * |
||||
7 | * This program is free software: you can redistribute it and/or modify |
||||
8 | * it under the terms of the GNU Affero General Public License as |
||||
9 | * published by the Free Software Foundation, either version 3 of the |
||||
10 | * License, or (at your option) any later version. |
||||
11 | * |
||||
12 | * This program is distributed in the hope that it will be useful, |
||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
15 | * GNU Affero General Public License for more details. |
||||
16 | * |
||||
17 | * You should have received a copy of the GNU Affero General Public License |
||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
19 | */ |
||||
20 | |||||
21 | namespace JuliusHaertl\PHPDocToRst\Extension; |
||||
22 | |||||
23 | use Exception; |
||||
24 | use JuliusHaertl\PHPDocToRst\Builder\FileBuilder; |
||||
25 | use JuliusHaertl\PHPDocToRst\Builder\PhpDomainBuilder; |
||||
26 | use phpDocumentor\Reflection\Element; |
||||
27 | use phpDocumentor\Reflection\Php\File; |
||||
28 | |||||
29 | /** |
||||
30 | * This extension adds a link to the source at github to all elements. |
||||
31 | * |
||||
32 | * Arguments |
||||
33 | * 0 => Url to the github repo (required) |
||||
34 | * 1 => Path to the git repository (required) |
||||
35 | * 2 => Branch to link to (default=master) |
||||
36 | */ |
||||
37 | class GithubLocationExtension extends Extension |
||||
38 | { |
||||
39 | protected $basePath; |
||||
40 | protected $githubRepo; |
||||
41 | protected $branch = 'master'; |
||||
42 | |||||
43 | public function prepare() |
||||
44 | { |
||||
45 | if (count($this->arguments) < 2) { |
||||
46 | throw new Exception('GithubLocationExtension requires the following arguments githubUrl, basePath.'); |
||||
47 | } |
||||
48 | $this->basePath = $this->arguments[0]; |
||||
49 | $this->githubRepo = $this->arguments[1]; |
||||
50 | if (count($this->arguments) > 2) { |
||||
51 | $this->branch = $this->arguments[2]; |
||||
52 | } |
||||
53 | } |
||||
54 | |||||
55 | /** |
||||
56 | * @param string $type |
||||
57 | * @param FileBuilder $builder |
||||
58 | * @param Element $element |
||||
59 | */ |
||||
60 | public function render($type, &$builder, $element) |
||||
61 | { |
||||
62 | if (!$builder instanceof FileBuilder) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
63 | return; |
||||
64 | } |
||||
65 | if ($type === PhpDomainBuilder::SECTION_AFTER_DESCRIPTION) { |
||||
66 | if (!$builder->getFile() instanceof File) { |
||||
0 ignored issues
–
show
|
|||||
67 | return; |
||||
68 | } |
||||
69 | $filePath = $builder->getFile()->getPath(); |
||||
70 | $filePath = preg_replace('/^'.preg_quote($this->basePath, '/').'/', '', $filePath); |
||||
71 | $lineNumber = $element->getLocation()->getLineNumber(); |
||||
0 ignored issues
–
show
The method
getLocation() does not exist on phpDocumentor\Reflection\Element . It seems like you code against a sub-type of said class. However, the method does not exist in phpDocumentor\Reflection\Php\Namespace_ . Are you sure you never get one of those?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
72 | $url = $this->getGithubLink($filePath, $lineNumber, $this->branch); |
||||
73 | $builder->addFieldList('Source', '`'.$filePath.'#'.$lineNumber.' <'.$url.'>`_'); |
||||
74 | } |
||||
75 | } |
||||
76 | |||||
77 | private function getGithubLink($file, $line = 1, $branch = 'master') |
||||
78 | { |
||||
79 | return $this->githubRepo.'/blob/'.$branch.'/'.$file.'#L'.$line; |
||||
80 | } |
||||
81 | } |
||||
82 |