Passed
Push — master ( 24583f...3a81a0 )
by Julius
01:46
created

GithubLocationExtension::getGithubLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace JuliusHaertl\PHPDocToRst\Extension;
25
26
use JuliusHaertl\PHPDocToRst\Builder\FileBuilder;
27
use JuliusHaertl\PHPDocToRst\Builder\PhpDomainBuilder;
28
use phpDocumentor\Reflection\Element;
29
use phpDocumentor\Reflection\Php\File;
30
31
/**
32
 * This extension adds a link to the source at github to all elements
33
 */
34
35
class GithubLocationExtension extends Extension {
36
37
    protected $basePath;
38
    protected $githubRepo;
39
40
    public function prepare() {
41
        if (count($this->arguments) !== 2) {
42
            throw new \Exception('GithubLocationExtension requires the following arguments githubUrl, basePath.');
43
        }
44
        $this->basePath = $this->arguments[0];
45
        $this->githubRepo = $this->arguments[1];
46
    }
47
48
    /**
49
     * @param string $type
50
     * @param FileBuilder $builder
51
     * @param Element $element
52
     */
53
    public function render($type, &$builder, $element) {
54
        if (!$builder instanceof FileBuilder) {
55
            return;
56
        }
57
        if ($type === PhpDomainBuilder::SECTION_AFTER_DESCRIPTION) {
58
            if (!$builder->getFile() instanceof File) {
59
                return;
60
            }
61
            $filePath = $builder->getFile()->getPath();
62
            $filePath = preg_replace('/^' . preg_quote($this->basePath, '/') . '/', '', $filePath);
63
            $lineNumber = $element->getLocation()->getLineNumber();
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

63
            $lineNumber = $element->/** @scrutinizer ignore-call */ getLocation()->getLineNumber();
Loading history...
64
            $url = $this->getGithubLink($filePath, $lineNumber);
65
            print_r($filePath);
66
            print_r($url);
67
            $builder->addFieldList('Source', '`' . $filePath. '#' . $lineNumber . ' <'.$url.'>`_');
68
            $builder->addLine();
69
        }
70
    }
71
72
    private function getGithubLink($file, $line=1, $branch='master') {
73
        return $this->githubRepo . '/blob/'.$branch.'/'.$file.'#L' . $line;
74
    }
75
76
}