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
Push — 2.2 ( 7ff136...c87423 )
by Simone
04:24
created

JsonPathFinder::getHashKeyForDestination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Mado\QueryBundle\Component\Meta;
4
5
use Psr\Log\LoggerInterface;
6
7
/**
8
 * @since Class available since Release 2.1.0
9
 */
10
class JsonPathFinder
11
{
12
    const INDEX_ENTITY_PARENT = 0;
13
14
    const INDEX_FK_RELATION_NAME = 1;
15
16
    const INDEX_ENTITY_FIRST_CHILD = 2;
17
18
    private $map;
19
20
    private $entity;
21
22
    private $entitiesPath = [];
23
24
    private $wrongPath = [];
25
26
    private $mapper;
27
28
    private $appendRootEntityToSubject;
29
30
    private $incrementSubject;
31
32
    private $allPaths = [];
33
34
    private static $indexesToDescriptionMap = [
35
        self::INDEX_ENTITY_PARENT      => 'parent',
36
        self::INDEX_FK_RELATION_NAME   => 'relation',
37
        self::INDEX_ENTITY_FIRST_CHILD => 'first child',
38
    ];
39
40
    private $logger;
41
42 17
    public function __construct(
43
        DataMapper $mapper,
44
        LoggerInterface $logger = null
45
    ) {
46 17
        $this->mapper = $mapper;
47 17
        $this->logger = $logger;
48
49 3
        $this->appendRootEntityToSubject = function($subject, $rootEntity) {
50 3
            $subject[] = $rootEntity;
51 3
            return $subject;
52
        };
53
54 10
        $this->incrementSubject = function($subject) {
55 10
            return ++$subject;
56
        };
57 17
    }
58
59 10
    public function setEntity(string $entity)
60
    {
61 10
        $this->entity = $entity;
62 10
    }
63
64 10
    public function getFirstParentOf(string $innerEntity)
65
    {
66 10
        $this->getMap();
67
68 10
        return $this->keep(
69 10
            self::INDEX_ENTITY_PARENT,
70
            $innerEntity
71
        );
72
    }
73
74 1
    public function getFirstChildOf(string $innerEntity)
75
    {
76 1
        return $this->keep(
77 1
            self::INDEX_ENTITY_FIRST_CHILD,
78
            $innerEntity
79
        );
80
    }
81
82 10
    public function getSourceRelation(string $innerEntity)
83
    {
84 10
        return $this->keep(
85 10
            self::INDEX_FK_RELATION_NAME,
86
            $innerEntity
87
        );
88
    }
89
90 2
    public function clearMap(string $innerEntity)
91
    {
92 2
        if (in_array($this->entity, $this->listOfParentsOf($innerEntity))) {
93 2
            foreach ($this->map as $rootEntity => $meta) {
94 2
                if ($this->entity != $rootEntity) {
95 2
                    unset($this->map[$rootEntity]);
96
                }
97
            }
98
        }
99 2
    }
100
101 9
    public function getPathTo(string $innerEntity = '', $nest = 0)
102
    {
103 9
        $this->entitiesPath[] = $innerEntity;
104
105 9
        $path = $this->getSourceRelation($innerEntity);
106
107 9
        if ($this->numberOfRelationsToEntity($innerEntity) != 1) {
108 2
            $this->clearMap($innerEntity);
109
        }
110
111 9
        if ($this->entity != $this->getFirstParentOf($innerEntity)) {
112 8
            if (!($relation = $this->getFirstParentOf($innerEntity))) {
113 1
                throw new Exceptions\UnreachablePathException(var_export([
114 1
                    'innerEntity' => $innerEntity,
115 1
                    'relation' => $relation,
116 1
                ], true));
117
            }
118
119 8
            if ($nest > 10) {
120
                // @codeCoverageIgnoreStart
121
                if ($this->logger) {
122
                    $this->logger->critical(json_encode([
123
                        'nest'         => $nest,
124
                        'entitiesPath' => $this->getEntitiesPath(),
125
                    ], true));
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $options of json_encode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
                    ], /** @scrutinizer ignore-type */ true));
Loading history...
126
                }
127
                // @codeCoverageIgnoreEnd
128
129 1
                throw new Exceptions\NestingException(
130
                    'Loop found in entities : ' .
131 1
                    var_export($this->getEntitiesPath(), true)
132
                );
133
            }
134
135 8
            return $this->getPathTo($relation, ++$nest) . '.' . $path;
136
        }
137
138 6
        return $path;
