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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

51.85
/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
    }
104✔
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) {
104✔
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');
104✔
UNCOV
46
        if (\is_array($orderByDqlPart) && \count($orderByDqlPart) > 0) {
104✔
UNCOV
47
            return;
12✔
48
        }
49

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

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

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

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

75
            return;
×
76
        }
77

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

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