Conditions | 8 |
Paths | 12 |
Total Lines | 58 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
48 | public function CommentsForm() |
||
49 | { |
||
50 | $form = Injector::inst()->create(CommentForm::class, __FUNCTION__, $this); |
||
51 | $fields = $form->Fields(); |
||
52 | |||
53 | $enable_url = $this->getOption('enable_url'); |
||
54 | $min = $this->getOption('min_rating'); |
||
55 | $max = $this->getOption('max_rating'); |
||
56 | $id = $this->getRequest()->postVar('ParentID'); |
||
57 | $class = $this->getRequest()->postVar('ParentClassName'); |
||
58 | |||
59 | // If we dont have exact values, look to see if we are using a post |
||
60 | if ((empty($min) || empty($max)) && $id && $class) { |
||
61 | if ($object = $class::get()->byID($id)) { |
||
62 | $min = $object->getCommentsOption('min_rating'); |
||
63 | $max = $object->getCommentsOption('max_rating'); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | // Add reviews field |
||
68 | $required_text = _t( |
||
69 | self::class . '.Rating_Required', |
||
70 | 'Please enter a Rating' |
||
71 | ); |
||
72 | |||
73 | // Setup possible ratings |
||
74 | $ratings = []; |
||
75 | |||
76 | for ($i = $min; $i <= $max; $i++) { |
||
77 | $ratings[$i] = $i; |
||
78 | } |
||
79 | |||
80 | // Add rating field to comment form |
||
81 | $fields->insertBefore( |
||
82 | 'Comment', |
||
83 | OptionsetField::create( |
||
84 | 'Rating', |
||
85 | _t( |
||
86 | self::class . '.Rating', |
||
87 | 'Rating' |
||
88 | ), |
||
89 | $ratings |
||
90 | )->setCustomValidationMessage($required_text) |
||
91 | ->setAttribute('data-msg-required', $required_text) |
||
92 | ); |
||
93 | |||
94 | // Website URL is possibly overkill for a review, disable unless we overwrite this |
||
95 | if ($enable_url !== true) { |
||
96 | $fields->removeByName("URL"); |
||
97 | } |
||
98 | |||
99 | $fields->setForm($form); |
||
100 | $form->setFields($fields); |
||
101 | |||
102 | // hook to allow further extensions to alter the comments form |
||
103 | $this->extend('alterCommentForm', $form); |
||
104 | |||
105 | return $form; |
||
106 | } |
||
107 | } |