Completed
Push — master ( ac3b8b...3c857d )
by Gaetano
18:21
created

EzSelection::hashToFieldValue()   D

Complexity

Conditions 10
Paths 19

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
c 0
b 0
f 0
rs 4.8196
cc 10
eloc 18
nc 19
nop 2

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\FieldHandler;
4
5
use eZ\Publish\Core\FieldType\Selection\Value as SelectionValue;
6
use Kaliop\eZMigrationBundle\API\FieldValueImporterInterface;
7
use \eZ\Publish\API\Repository\Repository;
8
9
class EzSelection extends AbstractFieldHandler implements FieldValueImporterInterface
10
{
11
    protected $repository;
12
13
    public function __construct(Repository $repository)
14
    {
15
        $this->repository = $repository;
16
    }
17
18
    /**
19
     * Creates a value object to use as the field value when setting a selection field type.
20
     *
21
     * @param $fieldValue int|string|string[]|int[] should all work
22
     * @param array $context The context for execution of the current migrations. Contains f.e. the path to the migration
23
     * @return SelectionValue
24
     */
25
    public function hashToFieldValue($fieldValue, array $context = array())
26
    {
27
        if ($fieldValue === null) {
28
            return new SelectionValue();
29
        }
30
31
        // allow user to pass in a single value
32
        if (is_string($fieldValue) || is_int($fieldValue)) {
33
            $fieldValue = array($fieldValue);
34
        }
35
36
        // allow user to pass in selection values by name
37
        $fieldSettings = null;
38
        foreach($fieldValue as $key => $val) {
0 ignored issues
show
Bug introduced by
The expression $fieldValue of type object|double|null|array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
39
            if (is_string($val)) {
40
                if (ctype_digit($val)) {
41
                    $fieldValue[$key] = (int)$val;
42
                } else {
43
                    if ($fieldSettings === null) {
44
                        $fieldSettings = $this->loadFieldSettings($context['contentTypeIdentifier'], $context['fieldIdentifier']);
45
                    }
46
                    foreach($fieldSettings['options'] as $pos => $name) {
47
                        if ($name === $val) {
48
                            $fieldValue[$key] = $pos;
49
                            break;
50
                        }
51
                    }
52
                }
53
            }
54
        }
55
56
        return new SelectionValue($fieldValue);
57
    }
58
59
    /**
60
     * @param string $contentTypeIdentifier
61
     * @param string $fieldIdentifier
62
     * @return mixed
63
     */
64
    protected function loadFieldSettings($contentTypeIdentifier, $fieldIdentifier)
65
    {
66
        $contentTypeService = $this->repository->getContentTypeService();
67
        $contentType = $contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier);
68
        $fieldDefinition = $contentType->fieldDefinitionsByIdentifier[$fieldIdentifier];
0 ignored issues
show
Bug introduced by
The property fieldDefinitionsByIdentifier does not seem to exist. Did you mean fieldDefinitions?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
69
        return $fieldDefinition->fieldSettings;
70
    }
71
}
72