| Conditions | 25 |
| Paths | 624 |
| Total Lines | 107 |
| Code Lines | 59 |
| 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 |
||
| 123 | public function importUsersFromFile(UploadedFile $file, $delimiter = ',') |
||
| 124 | { |
||
| 125 | $defaultGroup = $this->variableApi->get('ZikulaGroupsModule', 'defaultgroup'); |
||
| 126 | // get available groups |
||
| 127 | $allGroups = $this->entityManager->getRepository('ZikulaGroupsModule:GroupEntity')->findAllAndIndexBy('gid'); |
||
| 128 | // create an array with the groups identities where the user can add other users |
||
| 129 | $allGroupsArray = []; |
||
| 130 | foreach ($allGroups as $gid => $group) { |
||
| 131 | if ($this->permissionApi->hasPermission('ZikulaGroupsModule::', $group['gid'] . '::', ACCESS_EDIT)) { |
||
| 132 | $allGroupsArray[] = $gid; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | // read the choosen file |
||
| 137 | ini_set("auto_detect_line_endings", true); // allows for macintosh line endings ("/r") |
||
| 138 | if (!$lines = file($file->getPathname())) { |
||
| 139 | return $this->__("Error! It has not been possible to read the import file."); |
||
| 140 | } |
||
| 141 | $expectedFields = ['uname', 'pass', 'email', 'activated', 'sendmail', 'groups']; |
||
| 142 | $firstLineArray = explode($delimiter, str_replace('"', '', trim($lines[0]))); |
||
| 143 | foreach ($firstLineArray as $field) { |
||
| 144 | if (!in_array(trim(strtolower($field)), $expectedFields)) { |
||
| 145 | return $this->__f("Error! The import file does not have the expected field %s in the first row. Please check your import file.", ['%s' => $field]); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | unset($lines[0]); |
||
| 149 | |||
| 150 | $counter = 1; |
||
| 151 | $importValues = []; |
||
| 152 | |||
| 153 | // read the lines and create an array with the values. Check if the values passed are correct and set the default values if it is necessary |
||
| 154 | foreach ($lines as $line) { |
||
| 155 | $line = str_replace('"', '', trim($line)); |
||
| 156 | $lineArray = explode($delimiter, $line); |
||
| 157 | |||
| 158 | // check if the line has all the needed values |
||
| 159 | if (count($lineArray) != count($firstLineArray)) { |
||
| 160 | return $this->__f('Error! The number of parameters in line %s is not correct. Please check your import file.', ['%s' => $counter]); |
||
| 161 | } |
||
| 162 | $importValues[] = array_combine($firstLineArray, $lineArray); |
||
| 163 | |||
| 164 | // validate user name |
||
| 165 | $uname = trim($importValues[$counter - 1]['uname']); |
||
| 166 | $errors = $this->validator->validate($uname, new ValidUname()); |
||
| 167 | if ($errors->count() > 0) { |
||
| 168 | return $this->locateErrors($errors, 'username', $counter); |
||
| 169 | } |
||
| 170 | |||
| 171 | // validate password |
||
| 172 | $pass = (string)trim($importValues[$counter - 1]['pass']); |
||
| 173 | $errors = $this->validator->validate($pass, [new NotNull(), new ValidPassword()]); |
||
| 174 | if ($errors->count() > 0) { |
||
| 175 | return $this->locateErrors($errors, 'password', $counter); |
||
| 176 | } |
||
| 177 | |||
| 178 | // validate email |
||
| 179 | $email = trim($importValues[$counter - 1]['email']); |
||
| 180 | $errors = $this->validator->validate($email, new ValidEmail()); |
||
| 181 | if ($errors->count() > 0) { |
||
| 182 | return $this->locateErrors($errors, 'email', $counter); |
||
| 183 | } |
||
| 184 | |||
| 185 | // validate activation value |
||
| 186 | $importValues[$counter - 1]['activated'] = isset($importValues[$counter - 1]['activated']) ? (int)$importValues[$counter - 1]['activated'] : UsersConstant::ACTIVATED_ACTIVE; |
||
| 187 | $activated = $importValues[$counter - 1]['activated']; |
||
| 188 | if (($activated != UsersConstant::ACTIVATED_INACTIVE) && ($activated != UsersConstant::ACTIVATED_ACTIVE)) { |
||
| 189 | return $this->locateErrors($this->__('Error! The CSV is not valid: the "activated" column must contain 0 or 1 only.'), 'activated', $counter); |
||
| 190 | } |
||
| 191 | |||
| 192 | // validate sendmail |
||
| 193 | $importValues[$counter - 1]['sendmail'] = isset($importValues[$counter - 1]['sendmail']) ? (int)$importValues[$counter - 1]['sendmail'] : 0; |
||
| 194 | if ($importValues[$counter - 1]['sendmail'] < 0 || $importValues[$counter - 1]['sendmail'] > 1) { |
||
| 195 | return $this->locateErrors($this->__('Error! The CSV is not valid: the "sendmail" column must contain 0 or 1 only.'), 'sendmail', $counter); |
||
| 196 | } |
||
| 197 | |||
| 198 | // check groups and set defaultGroup as default if there are not groups defined |
||
| 199 | $importValues[$counter - 1]['groups'] = !empty($importValues[$counter - 1]['groups']) ? $importValues[$counter - 1]['groups'] : $defaultGroup; |
||
| 200 | $groupsArray = explode('|', $importValues[$counter - 1]['groups']); |
||
| 201 | foreach ($groupsArray as $group) { |
||
| 202 | if (!in_array($group, $allGroupsArray)) { |
||
| 203 | return $this->locateErrors($this->__f('Sorry! The identity of the group %gid% is not not valid. Perhaps it does not exist. Please check your import file.', ['%gid%' => $group]), 'groups', $counter); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | $counter++; |
||
| 208 | } |
||
| 209 | |||
| 210 | if (empty($importValues)) { |
||
| 211 | return $this->__("Error! The import file does not have values."); |
||
| 212 | } |
||
| 213 | |||
| 214 | // The values in import file are ready. Proceed creating users |
||
| 215 | if (!$this->createUsers($importValues)) { |
||
| 216 | return $this->__("Error! The creation of users has failed."); |
||
| 217 | } |
||
| 218 | // send email if indicated |
||
| 219 | foreach ($importValues as $importValue) { |
||
| 220 | if ($importValue['activated'] && $importValue['sendmail']) { |
||
| 221 | $templateArgs = [ |
||
| 222 | 'user' => $importValue |
||
| 223 | ]; |
||
| 224 | $this->mailHelper->sendNotification($importValue['email'], 'importnotify', $templateArgs); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | return ''; |
||
| 229 | } |
||
| 230 | |||
| 297 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.