LdapObjectChoiceList::getIndicesForChoices()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 10
nc 5
nop 1
1
<?php
2
/**
3
 * This file is part of the LdapToolsBundle package.
4
 *
5
 * (c) Chad Sikorra <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LdapTools\Bundle\LdapToolsBundle\Form\ChoiceList;
12
13
use LdapTools\Object\LdapObject;
14
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
15
16
/**
17
 * Overrides certain ChoiceList methods so the old extended ObjectChoiceList works properly
18
 *
19
 * @author Chad Sikorra <[email protected]>
20
 */
21
class LdapObjectChoiceList extends ObjectChoiceList
22
{
23
    /**
24
     * @var LdapObject[]
25
     */
26
    protected $ldapObjects;
27
28
    /**
29
     * @var string
30
     */
31
    protected $id;
32
33
    /**
34
     * LdapObjectChoiceList constructor.
35
     * @param array|\Traversable $choices
36
     * @param null $labelPath
37
     * @param array $preferredChoices
38
     * @param null $groupPath
39
     * @param null $valuePath
40
     * @param PropertyAccessorInterface|null $propertyAccessor
41
     */
42
    public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null)
43
    {
44
        $this->id = $valuePath;
45
        $this->ldapObjects = $choices;
0 ignored issues
show
Documentation Bug introduced by
It seems like $choices can also be of type object<Traversable>. However, the property $ldapObjects is declared as type array<integer,object<Lda...ols\Object\LdapObject>>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
46
        parent::__construct($choices, $labelPath, $preferredChoices, $groupPath, $valuePath);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 View Code Duplication
    public function getChoicesForValues(array $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
53
    {
54
        if (empty(array_filter($values))) {
55
            return [];
56
        }
57
58
        $choices = [];
59
        foreach ($values as $i => $value) {
60
            foreach ($this->ldapObjects as $ldapObject) {
61
                if ($ldapObject->has($this->id, $value)) {
62
                    $choices[$i] = $ldapObject;
63
                    break;
64
                }
65
            }
66
        }
67
68
        return $choices;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getValuesForChoices(array $choices)
75
    {
76
        if (empty(array_filter($choices))) {
77
            return [];
78
        }
79
80
        $values = [];
81
        foreach ($choices as $i => $choice) {
82
            $values[$i] = $choice->get($this->id);
83
        }
84
85
        return $values;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 View Code Duplication
    public function getIndicesForChoices(array $choices)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
92
    {
93
        if (empty(array_filter($choices))) {
94
            return [];
95
        }
96
97
        $indices = [];
98
        foreach ($choices as $k => $choice) {
99
            foreach ($this->ldapObjects as $i => $ldapObject) {
100
                if ($ldapObject->has($this->id, $choice->get($this->id))) {
101
                    $indices[$k] = $i;
102
                    break;
103
                }
104
            }
105
        }
106
107
        return $indices;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 View Code Duplication
    public function getIndicesForValues(array $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
114
    {
115
        if (empty(array_filter($values))) {
116
            return [];
117
        }
118
119
        $indices = [];
120
        foreach ($values as $k => $value) {
121
            foreach ($this->ldapObjects as $i => $ldapObject) {
122
                if ($ldapObject->has($this->id, $value)) {
123
                    $indices[$k] = $i;
124
                    break;
125
                }
126
            }
127
        }
128
129
        return $indices;
130
    }
131
}
132