139
    }
140
141 7
    public function setQueryStartEntity(string $startEntity)
142
    {
143 7
        $this->setEntity($startEntity);
144 7
    }
145
146 6
    public function getPathToEntity(string $entityToReach, $reloadMap = false)
147
    {
148 6
        $this->entitiesPath = [];
149
150 6
        foreach ($this->getMap($reloadMap) as $rootEntity => $meta) {
151 6
            if (in_array($rootEntity, $this->wrongPath)) {
152 6
                unset($this->map[$rootEntity]);
153
            }
154
        }
155
156 6
        $return = '_embedded.' . $this->getPathTo($entityToReach);
157
158 5
        $this->allPaths[] = $this->entitiesPath;
159
160 5
        return $return;
161
    }
162
163 12
    public function keep($val, $innerEntity)
164
    {
165 12
        foreach ($this->getMap() as $rootEntity => $meta) {
166 12
            foreach ($meta['relations'] as $name => $entity) {
167 12
                if (self::INDEX_ENTITY_FIRST_CHILD == $val) {
168 1
                    return $entity;
169
                }
170
171 11
                if ($entity == $innerEntity) {
172
                    $return = [
173 11
                        self::INDEX_ENTITY_PARENT      => $rootEntity,
174 11
                        self::INDEX_FK_RELATION_NAME   => $name,
175 11
                    ][$val];
176
177 11
                    return $return;
178
                }
179
            }
180
        }
181
182 1
        throw new Exceptions\UnexpectedValueException(var_export([
183 1
            'val'         => self::$indexesToDescriptionMap[$val],
184 1
            'innerEntity' => $innerEntity,
185 1
            'map'         => $this->getMap(),
186 1
        ], true));
187
    }
188
189 10
    public function numberOfRelationsToEntity(string $entityToReach)
190
    {
191 10
        return $this->mapTargetRelations(
192 10
            $this->incrementSubject,
193 10
            $subject = 0,
194
            $entityToReach
195
        );
196
    }
197
198 3
    public function listOfParentsOf(string $entityToReach)
199
    {
200 3
        return $this->mapTargetRelations(
201 3
            $this->appendRootEntityToSubject,
202 3
            $subject = [],
203
            $entityToReach
204
        );
205
    }
206
207 3
    public function getEntitiesPath()
208
    {
209 3
        if (!$this->entitiesPath) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->entitiesPath of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
210 1
            throw new Exceptions\UndefinedPathException(
211 1
                'Any path was requested'
212
            );
213
        }
214
215 2
        return $this->entitiesPath;
216
    }
217
218 1
    public function removeStep($parentToSkip)
219
    {
220 1
        $this->wrongPath[] = $parentToSkip;
221 1
    }
222
223 1
    public function getHashKeyForDestination(string $destination)
224
    {
225 1
        return md5($this->entity . $destination);
226
    }
227
228 14
    public function forceMapReloading()
229
    {
230 14
        $this->map = $this->mapper->getMap();
231 14
    }
232
233 14
    private function getMap($reloadMap = false)
234
    {
235 14
        if ($reloadMap || !$this->map) {
236 14
            $this->forceMapReloading();
237
        }
238
239 14
        return $this->map;
240
    }
241
242 1
    public function addEntity(array $parents, $rootEntity) : array
243
    {
244 1
        $parents[] = $rootEntity;
245
246 1
        return $parents;
247
    }
248
249 11
    public function mapTargetRelations(
250
        callable $action,
251
        $subject,
252
        string $entityToReach
253
    ) {
254 11
        foreach ($this->getMap() as $rootEntity => $meta) {
255 11
            foreach ($meta['relations'] as $name => $relationEntity) {
256 11
                if ($relationEntity == $entityToReach) {
257 11
                    $subject = $action($subject, $rootEntity);
258
                }
259
            }
260
        }
261
262 11
        return $subject;
263
    }
264
265
    public function getAllPaths() : array
266
    {
267
        array_multisort($this->allPaths);
268
        return $this->allPaths;
269
    }
270
271
    public function findAllPathsTo(string $dest)
272
    {
273
        while (true) {
274
            try {
275
                $this->getPathToEntity($dest);
276
                $entities = $this->getEntitiesPath();
277
                $lastEntityFound = end($entities);
278
                $this->removeStep($lastEntityFound);
279
            } catch (\Mado\QueryBundle\Component\Meta\Exceptions\UnexpectedValueException $e) {
280
                return;
281
            }
282
        }
283
    }
284
}
285