Test Failed
Pull Request — master (#4)
by
unknown
11:21
created

RelationInput   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 96
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A targetObjectType() 0 13 3
B resolveTargetObjectType() 0 21 7
B choiceObjMap() 0 27 6
1
<?php
2
3
namespace Charcoal\Admin\Property\Input\Selectize;
4
5
use InvalidArgumentException;
6
7
// From 'charcoal-admin'
8
use Charcoal\Admin\Property\Input\SelectizeInput;
9
10
/**
11
 * Relation Input Selectize
12
 */
13
class RelationInput extends SelectizeInput
14
{
15
    /**
16
     * The target object type to build the choices from.
17
     *
18
     * @var string
19
     */
20
    private $targetObjectType;
21
22
    /**
23
     * Check used to parse multi Choice map against the obj properties.
24
     *
25
     * @var boolean
26
     */
27
    protected $isChoiceObjMapFinalized = false;
28
29
    /**
30
     * Retrieve the target object type to build the choices from.
31
     *
32
     * @throws InvalidArgumentException If the target object type was not previously set.
33
     * @return string
34
     */
35
    public function targetObjectType()
36
    {
37
        if ($this->targetObjectType === null) {
38
            $resolved = false;
0 ignored issues
show
Unused Code introduced by
$resolved is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
39
            if (!$this->resolveTargetObjectType()) {
40
                throw new InvalidArgumentException(
41
                    'Target object type could not be properly determined.'
42
                );
43
            }
44
        }
45
46
        return $this->targetObjectType;
47
    }
48
49
    /**
50
     * Resolve the target object type from multiple sources & contexts
51
     *
52
     * @return boolean
53
     */
54
    private function resolveTargetObjectType()
0 ignored issues
show
Coding Style introduced by
resolveTargetObjectType uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
55
    {
56
        $resolved = false;
57
        $formData = $this->viewController()->formData();
58
        $param = isset($_GET['target_object_type']) ? $_GET['target_object_type'] : false;
59
60
        // Resolving through formData should be the most common occurence for this property input
61
        if (isset($formData['target_object_type']) &&
62
            is_string($formData['target_object_type']) &&
63
            !empty($formData['target_object_type'])
64
        ) {
65
            $this->targetObjectType = $formData['target_object_type'];
66
            $resolved = true;
67
        // Resolve through URL params
68
        } elseif (is_string($param) && !empty($param)) {
69
            $this->targetObjectType = $param;
70
            $resolved = true;
71
        }
72
73
        return $resolved;
74
    }
75
76
    /**
77
     * Retrieve the object-to-choice data map.
78
     *
79
     * @return array Returns a data map to abide.
80
     */
81
    public function choiceObjMap()
82
    {
83
        if ($this->choiceObjMap === null) {
84
            $map = $this->defaultChoiceObjMap();
85
86
            $model = $this->modelFactory()->get($this->targetObjectType());
87
            $objProperties = $model->properties();
88
89
            if ($objProperties instanceof \Iterator) {
90
                $objProperties = iterator_to_array($objProperties);
91
            }
92
93
            foreach ($map as &$mapProp) {
94
                $props = explode(':', $mapProp);
95
                foreach ($props as $p) {
96
                    if (isset($objProperties[$p])) {
97
                        $mapProp = $p;
98
                        break;
99
                    }
100
                }
101
            }
102
103
            $this->choiceObjMap = $map;
104
        }
105
106
        return $this->choiceObjMap;
107
    }
108
}
109