| Conditions | 9 |
| Paths | 13 |
| Total Lines | 69 |
| Code Lines | 52 |
| 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 |
||
| 52 | public function process(): void |
||
| 53 | { |
||
| 54 | $files = Finder::create() |
||
| 55 | ->files() |
||
| 56 | ->in($this->builder->getConfig()->getDataPath()) |
||
| 57 | ->name('/\.('.implode('|', (array) $this->builder->getConfig()->get('data.ext')).')$/') |
||
| 58 | ->sortByName(true); |
||
| 59 | $max = count($files); |
||
| 60 | |||
| 61 | if ($max <= 0) { |
||
| 62 | $message = 'No files'; |
||
| 63 | $this->builder->getLogger()->info($message); |
||
| 64 | |||
| 65 | return; |
||
| 66 | } |
||
| 67 | |||
| 68 | $serializerYaml = new Serializer([new ObjectNormalizer()], [new YamlEncoder()]); |
||
| 69 | $serializerJson = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); |
||
| 70 | $serializerCsv = new Serializer([new ObjectNormalizer()], [new CsvEncoder()]); |
||
| 71 | $serializerXml = new Serializer([new ObjectNormalizer()], [new XmlEncoder()]); |
||
| 72 | $count = 0; |
||
| 73 | |||
| 74 | /** @var \Symfony\Component\Finder\SplFileInfo $file */ |
||
| 75 | foreach ($files as $file) { |
||
| 76 | $count++; |
||
| 77 | set_error_handler( |
||
| 78 | function ($severity, $message, $file, $line) { |
||
| 79 | throw new \ErrorException($message, 0, $severity, $file, $line, null); |
||
| 80 | } |
||
| 81 | ); |
||
| 82 | $data = $file->getContents(); |
||
| 83 | restore_error_handler(); |
||
| 84 | |||
| 85 | switch ($file->getExtension()) { |
||
| 86 | case 'yml': |
||
| 87 | case 'yaml': |
||
| 88 | $dataArray = $serializerYaml->decode($data, 'yaml'); |
||
| 89 | break; |
||
| 90 | case 'json': |
||
| 91 | $dataArray = $serializerJson->decode($data, 'json'); |
||
| 92 | break; |
||
| 93 | case 'csv': |
||
| 94 | $dataArray = $serializerCsv->decode($data, 'csv'); |
||
| 95 | break; |
||
| 96 | case 'xml': |
||
| 97 | $dataArray = $serializerXml->decode($data, 'xml'); |
||
| 98 | break; |
||
| 99 | default: |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | |||
| 103 | $basename = $file->getBasename('.'.$file->getExtension()); |
||
| 104 | $subpath = \Cecil\Util\File::getFS()->makePathRelative( |
||
| 105 | $file->getPath(), |
||
| 106 | $this->builder->getConfig()->getDataPath() |
||
| 107 | ); |
||
| 108 | $subpath = trim($subpath, './'); |
||
| 109 | $array = []; |
||
| 110 | $path = !empty($subpath) ? Util::joinFile($subpath, $basename) : $basename; |
||
| 111 | $this->pathToArray($array, $path, $dataArray); |
||
| 112 | |||
| 113 | $dataArray = array_merge_recursive( |
||
| 114 | $this->builder->getData(), |
||
| 115 | $array |
||
| 116 | ); |
||
| 117 | $this->builder->setData($dataArray); |
||
| 118 | |||
| 119 | $message = \sprintf('File "%s.%s" loaded', Util::joinFile($path), $file->getExtension()); |
||
| 120 | $this->builder->getLogger()->info($message, ['progress' => [$count, $count]]); |
||
| 121 | } |
||
| 143 |