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
03:02
created

JsonPathFinder::removeStep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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