| Conditions | 12 |
| Paths | 184 |
| Total Lines | 80 |
| Code Lines | 45 |
| 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 |
||
| 97 | public function loadFile($filePath, $mandatoryFieldSettings, $contentOnly = false) |
||
| 98 | { |
||
| 99 | $results = []; |
||
| 100 | $mandatoryErrors = []; |
||
| 101 | $mandatoryFieldErrors = []; |
||
|
|
|||
| 102 | |||
| 103 | $mandatoryFields = array_map( |
||
| 104 | 'trim', |
||
| 105 | explode(',', $mandatoryFieldSettings) |
||
| 106 | ); |
||
| 107 | |||
| 108 | foreach ($mandatoryFields as $key => $value) { |
||
| 109 | $orFields = array_map( |
||
| 110 | 'trim', |
||
| 111 | explode('|', $value) |
||
| 112 | ); |
||
| 113 | $mandatoryFields[$key] = $orFields; |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($contentOnly) { |
||
| 117 | $bibTexEntries = $this->parseFile($filePath, $contentOnly); |
||
| 118 | } else { |
||
| 119 | $bibTexEntries = $this->parseFile($filePath); |
||
| 120 | } |
||
| 121 | |||
| 122 | $encoder = new XmlEncoder(); |
||
| 123 | |||
| 124 | foreach ($bibTexEntries as $index => $bibTexItem) { |
||
| 125 | |||
| 126 | $mandatoryFieldErrors = []; |
||
| 127 | foreach ($mandatoryFields as $combinedMandatoryField) { |
||
| 128 | |||
| 129 | $mandatoryOk = false; |
||
| 130 | foreach ($combinedMandatoryField as $key => $value) { |
||
| 131 | $mandatoryOk = $mandatoryOk || ( |
||
| 132 | array_key_exists($value, $bibTexItem) |
||
| 133 | && $bibTexItem[$value] |
||
| 134 | ); |
||
| 135 | } |
||
| 136 | |||
| 137 | if (!$mandatoryOk) { |
||
| 138 | $mandatoryFieldErrors[implode('|', $combinedMandatoryField)] = implode( |
||
| 139 | '|', |
||
| 140 | $combinedMandatoryField |
||
| 141 | ); |
||
| 142 | $mandatoryErrors[$index] = [ |
||
| 143 | 'index' => $index + 1, |
||
| 144 | 'title' => $bibTexItem['title'], |
||
| 145 | 'fields' => $mandatoryFieldErrors |
||
| 146 | ]; |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | if (!$mandatoryErrors[$index]) { |
||
| 151 | |||
| 152 | $bibTexData = $bibTexItem; |
||
| 153 | |||
| 154 | if (array_key_exists('author', $bibTexItem)) { |
||
| 155 | $bibTexData['author'] = $this->splitPersons($bibTexItem['author']); |
||
| 156 | } |
||
| 157 | |||
| 158 | if (array_key_exists('editor', $bibTexItem)) { |
||
| 159 | $bibTexData['editor'] = $this->splitPersons($bibTexItem['editor']); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** @var BibTexMetadata $bibTexMetadata */ |
||
| 163 | $bibTexMetadata = $this->objectManager->get(BibTexMetadata::class); |
||
| 164 | |||
| 165 | $bibTexMetadata->setSource(get_class($this)); |
||
| 166 | $bibTexMetadata->setFeUser($this->security->getUser()->getUid()); |
||
| 167 | $bibTexMetadata->setData($encoder->encode($bibTexData, 'xml')); |
||
| 168 | |||
| 169 | $results[] = $bibTexMetadata; |
||
| 170 | } |
||
| 171 | |||
| 172 | } |
||
| 173 | |||
| 174 | $this->mandatoryErrors = $mandatoryErrors; |
||
| 175 | |||
| 176 | return $results; |
||
| 177 | } |
||
| 245 | } |