DoctrineDataCollector::getEntities()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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 Saxulum\SaxulumWebProfiler\DataCollector;
16
17
use Doctrine\Common\Persistence\ManagerRegistry;
18
use Doctrine\ORM\Tools\SchemaValidator;
19
use Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector as BaseCollector;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
23
/**
24
 * DoctrineDataCollector.
25
 *
26
 * @author Christophe Coevoet <[email protected]>
27
 */
28
class DoctrineDataCollector extends BaseCollector
29
{
30
    private $registry;
31
    private $invalidEntityCount;
32
33
    public function __construct(ManagerRegistry $registry)
34
    {
35
        $this->registry = $registry;
36
37
        parent::__construct($registry);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function collect(Request $request, Response $response, \Exception $exception = null)
44
    {
45
        parent::collect($request, $response, $exception);
46
47
        $errors = array();
48
        $entities = array();
49
50
        foreach ($this->registry->getManagers() as $name => $em) {
51
            $entities[$name] = array();
52
            /** @var $factory \Doctrine\ORM\Mapping\ClassMetadataFactory */
53
            $factory = $em->getMetadataFactory();
54
            $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...
55
56
            /** @var $class \Doctrine\ORM\Mapping\ClassMetadataInfo */
57
            foreach ($factory->getLoadedMetadata() as $class) {
58
                $entities[$name][] = $class->getName();
59
                $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...
60
61
                if (!empty($classErrors)) {
62
                    $errors[$name][$class->getName()] = $classErrors;
63
                }
64
            }
65
        }
66
67
        $this->data['entities'] = $entities;
68
        $this->data['errors'] = $errors;
69
    }
70
71
    public function getEntities()
72
    {
73
        return $this->data['entities'];
74
    }
75
76
    public function getMappingErrors()
77
    {
78
        return $this->data['errors'];
79
    }
80
81
    public function getInvalidEntityCount()
82
    {
83
        if (null === $this->invalidEntityCount) {
84
            $this->invalidEntityCount = array_sum(array_map('count', $this->data['errors']));
85
        }
86
87
        return $this->invalidEntityCount;
88
    }
89
}
90