LdapObjectChoiceLoader   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 27.16 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 22
loc 81
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A loadChoiceList() 0 16 3
A loadValuesForChoices() 11 11 3
A loadChoicesForValues() 11 11 3

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
 * 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\ChoiceLoader;
12
13
use LdapTools\LdapManager;
14
use LdapTools\Object\LdapObject;
15
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
16
use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
17
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
18
use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
19
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
20
21
/**
22
 * Provides a choice list loader for LDAP objects.
23
 *
24
 * @author Chad Sikorra <[email protected]>
25
 */
26
class LdapObjectChoiceLoader implements ChoiceLoaderInterface
27
{
28
    use LdapObjectChoiceTrait;
29
30
    /**
31
     * @var ChoiceListFactoryInterface
32
     */
33
    private $factory;
34
35
    /**
36
     * @var ChoiceListInterface|null
37
     */
38
    protected $choiceList;
39
40
    /**
41
     * @param LdapManager $ldap
42
     * @param string $type The LDAP object type.
43
     * @param string $labelAttribute The LDAP attribute to use for the label.
44
     * @param string $id The attribute to use for the ID.
45
     * @param LdapQueryBuilder|\Closure|null
46
     */
47
    public function __construct(LdapManager $ldap, $type, $labelAttribute = 'name', $id = 'guid', $query = null)
48
    {
49
        $this->factory = new PropertyAccessDecorator(new DefaultChoiceListFactory());
50
        $this->ldap = $ldap;
51
        $this->type = $type;
52
        $this->id = $id;
53
        $this->labelAttribute = $labelAttribute;
54
        $this->setClosureOrQuery($query);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function loadChoiceList($value = null)
61
    {
62
        if ($this->choiceList) {
63
            return $this->choiceList;
64
        }
65
        $ldapObjects = $this->getLdapObjectsByQuery();
66
67
        $choices = [];
68
        /** @var LdapObject $object */
69
        foreach ($ldapObjects as $object) {
70
            $choices[$object->get($this->labelAttribute)] = $object;
71
        }
72
        $this->choiceList = $this->factory->createListFromChoices($choices, $this->id);
0 ignored issues
show
Documentation introduced by
$choices is of type array, but the function expects a object<Symfony\Component...eList\Factory\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
74
        return $this->choiceList;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 View Code Duplication
    public function loadValuesForChoices(array $choices, $value = null)
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...
81
    {
82
        if (empty(array_filter($choices))) {
83
            return [];
84
        }
85
        if (!$this->choiceList) {
86
            return $this->getLdapValuesForChoices(...$choices);
87
        }
88
89
        return $this->loadChoiceList($value)->getValuesForChoices($choices);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 View Code Duplication
    public function loadChoicesForValues(array $values, $value = null)
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...
96
    {
97
        if (empty(array_filter($values))) {
98
            return [];
99
        }
100
        if (!$this->choiceList) {
101
            $this->loadChoiceList($value);
102
        }
103
104
        return $this->choiceList->getChoicesForValues($values);
105
    }
106
}
107