Completed
Push — fm-matches ( f867e2...7fc64f )
by Vladimir
14:12
created

reverseTransform()   C

Complexity

Conditions 11
Paths 13

Size

Total Lines 53
Code Lines 25

Duplication

Lines 7
Ratio 13.21 %

Code Coverage

Tests 23
CRAP Score 11.0619

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 7
loc 53
ccs 23
cts 25
cp 0.92
rs 6.2926
cc 11
eloc 25
nc 13
nop 1
crap 11.0619

How to fix   Long Method    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 BZIon\Form\Transformer;
4
5
class MultipleAdvancedModelTransformer extends AdvancedModelTransformer
6
{
7
    /**
8
     * A list of Models to include to the results, even if they are not
9
     * specified by the user
10
     *
11
     * @var array
12
     */
13
    private $included = array();
14
15
    /**
16
     * Transforms data to an object
17
     *
18
     * @param  string $data
19
     * @return Model[]
20
     */
21 1
    public function reverseTransform($data)
22
    {
23
        // Handle the data provided by Javascript, if any
24 1
        $transformed = parent::transformJSON($data, $this->included);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (transformJSON() instead of reverseTransform()). Are you sure this is correct? If so, you might want to change this to $this->transformJSON().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
25
26 1
        if ($transformed !== false) {
27
            return $transformed;
28
        }
29
30 1
        $models = array();
31
32 1
        foreach ($this->types as $type) {
33 1
            if (trim($data[$type]) === '') {
34 1
                continue;
35
            }
36
37
            // Array to store IDs for quick access so we can be sure that no
38
            // duplicates are saved
39 1
            $ids = array();
40
41
            // Add force-included models
42 1
            if (isset($this->included[$type])) {
43 1
                foreach ($this->included[$type] as $model) {
44 1
                    $ids[$model->getID()] = true; // Prevent duplication
45 1
                    $models[] = $model;
46
                }
47
            }
48
49 1
            $input = explode(',', $data[$type]);
50 1
            $input = array_unique(array_map('trim', $input));
51
52 1
            foreach ($input as $name) {
53 1
                if ($name === '') {
54 1
                    continue;
55
                }
56
57 1
                $model = $this->getModelFromName($name, $type);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as $this->getModelFromName($name, $type) (which targets BZIon\Form\Transformer\A...mer::getModelFromName()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
58
59 1
                if (!$model) {
60
                    // No model was found matching that name
61
                    $models[] = $this->invalidModel($type);
62 1 View Code Duplication
                } elseif (!$model->isValid() || !isset($ids[$model->getID()])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
63
                    // We can proceed, since we're not storing a duplicate
64
                    // (We don't check invalid models, since they might be
65
                    // corresponding to different names)
66 1
                    $models[] = $model;
67 1
                    $ids[$model->getID()] = true;
68
                }
69
            }
70
        }
71
72 1
        return $models;
73
    }
74
75
    /**
76
     * Add a model to the list of models that should be included in all cases
77
     *
78
     * @param  \Model $model The model to include
79
     * @throws \Exception When a model is of an unsupported type
80
     */
81 1
    public function addInclude(\Model $model)
82
    {
83 1
        $type = strtolower($model->getType());
84
85 1
        if (!in_array($type, $this->types)) {
86
            throw new \Exception(
87
                "Objects of type \"{$model->getTypeForHumans()}\" are not supported"
88
            );
89
        }
90
91 1
        $this->included[$type][] = $model;
92 1
    }
93
}
94