| Conditions | 10 |
| Paths | 6 |
| Total Lines | 43 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 | protected function attachSeo($event) |
||
| 39 | { |
||
| 40 | /** @var Seo $seo */ |
||
| 41 | $seo = LarrockAdminSeo::getModel()->whereSeoIdConnect($event->model->id)->whereSeoTypeConnect($event->component->name)->first(); |
||
| 42 | |||
| 43 | if($seo){ |
||
| 44 | if( !empty($event->request->get('seo_title')) || |
||
| 45 | !empty($event->request->get('seo_description')) || |
||
| 46 | !empty($event->request->get('seo_seo_keywords'))){ |
||
| 47 | $seo->seo_id_connect = $event->model->id; |
||
| 48 | $seo->seo_url_connect = $event->request->get('url_connect'); |
||
| 49 | $seo->seo_title = $event->request->get('seo_title'); |
||
| 50 | $seo->seo_description = $event->request->get('seo_description'); |
||
| 51 | $seo->seo_keywords = $event->request->get('seo_keywords'); |
||
| 52 | $seo->seo_type_connect = $event->component->name; |
||
| 53 | if($seo->save()){ |
||
| 54 | MessageLarrock::success('SEO обновлено'); |
||
| 55 | return $seo; |
||
| 56 | } |
||
| 57 | }else{ |
||
| 58 | $seo->delete(); |
||
| 59 | MessageLarrock::success('SEO удалено'); |
||
| 60 | return $seo; |
||
| 61 | } |
||
| 62 | }else{ |
||
| 63 | if( !empty($event->request->get('seo_title')) || |
||
| 64 | !empty($event->request->get('seo_description')) || |
||
| 65 | !empty($event->request->get('seo_seo_keywords'))){ |
||
| 66 | $seo = LarrockAdminSeo::getModel(); |
||
| 67 | $seo->seo_id_connect = $event->request->get('id_connect'); |
||
| 68 | $seo->seo_url_connect = $event->request->get('url_connect'); |
||
| 69 | $seo->seo_title = $event->request->get('seo_title'); |
||
| 70 | $seo->seo_description = $event->request->get('seo_description'); |
||
| 71 | $seo->seo_keywords = $event->request->get('seo_keywords'); |
||
| 72 | $seo->seo_type_connect = $event->request->get('type_connect'); |
||
| 73 | if($seo->save()){ |
||
| 74 | MessageLarrock::success('SEO добавлено'); |
||
| 75 | return $seo; |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | return null; |
||
| 80 | } |
||
| 81 | |||
| 207 | } |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.