RolesDataSource   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 17.11 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 1
dl 13
loc 76
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getRoles() 0 6 1
A getRolesFormParams() 13 13 1
B getAllAvailableRoles() 0 17 5

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 Victoire\Bundle\CriteriaBundle\DataSource;
4
5
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
6
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
7
8
class RolesDataSource
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
9
{
10
    /**
11
     * @var TokenStorage
12
     */
13
    private $tokenStorage;
14
15
    /*
16
     * @var array
17
     */
18
    private $roleHierarchy;
19
20
    /**
21
     * RolesDataSource constructor.
22
     *
23
     * @param TokenStorage $tokenStorage
24
     * @param array        $roleHierarchy
25
     */
26
    public function __construct(TokenStorage $tokenStorage, $roleHierarchy)
27
    {
28
        $this->tokenStorage = $tokenStorage;
29
        $this->roleHierarchy = $roleHierarchy;
30
    }
31
32
    /**
33
     * @return mixed
34
     */
35
    public function getRoles()
36
    {
37
        $user = $this->tokenStorage->getToken()->getUser();
38
39
        return $user;
40
    }
41
42
    /**
43
     * @return array
44
     */
45 View Code Duplication
    public function getRolesFormParams()
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...
46
    {
47
        return [
48
            'type'    => ChoiceType::class,
49
            'options' => [
50
                'choices'           => $this->getAllAvailableRoles($this->roleHierarchy),
51
                'choices_as_values' => true,
52
                'choice_label'      => function ($value) {
53
                    return $value;
54
                },
55
            ],
56
        ];
57
    }
58
59
    /**
60
     * flatten the array of all roles defined in role_hierarchy.
61
     *
62
     * @param array $roleHierarchy
63
     *
64
     * @return array
65
     */
66
    public function getAllAvailableRoles($roleHierarchy)
67
    {
68
        $roles = [];
69
        foreach ($roleHierarchy as $key => $value) {
70
            if (is_array($value)) {
71
                $roles = array_merge($roles, $this->getAllAvailableRoles($value));
72
            }
73
            if (is_string($key)) {
74
                $roles[] = $key;
75
            }
76
            if (is_string($value)) {
77
                $roles[] = $value;
78
            }
79
        }
80
81
        return $roles;
82
    }
83
}
84