Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

Converter/RestructuredText/Directives/Toctree.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Plugin\Scrybe\Converter\RestructuredText\Directives;
17
18
use phpDocumentor\Plugin\Scrybe\Converter\RestructuredText\Visitors\Creator;
19
use phpDocumentor\Plugin\Scrybe\Converter\RestructuredText\Visitors\Discover;
20
21
/**
22
 * Directive used to process `.. toctree::` and insert entries from the table of contents.
23
 *
24
 * This directive tries to match the file with an entry in the table of contents during the creation phase. If a
25
 * document is found it will generate a mini-table of contents at that location with the depth given using the
26
 * `:maxdepth:` parameter.
27
 *
28
 * Another option is :hidden: that will hide the toc from view while still making connections.
29
 *
30
 * This directive is inspired by {@link http://sphinx.pocoo.org/concepts.html#the-toc-tree Sphinx' toctree} directive.
31
 */
32
class Toctree extends \ezcDocumentRstDirective implements \ezcDocumentRstXhtmlDirective
33
{
34
    protected $links = [];
35
36
    public function __construct(\ezcDocumentRstDocumentNode $ast, $path, \ezcDocumentRstDirectiveNode $node)
37
    {
38
        parent::__construct($ast, $path, $node);
39
40
        $this->parseLinks();
41
    }
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)
82
    {
83
        $this->addLinksToTableOfContents();
84
85
        // if the hidden flag is set then this item should not be rendered but still processed (see above)
86
        if (isset($this->node->options['hidden'])) {
87
            return;
88
        }
89
90
        $list = $document->createElement('ol');
91
        $root->appendChild($list);
92
93
        foreach ($this->links as $link) {
94
            $list_item = $document->createElement('li');
95
            $list->appendChild($list_item);
96
97
            $link_element = $document->createElement('a');
98
            $list_item->appendChild($link_element);
99
            $link_element->appendChild($document->createTextNode($this->getCaption($link)));
100
            $link_element->setAttribute('href', str_replace('\\', '/', $link) . '.html');
101
        }
102
    }
103
104
    protected function addLinksToTableOfContents()
105
    {
106
        foreach ($this->links as $file_name) {
107
            /** @var Discover $visitor */
108
            $visitor = $this->visitor;
109
            if ($visitor instanceof Discover) {
110
                $toc = $visitor->getTableOfContents();
111
                $file = $toc[$file_name];
112
113
                $visitor->addFileToLastHeading($file);
114
            }
115
        }
116
    }
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)
128
    {
129
        if ($this->visitor instanceof Creator) {
130
            $toc = $this->visitor->getTableOfContents();
131
            $name = $toc[$file_name]->getName();
132
            return $name ?: $file_name;
133
        }
134
135
        return $file_name;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $file_name; (ezcDocumentRstToken) is incompatible with the return type documented by phpDocumentor\Plugin\Scr...ves\Toctree::getCaption of type string.

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:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
136
    }
137
}
138