Completed
Push — develop ( 4b49c4...89d32a )
by Jaap
09:06 queued 05:30
created

LicenseTag::process()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 68
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 41
nc 6
nop 1
dl 0
loc 68
ccs 0
cts 22
cp 0
crap 56
rs 6.9654
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
/**
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\Core\Transformer\Behaviour\Tag;
13
14
/**
15
 * Behaviour that enables links to URLs in the @license tag.
16
 */
17
class LicenseTag
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
     * @return \DOMDocument
26
     */
27
    public function process(\DOMDocument $xml)
28
    {
29
        $licenseMap = array(
30
            '#^\s*(GPL|GNU General Public License)((\s?v?|version)?2)\s*$#i'
31
                => 'http://opensource.org/licenses/GPL-2.0',
32
            '#^\s*(GPL|GNU General Public License)((\s?v?|version)?3?)\s*$#i'
33
                => 'http://opensource.org/licenses/GPL-3.0',
34
            '#^\s*(LGPL|GNU (Lesser|Library) (General Public License|GPL))'
35
                .'((\s?v?|version)?2(\.1)?)\s*$#i'
36
                => 'http://opensource.org/licenses/LGPL-2.1',
37
            '#^\s*(LGPL|GNU (Lesser|Library) (General Public License|GPL))'
38
                .'((\s?v?|version)?3?)\s*$#i'
39
                => 'http://opensource.org/licenses/LGPL-3.0',
40
            '#^\s*((new |revised |modified |three-clause |3-clause )BSD'
41
                .'( License)?)\s*$#i'
42
                => 'http://opensource.org/licenses/BSD-3-Clause',
43
            '#^\s*((simplified |two-clause |2-clause |Free)BSD)( License)?\s*$#i'
44
                => 'http://opensource.org/licenses/BSD-2-Clause',
45
            '#^\s*MIT( License)?\s*$#i' => 'http://opensource.org/licenses/MIT',
46
        );
47
48
        $xpath = new \DOMXPath($xml);
49
        $nodes = $xpath->query('//tag[@name="license"]/@description');
50
51
        /** @var \DOMElement $node */
52
        foreach ($nodes as $node) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
53
54
            $license = $node->nodeValue;
55
56
            // FIXME: migrate to '#^' . PHPDOC::LINK_REGEX . '(\s+(?P<text>.+))
57
            // ?$#u' once that const exists
58
            if (preg_match(
59
                '#^(?i)\b(?P<url>(?:https?://|www\d{0,3}\.|[a-z0-9.\-]+\.'
60
                .'[a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+'
61
                .'(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|'
62
                .'[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))(\s+(?P<text>.+))?$#u',
63
                $license,
64
                $matches
65
            )) {
66
                if (!isset($matches['text']) || !$matches['text']) {
67
                    // set text to URL if not present
68
                    $matches['text'] = $matches['url'];
69
                }
70
                /** @var \DOMElement $parentNode */
71
                $parentNode = $node->parentNode;
72
                $parentNode->setAttribute('link', $matches['url']);
73
                $node->nodeValue = $matches['text'];
74
75
                // bail out early
76
                continue;
77
            }
78
79
            // check map if any license matches
80
            foreach ($licenseMap as $regex => $url) {
81
                if (preg_match($regex, $license, $matches)) {
82
83
                    /** @var \DOMElement $parentNode */
84
                    $parentNode = $node->parentNode;
85
                    $parentNode->setAttribute('link', $url);
86
87
                    // we're done here
88
                    break;
89
                }
90
            }
91
        }
92
93
        return $xml;
94
    }
95
}
96