Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

Doc::toXhtml()   D

Complexity

Conditions 9
Paths 36

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 36
nop 2
dl 0
loc 37
rs 4.909
c 0
b 0
f 0
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();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class ezcDocumentRstVisitor as the method getTableOfContents() does only exist in the following sub-classes of ezcDocumentRstVisitor: phpDocumentor\Plugin\Scr...edText\Visitors\Creator, phpDocumentor\Plugin\Scr...dText\Visitors\Discover. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
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