Conditions | 8 |
Paths | 96 |
Total Lines | 69 |
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 |
||
30 | public function getCMSFields() |
||
31 | { |
||
32 | $fields = parent::getCMSFields(); |
||
33 | |||
34 | $hiddenFields = $this->config()->get('hideCMSInputs'); |
||
35 | |||
36 | // short title? |
||
37 | if (!in_array('AlternativeTitle', $hiddenFields)) { |
||
38 | $fields->addFieldToTab( |
||
39 | 'Root.Main', |
||
40 | TextField::create('AlternativeTitle', 'Alternative Title'), |
||
41 | 'URLSegment' |
||
42 | ); |
||
43 | } |
||
44 | |||
45 | // do we want an intro? |
||
46 | if (!in_array('Intro', $hiddenFields)) { |
||
47 | $fields->addFieldToTab( |
||
48 | 'Root.Main', |
||
49 | TextareaField::create('Intro', 'Intro'), |
||
50 | 'Content' |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | // add an image |
||
55 | if (!in_array('Image', $hiddenFields)) { |
||
56 | $fields->addFieldToTab( |
||
57 | 'Root.Main', |
||
58 | UploadField::create('ImageID', 'Image') |
||
59 | ->setAllowedFileCategories('image') |
||
60 | ->setAllowedMaxFileNumber(1), |
||
61 | 'Content' |
||
62 | ); |
||
63 | } |
||
64 | |||
65 | // do we use a section color? |
||
66 | if (!in_array('Color', $hiddenFields)) { |
||
67 | // default is this brand color. |
||
68 | $field = NoticeMessage::create('The brand colors have not been defined yet.'); |
||
69 | |||
70 | // usually we replace this with the color palette. |
||
71 | if (BrandColors::get()->count() > 0) { |
||
72 | $field = ColorPaletteField::create( |
||
73 | 'Color', |
||
74 | 'Color', |
||
75 | BrandColors::get()->map('BrandColor', 'ColorName') |
||
76 | )->setRightTitle('Please select a brand color.'); |
||
77 | } |
||
78 | |||
79 | $fields->addFieldToTab('Root.Main', $field, 'Content'); |
||
80 | } |
||
81 | |||
82 | // It will repalce Content area field to notice message field, or nothing at the end. |
||
83 | if ($this->config()->get('useBlocksModule')) { |
||
84 | // move the block related fields over |
||
85 | $fields->addFieldsToTab('Root.Main', $fields->findOrMakeTab('Root.Blocks')->Fields(), 'Content'); |
||
86 | |||
87 | // now we really don't need the content field anymore. |
||
88 | $fields->removeByName('Content'); |
||
89 | |||
90 | // remove the blocks tab |
||
91 | $fields->removeByName('Root.Blocks'); |
||
92 | } |
||
93 | if ($this->config()->get('hideContentField')) { |
||
94 | $fields->removebyName('Content'); |
||
95 | } |
||
96 | |||
97 | return $fields; |
||
98 | } |
||
99 | |||
174 |
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.