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

api-platform / core / 15063570239

16 May 2025 07:56AM UTC coverage: 27.494% (-0.001%) from 27.495%
15063570239

push

github

web-flow
fix: error formats (#7148)

0 of 2 new or added lines in 2 files covered. (0.0%)

11139 existing lines in 371 files now uncovered.

13476 of 49015 relevant lines covered (27.49%)

74.77 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
    {
UNCOV
33
    }
511✔
34

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

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

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

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

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

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

75
            return;
9✔
76
        }
77

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

UNCOV
84
            foreach ($identifiers as $identifier) {
461✔
UNCOV
85
                $queryBuilder->addOrderBy("{$rootAlias}.{$identifier}", $this->order);
461✔
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

© 2026 Coveralls, Inc