Passed
Pull Request — master (#2144)
by Alan
04:12
created

PropertyHelperTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 26
dl 0
loc 51
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B addJoinsForNestedProperty() 0 40 9
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;
15
16
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryBuilderHelper;
17
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18
use ApiPlatform\Core\Exception\InvalidArgumentException;
19
use Doctrine\ORM\QueryBuilder;
20
21
/**
22
 * Helper trait regarding a property in an entity using the resource metadata.
23
 *
24
 * @author Kévin Dunglas <[email protected]>
25
 * @author Théo FIDRY <[email protected]>
26
 */
27
trait PropertyHelperTrait
28
{
29
    /**
30
     * Adds the necessary joins for a nested property.
31
     *
32
     * @throws InvalidArgumentException If property is not nested
33
     *
34
     * @return array An array where the first element is the join $alias of the leaf entity,
35
     *               the second element is the $field name
36
     *               the third element is the $associations array
37
     */
38
    protected function addJoinsForNestedProperty(string $property, string $rootAlias, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator/*, string $resourceClass, string $joinType*/): array
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
    {
40
        if (\func_num_args() > 4) {
41
            $resourceClass = func_get_arg(4);
42
        } else {
43
            if (__CLASS__ !== \get_class($this)) {
44
                $r = new \ReflectionMethod($this, __FUNCTION__);
45
                if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
46
                    @trigger_error(sprintf('Method %s() will have a fifth `$resourceClass` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.1.', __FUNCTION__), E_USER_DEPRECATED);
47
                }
48
            }
49
            $resourceClass = null;
50
        }
51
52
        if (\func_num_args() > 5) {
53
            $joinType = func_get_arg(5);
54
        } else {
55
            if (__CLASS__ !== \get_class($this)) {
56
                $r = new \ReflectionMethod($this, __FUNCTION__);
57
                if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
58
                    @trigger_error(sprintf('Method %s() will have a sixth `$joinType` argument in version API Platform 3.0. Not defining it is deprecated since API Platform 2.3.', __FUNCTION__), E_USER_DEPRECATED);
59
                }
60
            }
61
            $joinType = null;
62
        }
63
64
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
0 ignored issues
show
Bug introduced by
It seems like splitPropertyParts() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

64
        /** @scrutinizer ignore-call */ 
65
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
Loading history...
65
        $parentAlias = $rootAlias;
66
        $alias = null;
67
68
        foreach ($propertyParts['associations'] as $association) {
69
            $alias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $parentAlias, $association, $joinType);
70
            $parentAlias = $alias;
71
        }
72
73
        if (null === $alias) {
74
            throw new InvalidArgumentException(sprintf('Cannot add joins for property "%s" - property is not nested.', $property));
75
        }
76
77
        return [$alias, $propertyParts['field'], $propertyParts['associations']];
78
    }
79
}
80