| Conditions | 39 |
| Paths | 5664 |
| Total Lines | 206 |
| Code Lines | 131 |
| Lines | 61 |
| Ratio | 29.61 % |
| 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 |
||
| 72 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 73 | { |
||
| 74 | $location = $input->getArgument('location'); |
||
| 75 | |||
| 76 | $this->container = $container = $this->getContainer(); |
||
|
|
|||
| 77 | $this->modelTranslatable = $container->get('graviton.i18n.model.translatable'); |
||
| 78 | /** @var DocumentManager $documentManager */ |
||
| 79 | $documentManager = $container->get('doctrine_mongodb.odm.default_document_manager'); |
||
| 80 | |||
| 81 | $output->writeln('Importing from: '.$location); |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param SplFileInfo $file |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | $filter = function (SplFileInfo $file) { |
||
| 88 | if ($file->getExtension() !== 'yml' || $file->isDir()) { |
||
| 89 | return false; |
||
| 90 | } |
||
| 91 | return true; |
||
| 92 | }; |
||
| 93 | |||
| 94 | $finder = new Finder(); |
||
| 95 | $finder->files()->in($location)->ignoreDotFiles(true)->filter($filter); |
||
| 96 | |||
| 97 | $totalFiles = $finder->count(); |
||
| 98 | $progress = new ProgressBar($output, $totalFiles); |
||
| 99 | $errors = []; |
||
| 100 | $success = []; |
||
| 101 | |||
| 102 | // CoreTypes and Refferences |
||
| 103 | $referenceObjects = []; |
||
| 104 | |||
| 105 | // Do this by rest |
||
| 106 | $restObjects = []; |
||
| 107 | |||
| 108 | /** @var SplFileInfo $file */ |
||
| 109 | foreach ($finder as $file) { |
||
| 110 | $fileName = str_replace($location, '', $file->getPathname()); |
||
| 111 | |||
| 112 | $progress->advance(); |
||
| 113 | |||
| 114 | $contents = explode('---', $file->getContents()); |
||
| 115 | if (!$contents || count($contents)!==3) { |
||
| 116 | $errors[$fileName] = 'Content of file is incorrect, could not parse it with --- '; |
||
| 117 | continue; |
||
| 118 | } |
||
| 119 | |||
| 120 | $target = trim(str_replace('target:', '', $contents[1])); |
||
| 121 | $yaml = $contents[2]; |
||
| 122 | |||
| 123 | // Make Service Name: |
||
| 124 | $targets = explode('/', $target); |
||
| 125 | if (!$targets || count($targets)<3) { |
||
| 126 | $errors[$fileName] = 'Target is not correctly defined: '.json_encode($targets); |
||
| 127 | continue; |
||
| 128 | } |
||
| 129 | $domain = $targets[1]; |
||
| 130 | |||
| 131 | try { |
||
| 132 | $data = Yaml::parse($yaml); |
||
| 133 | } catch (ParseException $e) { |
||
| 134 | $errors[$fileName] = 'Could not parse yml file'; |
||
| 135 | continue; |
||
| 136 | } |
||
| 137 | |||
| 138 | if (!$data) { |
||
| 139 | $errors[$fileName] = 'Yml file is empty or parsed as empty.'; |
||
| 140 | continue; |
||
| 141 | } |
||
| 142 | |||
| 143 | // Locate class name graviton.i18n.document.language |
||
| 144 | $service = $this->findServiceObject($targets, $fileName); |
||
| 145 | $objectClass = $service['class']; |
||
| 146 | $serviceName = $service['service']; |
||
| 147 | |||
| 148 | if (!$objectClass) { |
||
| 149 | $restObjects[] = ['target'=> $target, 'data' =>$data]; |
||
| 150 | continue; |
||
| 151 | } |
||
| 152 | |||
| 153 | if (array_key_exists('items', $data) && is_array($data['items'])) { |
||
| 154 | View Code Duplication | foreach ($data['items'] as $item) { |
|
| 155 | $object = clone $objectClass; |
||
| 156 | $translated = []; |
||
| 157 | if ($object instanceof TranslatableDocumentInterface) { |
||
| 158 | $translated = $object->getTranslatableFields(); |
||
| 159 | } |
||
| 160 | if (is_object($object)) { |
||
| 161 | foreach ($item as $key => $value) { |
||
| 162 | $method = 'set' . ucfirst($key); |
||
| 163 | if (method_exists($object, $method)) { |
||
| 164 | if (is_array($value) && in_array($key, $translated)) { |
||
| 165 | $this->generateLanguages($domain, $value); |
||
| 166 | } elseif (is_array($value)) { |
||
| 167 | $referenceObjects[$key] = [ |
||
| 168 | 'data' => $data, |
||
| 169 | 'file' => $fileName, |
||
| 170 | 'service' => $serviceName |
||
| 171 | ]; |
||
| 172 | } else { |
||
| 173 | try { |
||
| 174 | $object->$method($value); |
||
| 175 | } catch (\Exception $e) { |
||
| 176 | $errors[$fileName] = 'Method: '.$method. |
||
| 177 | ' with:'.$value.' error:'.$e->getMessage(); |
||
| 178 | continue; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | $documentManager->persist($object); |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } else { |
||
| 187 | $object = clone $objectClass; |
||
| 188 | $method = false; |
||
| 189 | $id = false; |
||
| 190 | View Code Duplication | foreach ($data as $key => $value) { |
|
| 191 | $translated = []; |
||
| 192 | if ($object instanceof TranslatableDocumentInterface) { |
||
| 193 | $translated = $object->getTranslatableFields(); |
||
| 194 | } |
||
| 195 | if ($key == 'id') { |
||
| 196 | $id = $value; |
||
| 197 | } |
||
| 198 | $method = 'set' . ucfirst($key); |
||
| 199 | if (method_exists($object, $method)) { |
||
| 200 | if (is_array($value) && in_array($key, $translated)) { |
||
| 201 | $this->generateLanguages($domain, $value, $id); |
||
| 202 | $object->$method(reset($value)); |
||
| 203 | } elseif (is_array($value)) { |
||
| 204 | $referenceObjects[$key] = [ |
||
| 205 | 'data' => $data, |
||
| 206 | 'file' => $fileName, |
||
| 207 | 'service' => $serviceName |
||
| 208 | ]; |
||
| 209 | } else { |
||
| 210 | try { |
||
| 211 | $object->$method($value); |
||
| 212 | } catch (\Exception $e) { |
||
| 213 | $errors[$fileName] = 'Method: '.$method.' with:'.$value.' error:'.$e->getMessage(); |
||
| 214 | continue; |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | } |
||
| 219 | if ($method) { |
||
| 220 | $documentManager->persist($object); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | try { |
||
| 225 | $documentManager->flush(); |
||
| 226 | $documentManager->clear(); |
||
| 227 | } catch (\Exception $e) { |
||
| 228 | $errors[$fileName] = 'Save error:'.$e->getMessage(); |
||
| 229 | continue; |
||
| 230 | } |
||
| 231 | $success[] = $fileName; |
||
| 232 | } |
||
| 233 | |||
| 234 | $progress->finish(); |
||
| 235 | |||
| 236 | // Resume: |
||
| 237 | $inserted = $totalFiles - count($restObjects) - count($errors); |
||
| 238 | $output->writeln("\n".'<info>Inserted Objects:'.$inserted.'</info>'); |
||
| 239 | |||
| 240 | // REST $restObjects |
||
| 241 | if ($restObjects) { |
||
| 242 | $output->writeln("\n".'Rest Objects:'.count($restObjects)); |
||
| 243 | $errorsRest = $this->putRestObject($restObjects, $output); |
||
| 244 | $errors = array_merge($errors, $errorsRest); |
||
| 245 | } |
||
| 246 | |||
| 247 | // Referenced object update |
||
| 248 | if ($referenceObjects) { |
||
| 249 | $output->writeln("\n".'References Updating: '.count($referenceObjects)); |
||
| 250 | $errorsRefs = $this->updateReferences($referenceObjects, $output); |
||
| 251 | $errors = array_merge($errors, $errorsRefs); |
||
| 252 | } |
||
| 253 | |||
| 254 | |||
| 255 | // Output's |
||
| 256 | $output->writeln("\n".'<error>============ Errors: '.count($errors).' ============</error>'); |
||
| 257 | if (!$errors) { |
||
| 258 | $output->writeln('<comment>Done without errors</comment>'); |
||
| 259 | } else { |
||
| 260 | foreach ($errors as $fileName => $error) { |
||
| 261 | $errString = is_array($error) ? json_encode($error) : $error; |
||
| 262 | $output->writeln("<comment>$fileName : $errString</comment>"); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | $documentManager->flush(); |
||
| 267 | $output->writeln("\n".'<info>============ Success: '.count($success).' ============</info>'); |
||
| 268 | if (!$success) { |
||
| 269 | $output->writeln('No files imported'); |
||
| 270 | } |
||
| 271 | |||
| 272 | $output->writeln('<info>============ End ============</info>'."\n"); |
||
| 273 | if (count($errors)) { |
||
| 274 | $output->writeln('<error>============ Errors: '.count($errors).', please review ============</error>'."\n"); |
||
| 275 | } |
||
| 276 | |||
| 277 | } |
||
| 278 | |||
| 433 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.