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

api-platform / core / 14306224552

07 Apr 2025 09:55AM UTC coverage: 8.425% (+1.2%) from 7.202%
14306224552

push

github

web-flow
fix(metadata): yaml link security (#7066)

1 of 3 new or added lines in 2 files covered. (33.33%)

2165 existing lines in 153 files now uncovered.

13288 of 157729 relevant lines covered (8.42%)

22.89 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
    }
505✔
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) {
505✔
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');
505✔
46
        if (\is_array($orderByDqlPart) && \count($orderByDqlPart) > 0) {
505✔
47
            return;
39✔
48
        }
49

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

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

56
        if ([] !== $defaultOrder) {
468✔
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) {
461✔
79
            // A foreign identifier cannot be used for ordering.
80
            if ($classMetaData->containsForeignIdentifier) {
461✔
UNCOV
81
                return;
4✔
82
            }
83

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