Conditions | 5 |
Paths | 6 |
Total Lines | 57 |
Code Lines | 27 |
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 |
||
125 | public function buildConfigurationForm(array $form, FormStateInterface $form_state) { |
||
126 | $form = parent::buildConfigurationForm($form, $form_state); |
||
127 | |||
128 | // File field support descriptions, but images do not. |
||
129 | unset($form['description']); |
||
130 | |||
131 | // Ensure that the 'Link image to: Content' setting is not available. |
||
132 | if ($this->getDerivativeId() == 'image') { |
||
133 | unset($form['image_link']['#options']['content']); |
||
134 | } |
||
135 | |||
136 | $entity_element = $form_state->get('entity_element'); |
||
137 | // The alt attribute is *required*, but we allow users to opt-in to empty |
||
138 | // alt attributes for the very rare edge cases where that is valid by |
||
139 | // specifying two double quotes as the alternative text in the dialog. |
||
140 | // However, that *is* stored as an empty alt attribute, so if we're editing |
||
141 | // an existing image (which means the src attribute is set) and its alt |
||
142 | // attribute is empty, then we show that as two double quotes in the dialog. |
||
143 | // @see https://www.drupal.org/node/2307647 |
||
144 | // Alt attribute behavior is taken from the Core image dialog to ensure a |
||
145 | // consistent UX across various forms. |
||
146 | // @see Drupal\editor\Form\EditorImageDialog::buildForm() |
||
147 | $alt = $this->getAttributeValue('alt', ''); |
||
148 | if ($alt === '') { |
||
149 | // Do not change empty alt text to two double quotes if the previously |
||
150 | // used Entity Embed Display plugin was not 'image:image'. That means that |
||
151 | // some other plugin was used so if this image formatter is selected at a |
||
152 | // later stage, then this should be treated as a new edit. We show two |
||
153 | // double quotes in place of empty alt text only if that was filled |
||
154 | // intentionally by the user. |
||
155 | if (!empty($entity_element) && $entity_element['data-entity-embed-display'] == 'image:image') { |
||
156 | $alt = '""'; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | // Add support for editing the alternate and title text attributes. |
||
161 | $form['alt'] = array( |
||
162 | '#type' => 'textfield', |
||
163 | '#title' => $this->t('Alternate text'), |
||
164 | '#default_value' => $alt, |
||
165 | '#description' => $this->t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'), |
||
166 | '#parents' => array('attributes', 'alt'), |
||
167 | '#required' => TRUE, |
||
168 | '#required_error' => $this->t('Alternative text is required.<br />(Only in rare cases should this be left empty. To create empty alternative text, enter <code>""</code> — two double quotes without any content).'), |
||
169 | '#maxlength' => 512, |
||
170 | ); |
||
171 | $form['title'] = array( |
||
172 | '#type' => 'textfield', |
||
173 | '#title' => $this->t('Title'), |
||
174 | '#default_value' => $this->getAttributeValue('title', ''), |
||
175 | '#description' => t('The title is used as a tool tip when the user hovers the mouse over the image.'), |
||
176 | '#parents' => array('attributes', 'title'), |
||
177 | '#maxlength' => 1024, |
||
178 | ); |
||
179 | |||
180 | return $form; |
||
181 | } |
||
182 | |||
195 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.