| Conditions | 7 |
| Paths | 10 |
| Total Lines | 33 |
| Code Lines | 20 |
| Lines | 33 |
| Ratio | 100 % |
| Tests | 9 |
| CRAP Score | 7 |
| Changes | 0 | ||
| 1 | <?php |
||
| 17 | 1 | public function createValue($fieldValue, array $context = array()) |
|
| 18 | { |
||
| 19 | 1 | $altText = ''; |
|
| 20 | 1 | $fileName = ''; |
|
| 21 | if (is_string($fieldValue)) { |
||
| 22 | 1 | $filePath = $fieldValue; |
|
| 23 | } else { |
||
| 24 | 1 | $filePath = $fieldValue['path']; |
|
| 25 | 1 | if (isset($fieldValue['alt_text'])) { |
|
| 26 | 1 | $altText = $fieldValue['alt_text']; |
|
| 27 | } |
||
| 28 | 1 | if (isset($fieldValue['filename'])) { |
|
| 29 | 1 | $fileName = $fieldValue['filename']; |
|
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | // default format: path is relative to the 'images' dir |
||
| 34 | $realFilePath = dirname($context['path']) . '/images/' . $filePath['path']; |
||
| 35 | |||
| 36 | // but in the past, when using a string, this worked as well as an absolute path, so we have to support it as well |
||
| 37 | if (!is_file($realFilePath) && is_file($filePath)) { |
||
| 38 | $realFilePath = $filePath; |
||
| 39 | } |
||
| 40 | |||
| 41 | return new ImageValue( |
||
| 42 | array( |
||
| 43 | 'path' => $realFilePath, |
||
| 44 | 'fileSize' => filesize($realFilePath), |
||
| 45 | 'fileName' => $fileName != '' ? $fileName : basename($realFilePath), |
||
| 46 | 'alternativeText' => $altText |
||
| 47 | ) |
||
| 48 | ); |
||
| 49 | } |
||
| 50 | } |
||
| 51 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.