Completed
Pull Request — 2.0 (#1087)
by Antoine
03:21
created

ShouldEagerLoad   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getOperationForceEager() 0 12 4
B hasFetchEagerAssociation() 0 22 5
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Util;
15
16
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
17
use Doctrine\ORM\EntityManager;
18
use Doctrine\ORM\Mapping\ClassMetadataInfo;
19
20
/**
21
 * @author Antoine Bluchet <[email protected]>
22
 */
23
trait ShouldEagerLoad
24
{
25
    /**
26
     * Checks if an operation has a `force_eager` attribute.
27
     *
28
     * @param string $resourceClass
0 ignored issues
show
Bug introduced by
There is no parameter named $resourceClass. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
29
     * @param array  $options
30
     *
31
     * @return bool
32
     */
33
    private function getOperationForceEager(ResourceMetadata $resourceMetadata, array $options): bool
34
    {
35
        if (isset($options['collection_operation_name'])) {
36
            $forceEager = $resourceMetadata->getCollectionOperationAttribute($options['collection_operation_name'], 'force_eager', null, true);
37
        } elseif (isset($options['item_operation_name'])) {
38
            $forceEager = $resourceMetadata->getItemOperationAttribute($options['item_operation_name'], 'force_eager', null, true);
39
        } else {
40
            $forceEager = $resourceMetadata->getAttribute('force_eager');
41
        }
42
43
        return is_bool($forceEager) ? $forceEager : $this->forceEager;
0 ignored issues
show
Bug introduced by
The property forceEager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
    }
45
46
    /**
47
     * Checkes if the class has an associationMapping with FETCH=EAGER.
48
     *
49
     * @param EntityManager     $em
50
     * @param ClassMetadataInfo $classMetadata
51
     * @param array             $checked       array cache of tested metadata classes
52
     */
53
    private function hasFetchEagerAssociation(EntityManager $em, ClassMetadataInfo $classMetadata, &$checked = [])
54
    {
55
        $checked[] = $classMetadata->name;
56
57
        foreach ($classMetadata->associationMappings as $mapping) {
58
            if (ClassMetadataInfo::FETCH_EAGER === $mapping['fetch']) {
59
                return true;
60
            }
61
62
            $related = $em->getClassMetadata($mapping['targetEntity']);
63
64
            if (in_array($related->name, $checked, true)) {
65
                continue;
66
            }
67
68
            if (true === $this->hasFetchEagerAssociation($em, $related, $checked)) {
69
                return true;
70
            }
71
        }
72
73
        return false;
74
    }
75
}
76