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

tarlepp / symfony-flex-backend / #6193

14 May 2026 11:44AM UTC coverage: 99.875% (-0.1%) from 100.0%
#6193

push

php-coveralls

web-flow
Merge pull request #3273 from tarlepp/chore(deps)/dependency-update

Chore(deps) - Dependency update

2392 of 2395 relevant lines covered (99.87%)

88.27 hits per line

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

97.83
/src/Repository/Traits/RepositoryMethodsTrait.php
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Repository/Traits/RepositoryMethodsTrait.php
5
 *
6
 * @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
7
 */
8

9
namespace App\Repository\Traits;
10

11
use App\Entity\Interfaces\EntityInterface;
12
use App\Rest\RepositoryHelper;
13
use App\Rest\UuidHelper;
14
use ArrayIterator;
15
use Doctrine\DBAL\LockMode;
16
use Doctrine\ORM\AbstractQuery;
17
use Doctrine\ORM\QueryBuilder;
18
use Doctrine\ORM\Tools\Pagination\Paginator;
19
use InvalidArgumentException;
20
use function array_column;
21
use function assert;
22

23
/**
24
 * @package App\Repository\Traits
25
 * @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com>
26
 */
27
trait RepositoryMethodsTrait
28
{
29
    public function find(string $id, LockMode|int|null $lockMode = null, ?int $lockVersion = null): ?EntityInterface
30
    {
31
        /** @psalm-suppress InvalidArgument ORM 3 EntityManager::find() accepts LockMode|int|null */
32
        $output = $this->getEntityManager()->find($this->getEntityName(), $id, $lockMode, $lockVersion);
49✔
33

34
        return $output instanceof EntityInterface ? $output : null;
49✔
35
    }
36

37
    /**
38
     * @psalm-param string|AbstractQuery::HYDRATE_*|null $hydrationMode
39
     *
40
     * @psalm-return array<int|string, mixed>|EntityInterface|null
41
     */
42
    public function findAdvanced(string $id, string | int | null $hydrationMode = null): null | array | EntityInterface
43
    {
44
        // Get query builder
45
        $queryBuilder = $this->getQueryBuilder();
79✔
46

47
        // Process custom QueryBuilder actions
48
        $this->processQueryBuilder($queryBuilder);
79✔
49

50
        $queryBuilder
79✔
51
            ->where('entity.id = :id')
79✔
52
            ->setParameter('id', $id, UuidHelper::getType($id));
79✔
53

54
        /*
55
         * This is just to help debug queries
56
         *
57
         * dd($queryBuilder->getQuery()->getDQL(), $queryBuilder->getQuery()->getSQL());
58
         */
59

60
        return $queryBuilder->getQuery()->getOneOrNullResult($hydrationMode);
79✔
61
    }
62

63
    public function findOneBy(array $criteria, ?array $orderBy = null): ?object
64
    {
65
        $repository = $this->getEntityManager()->getRepository($this->getEntityName());
35✔
66

67
        return $repository->findOneBy($criteria, $orderBy);
35✔
68
    }
69

70
    /**
71
     * @psalm-return list<object|EntityInterface>
72
     */
73
    public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
74
    {
75
        return $this
1✔
76
            ->getEntityManager()
1✔
77
            ->getRepository($this->getEntityName())
1✔
78
            ->findBy($criteria, $orderBy, $limit, $offset);
1✔
79
    }
80

81
    /**
82
     * @return array<int, EntityInterface>
83
     */
84
    public function findByAdvanced(
85
        array $criteria,
86
        ?array $orderBy = null,
87
        ?int $limit = null,
88
        ?int $offset = null,
89
        ?array $search = null
90
    ): array {
91
        // Get query builder
92
        $queryBuilder = $this->getQueryBuilder($criteria, $search, $orderBy, $limit, $offset);
27✔
93

94
        // Process custom QueryBuilder actions
95
        $this->processQueryBuilder($queryBuilder);
27✔
96

97
        /*
98
         * This is just to help debug queries
99
         *
100
         * dd($queryBuilder->getQuery()->getDQL(), $queryBuilder->getQuery()->getSQL());
101
         */
102
        RepositoryHelper::resetParameterCount();
27✔
103

104
        $iterator = new Paginator($queryBuilder, true)->getIterator();
27✔
105

106
        assert($iterator instanceof ArrayIterator);
×
107

108
        return $iterator->getArrayCopy();
27✔
109
    }
110

111
    /**
112
     * @psalm-return list<object|EntityInterface>
113
     */
114
    public function findAll(): array
115
    {
116
        return $this
4✔
117
            ->getEntityManager()
4✔
118
            ->getRepository($this->getEntityName())
4✔
119
            ->findAll();
4✔
120
    }
121

122
    /**
123
     * @return array<int, string>
124
     */
125
    public function findIds(?array $criteria = null, ?array $search = null): array
126
    {
127
        // Get query builder
128
        $queryBuilder = $this->getQueryBuilder($criteria, $search);
14✔
129

130
        // Build query
131
        $queryBuilder
14✔
132
            ->select('entity.id')
14✔
133
            ->distinct();
14✔
134

135
        // Process custom QueryBuilder actions
136
        $this->processQueryBuilder($queryBuilder);
14✔
137

138
        /*
139
         * This is just to help debug queries
140
         *
141
         * dd($queryBuilder->getQuery()->getDQL(), $queryBuilder->getQuery()->getSQL());
142
         */
143
        RepositoryHelper::resetParameterCount();
14✔
144

145
        return array_column($queryBuilder->getQuery()->getArrayResult(), 'id');
14✔
146
    }
147

148
    public function countAdvanced(?array $criteria = null, ?array $search = null): int
149
    {
150
        // Get query builder
151
        $queryBuilder = $this->getQueryBuilder($criteria, $search);
19✔
152

153
        // Build query
154
        $queryBuilder->select('COUNT(DISTINCT(entity.id))');
19✔
155

156
        // Process custom QueryBuilder actions
157
        $this->processQueryBuilder($queryBuilder);
19✔
158

159
        /*
160
         * This is just to help debug queries
161
         *
162
         * dd($queryBuilder->getQuery()->getDQL(), $queryBuilder->getQuery()->getSQL());
163
         */
164
        RepositoryHelper::resetParameterCount();
19✔
165

166
        return (int)$queryBuilder->getQuery()->getSingleScalarResult();
19✔
167
    }
168

169
    public function reset(): int
170
    {
171
        // Create query builder
172
        $queryBuilder = $this->createQueryBuilder();
2✔
173

174
        // Define delete query
175
        $queryBuilder->delete();
2✔
176

177
        // Return deleted row count
178
        return (int)$queryBuilder->getQuery()->execute();
2✔
179
    }
180

181
    /**
182
     * Helper method to get QueryBuilder for current instance within specified default parameters.
183
     *
184
     * @param array<int|string, mixed>|null $criteria
185
     * @param array<string, string>|null $search
186
     * @param array<string, string>|null $orderBy
187
     *
188
     * @throws InvalidArgumentException
189
     */
190
    private function getQueryBuilder(
191
        ?array $criteria = null,
192
        ?array $search = null,
193
        ?array $orderBy = null,
194
        ?int $limit = null,
195
        ?int $offset = null
196
    ): QueryBuilder {
197
        // Create new QueryBuilder for this instance
198
        $queryBuilder = $this->createQueryBuilder();
134✔
199

200
        // Process normal and search term criteria
201
        RepositoryHelper::processCriteria($queryBuilder, $criteria);
134✔
202
        RepositoryHelper::processSearchTerms($queryBuilder, $this->getSearchColumns(), $search);
134✔
203
        RepositoryHelper::processOrderBy($queryBuilder, $orderBy);
134✔
204

205
        // Process limit and offset
206
        $queryBuilder->setMaxResults($limit);
134✔
207
        $queryBuilder->setFirstResult($offset ?? 0);
134✔
208

209
        return $queryBuilder;
134✔
210
    }
211
}
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