| Conditions | 11 |
| Paths | 12 |
| Total Lines | 96 |
| Code Lines | 59 |
| 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 | $mutationRules = $this->templateUtils->collectValueMutationRules($template, array($variablePattern)); |
||
| 89 | |||
| 90 | $templateVariables = array_replace( |
||
| 91 | $this->templateUtils->applyMutations( |
||
| 92 | array_replace($pathVariables, $extraVariables), |
||
| 93 | $mutationRules, |
||
| 94 | ' :-_.' |
||
| 95 | ), |
||
| 96 | $pathVariables, |
||
| 97 | $extraVariables |
||
| 98 | ); |
||
| 99 | |||
| 100 | $source = $this->templateUtils->compose( |
||
| 101 | $template . ($sourceTags ? ('#' . $sourceTags) : ''), |
||
| 102 | $templateVariables, |
||
| 103 | array_fill_keys(array($variablePattern), false) |
||
| 104 | ); |
||
| 105 | |||
| 106 | $filename = basename($source); |
||
| 107 | |||
| 108 | if (substr($label, -strlen($filename)) == $filename) { |
||
| 109 | $label = str_replace( |
||
| 110 | $filename, |
||
| 111 | preg_replace('/\s{2,}/', ' ', preg_replace('/[^A-Za-z0-9]/', ' ', strtok($filename, '.'))), |
||
| 112 | $label |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | return array( |
||
| 117 | PatchDefinition::LABEL => $label, |
||
| 118 | PatchDefinition::SOURCE => $source |
||
| 119 | ); |
||
| 194 |