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