Conditions | 11 |
Paths | 2 |
Total Lines | 45 |
Code Lines | 32 |
Lines | 45 |
Ratio | 100 % |
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 |
||
111 | View Code Duplication | public function validate() |
|
112 | { |
||
113 | $result = parent::validate(); |
||
114 | $fieldLabels = $this->FieldLabels(); |
||
115 | $indexes = $this->Config()->get('indexes'); |
||
116 | $requiredFields = $this->Config()->get('required_fields'); |
||
117 | if(is_array($requiredFields)) { |
||
118 | foreach($requiredFields as $field) { |
||
119 | $value = $this->$field; |
||
120 | if(! $value) { |
||
121 | $fieldWithoutID = $field; |
||
122 | if(substr($fieldWithoutID, -2) === 'ID') { |
||
123 | $fieldWithoutID = substr($fieldWithoutID, 0, -2); |
||
124 | } |
||
125 | $myName = isset($fieldLabels[$fieldWithoutID]) ? $fieldLabels[$fieldWithoutID] : $fieldWithoutID; |
||
126 | $result->error( |
||
127 | _t( |
||
128 | 'ImagesWithStyleSelection.'.$field.'_REQUIRED', |
||
129 | $myName.' is required' |
||
130 | ), |
||
131 | 'REQUIRED_ImagesWithStyleSelection_'.$field |
||
132 | ); |
||
133 | } |
||
134 | if (isset($indexes[$field]) && isset($indexes[$field]['type']) && $indexes[$field]['type'] === 'unique') { |
||
135 | $id = (empty($this->ID) ? 0 : $this->ID); |
||
136 | $count = ImagesWithStyleSelection::get() |
||
137 | ->filter(array($field => $value)) |
||
138 | ->exclude(array('ID' => $id)) |
||
139 | ->count(); |
||
140 | if($count > 0) { |
||
141 | $myName = $fieldLabels['$field']; |
||
142 | $result->error( |
||
143 | _t( |
||
144 | 'ImagesWithStyleSelection.'.$field.'_UNIQUE', |
||
145 | $myName.' needs to be unique' |
||
146 | ), |
||
147 | 'UNIQUE_ImagesWithStyleSelection_'.$field |
||
148 | ); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | } |
||
153 | |||
154 | return $result; |
||
155 | } |
||
156 | |||
243 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.