| Conditions | 29 |
| Paths | 1184 |
| Total Lines | 100 |
| Code Lines | 63 |
| 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 |
||
| 46 | public function getFieldList(): FieldList |
||
| 47 | { |
||
| 48 | $fields = new FieldList(); |
||
| 49 | |||
| 50 | // tabbed or untabbed |
||
| 51 | if ($this->tabbed) { |
||
| 52 | $fields->push(new TabSet("Root", $mainTab = new Tab("Main"))); |
||
| 53 | $mainTab->setTitle(_t(__CLASS__ . '.TABMAIN', 'Main')); |
||
| 54 | } |
||
| 55 | |||
| 56 | // Add logical fields directly specified in db config |
||
| 57 | foreach ($this->obj->config()->get('db') as $fieldName => $fieldType) { |
||
| 58 | // Skip restricted fields |
||
| 59 | if ($this->restrictFields && !in_array($fieldName, $this->restrictFields)) { |
||
|
|
|||
| 60 | continue; |
||
| 61 | } |
||
| 62 | |||
| 63 | // @todo Pass localized title |
||
| 64 | if ($this->fieldClasses && isset($this->fieldClasses[$fieldName])) { |
||
| 65 | $fieldClass = $this->fieldClasses[$fieldName]; |
||
| 66 | $fieldObject = new $fieldClass($fieldName); |
||
| 67 | } else { |
||
| 68 | $fieldObject = $this |
||
| 69 | ->obj |
||
| 70 | ->dbObject($fieldName) |
||
| 71 | ->scaffoldFormField(null, $this->getParamsArray()); |
||
| 72 | } |
||
| 73 | // Allow fields to opt-out of scaffolding |
||
| 74 | if (!$fieldObject) { |
||
| 75 | continue; |
||
| 76 | } |
||
| 77 | $fieldObject->setTitle($this->obj->fieldLabel($fieldName)); |
||
| 78 | if ($this->tabbed) { |
||
| 79 | $fields->addFieldToTab("Root.Main", $fieldObject); |
||
| 80 | } else { |
||
| 81 | $fields->push($fieldObject); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | // add has_one relation fields |
||
| 86 | if ($this->obj->hasOne()) { |
||
| 87 | foreach ($this->obj->hasOne() as $relationship => $component) { |
||
| 88 | if ($this->restrictFields && !in_array($relationship, $this->restrictFields)) { |
||
| 89 | continue; |
||
| 90 | } |
||
| 91 | $fieldName = $component === DataObject::class |
||
| 92 | ? $relationship // Polymorphic has_one field is composite, so don't refer to ID subfield |
||
| 93 | : "{$relationship}ID"; |
||
| 94 | if ($this->fieldClasses && isset($this->fieldClasses[$fieldName])) { |
||
| 95 | $fieldClass = $this->fieldClasses[$fieldName]; |
||
| 96 | $hasOneField = new $fieldClass($fieldName); |
||
| 97 | } else { |
||
| 98 | $hasOneField = $this->obj->dbObject($fieldName)->scaffoldFormField(null, $this->getParamsArray()); |
||
| 99 | } |
||
| 100 | if (empty($hasOneField)) { |
||
| 101 | continue; // Allow fields to opt out of scaffolding |
||
| 102 | } |
||
| 103 | $hasOneField->setTitle($this->obj->fieldLabel($relationship)); |
||
| 104 | if ($this->tabbed) { |
||
| 105 | $fields->addFieldToTab("Root.Main", $hasOneField); |
||
| 106 | } else { |
||
| 107 | $fields->push($hasOneField); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | // only add relational fields if an ID is present |
||
| 113 | if ($this->obj->ID) { |
||
| 114 | // add has_many relation fields |
||
| 115 | $includeHasMany = $this->obj->hasMany() && ($this->includeRelations === true || isset($this->includeRelations['has_many'])); |
||
| 116 | |||
| 117 | if ($includeHasMany) { |
||
| 118 | foreach ($this->obj->hasMany() as $relationship => $component) { |
||
| 119 | static::addDefaultRelationshipFields( |
||
| 120 | $fields, |
||
| 121 | $relationship, |
||
| 122 | (isset($this->fieldClasses[$relationship])) |
||
| 123 | ? $this->fieldClasses[$relationship] : null, |
||
| 124 | $this->tabbed, |
||
| 125 | $this->obj |
||
| 126 | ); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | $includeManyMany = $this->obj->manyMany() && ($this->includeRelations === true || isset($this->includeRelations['many_many'])); |
||
| 131 | if ($includeManyMany) { |
||
| 132 | foreach ($this->obj->manyMany() as $relationship => $component) { |
||
| 133 | static::addDefaultRelationshipFields( |
||
| 134 | $fields, |
||
| 135 | $relationship, |
||
| 136 | (isset($this->fieldClasses[$relationship])) |
||
| 137 | ? $this->fieldClasses[$relationship] : null, |
||
| 138 | $this->tabbed, |
||
| 139 | $this->obj |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | return $fields; |
||
| 146 | } |
||
| 188 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.