• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 14008635868

22 Mar 2025 12:39PM UTC coverage: 8.52% (+0.005%) from 8.515%
14008635868

Pull #7042

github

web-flow
Merge fdd88ef56 into 47a6dffbb
Pull Request #7042: Purge parent collections in inheritance cases

4 of 9 new or added lines in 1 file covered. (44.44%)

540 existing lines in 57 files now uncovered.

13394 of 157210 relevant lines covered (8.52%)

22.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/src/Doctrine/Orm/Extension/OrderExtension.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Doctrine\Orm\Extension;
15

16
use ApiPlatform\Doctrine\Orm\Util\QueryBuilderHelper;
17
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
19
use ApiPlatform\Metadata\Operation;
20
use Doctrine\ORM\QueryBuilder;
21

22
/**
23
 * Applies selected ordering while querying resource collection.
24
 *
25
 * @author Kévin Dunglas <dunglas@gmail.com>
26
 * @author Samuel ROZE <samuel.roze@gmail.com>
27
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
28
 */
29
final class OrderExtension implements QueryCollectionExtensionInterface
30
{
31
    public function __construct(private readonly ?string $order = null)
32
    {
33
    }
503✔
34

35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, ?string $resourceClass = null, ?Operation $operation = null, array $context = []): void
39
    {
40
        if (null === $resourceClass) {
503✔
41
            throw new InvalidArgumentException('The "$resourceClass" parameter must not be null');
×
42
        }
43

44
        // Do not apply order if already defined on queryBuilder
45
        $orderByDqlPart = $queryBuilder->getDQLPart('orderBy');
503✔
46
        if (\is_array($orderByDqlPart) && \count($orderByDqlPart) > 0) {
503✔
47
            return;
39✔
48
        }
49

50
        $rootAlias = $queryBuilder->getRootAliases()[0];
466✔
51

52
        $classMetaData = $queryBuilder->getEntityManager()->getClassMetadata($resourceClass);
466✔
53
        $identifiers = $classMetaData->getIdentifier();
466✔
54
        $defaultOrder = $operation?->getOrder() ?? [];
466✔
55

56
        if ([] !== $defaultOrder) {
466✔
UNCOV
57
            foreach ($defaultOrder as $field => $order) {
9✔
UNCOV
58
                if (\is_int($field)) {
9✔
59
                    // Default direction
UNCOV
60
                    $field = $order;
7✔
UNCOV
61
                    $order = 'ASC';
7✔
62
                }
63

UNCOV
64
                $pos = strpos($field, '.');
9✔
UNCOV
65
                if (false === $pos || isset($classMetaData->embeddedClasses[substr($field, 0, $pos)])) {
9✔
66
                    // Configure default filter with property
UNCOV
67
                    $field = "{$rootAlias}.{$field}";
4✔
68
                } else {
UNCOV
69
                    $alias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $rootAlias, substr($field, 0, $pos));
5✔
UNCOV
70
                    $field = \sprintf('%s.%s', $alias, substr($field, $pos + 1));
5✔
71
                }
UNCOV
72
                $queryBuilder->addOrderBy($field, $order);
9✔
73
            }
74

UNCOV
75
            return;
9✔
76
        }
77

78
        if (null !== $this->order) {
459✔
79
            // A foreign identifier cannot be used for ordering.
80
            if ($classMetaData->containsForeignIdentifier) {
459✔
UNCOV
81
                return;
4✔
82
            }
83

84
            foreach ($identifiers as $identifier) {
455✔
85
                $queryBuilder->addOrderBy("{$rootAlias}.{$identifier}", $this->order);
455✔
86
            }
87
        }
88
    }
89
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc