1 | <?php |
||
32 | class Toctree extends \ezcDocumentRstDirective implements \ezcDocumentRstXhtmlDirective |
||
33 | { |
||
34 | protected $links = []; |
||
35 | |||
36 | public function __construct(\ezcDocumentRstDocumentNode $ast, $path, \ezcDocumentRstDirectiveNode $node) |
||
42 | |||
43 | protected function parseLinks() |
||
44 | { |
||
45 | $line = ''; |
||
46 | |||
47 | /** @var \ezcDocumentRstToken $token */ |
||
48 | foreach ($this->node->tokens as $token) { |
||
49 | if ($token->type === 2) { |
||
50 | $line = trim($line); |
||
51 | if ($line) { |
||
52 | $this->links[] = $line; |
||
53 | $line = ''; |
||
54 | } |
||
55 | } |
||
56 | |||
57 | if ($token->type !== 5 && $token->type !== 4) { |
||
58 | continue; |
||
59 | } |
||
60 | |||
61 | $line .= $token->content; |
||
62 | } |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Transform directive to docbook |
||
67 | * |
||
68 | * Create a docbook XML structure at the directives position in the document. |
||
69 | */ |
||
70 | public function toDocbook(\DOMDocument $document, \DOMElement $root) |
||
71 | { |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Transform directive to HTML |
||
76 | * |
||
77 | * Create a XHTML structure at the directives position in the document. |
||
78 | * |
||
79 | * @todo use the TableofContents collection to extract a sublisting up to the given depth or 2 if none is provided |
||
80 | */ |
||
81 | public function toXhtml(\DOMDocument $document, \DOMElement $root) |
||
103 | |||
104 | protected function addLinksToTableOfContents() |
||
105 | { |
||
117 | |||
118 | /** |
||
119 | * Retrieves the caption for the given $token. |
||
120 | * |
||
121 | * The caption is retrieved by converting the filename to a human-readable format. |
||
122 | * |
||
123 | * @param \ezcDocumentRstToken $file_name |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | protected function getCaption($file_name) |
||
137 | } |
||
138 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.