Completed
Push — develop ( 80740b...61b5c3 )
by Mike
10:20
created

LicenseTag::process()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 6
nop 1
dl 0
loc 68
rs 7.7648
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 enables links to URLs in the @license tag.
20
 */
21
class LicenseTag
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
        $licenseMap = [
34
            '#^\s*(GPL|GNU General Public License)((\s?v?|version)?2)\s*$#i'
35
            => 'http://opensource.org/licenses/GPL-2.0',
36
            '#^\s*(GPL|GNU General Public License)((\s?v?|version)?3?)\s*$#i'
37
            => 'http://opensource.org/licenses/GPL-3.0',
38
            '#^\s*(LGPL|GNU (Lesser|Library) (General Public License|GPL))'
39
                . '((\s?v?|version)?2(\.1)?)\s*$#i'
40
            => 'http://opensource.org/licenses/LGPL-2.1',
41
            '#^\s*(LGPL|GNU (Lesser|Library) (General Public License|GPL))'
42
                . '((\s?v?|version)?3?)\s*$#i'
43
            => 'http://opensource.org/licenses/LGPL-3.0',
44
            '#^\s*((new |revised |modified |three-clause |3-clause )BSD'
45
                . '( License)?)\s*$#i'
46
            => 'http://opensource.org/licenses/BSD-3-Clause',
47
            '#^\s*((simplified |two-clause |2-clause |Free)BSD)( License)?\s*$#i'
48
            => 'http://opensource.org/licenses/BSD-2-Clause',
49
            '#^\s*MIT( License)?\s*$#i' => 'http://opensource.org/licenses/MIT',
50
        ];
51
52
        $xpath = new \DOMXPath($xml);
53
        $nodes = $xpath->query('//tag[@name="license"]/@description');
54
55
        /** @var \DOMElement $node */
56
        foreach ($nodes as $node) {
57
            $license = $node->nodeValue;
58
59
            // FIXME: migrate to '#^' . PHPDOC::LINK_REGEX . '(\s+(?P<text>.+))
60
            // ?$#u' once that const exists
61
            if (preg_match(
62
                '#^(?i)\b(?P<url>(?:https?://|www\d{0,3}\.|[a-z0-9.\-]+\.'
63
                . '[a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+'
64
                . '(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|'
65
                . '[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))(\s+(?P<text>.+))?$#u',
66
                $license,
67
                $matches
68
            )) {
69
                if (!isset($matches['text']) || !$matches['text']) {
70
                    // set text to URL if not present
71
                    $matches['text'] = $matches['url'];
72
                }
73
74
                /** @var \DOMElement $parentNode */
75
                $parentNode = $node->parentNode;
76
                $parentNode->setAttribute('link', $matches['url']);
77
                $node->nodeValue = $matches['text'];
78
79
                // bail out early
80
                continue;
81
            }
82
83
            // check map if any license matches
84
            foreach ($licenseMap as $regex => $url) {
85
                if (preg_match($regex, $license, $matches)) {
86
87
                    /** @var \DOMElement $parentNode */
88
                    $parentNode = $node->parentNode;
89
                    $parentNode->setAttribute('link', $url);
90
91
                    // we're done here
92
                    break;
93
                }
94
            }
95
        }
96
97
        return $xml;
98
    }
99
}
100