| Conditions | 31 |
| Paths | 160 |
| Total Lines | 115 |
| Code Lines | 73 |
| 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 |
||
| 73 | public function getFieldList() |
||
| 74 | { |
||
| 75 | $fields = new FieldList(); |
||
| 76 | |||
| 77 | // tabbed or untabbed |
||
| 78 | if ($this->tabbed) { |
||
| 79 | $fields->push(new TabSet("Root", $mainTab = new Tab("Main"))); |
||
| 80 | $mainTab->setTitle(_t(__CLASS__ . '.TABMAIN', 'Main')); |
||
| 81 | } |
||
| 82 | |||
| 83 | // Add logical fields directly specified in db config |
||
| 84 | foreach ($this->obj->config()->get('db') as $fieldName => $fieldType) { |
||
| 85 | // Skip restricted fields |
||
| 86 | if ($this->restrictFields && !in_array($fieldName, $this->restrictFields)) { |
||
|
|
|||
| 87 | continue; |
||
| 88 | } |
||
| 89 | |||
| 90 | // @todo Pass localized title |
||
| 91 | if ($this->fieldClasses && isset($this->fieldClasses[$fieldName])) { |
||
| 92 | $fieldClass = $this->fieldClasses[$fieldName]; |
||
| 93 | $fieldObject = new $fieldClass($fieldName); |
||
| 94 | } else { |
||
| 95 | $fieldObject = $this |
||
| 96 | ->obj |
||
| 97 | ->dbObject($fieldName) |
||
| 98 | ->scaffoldFormField(null, $this->getParamsArray()); |
||
| 99 | } |
||
| 100 | // Allow fields to opt-out of scaffolding |
||
| 101 | if (!$fieldObject) { |
||
| 102 | continue; |
||
| 103 | } |
||
| 104 | $fieldObject->setTitle($this->obj->fieldLabel($fieldName)); |
||
| 105 | if ($this->tabbed) { |
||
| 106 | $fields->addFieldToTab("Root.Main", $fieldObject); |
||
| 107 | } else { |
||
| 108 | $fields->push($fieldObject); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | // add has_one relation fields |
||
| 113 | if ($this->obj->hasOne()) { |
||
| 114 | foreach ($this->obj->hasOne() as $relationship => $component) { |
||
| 115 | if ($this->restrictFields && !in_array($relationship, $this->restrictFields)) { |
||
| 116 | continue; |
||
| 117 | } |
||
| 118 | $fieldName = $component === 'SilverStripe\\ORM\\DataObject' |
||
| 119 | ? $relationship // Polymorphic has_one field is composite, so don't refer to ID subfield |
||
| 120 | : "{$relationship}ID"; |
||
| 121 | if ($this->fieldClasses && isset($this->fieldClasses[$fieldName])) { |
||
| 122 | $fieldClass = $this->fieldClasses[$fieldName]; |
||
| 123 | $hasOneField = new $fieldClass($fieldName); |
||
| 124 | } else { |
||
| 125 | $hasOneField = $this->obj->dbObject($fieldName)->scaffoldFormField(null, $this->getParamsArray()); |
||
| 126 | } |
||
| 127 | if (empty($hasOneField)) { |
||
| 128 | continue; // Allow fields to opt out of scaffolding |
||
| 129 | } |
||
| 130 | $hasOneField->setTitle($this->obj->fieldLabel($relationship)); |
||
| 131 | if ($this->tabbed) { |
||
| 132 | $fields->addFieldToTab("Root.Main", $hasOneField); |
||
| 133 | } else { |
||
| 134 | $fields->push($hasOneField); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | // only add relational fields if an ID is present |
||
| 140 | if ($this->obj->ID) { |
||
| 141 | // add has_many relation fields |
||
| 142 | if ($this->obj->hasMany() |
||
| 143 | && ($this->includeRelations === true || isset($this->includeRelations['has_many'])) |
||
| 144 | ) { |
||
| 145 | foreach ($this->obj->hasMany() as $relationship => $component) { |
||
| 146 | if ($this->tabbed) { |
||
| 147 | $fields->findOrMakeTab( |
||
| 148 | "Root.$relationship", |
||
| 149 | $this->obj->fieldLabel($relationship) |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | $fieldClass = (isset($this->fieldClasses[$relationship])) |
||
| 153 | ? $this->fieldClasses[$relationship] |
||
| 154 | : 'SilverStripe\\Forms\\GridField\\GridField'; |
||
| 155 | /** @var GridField $grid */ |
||
| 156 | $grid = Injector::inst()->create( |
||
| 157 | $fieldClass, |
||
| 158 | $relationship, |
||
| 159 | $this->obj->fieldLabel($relationship), |
||
| 160 | $this->obj->$relationship(), |
||
| 161 | GridFieldConfig_RelationEditor::create() |
||
| 162 | ); |
||
| 163 | if ($this->tabbed) { |
||
| 164 | $fields->addFieldToTab("Root.$relationship", $grid); |
||
| 165 | } else { |
||
| 166 | $fields->push($grid); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | if ($this->obj->manyMany() |
||
| 172 | && ($this->includeRelations === true || isset($this->includeRelations['many_many'])) |
||
| 173 | ) { |
||
| 174 | foreach ($this->obj->manyMany() as $relationship => $component) { |
||
| 175 | static::addManyManyRelationshipFields( |
||
| 176 | $fields, |
||
| 177 | $relationship, |
||
| 178 | (isset($this->fieldClasses[$relationship])) |
||
| 179 | ? $this->fieldClasses[$relationship] : null, |
||
| 180 | $this->tabbed, |
||
| 181 | $this->obj |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | return $fields; |
||
| 188 | } |
||
| 248 |
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.