Conditions | 11 |
Paths | 12 |
Total Lines | 90 |
Code Lines | 55 |
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 |
||
23 | public function normalize($target, $label, array $data, array $ownerConfig) |
||
24 | { |
||
25 | if (!isset($ownerConfig[PluginConfig::PATCHES_BASE])) { |
||
26 | return array(); |
||
27 | } |
||
28 | |||
29 | $source = $data[PatchDefinition::SOURCE]; |
||
30 | |||
31 | if (parse_url($source, PHP_URL_SCHEME)) { |
||
32 | return array(); |
||
33 | } |
||
34 | |||
35 | if (isset($data[PatchDefinition::LABEL]) && is_numeric($label)) { |
||
36 | $label = $data[PatchDefinition::LABEL]; |
||
37 | } |
||
38 | |||
39 | if (strpos($data[PatchDefinition::PATH], DIRECTORY_SEPARATOR) === 0 |
||
40 | && file_exists($data[PatchDefinition::PATH]) |
||
41 | ) { |
||
42 | return array( |
||
43 | PatchDefinition::LABEL => $label, |
||
44 | PatchDefinition::SOURCE => $source |
||
45 | ); |
||
46 | } |
||
47 | |||
48 | $template = $this->resolveTemplate($ownerConfig, $target); |
||
49 | |||
50 | $nameParts = explode('/', $target); |
||
51 | |||
52 | $sourceTags = ''; |
||
53 | |||
54 | if (strpos($source, '#') !== false) { |
||
55 | $sourceSegments = explode('#', $source); |
||
56 | $sourceTags = array_pop($sourceSegments); |
||
57 | $source = implode('#', $sourceSegments); |
||
58 | } |
||
59 | |||
60 | $pathVariables = array( |
||
61 | 'file' => $source, |
||
62 | 'vendor' => array_shift($nameParts), |
||
63 | 'package' => implode('/', $nameParts), |
||
64 | 'label' => $label |
||
65 | ); |
||
66 | |||
67 | $mutationNamesMap = array( |
||
68 | 'file' => 'file name', |
||
69 | 'vendor' => 'vendor name', |
||
70 | 'package' => 'module name', |
||
71 | 'label' => 'label value' |
||
72 | ); |
||
73 | |||
74 | $pathVariables = $this->expandPathVariables($pathVariables, $mutationNamesMap); |
||
75 | |||
76 | $extraVariables = array( |
||
77 | 'version' => preg_replace( |
||
78 | '/[^A-Za-z0-9.-]/', |
||
79 | '', |
||
80 | strtok(reset($data[PatchDefinition::DEPENDS]) ?: '0.0.0', ' ') |
||
81 | ), |
||
82 | 'file' => $source, |
||
83 | 'label' => $label |
||
84 | ); |
||
85 | |||
86 | $variablePattern = '{{%s}}'; |
||
87 | |||
88 | $templateVariables = $this->prepareTemplateValues( |
||
89 | $template, |
||
90 | $variablePattern, |
||
91 | array_replace($pathVariables, $extraVariables) |
||
92 | ); |
||
93 | |||
94 | $source = $this->templateUtils->compose( |
||
95 | $template . ($sourceTags ? ('#' . $sourceTags) : ''), |
||
96 | $templateVariables, |
||
97 | array_fill_keys(array($variablePattern), false) |
||
98 | ); |
||
99 | |||
100 | $filename = basename($source); |
||
101 | |||
102 | if (substr($label, -strlen($filename)) === $filename) { |
||
103 | $label = str_replace( |
||
104 | $filename, |
||
105 | preg_replace('/\s{2,}/', ' ', preg_replace('/[^A-Za-z0-9]/', ' ', strtok($filename, '.'))), |
||
106 | $label |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | return array( |
||
111 | PatchDefinition::LABEL => $label, |
||
112 | PatchDefinition::SOURCE => $source |
||
113 | ); |
||
205 |