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\Plugin\Scrybe\Converter\RestructuredText\Roles; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The :doc: role creates a link to an external document. |
16
|
|
|
* |
17
|
|
|
* For this link you can either use relative locations or an absolute notation. |
18
|
|
|
* The absolute notation uses the documentation root as starting directory. |
19
|
|
|
*/ |
20
|
|
|
class Doc extends \ezcDocumentRstTextRole implements \ezcDocumentRstXhtmlTextRole |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Transform text role to docbook. |
24
|
|
|
* |
25
|
|
|
* Create a docbook XML structure at the text roles position in the document. |
26
|
|
|
* |
27
|
|
|
* @param \DOMDocument $document |
28
|
|
|
* @param \DOMElement $root |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function toDocbook(\DOMDocument $document, \DOMElement $root) |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Transform text role to HTML. |
39
|
|
|
* |
40
|
|
|
* Create a XHTML structure at the text roles position in the document. |
41
|
|
|
* |
42
|
|
|
* @param \DOMDocument $document |
43
|
|
|
* @param \DOMElement $root |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function toXhtml(\DOMDocument $document, \DOMElement $root) |
48
|
|
|
{ |
49
|
|
|
$content = ''; |
50
|
|
|
$caption = ''; |
51
|
|
|
|
52
|
|
|
foreach ($this->node->nodes as $node) { |
53
|
|
|
$content .= $node->token->content; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$matches = array(); |
57
|
|
|
if (preg_match('/([^<]*)<?([^>]*)>?/', $content, $matches)) { |
58
|
|
|
// if the role uses the `caption<content>` notation; extract the two parts |
59
|
|
|
if (isset($matches[2]) && $matches[2]) { |
60
|
|
|
$content = $matches[2]; |
61
|
|
|
$caption = trim($matches[1]); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// check the table of contents for a caption. |
66
|
|
|
if (!$caption && $this->visitor) { |
67
|
|
|
$toc = $this->visitor->getTableOfContents(); |
|
|
|
|
68
|
|
|
$caption = isset($toc[$content]) ? $toc[$content]->getName() : ''; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// if no caption is captured; create one. |
72
|
|
|
if (!$caption) { |
73
|
|
|
$caption = str_replace( |
74
|
|
|
array('-', '_'), |
75
|
|
|
' ', |
76
|
|
|
ucfirst(ltrim(substr(htmlspecialchars($content), strrpos($content, '/')), '\\/')) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$link = $document->createElement('a', $caption); |
81
|
|
|
$root->appendChild($link); |
82
|
|
|
$link->setAttribute('href', str_replace('\\', '/', $content) . '.html'); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: