1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiGen\Templating\Filters; |
4
|
|
|
|
5
|
|
|
use ApiGen\Contracts\Configuration\ConfigurationInterface; |
6
|
|
|
use ApiGen\Contracts\Parser\Reflection\Behavior\InClassInterface; |
7
|
|
|
use ApiGen\Contracts\Parser\Reflection\Behavior\LinedInterface; |
8
|
|
|
use ApiGen\Contracts\Parser\Reflection\ClassReflectionInterface; |
9
|
|
|
use ApiGen\Contracts\Parser\Reflection\ConstantReflectionInterface; |
10
|
|
|
use ApiGen\Contracts\Parser\Reflection\ElementReflectionInterface; |
11
|
|
|
use ApiGen\Contracts\Parser\Reflection\FunctionReflectionInterface; |
12
|
|
|
|
13
|
|
|
final class SourceFilters extends Filters |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ConfigurationInterface |
17
|
|
|
*/ |
18
|
|
|
private $configuration; |
19
|
|
|
|
20
|
|
|
public function __construct(ConfigurationInterface $configuration) |
21
|
|
|
{ |
22
|
|
|
$this->configuration = $configuration; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function staticFile(string $name): string |
26
|
|
|
{ |
27
|
|
|
$filename = $this->configuration->getOption('destination') . '/' . $name; |
28
|
|
|
if (is_file($filename)) { |
29
|
|
|
$name .= '?' . sha1_file($filename); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $name; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ElementReflectionInterface $element |
37
|
|
|
* @param bool $withLine Include file line number into the link |
38
|
|
|
*/ |
39
|
|
|
public function sourceUrl(ElementReflectionInterface $element, bool $withLine = true): string |
40
|
|
|
{ |
41
|
|
|
$file = ''; |
42
|
|
|
$elementName = ''; |
43
|
|
|
|
44
|
|
|
if ($this->isDirectUrl($element)) { |
45
|
|
|
$elementName = $element->getName(); |
46
|
|
|
if ($element instanceof ClassReflectionInterface) { |
47
|
|
|
$file = 'class-'; |
48
|
|
|
} elseif ($element instanceof ConstantReflectionInterface) { |
49
|
|
|
$file = 'constant-'; |
50
|
|
|
} elseif ($element instanceof FunctionReflectionInterface) { |
51
|
|
|
$file = 'function-'; |
52
|
|
|
} |
53
|
|
|
} elseif ($element instanceof InClassInterface) { |
54
|
|
|
$elementName = $element->getDeclaringClassName(); |
55
|
|
|
$file = 'class-'; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$file .= self::urlize($elementName); |
59
|
|
|
|
60
|
|
|
$url = sprintf($this->configuration->getOption('template')['templates']['source']['filename'], $file); |
61
|
|
|
if ($withLine) { |
62
|
|
|
$url .= $this->getElementLinesAnchor($element); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $url; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function isDirectUrl(ElementReflectionInterface $element): bool |
69
|
|
|
{ |
70
|
|
|
if ($element instanceof ClassReflectionInterface |
71
|
|
|
|| $element instanceof FunctionReflectionInterface |
72
|
|
|
|| $element instanceof ConstantReflectionInterface |
73
|
|
|
) { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function getElementLinesAnchor(LinedInterface $element): string |
81
|
|
|
{ |
82
|
|
|
$anchor = '#' . $element->getStartLine(); |
83
|
|
|
if ($element->getStartLine() !== $element->getEndLine()) { |
84
|
|
|
$anchor .= '-' . $element->getEndLine(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $anchor; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: