Conditions | 7 |
Paths | 6 |
Total Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 |