| Conditions | 12 |
| Paths | 85 |
| Total Lines | 75 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 108 | public function findOrCreateMember($usrSettings = array()) { |
||
| 109 | |||
| 110 | $defaults = array( |
||
| 111 | /** |
||
| 112 | * Link this identity to any newly discovered member. |
||
| 113 | */ |
||
| 114 | 'linkOnMatch' => true, |
||
| 115 | /** |
||
| 116 | * True, false, or an array of fields to overwrite if we merge data. |
||
| 117 | * Exception to this rule is overwriteEmail, which takes precedence. |
||
| 118 | */ |
||
| 119 | 'overwriteExistingFields' => false, |
||
| 120 | /** |
||
| 121 | * Overwrite the email field if it's different. Effectively changes |
||
| 122 | * the Member login details, so it's set to false for now. |
||
| 123 | */ |
||
| 124 | 'overwriteEmail' => false, |
||
| 125 | ); |
||
| 126 | |||
| 127 | $settings = array_merge($defaults, $usrSettings); |
||
| 128 | |||
| 129 | if($this->isInDB()) { |
||
| 130 | $member = $this->Member(); |
||
| 131 | if($member->exists()) { |
||
| 132 | return $member; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | $record = $this->getMemberRecordFromAuth(); |
||
| 137 | |||
| 138 | if(empty($record['Email'])) { |
||
| 139 | $member = new Member(); |
||
| 140 | } |
||
| 141 | else { |
||
| 142 | $member = Member::get()->filter('Email', $record['Email'])->first(); |
||
| 143 | |||
| 144 | if(!$member) { |
||
| 145 | $member = new Member(); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | if($settings['linkOnMatch'] && $member->isInDB()) { |
||
| 150 | $this->MemberID = $member->ID; |
||
| 151 | } |
||
| 152 | |||
| 153 | // If this is a new member, give it everything we have. |
||
| 154 | if(!$member->isInDB()) { |
||
| 155 | $member->update($record); |
||
| 156 | } |
||
| 157 | // If not, we update it carefully using the settings described above. |
||
| 158 | else { |
||
| 159 | $overwrite = $settings['overwriteExistingFields']; |
||
| 160 | $overwriteEmail = $settings['overwriteEmail']; |
||
| 161 | $fieldsToWrite = array(); |
||
| 162 | |||
| 163 | // If overwrite is true, take everything (subtract Email later) |
||
| 164 | if($overwrite === true) { |
||
| 165 | $fieldsToWrite = $record; |
||
| 166 | } |
||
| 167 | else if(is_array($overwrite)) { |
||
| 168 | $fieldsToWrite = array_intersect_key($record, ArrayLib::valuekey($overwrite)); |
||
| 169 | } |
||
| 170 | // If false then fieldsToWrite remains empty, let's coast it out. |
||
| 171 | |||
| 172 | // Subtract email if setting is not precisely true: |
||
| 173 | if($overwriteEmail !== true && isset($fieldsToWrite['Email'])) { |
||
| 174 | unset($fieldsToWrite['Email']); |
||
| 175 | } |
||
| 176 | |||
| 177 | // Boom, we're so done. |
||
| 178 | $member->update($fieldsToWrite); |
||
| 179 | } |
||
| 180 | |||
| 181 | return $member; |
||
| 182 | } |
||
| 183 | |||
| 239 |
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.