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

Core/Transformer/Behaviour/Tag/CoversTag.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\Core\Transformer\Behaviour\Tag;
13
14
/**
15
 * Behaviour that adds support for the @covers tag
16
 */
17
class CoversTag
18
{
19
    /**
20
     * Find all return tags that contain 'self' or '$this' and replace those
21
     * terms for the name of the current class' type.
22
     *
23
     * @param \DOMDocument $xml Structure source to apply behaviour onto.
24
     *
25
     * @todo split method into submethods
26
     *
27
     * @return \DOMDocument
28
     */
29
    public function process(\DOMDocument $xml)
30
    {
31
        $xpath = new \DOMXPath($xml);
32
        $nodes = $xpath->query('//tag[@name=\'covers\']');
33
34
        /** @var \DOMElement $node */
35
        foreach ($nodes as $node) {
36
            $refers = $node->getAttribute('refers');
37
            $refers_array = explode('::', $refers);
38
39
            // determine the type so we know where to put the @coveredby tag on
40
            $type = 'class';
41
            if (isset($refers_array[1])) {
42
                // starts with $ = property, ends with () = method,
43
                // otherwise constant
44
                $type = $refers_array[1][0] === '$' ? 'property' : 'constant';
45
                $type = substr($refers_array[1], -2) === '()' ? 'method' : $type;
46
            }
47
48
            switch ($type) {
49
                case 'class':
50
                    // escape single quotes in the class name
51
                    $xpath_refers = 'concat(\'' . str_replace(
52
                        ["'", '"'],
53
                        ['\', "\'", \'', '\', \'"\' , \''],
54
                        $refers
55
                    ) . "', '')";
56
57
                    $qry = '/project/file/class[full_name=' . $xpath_refers . ']';
58
                    break;
59
                default:
60
                    $class_name = $refers_array[0];
61
62
                    // escape single quotes in the class name
63
                    $xpath_class_name = 'concat(\'' . str_replace(
64
                        ["'", '"'],
65
                        ['\', "\'", \'', '\', \'"\' , \''],
66
                        $class_name
67
                    ) . "', '')";
68
69
                    // escape single quotes in the method name
70
                    $xpath_method_name = 'concat(\'' . str_replace(
71
                        ["'", '"'],
72
                        ['\', "\'", \'', '\', \'"\' , \''],
73
                        rtrim($refers_array[1], '()')
74
                    ) . "', '')";
75
76
                    $qry = '/project/file/class[full_name=' . $xpath_class_name
77
                        . ']/' . $type . '[name=' . $xpath_method_name . ']';
78
                    break;
79
            }
80
81
            /** @noinspection PhpUsageOfSilenceOperatorInspection as there is no pre-validation possible */
82
            $referral_nodes = @$xpath->query($qry);
83
84
            // if the query is wrong; output a Critical error and continue to
85
            // the next @covers
86
            if ($referral_nodes === false) {
87
                // $this->log(
88
                //    'An XPath error occurs while processing @covers, the query used was: ' . $qry,
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
89
                //    LogLevel::CRITICAL
90
                // );
91
                continue;
92
            }
93
94
            // check if the result is unique; if not we error and continue
95
            // to the next @covers
96
            if ($referral_nodes->length > 1) {
97
                continue;
98
            }
99
100
            // if there is one matching element; link them together
101
            if ($referral_nodes->length > 0) {
102
                /** @var \DOMElement $referral */
103
                $referral = $referral_nodes->item(0);
104
                $docblock = $referral->getElementsByTagName('docblock');
105
                if ($docblock->length < 1) {
106
                    $docblock = new \DOMElement('docblock');
107
                    $referral->appendChild($docblock);
108
                } else {
109
                    $docblock = $docblock->item(0);
110
                }
111
112
                $used_by = new \DOMElement('tag');
113
                $docblock->appendChild($used_by);
114
                $used_by->setAttribute('name', 'used_by');
115
                $used_by->setAttribute('line', '');
116
117
                // gather the name of the referring element and set that as refers
118
                // attribute
119
                if ($node->parentNode->parentNode->nodeName === 'class') {
120
                    // if the element where the @covers is in is a class; nothing
121
                    // more than the class name need to returned
122
123
                    /** @var \DOMElement $grandParentNode */
124
                    $grandParentNode = $node->parentNode->parentNode;
125
                    $referral_name = $grandParentNode
126
                        ->getElementsByTagName('full_name')->item(0)->nodeValue;
127
                } else {
128
                    $referral_class_name = null;
129
                    if ($node->parentNode->parentNode->nodeName === 'method') {
130
                        // gather the name of the class where the @covers is in
131
132
                        /** @var \DOMElement $greatGrandParentNode */
133
                        $greatGrandParentNode = $node->parentNode->parentNode->parentNode;
134
                        $referral_class_name = $greatGrandParentNode
135
                            ->getElementsByTagName('full_name')->item(0)
136
                            ->nodeValue;
137
                    }
138
139
                    // gather the name of the subelement of the class where
140
                    // the @covers is in
141
142
                    /** @var \DOMElement $grandParentNode */
143
                    $grandParentNode = $node->parentNode->parentNode;
144
                    $referral_name = $grandParentNode
145
                        ->getElementsByTagName('name')->item(0)->nodeValue;
146
147
                    // if it is a method; suffix with ()
148
                    if ($node->parentNode->parentNode->nodeName === 'method'
149
                        || $node->parentNode->parentNode->nodeName === 'function'
150
                    ) {
151
                        $referral_name .= '()';
152
                    }
153
154
                    // only prefix class name if this is a class member
155
                    if ($referral_class_name) {
156
                        $referral_name = $referral_class_name . '::'
157
                            . $referral_name;
158
                    }
159
                }
160
161
                $used_by->setAttribute('description', $referral_name);
162
                $used_by->setAttribute('refers', $referral_name);
163
            }
164
        }
165
166
        return $xml;
167
    }
168
}
169