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

Transformer/Behaviour/Tag/AuthorTag.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\Transformer\Behaviour\Tag;
17
18
/**
19
 * Behaviour that links to email addresses in the @author tag.
20
 */
21
class AuthorTag
22
{
23
    /**
24
     * Find all return tags that contain 'self' or '$this' and replace those
25
     * terms for the name of the current class' type.
26
     *
27
     * @param \DOMDocument $xml Structure source to apply behaviour onto.
28
     *
29
     * @return \DOMDocument
30
     */
31
    public function process(\DOMDocument $xml)
32
    {
33
        // matches:
34
        // - [email protected]
35
        // - <[email protected]>
36
        // - Some Name <[email protected]>
37
        // ignores leading and trailing whitespace
38
        // requires angled brackets when a name is given (that's what the
39
        //   two (?(1)) conditions do)
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
40
        // requires closing angled bracket if email address is given with an
41
        //   opening angled bracket but no name (that's what the (?(3))
42
        //   condition is for)
43
        $regex = '#^\s*(?P<name>[^<]+?)?\s*((?(1)<|<?)(?:mailto:)?'
44
            . '(?P<email>\b[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}\b)'
45
            . '(?(1)>|(?(3)>|>?)))\s*$#u';
46
47
        $xpath = new \DOMXPath($xml);
48
        $nodes = $xpath->query('//tag[@name="author"]/@description');
49
50
        /** @var \DOMElement $node */
51
        foreach ($nodes as $node) {
52
            // FIXME: #193
53
            if (preg_match($regex, $node->nodeValue, $matches)) {
54
                if ($matches['name']) {
55
                    $value = $matches['name'];
56
                } else {
57
                    // in case there were <> but no name... this cleans up the
58
                    // output a bit
59
                    $value = $matches['email'];
60
                }
61
62
                $node->nodeValue = $value;
63
64
                /** @var \DOMElement $parentNode */
65
                $parentNode = $node->parentNode;
66
                $parentNode->setAttribute(
67
                    'link',
68
                    'mailto:' . $matches['email']
69
                );
70
            }
71
        }
72
73
        return $xml;
74
    }
75
}
76