| Conditions | 10 |
| Paths | 27 |
| Total Lines | 47 |
| Code Lines | 29 |
| 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 |
||
| 28 | public function humanizedChanges($from, $to) { |
||
| 29 | if(!$from) { |
||
| 30 | return _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UPLOADEDFILE', "Uploaded file"); |
||
| 31 | } |
||
| 32 | |||
| 33 | $fromRecord = Versioned::get_version($this->owner->class, $this->owner->ID, $from); |
||
| 34 | $toRecord = Versioned::get_version($this->owner->class, $this->owner->ID, $to); |
||
| 35 | |||
| 36 | $diff = new DataDifferencer($fromRecord, $toRecord); |
||
| 37 | $changes = $diff->changedFieldNames(); |
||
| 38 | |||
| 39 | $k = array_search('LastEdited', $changes); |
||
| 40 | |||
| 41 | if($k !== false) { |
||
| 42 | unset($changes[$k]); |
||
| 43 | } |
||
| 44 | |||
| 45 | $output = array(); |
||
| 46 | |||
| 47 | foreach($changes as $change) { |
||
| 48 | $human = $change; |
||
| 49 | |||
| 50 | if($change == "ParentID") { |
||
| 51 | // updated folder ID |
||
| 52 | $human = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdminFile.MOVEDFOLDER', "Moved file"); |
||
| 53 | } else if($change == 'Title') { |
||
| 54 | $human = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdminFile.RENAMEDTITLE', "Updated title to ") . $fromRecord->Title; |
||
| 55 | } else if($change == 'Name') { |
||
| 56 | $human = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdminFile.RENAMEDFILE', "Renamed file to ") . $fromRecord->Filename; |
||
| 57 | } else if($change == 'File') { |
||
| 58 | // check to make sure the files are actually different |
||
| 59 | if($fromRecord->getHash() != $toRecord->getHash()) { |
||
| 60 | $human = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdminFile.RENAMEDFILE', "Replaced file"); |
||
| 61 | } else { |
||
| 62 | $human = false; |
||
| 63 | } |
||
| 64 | } else { |
||
| 65 | $human = false; |
||
| 66 | } |
||
| 67 | |||
| 68 | if($human) { |
||
| 69 | $output[] = $human; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | return implode(", ", $output); |
||
| 74 | } |
||
| 75 | } |
||
| 76 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.