| Conditions | 10 |
| Paths | 48 |
| Total Lines | 67 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 110 |
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 |
||
| 16 | function php($data) { |
||
| 17 | parent::php($data); |
||
| 18 | $valid = true; |
||
| 19 | //Debug::message("Validating data: " . print_r($data, true)); |
||
| 20 | $valid = parent::php($data); |
||
| 21 | //Debug::message("Returning false, just to check"); |
||
| 22 | //return false; |
||
| 23 | // |
||
| 24 | |||
| 25 | if ($data['ClassesToSearch'] == array()) { |
||
| 26 | $data['ClassesToSearch'] = ''; |
||
| 27 | } |
||
| 28 | $debug = $data['SiteTreeOnly']; |
||
| 29 | // Check if any classes to search if site tree only is not ticked |
||
| 30 | if (!$data['SiteTreeOnly']) { |
||
| 31 | if (!$data['ClassesToSearch']) { |
||
| 32 | $valid = false; |
||
| 33 | $this->validationError("ClassesToSearch", |
||
| 34 | "Please provide at least one class to search, or select 'Site Tree Only'", |
||
| 35 | 'error' |
||
| 36 | ); |
||
| 37 | } else { |
||
| 38 | $toSearch = $data['ClassesToSearch']; |
||
| 39 | foreach ($toSearch as $clazz) { |
||
| 40 | try { |
||
| 41 | $instance = Injector::inst()->create($clazz); |
||
| 42 | if (!$instance->hasExtension('SilverStripe\Elastica\Searchable')) { |
||
| 43 | $this->validationError('ClassesToSearch', 'The class '.$clazz.' must have the Searchable extension'); |
||
| 44 | } |
||
| 45 | } catch (ReflectionException $e) { |
||
| 46 | $this->validationError("ClassesToSearch", |
||
| 47 | 'The class '.$clazz.' does not exist', |
||
| 48 | 'error' |
||
| 49 | ); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | |||
| 56 | // Check the identifier is unique |
||
| 57 | $mode = Versioned::get_reading_mode(); |
||
| 58 | $suffix = ''; |
||
| 59 | if ($mode == 'Stage.Live') { |
||
| 60 | $suffix = '_Live'; |
||
| 61 | } |
||
| 62 | $where = 'ElasticSearchPage'.$suffix.'.ID != '.$data['ID']." AND `Identifier` = '".$data['Identifier']."'"; |
||
| 63 | $existing = ElasticSearchPage::get()->where($where)->count(); |
||
| 64 | if ($existing > 0) { |
||
| 65 | $valid = false; |
||
| 66 | $this->validationError('Identifier', |
||
| 67 | 'The identifier '.$data['Identifier'].' already exists', |
||
| 68 | 'error' |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | |||
| 72 | |||
| 73 | // Check number of results per page >= 1 |
||
| 74 | if ($data['ResultsPerPage'] <= 0) { |
||
| 75 | $valid = false; |
||
| 76 | $this->validationError('ResultsPerPage', |
||
| 77 | 'Results per page must be >=1' |
||
| 78 | ,'error' |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | return $valid; |
||
| 82 | } |
||
| 83 | } |
||
| 84 |
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider.