GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#62)
by Simone
02:45
created

JsonPathFinder::keep()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 2
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Mado\QueryBundle\Component\Meta;
4
5
class JsonPathFinder
6
{
7
    const INDEX_ENTITY_PARENT = 0;
8
9
    const INDEX_FK_RELATION_NAME = 1;
10
11
    const INDEX_ENTITY_FIRST_CHILD = 2;
12
13
    private $map;
14
15
    private $entity;
16
17
    private $entitiesPath = [];
18
19
    private $wrongPath = [];
20
21
    public function __construct(
22
        RelationDatamapper $mappaer
23
    ) {
24
        $this->map = $mappaer->getMap();
25
    }
26
27
    public function setEntity(string $entity)
28
    {
29
        $this->entity = $entity;
30
    }
31
32
    public function getFirstParentOf(string $innerEntity)
33
    {
34
        return $this->keep(
35
            self::INDEX_ENTITY_PARENT,
36
            $innerEntity
37
        );
38
    }
39
40
    public function getFirstChildOf(string $innerEntity)
41
    {
42
        return $this->keep(
43
            self::INDEX_ENTITY_FIRST_CHILD,
44
            $innerEntity
45
        );
46
    }
47
48
    public function getSourceRelation(string $innerEntity)
49
    {
50
        return $this->keep(
51
            self::INDEX_FK_RELATION_NAME,
52
            $innerEntity
53
        );
54
    }
55
56
    public function clearMap(string $innerEntity)
57
    {
58
        if (in_array($this->entity, $this->listOfParentsOf($innerEntity))) {
59
            foreach ($this->map as $root => $meta) {
60
                if ($this->entity != $root) {
61
                    unset($this->map[$root]);
62
                }
63
            }
64
        }
65
    }
66
67
    public function getPathTo(string $innerEntity = '')
68
    {
69
        $this->entitiesPath[] = $innerEntity;
70
        $mem = $innerEntity;
0 ignored issues
show
Unused Code introduced by
The assignment to $mem is dead and can be removed.
Loading history...
71
72
        $path = $this->getSourceRelation($innerEntity);
73
74
        if (
75
            $this->numberOfRealtionTo($innerEntity) != 1 &&
76
            $innerEntity != $this->entity
77
        ) {
78
            $this->clearMap($innerEntity);
79
        }
80
81
        if ($this->entity != $this->getFirstParentOf($innerEntity)) {
82
            if (!($relation = $this->getFirstParentOf($innerEntity))) {
83
                throw new \RuntimeException(var_export([
84
                    'innerEntity' => $innerEntity,
85
                    'relation' => $relation,
86
                ], true));
87
            }
88
89
            return $this->getPathTo($relation) . '.' . $path;
90
        }
91
92
        return $path;
93
    }
94
95
    public function setQueryStartEntity(string $startEntity)
96
    {
97
        $this->setEntity($startEntity);
98
    }
99
100
    public function getPathToEntity(string $entityToReach)
101
    {
102
        foreach ($this->map as $rootEntity => $meta) {
103
            if (in_array($rootEntity, $this->wrongPath)) {
104
                unset($this->map[$rootEntity]);
105
            }
106
        }
107
108
        return '_embedded.' . $this->getPathTo($entityToReach);
109
    }
110
111
    public function keep($val, $innerEntity)
112
    {
113
        foreach ($this->map as $rootEntity => $meta) {
114
            foreach ($meta['relations'] as $name => $entity) {
115
                if (self::INDEX_ENTITY_FIRST_CHILD == $val) {
116
                    return $entity;
117
                }
118
119
                if ($entity == $innerEntity) {
120
                    $return = [
121
                        self::INDEX_ENTITY_PARENT      => $rootEntity,
122
                        self::INDEX_FK_RELATION_NAME   => $name,
123
                    ][$val];
124
125
                    return $return;
126
                }
127
            }
128
        }
129
    }
130
131 View Code Duplication
    public function numberOfRealtionTo(string $entityToReach)
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...
132
    {
133
        $numberOfRealtionTo = 0;
134
135
        foreach ($this->map as $rootEntity => $meta) {
136
            foreach ($meta['relations'] as $name => $entity) {
137
                if ($entity == $entityToReach) {
138
                    $numberOfRealtionTo++;
139
                }
140
            }
141
        }
142
143
        return $numberOfRealtionTo;
144
    }
145
146 View Code Duplication
    public function listOfParentsOf(string $entityToReach)
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...
147
    {
148
        $parents = [];
149
150
        foreach ($this->map as $rootEntity => $meta) {
151
            foreach ($meta['relations'] as $name => $entity) {
152
                if ($entity == $entityToReach) {
153
                    $parents[] = $rootEntity;
154
                }
155
            }
156
        }
157
158
        return $parents;
159
    }
160
161
    public function getEntitiesPath()
162
    {
163
        return $this->entitiesPath;
164
    }
165
166
    public function addWrongPathTo($parentToSkip)
167
    {
168
        $this->wrongPath[] = $parentToSkip;
169
    }
170
}
171