Completed
Push — develop ( a89061...54507c )
by Jaap
08:53
created

Converter/RestructuredText/Visitors/Discover.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
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2018 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\Visitors;
13
14
use \phpDocumentor\Plugin\Scrybe\Converter\Metadata\TableOfContents;
15
use ezcDocumentRstDocumentNode;
16
use ezcDocumentRstSectionNode;
17
18
/**
19
 * A specialized RestructuredText Parser/Visitor to aid in the discovery phase.
20
 *
21
 * This class collects all headings and their titles and populates the TableOfContents collection.
22
 */
23
class Discover extends Creator
24
{
25
    /**
26
     * This array is meant as a cache of the last entry per depth.
27
     *
28
     * To build a hierarchy from a non-recursive method, such as visitSection(), you need a way to reference the last
29
     * Entry per depth.
30
     *
31
     * By keeping track of these pointers you know onto which parent you will need to add a node by checking which of
32
     * higher depth was parsed last.
33
     *
34
     * Important: because it is possible that levels are 'skipped' we will need to unset all 'deeper' depths when
35
     * setting a new one. Otherwise we might inadvertently add an entry to the wrong tree.
36
     *
37
     * @var TableOfContents\Heading[]
38
     */
39
    protected $entry_pointers = [];
40
41
    /**
42
     * This is a pointer to the last discovered heading.
43
     *
44
     * Directives and roles may 'include' Files as children of the currently parsed heading. Elements as the toctree
45
     * directive or a plain include are examples of such.
46
     *
47
     * @var TableOfContents\Heading
48
     */
49
    protected $last_heading = null;
50
51
    public function visit(\ezcDocumentRstDocumentNode $ast)
52
    {
53
        $toc = $this->getTableOfContents();
54
        $file = $toc[$this->getFilenameWithoutExtension()];
55
        $this->entry_pointers[0] = null; // there is no level 0, 1-based list
56
        $this->entry_pointers[1] = $file;
57
        $this->last_heading = $file;
58
59
        return parent::visit($ast);
60
    }
61
62
    /**
63
     * Visitor for the section heading used to populate the TableOfContents.
64
     *
65
     * This method interprets the heading and its containing text and adds new entries to the TableOfContents object
66
     * in the RestructuredText document.
67
     *
68
     * @see getDocument() for the document containing the TableOfContents.
69
     * @see phpDocumentor\Plugin\Scrybe\Converter\Metadata\TableOfContents for the Table of Contents class.
70
     */
71
    protected function visitSection(\DOMNode $root, \ezcDocumentRstNode $node)
72
    {
73
        if ($node instanceof ezcDocumentRstSectionNode || $node instanceof ezcDocumentRstDocumentNode) {
74
            if ($node->depth === 1) {
75
                $toc = $this->getTableOfContents();
76
                $file = $toc[$this->getFilenameWithoutExtension()];
77
                $file->setName($this->nodeToString($node->title));
78
            } else {
79
                // find nearest parent pointer depth-wise
80
                $parent_depth = $node->depth - 1;
81
                while (!isset($this->entry_pointers[$parent_depth]) && $parent_depth > 0) {
82
                    --$parent_depth;
83
                }
84
85
                $parent = $this->entry_pointers[$parent_depth];
86
                $heading = new TableOfContents\Heading($parent);
87
                $heading->setName($this->nodeToString($node->title));
88
                $heading->setSlug($node->reference);
89
                $parent->addChild($heading);
0 ignored issues
show
$heading is of type object<phpDocumentor\Plu...ableOfContents\Heading>, but the function expects a object<self>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
91
                // set as last indexed heading
92
                $this->last_heading = $heading;
93
94
                // add as new entry pointer
95
                array_splice($this->entry_pointers, $parent_depth + 1, count($this->entry_pointers), [$heading]);
96
            }
97
        }
98
99
        parent::visitSection($root, $node);
100
    }
101
102
    /**
103
     * Adds a TableOfContents File object to the last heading that was discovered.
104
     *
105
     * This may be used by roles or directives to insert an include file into the TableOfContents and thus all
106
     * its headings.
107
     *
108
     * This method is explicitly bound to File objects and not other BaseEntry descendents because inline elements
109
     * such as headings should also modify the internal pointers for this visitor.
110
     */
111
    public function addFileToLastHeading(TableOfContents\File $file)
112
    {
113
        $this->last_heading->addChild($file);
114
        $file->setParent($this->last_heading);
115
    }
116
}
117