1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Facile\MongoDbBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Facile\MongoDbBundle\DataCollector\MongoQuerySerializer; |
6
|
|
|
use MongoDB\BSON\UTCDateTime; |
7
|
|
|
use Symfony\Component\DependencyInjection\Container; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
11
|
|
|
|
12
|
|
|
class ProfilerController implements ContainerAwareInterface |
13
|
|
|
{ |
14
|
|
|
/** @var Container */ |
15
|
|
|
private $container; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Sets the container. |
19
|
|
|
* |
20
|
|
|
* @param ContainerInterface|null $container A ContainerInterface instance or null |
21
|
|
|
*/ |
22
|
2 |
|
public function setContainer(ContainerInterface $container = null) |
23
|
|
|
{ |
24
|
2 |
|
$this->container = $container; |
|
|
|
|
25
|
2 |
|
} |
26
|
|
|
|
27
|
2 |
|
public function explainAction($token, $queryNumber) |
28
|
|
|
{ |
29
|
|
|
/** @var $profiler \Symfony\Component\HttpKernel\Profiler\Profiler */ |
30
|
2 |
|
$profiler = $this->container->get('profiler'); |
31
|
2 |
|
$profiler->disable(); |
32
|
|
|
|
33
|
2 |
|
$profile = $profiler->loadProfile($token); |
34
|
2 |
|
$queries = $profile->getCollector('mongodb')->getQueries(); |
35
|
|
|
|
36
|
2 |
|
$query = $queries[$queryNumber]; |
37
|
|
|
|
38
|
2 |
|
$query->setFilters($this->walkAndConvertToUTCDatetime($query->getFilters())); |
39
|
|
|
|
40
|
2 |
|
$service = $this->container->get('mongo.explain_query_service'); |
41
|
|
|
|
42
|
|
|
try{ |
43
|
2 |
|
$result = $service->execute($query); |
44
|
1 |
|
} catch (\InvalidArgumentException $e) { |
45
|
1 |
|
return new JsonResponse([ |
46
|
1 |
|
"err" => $e->getMessage() |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
return new JsonResponse(MongoQuerySerializer::prepareItemData($result->toArray())); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $data |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
2 |
|
private function walkAndConvertToUTCDatetime($data) |
59
|
|
|
{ |
60
|
2 |
|
if (!is_array($data)) { |
61
|
1 |
|
return $data; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
foreach ($data as $key => $item) { |
65
|
|
|
|
66
|
1 |
|
if (is_string($item) && preg_match('/^ISODate/', $item)) { |
67
|
|
|
$time = str_replace(['ISODate("','")'], '', $item); |
68
|
|
|
$dateTime = new \DateTime($time); |
69
|
|
|
$item = new UTCDatetime($dateTime->getTimestamp() * 1000); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
$data[$key] = $this->walkAndConvertToUTCDatetime($item); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
return $data; |
76
|
|
|
} |
77
|
|
|
} |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.