Completed
Push — feature/player-list ( ee00f1...3478af )
by Vladimir
04:05
created

MultipleAdvancedModelTransformer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 7.87 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 78.13%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 7
loc 89
ccs 25
cts 32
cp 0.7813
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addInclude() 0 12 2
C reverseTransform() 7 53 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 = self::transformJSON($data, $this->included);
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
                foreach ($this->included[$type] as $model) {
44
                    $ids[$model->getID()] = true; // Prevent duplication
45
                    $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