Completed
Pull Request — master (#579)
by Mindaugas
02:14
created

DoctrineDataCollector::getGroupedQueries()   D

Complexity

Conditions 9
Paths 13

Size

Total Lines 40
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
rs 4.909
cc 9
eloc 27
nc 13
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Doctrine Bundle
5
 *
6
 * The code was originally distributed inside the Symfony framework.
7
 *
8
 * (c) Fabien Potencier <[email protected]>
9
 * (c) Doctrine Project, Benjamin Eberlei <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Doctrine\Bundle\DoctrineBundle\DataCollector;
16
17
use Doctrine\Common\Persistence\ManagerRegistry;
18
use Doctrine\ORM\Tools\SchemaValidator;
19
use Doctrine\ORM\Version;
20
use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector as BaseCollector;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
24
/**
25
 * DoctrineDataCollector.
26
 *
27
 * @author Christophe Coevoet <[email protected]>
28
 */
29
class DoctrineDataCollector extends BaseCollector
30
{
31
    private $registry;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
32
    private $invalidEntityCount;
33
34
    public function __construct(ManagerRegistry $registry)
35
    {
36
        $this->registry = $registry;
37
38
        parent::__construct($registry);
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function collect(Request $request, Response $response, \Exception $exception = null)
45
    {
46
        parent::collect($request, $response, $exception);
47
48
        $errors = array();
49
        $entities = array();
50
        $caches = array(
51
            'enabled' => false,
52
            'log_enabled' => false,
53
            'counts' => array(
54
                'puts' => 0,
55
                'hits' => 0,
56
                'misses' => 0,
57
            ),
58
            'regions' => array(
59
                'puts' => array(),
60
                'hits' => array(),
61
                'misses' => array(),
62
            ),
63
        );
64
65
        foreach ($this->registry->getManagers() as $name => $em) {
66
            $entities[$name] = array();
67
            /** @var $factory \Doctrine\ORM\Mapping\ClassMetadataFactory */
68
            $factory = $em->getMetadataFactory();
69
            $validator = new SchemaValidator($em);
0 ignored issues
show
Compatibility introduced by
$em of type object<Doctrine\Common\Persistence\ObjectManager> is not a sub-type of object<Doctrine\ORM\EntityManagerInterface>. It seems like you assume a child interface of the interface Doctrine\Common\Persistence\ObjectManager to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
70
71
            /** @var $class \Doctrine\ORM\Mapping\ClassMetadataInfo */
72
            foreach ($factory->getLoadedMetadata() as $class) {
73
                if (!isset($entities[$name][$class->getName()])) {
74
                    $classErrors = $validator->validateClass($class);
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ORM\Mapping\ClassMetadataInfo>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
75
                    $entities[$name][$class->getName()] = $class->getName();
76
77
                    if (!empty($classErrors)) {
78
                        $errors[$name][$class->getName()] = $classErrors;
79
                    }
80
                }
81
            }
82
83
            if (version_compare(Version::VERSION, '2.5.0-DEV') < 0) {
84
                continue;
85
            }
86
87
            /** @var $emConfig \Doctrine\ORM\Configuration */
88
            $emConfig = $em->getConfiguration();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Common\Persistence\ObjectManager as the method getConfiguration() does only exist in the following implementations of said interface: Doctrine\ORM\Decorator\EntityManagerDecorator, Doctrine\ORM\EntityManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
89
            $slcEnabled = $emConfig->isSecondLevelCacheEnabled();
90
91
            if (!$slcEnabled) {
92
                continue;
93
            }
94
95
            $caches['enabled'] = true;
96
97
            /** @var $cacheConfiguration \Doctrine\ORM\Cache\CacheConfiguration */
98
            /** @var $cacheLoggerChain \Doctrine\ORM\Cache\Logging\CacheLoggerChain */
99
            $cacheConfiguration = $emConfig->getSecondLevelCacheConfiguration();
100
            $cacheLoggerChain = $cacheConfiguration->getCacheLogger();
101
102
            if (!$cacheLoggerChain || !$cacheLoggerChain->getLogger('statistics')) {
103
                continue;
104
            }
105
106
            /** @var $cacheLoggerStats \Doctrine\ORM\Cache\Logging\StatisticsCacheLogger */
107
            $cacheLoggerStats = $cacheLoggerChain->getLogger('statistics');
108
            $caches['log_enabled'] = true;
109
110
            $caches['counts']['puts'] += $cacheLoggerStats->getPutCount();
111
            $caches['counts']['hits'] += $cacheLoggerStats->getHitCount();
112
            $caches['counts']['misses'] += $cacheLoggerStats->getMissCount();
113
114 View Code Duplication
            foreach ($cacheLoggerStats->getRegionsPut() as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
115
                if (!isset($caches['regions']['puts'][$key])) {
116
                    $caches['regions']['puts'][$key] = 0;
117
                }
118
119
                $caches['regions']['puts'][$key] += $value;
120
            }
121
122 View Code Duplication
            foreach ($cacheLoggerStats->getRegionsHit() as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
123
                if (!isset($caches['regions']['hits'][$key])) {
124
                    $caches['regions']['hits'][$key] = 0;
125
                }
126
127
                $caches['regions']['hits'][$key] += $value;
128
            }
129
130 View Code Duplication
            foreach ($cacheLoggerStats->getRegionsMiss() as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
                if (!isset($caches['regions']['misses'][$key])) {
132
                    $caches['regions']['misses'][$key] = 0;
133
                }
134
135
                $caches['regions']['misses'][$key] += $value;
136
            }
137
        }
138
139
        $this->data['entities'] = $entities;
140
        $this->data['errors'] = $errors;
141
        $this->data['caches'] = $caches;
142
    }
143
144
    public function getEntities()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
145
    {
146
        return $this->data['entities'];
147
    }
148
149
    public function getMappingErrors()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
150
    {
151
        return $this->data['errors'];
152
    }
153
154
    public function getCacheHitsCount()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
155
    {
156
        return $this->data['caches']['counts']['hits'];
157
    }
158
159
    public function getCachePutsCount()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
160
    {
161
        return $this->data['caches']['counts']['puts'];
162
    }
163
164
    public function getCacheMissesCount()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
165
    {
166
        return $this->data['caches']['counts']['misses'];
167
    }
168
169
    public function getCacheEnabled()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
170
    {
171
        return $this->data['caches']['enabled'];
172
    }
173
174
    public function getCacheRegions()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
175
    {
176
        return $this->data['caches']['regions'];
177
    }
178
179
    public function getCacheCounts()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
180
    {
181
        return $this->data['caches']['counts'];
182
    }
183
184
    public function getInvalidEntityCount()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
185
    {
186
        if (null === $this->invalidEntityCount) {
187
            $this->invalidEntityCount = array_sum(array_map('count', $this->data['errors']));
188
        }
189
190
        return $this->invalidEntityCount;
191
    }
192
193
    public function getGroupedQueries()
194
    {
195
        static $groupedQueries = null;
196
197
        if ($groupedQueries !== null) {
198
            return $groupedQueries;
199
        }
200
201
        $groupedQueries = array();
202
        $totalExecutionMS = 0;
203
        foreach ($this->data['queries'] as $connection => $queries) {
204
            $connectionGroupedQueries = array();
205
            foreach ($queries as $i => $query) {
206
                $key = $query['sql'];
207
                if (!isset($connectionGroupedQueries[$key])) {
208
                    $connectionGroupedQueries[$key] = $query;
209
                    $connectionGroupedQueries[$key]['executionMS'] = 0;
210
                    $connectionGroupedQueries[$key]['count'] = 0;
211
                    $connectionGroupedQueries[$key]['index'] = $i; // "Explain query" relies on query index in 'queries'.
212
                }
213
                $connectionGroupedQueries[$key]['executionMS'] += $query['executionMS'];
214
                $connectionGroupedQueries[$key]['count']++;
215
                $totalExecutionMS += $query['executionMS'];
216
            }
217
            usort($connectionGroupedQueries, function ($a, $b) {
218
                if ($a['executionMS'] === $b['executionMS']) {
219
                    return 0;
220
                }
221
                return ($a['executionMS'] < $b['executionMS']) ? 1 : -1;
222
            });
223
            $groupedQueries[$connection] = $connectionGroupedQueries;
224
        }
225
        foreach ($groupedQueries as $connection => $queries) {
226
            foreach ($queries as $i => $query) {
227
                $groupedQueries[$connection][$i]['executionPercent'] = $query['executionMS'] / $totalExecutionMS * 100;
228
            }
229
        }
230
231
        return $groupedQueries;
232
    }
233
234
    public function getGroupedQueryCount()
235
    {
236
        $count = 0;
237
        foreach ($this->getGroupedQueries() as $connectionGroupedQueries) {
0 ignored issues
show
Bug introduced by
The expression $this->getGroupedQueries() of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
238
            $count += count($connectionGroupedQueries);
239
        }
240
241
        return $count;
242
    }
243
}
244