Completed
Push — master ( ac8ec4...1a57ac )
by Kévin
04:37 queued 11s
created

src/Bridge/Doctrine/Orm/PropertyHelperTrait.php (2 issues)

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
     * Splits the given property into parts.
31
     */
32
    abstract protected function splitPropertyParts(string $property/*, string $resourceClass*/): 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...
33
34
    /**
35
     * Adds the necessary joins for a nested property.
36
     *
37
     * @throws InvalidArgumentException If property is not nested
38
     *
39
     * @return array An array where the first element is the join $alias of the leaf entity,
40
     *               the second element is the $field name
41
     *               the third element is the $associations array
42
     */
43
    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...
44
    {
45
        if (\func_num_args() > 4) {
46
            $resourceClass = func_get_arg(4);
47
        } else {
48
            if (__CLASS__ !== \get_class($this)) {
49
                $r = new \ReflectionMethod($this, __FUNCTION__);
50
                if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
51
                    @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);
52
                }
53
            }
54
            $resourceClass = null;
55
        }
56
57
        if (\func_num_args() > 5) {
58
            $joinType = func_get_arg(5);
59
        } else {
60
            if (__CLASS__ !== \get_class($this)) {
61
                $r = new \ReflectionMethod($this, __FUNCTION__);
62
                if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
63
                    @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);
64
                }
65
            }
66
            $joinType = null;
67
        }
68
69
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
70
        $parentAlias = $rootAlias;
71
        $alias = null;
72
73
        foreach ($propertyParts['associations'] as $association) {
74
            $alias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $parentAlias, $association, $joinType);
75
            $parentAlias = $alias;
76
        }
77
78
        if (null === $alias) {
79
            throw new InvalidArgumentException(sprintf('Cannot add joins for property "%s" - property is not nested.', $property));
80
        }
81
82
        return [$alias, $propertyParts['field'], $propertyParts['associations']];
83
    }
84
}
85