Conditions | 18 |
Paths | 19 |
Total Lines | 83 |
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 |
||
11 | public function init() |
||
12 | { |
||
13 | $req = $this->owner->getRequest(); |
||
14 | |||
15 | // Ignore being called on LeftAndMain base class, |
||
16 | // which is the case when requests are first routed through AdminRootController |
||
17 | // as an intermediary rather than the endpoint controller |
||
18 | if (!$this->owner->stat('tree_class')) { |
||
19 | return; |
||
20 | } |
||
21 | |||
22 | // Locale" attribute is either explicitly added by LeftAndMain Javascript logic, |
||
23 | // or implied on a translated record (see {@link Translatable->updateCMSFields()}). |
||
24 | // $Lang serves as a "context" which can be inspected by Translatable - hence it |
||
25 | // has the same name as the database property on Translatable. |
||
26 | $id = $req->param('ID'); |
||
27 | if ($req->requestVar("Locale")) { |
||
28 | $this->owner->Locale = $req->requestVar("Locale"); |
||
29 | } elseif ($id && is_numeric($id)) { |
||
30 | $record = DataObject::get_by_id($this->owner->stat('tree_class'), $id); |
||
31 | if ($record && $record->Locale) { |
||
32 | $this->owner->Locale = $record->Locale; |
||
33 | } |
||
34 | } else { |
||
35 | $this->owner->Locale = Translatable::default_locale(); |
||
36 | if ($this->owner->class == 'CMSPagesController') { |
||
37 | // the CMSPagesController always needs to have the locale set, |
||
38 | // otherwise page editing will cause an extra |
||
39 | // ajax request which looks weird due to multiple "loading"-flashes |
||
40 | $getVars = $req->getVars(); |
||
41 | if (isset($getVars['url'])) { |
||
42 | unset($getVars['url']); |
||
43 | } |
||
44 | return $this->owner->redirect(Controller::join_links( |
||
45 | $this->owner->Link(), |
||
46 | $req->param('Action'), |
||
47 | $req->param('ID'), |
||
48 | $req->param('OtherID'), |
||
49 | ($query = http_build_query($getVars)) ? "?$query" : null |
||
50 | )); |
||
51 | } |
||
52 | } |
||
53 | Translatable::set_current_locale($this->owner->Locale); |
||
54 | |||
55 | // If a locale is set, it needs to match to the current record |
||
56 | $requestLocale = $req->requestVar("Locale"); |
||
57 | $page = $this->owner->currentPage(); |
||
58 | if ( |
||
59 | $req->httpMethod() == 'GET' // leave form submissions alone |
||
60 | && $requestLocale |
||
61 | && $page |
||
62 | && $page->hasExtension('Translatable') |
||
63 | && $page->Locale != $requestLocale |
||
64 | && $req->latestParam('Action') != 'EditorToolbar' |
||
65 | ) { |
||
66 | $transPage = $page->getTranslation($requestLocale); |
||
67 | if ($transPage) { |
||
68 | Translatable::set_current_locale($transPage->Locale); |
||
69 | return $this->owner->redirect(Controller::join_links( |
||
70 | $this->owner->Link('show'), |
||
71 | $transPage->ID |
||
72 | // ?locale will automatically be added |
||
73 | )); |
||
74 | } elseif ($this->owner->class != 'CMSPagesController') { |
||
75 | // If the record is not translated, redirect to pages overview |
||
76 | return $this->owner->redirect(Controller::join_links( |
||
77 | singleton('CMSPagesController')->Link(), |
||
78 | '?Locale=' . $requestLocale |
||
79 | )); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | // collect languages for TinyMCE spellchecker plugin. |
||
84 | // see http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker |
||
85 | $langName = i18n::get_locale_name($this->owner->Locale); |
||
86 | HtmlEditorConfig::get('cms')->setOption( |
||
87 | 'spellchecker_languages', |
||
88 | "+{$langName}={$this->owner->Locale}" |
||
89 | ); |
||
90 | |||
91 | Requirements::javascript('translatable/javascript/CMSMain.Translatable.js'); |
||
92 | Requirements::css('translatable/css/CMSMain.Translatable.css'); |
||
93 | } |
||
94 | |||
246 |
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.