| Conditions | 12 |
| Paths | 26 |
| Total Lines | 79 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 59 | public function process(): void |
||
| 60 | { |
||
| 61 | $files = Finder::create() |
||
| 62 | ->files() |
||
| 63 | ->in($this->config->getDataPath()) |
||
| 64 | ->name('/\.(' . implode('|', (array) $this->config->get('data.ext')) . ')$/') |
||
| 65 | ->sortByName(true); |
||
| 66 | |||
| 67 | if ($this->config->hasTheme()) { |
||
| 68 | $themes = $this->config->getTheme(); |
||
| 69 | foreach ($themes ?? [] as $theme) { |
||
| 70 | if (Util\File::getFS()->exists($this->config->getThemeDirPath($theme, 'data'))) { |
||
| 71 | $files->in($this->config->getThemeDirPath($theme, 'data')); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | $total = \count($files); |
||
| 77 | |||
| 78 | if ($total < 1) { |
||
| 79 | $message = 'No files'; |
||
| 80 | $this->builder->getLogger()->info($message); |
||
| 81 | |||
| 82 | return; |
||
| 83 | } |
||
| 84 | |||
| 85 | $serializerYaml = new Serializer([new ObjectNormalizer()], [new YamlEncoder()]); |
||
| 86 | $serializerJson = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); |
||
| 87 | $serializerCsv = new Serializer([new ObjectNormalizer()], [new CsvEncoder()]); |
||
| 88 | $serializerXml = new Serializer([new ObjectNormalizer()], [new XmlEncoder()]); |
||
| 89 | $count = 0; |
||
| 90 | |||
| 91 | /** @var \Symfony\Component\Finder\SplFileInfo $file */ |
||
| 92 | foreach ($files as $file) { |
||
| 93 | $count++; |
||
| 94 | set_error_handler( |
||
| 95 | function ($severity, $message, $file, $line) { |
||
| 96 | throw new \ErrorException($message, 0, $severity, $file, $line, null); |
||
| 97 | } |
||
| 98 | ); |
||
| 99 | $data = $file->getContents(); |
||
| 100 | restore_error_handler(); |
||
| 101 | |||
| 102 | switch ($file->getExtension()) { |
||
| 103 | case 'yml': |
||
| 104 | case 'yaml': |
||
| 105 | $dataAsArray = $serializerYaml->decode($data, 'yaml'); |
||
| 106 | break; |
||
| 107 | case 'json': |
||
| 108 | $dataAsArray = $serializerJson->decode($data, 'json'); |
||
| 109 | break; |
||
| 110 | case 'csv': |
||
| 111 | $dataAsArray = $serializerCsv->decode($data, 'csv'); |
||
| 112 | break; |
||
| 113 | case 'xml': |
||
| 114 | $dataAsArray = $serializerXml->decode($data, 'xml'); |
||
| 115 | break; |
||
| 116 | default: |
||
| 117 | return; |
||
| 118 | } |
||
| 119 | |||
| 120 | $lang = $this->config->getLanguageDefault(); |
||
| 121 | if (PrefixSuffix::hasSuffix($file->getFilenameWithoutExtension())) { |
||
| 122 | $lang = PrefixSuffix::getSuffix($file->getFilenameWithoutExtension()); |
||
| 123 | } |
||
| 124 | $array = []; |
||
| 125 | $path = Util::joinFile((string) pathinfo($file->getRelativePathname(), \PATHINFO_DIRNAME), (string) pathinfo($file->getRelativePathname(), \PATHINFO_FILENAME)); |
||
| 126 | $path = trim($path, './'); |
||
| 127 | $localizedPath = Util::joinFile((string) $lang, PrefixSuffix::sub($path)); |
||
| 128 | $this->pathToArray($array, $localizedPath, $dataAsArray); |
||
| 129 | |||
| 130 | $dataAsArray = array_merge_recursive( |
||
| 131 | $this->builder->getData(), |
||
| 132 | $array |
||
| 133 | ); |
||
| 134 | $this->builder->setData($dataAsArray); |
||
| 135 | |||
| 136 | $message = \sprintf('File "%s" loaded', $file->getRelativePathname()); |
||
| 137 | $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); |
||
| 138 | } |
||
| 158 |