Conditions | 20 |
Paths | 2636 |
Total Lines | 134 |
Code Lines | 72 |
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 |
||
38 | function df_tools_color_color_scheme_form_submit($form, FormStateInterface $form_state) { |
||
|
|||
39 | |||
40 | // Avoid color settings spilling over to theme settings. |
||
41 | $color_settings = array('theme', 'palette', 'scheme'); |
||
42 | if ($form_state->hasValue('info')) { |
||
43 | $color_settings[] = 'info'; |
||
44 | } |
||
45 | foreach ($color_settings as $setting_name) { |
||
46 | ${$setting_name} = $form_state->getValue($setting_name); |
||
47 | $form_state->unsetValue($setting_name); |
||
48 | } |
||
49 | if (!isset($info)) { |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | $config = \Drupal::configFactory()->getEditable('color.theme.' . $theme); |
||
54 | |||
55 | // Resolve palette. |
||
56 | if ($scheme != '') { |
||
57 | foreach ($palette as $key => $color) { |
||
58 | if (isset($info['schemes'][$scheme]['colors'][$key])) { |
||
59 | $palette[$key] = $info['schemes'][$scheme]['colors'][$key]; |
||
60 | } |
||
61 | } |
||
62 | $palette += $info['schemes']['default']['colors']; |
||
63 | } |
||
64 | |||
65 | // Make sure enough memory is available. |
||
66 | if (isset($info['base_image'])) { |
||
67 | // Fetch source image dimensions. |
||
68 | $source = drupal_get_path('theme', $theme) . '/' . $info['base_image']; |
||
69 | list($width, $height) = getimagesize($source); |
||
70 | |||
71 | // We need at least a copy of the source and a target buffer of the same |
||
72 | // size (both at 32bpp). |
||
73 | $required = $width * $height * 8; |
||
74 | // We intend to prevent color scheme changes if there isn't enough memory |
||
75 | // available. memory_get_usage(TRUE) returns a more accurate number than |
||
76 | // memory_get_usage(), therefore we won't inadvertently reject a color |
||
77 | // scheme change based on a faulty memory calculation. |
||
78 | $usage = memory_get_usage(TRUE); |
||
79 | $memory_limit = ini_get('memory_limit'); |
||
80 | $size = Bytes::toInt($memory_limit); |
||
81 | if (!Environment::checkMemoryLimit($usage + $required, $memory_limit)) { |
||
82 | drupal_set_message(t('There is not enough memory available to PHP to change this theme\'s color scheme. You need at least %size more. Check the <a href="http://php.net/manual/ini.core.php#ini.sect.resource-limits">PHP documentation</a> for more information.', array('%size' => format_size($usage + $required - $size))), 'error'); |
||
83 | return; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | // Delete old files. |
||
88 | $files = $config->get('files'); |
||
89 | if (isset($files)) { |
||
90 | foreach ($files as $file) { |
||
91 | @drupal_unlink($file); |
||
92 | } |
||
93 | } |
||
94 | if (isset($file) && $file = dirname($file)) { |
||
95 | @drupal_rmdir($file); |
||
96 | } |
||
97 | |||
98 | // No change in color config, use the standard theme from color.inc. |
||
99 | if (implode(',', color_get_palette($theme, TRUE)) == implode(',', $palette)) { |
||
100 | $config->delete(); |
||
101 | return; |
||
102 | } |
||
103 | |||
104 | // Prepare target locations for generated files. |
||
105 | $id = $theme . '-' . substr(hash('sha256', serialize($palette) . microtime()), 0, 8); |
||
106 | $paths['color'] = 'public://color'; |
||
107 | $paths['target'] = $paths['color'] . '/' . $id; |
||
108 | foreach ($paths as $path) { |
||
109 | file_prepare_directory($path, FILE_CREATE_DIRECTORY); |
||
110 | } |
||
111 | $paths['target'] = $paths['target'] . '/'; |
||
112 | $paths['id'] = $id; |
||
113 | $paths['source'] = drupal_get_path('theme', $theme) . '/'; |
||
114 | $paths['files'] = $paths['map'] = array(); |
||
115 | |||
116 | // Save palette and logo location. |
||
117 | $config |
||
118 | ->set('palette', $palette) |
||
119 | ->set('logo', $paths['target'] . 'logo.svg') |
||
120 | ->save(); |
||
121 | |||
122 | // Copy over neutral images. |
||
123 | foreach ($info['copy'] as $file) { |
||
124 | $base = drupal_basename($file); |
||
125 | $source = $paths['source'] . $file; |
||
126 | $filepath = file_unmanaged_copy($source, $paths['target'] . $base); |
||
127 | $paths['map'][$file] = $base; |
||
128 | $paths['files'][] = $filepath; |
||
129 | } |
||
130 | |||
131 | // Render new images, if image has been provided. |
||
132 | if (isset($info['base_image'])) { |
||
133 | _color_render_images($theme, $info, $paths, $palette); |
||
134 | } |
||
135 | |||
136 | // Rewrite theme stylesheets. |
||
137 | $css = array(); |
||
138 | foreach ($info['css'] as $stylesheet) { |
||
139 | // Build a temporary array with CSS files. |
||
140 | $files = array(); |
||
141 | if (file_exists($paths['source'] . $stylesheet)) { |
||
142 | $files[] = $stylesheet; |
||
143 | } |
||
144 | |||
145 | foreach ($files as $file) { |
||
146 | $css_optimizer = new CssOptimizer(); |
||
147 | // Aggregate @imports recursively for each configured top level CSS file |
||
148 | // without optimization. Aggregation and optimization will be |
||
149 | // handled by drupal_build_css_cache() only. |
||
150 | $style = $css_optimizer->loadFile($paths['source'] . $file, FALSE); |
||
151 | |||
152 | // Return the path to where this CSS file originated from, stripping |
||
153 | // off the name of the file at the end of the path. |
||
154 | $css_optimizer->rewriteFileURIBasePath = base_path() . dirname($paths['source'] . $file) . '/'; |
||
155 | |||
156 | // Prefix all paths within this CSS file, ignoring absolute paths. |
||
157 | $style = preg_replace_callback('/url\([\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\)/i', array($css_optimizer, 'rewriteFileURI'), $style); |
||
158 | |||
159 | // Rewrite stylesheet with new colors. |
||
160 | $style = _df_tools_color_color_rewrite_stylesheet($theme, $info, $paths, $palette, $style); |
||
161 | $base_file = drupal_basename($file); |
||
162 | $css[] = $paths['target'] . $base_file; |
||
163 | _color_save_stylesheet($paths['target'] . $base_file, $style, $paths); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | // Maintain list of files. |
||
168 | $config |
||
169 | ->set('stylesheets', $css) |
||
170 | ->set('files', $paths['files']) |
||
171 | ->save(); |
||
172 | } |
||
301 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.