35
35
*/
36
36
class EntityRepository implements ObjectRepository, Selectable
37
37
{
38
- /**
39
- * @internal This property will be private in 3.0, call {@see getEntityName()} instead.
40
- *
41
- * @var string
42
- */
43
- protected $ _entityName ;
44
-
45
- /**
46
- * @internal This property will be private in 3.0, call {@see getEntityManager()} instead.
47
- *
48
- * @var EntityManagerInterface
49
- */
50
- protected $ _em ;
38
+ /** @psalm-var class-string<T> */
39
+ private string $ entityName ;
40
+ private static ?Inflector $ inflector = null ;
51
41
52
42
/**
53
- * @internal This property will be private in 3.0, call {@see getClassMetadata()} instead.
54
- *
55
- * @var ClassMetadata
43
+ * @psalm-param ClassMetadata<T> $class
56
44
*/
57
- protected $ _class ;
58
-
59
- /** @var Inflector|null */
60
- private static $ inflector ;
61
-
62
- public function __construct (EntityManagerInterface $ em , ClassMetadata $ class )
63
- {
64
- $ this ->_entityName = $ class ->name ;
65
- $ this ->_em = $ em ;
66
- $ this ->_class = $ class ;
45
+ public function __construct (
46
+ private EntityManagerInterface $ em ,
47
+ private ClassMetadata $ class
48
+ ) {
49
+ $ this ->entityName = $ class ->name ;
67
50
}
68
51
69
52
/**
70
53
* Creates a new QueryBuilder instance that is prepopulated for this entity name.
71
- *
72
- * @param string $alias
73
- * @param string|null $indexBy The index for the from.
74
- *
75
- * @return QueryBuilder
76
54
*/
77
- public function createQueryBuilder ($ alias , $ indexBy = null )
55
+ public function createQueryBuilder (string $ alias , ? string $ indexBy = null ): QueryBuilder
78
56
{
79
- return $ this ->_em ->createQueryBuilder ()
57
+ return $ this ->em ->createQueryBuilder ()
80
58
->select ($ alias )
81
- ->from ($ this ->_entityName , $ alias , $ indexBy );
59
+ ->from ($ this ->entityName , $ alias , $ indexBy );
82
60
}
83
61
84
62
/**
85
63
* Creates a new result set mapping builder for this entity.
86
64
*
87
65
* The column naming strategy is "INCREMENT".
88
- *
89
- * @param string $alias
90
- *
91
- * @return ResultSetMappingBuilder
92
66
*/
93
- public function createResultSetMappingBuilder ($ alias )
67
+ public function createResultSetMappingBuilder (string $ alias ): ResultSetMappingBuilder
94
68
{
95
- $ rsm = new ResultSetMappingBuilder ($ this ->_em , ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT );
96
- $ rsm ->addRootEntityFromClassMetadata ($ this ->_entityName , $ alias );
69
+ $ rsm = new ResultSetMappingBuilder ($ this ->em , ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT );
70
+ $ rsm ->addRootEntityFromClassMetadata ($ this ->entityName , $ alias );
97
71
98
72
return $ rsm ;
99
73
}
100
74
101
75
/**
102
76
* Finds an entity by its primary key / identifier.
103
77
*
104
- * @param mixed $id The identifier.
105
- * @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants
106
- * or NULL if no specific lock mode should be used
107
- * during the search.
108
- * @param int|null $lockVersion The lock version.
78
+ * @param int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants
79
+ * or NULL if no specific lock mode should be used
80
+ * during the search.
109
81
* @psalm-param LockMode::*|null $lockMode
110
82
*
111
83
* @return object|null The entity instance or NULL if the entity can not be found.
112
84
* @psalm-return ?T
113
85
*/
114
- public function find ($ id , $ lockMode = null , $ lockVersion = null )
86
+ public function find (mixed $ id , ? int $ lockMode = null , ? int $ lockVersion = null ): ? object
115
87
{
116
- return $ this ->_em ->find ($ this ->_entityName , $ id , $ lockMode , $ lockVersion );
88
+ return $ this ->em ->find ($ this ->entityName , $ id , $ lockMode , $ lockVersion );
117
89
}
118
90
119
91
/**
120
92
* Finds all entities in the repository.
121
93
*
122
94
* @psalm-return list<T> The entities.
123
95
*/
124
- public function findAll ()
96
+ public function findAll (): array
125
97
{
126
98
return $ this ->findBy ([]);
127
99
}
@@ -137,9 +109,9 @@ public function findAll()
137
109
* @return object[] The objects.
138
110
* @psalm-return list<T>
139
111
*/
140
- public function findBy (array $ criteria , ?array $ orderBy = null , $ limit = null , $ offset = null )
112
+ public function findBy (array $ criteria , ?array $ orderBy = null , $ limit = null , $ offset = null ): array
141
113
{
142
- $ persister = $ this ->_em ->getUnitOfWork ()->getEntityPersister ($ this ->_entityName );
114
+ $ persister = $ this ->em ->getUnitOfWork ()->getEntityPersister ($ this ->entityName );
143
115
144
116
return $ persister ->loadAll ($ criteria , $ orderBy , $ limit , $ offset );
145
117
}
@@ -153,9 +125,9 @@ public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $
153
125
* @return object|null The entity instance or NULL if the entity can not be found.
154
126
* @psalm-return ?T
155
127
*/
156
- public function findOneBy (array $ criteria , ?array $ orderBy = null )
128
+ public function findOneBy (array $ criteria , ?array $ orderBy = null ): ? object
157
129
{
158
- $ persister = $ this ->_em ->getUnitOfWork ()->getEntityPersister ($ this ->_entityName );
130
+ $ persister = $ this ->em ->getUnitOfWork ()->getEntityPersister ($ this ->entityName );
159
131
160
132
return $ persister ->load ($ criteria , null , null , [], null , 1 , $ orderBy );
161
133
}
@@ -169,23 +141,20 @@ public function findOneBy(array $criteria, ?array $orderBy = null)
169
141
*
170
142
* @todo Add this method to `ObjectRepository` interface in the next major release
171
143
*/
172
- public function count (array $ criteria = [])
144
+ public function count (array $ criteria = []): int
173
145
{
174
- return $ this ->_em ->getUnitOfWork ()->getEntityPersister ($ this ->_entityName )->count ($ criteria );
146
+ return $ this ->em ->getUnitOfWork ()->getEntityPersister ($ this ->entityName )->count ($ criteria );
175
147
}
176
148
177
149
/**
178
150
* Adds support for magic method calls.
179
151
*
180
- * @param string $method
181
152
* @param mixed[] $arguments
182
153
* @psalm-param list<mixed> $arguments
183
154
*
184
- * @return mixed The returned value from the resolved method.
185
- *
186
155
* @throws BadMethodCallException If the method called is invalid.
187
156
*/
188
- public function __call ($ method , $ arguments )
157
+ public function __call (string $ method , array $ arguments ): mixed
189
158
{
190
159
if (str_starts_with ($ method , 'findBy ' )) {
191
160
return $ this ->resolveMagicCall ('findBy ' , substr ($ method , 6 ), $ arguments );
@@ -207,47 +176,37 @@ public function __call($method, $arguments)
207
176
}
208
177
209
178
/**
210
- * @return string
179
+ * @psalm- return class- string<T>
211
180
*/
212
- protected function getEntityName ()
181
+ protected function getEntityName (): string
213
182
{
214
- return $ this ->_entityName ;
183
+ return $ this ->entityName ;
215
184
}
216
185
217
- /**
218
- * @return string
219
- */
220
- public function getClassName ()
186
+ public function getClassName (): string
221
187
{
222
188
return $ this ->getEntityName ();
223
189
}
224
190
225
- /**
226
- * @return EntityManagerInterface
227
- */
228
- protected function getEntityManager ()
191
+ protected function getEntityManager (): EntityManagerInterface
229
192
{
230
- return $ this ->_em ;
193
+ return $ this ->em ;
231
194
}
232
195
233
- /**
234
- * @return ClassMetadata
235
- */
236
- protected function getClassMetadata ()
196
+ protected function getClassMetadata (): ClassMetadata
237
197
{
238
- return $ this ->_class ;
198
+ return $ this ->class ;
239
199
}
240
200
241
201
/**
242
202
* Select all elements from a selectable that match the expression and
243
203
* return a new collection containing these elements.
244
204
*
245
- * @return AbstractLazyCollection
246
205
* @psalm-return AbstractLazyCollection<int, T>&Selectable<int, T>
247
206
*/
248
- public function matching (Criteria $ criteria )
207
+ public function matching (Criteria $ criteria ): AbstractLazyCollection
249
208
{
250
- $ persister = $ this ->_em ->getUnitOfWork ()->getEntityPersister ($ this ->_entityName );
209
+ $ persister = $ this ->em ->getUnitOfWork ()->getEntityPersister ($ this ->entityName );
251
210
252
211
return new LazyCriteriaCollection ($ persister , $ criteria );
253
212
}
@@ -259,26 +218,22 @@ public function matching(Criteria $criteria)
259
218
* @param string $by The property name used as condition
260
219
* @psalm-param list<mixed> $arguments The arguments to pass at method call
261
220
*
262
- * @return mixed
263
- *
264
221
* @throws InvalidMagicMethodCall If the method called is invalid or the
265
222
* requested field/association does not exist.
266
223
*/
267
- private function resolveMagicCall (string $ method , string $ by , array $ arguments )
224
+ private function resolveMagicCall (string $ method , string $ by , array $ arguments ): mixed
268
225
{
269
226
if (! $ arguments ) {
270
227
throw InvalidMagicMethodCall::onMissingParameter ($ method . $ by );
271
228
}
272
229
273
- if (self ::$ inflector === null ) {
274
- self ::$ inflector = InflectorFactory::create ()->build ();
275
- }
230
+ self ::$ inflector ??= InflectorFactory::create ()->build ();
276
231
277
232
$ fieldName = lcfirst (self ::$ inflector ->classify ($ by ));
278
233
279
- if (! ($ this ->_class ->hasField ($ fieldName ) || $ this ->_class ->hasAssociation ($ fieldName ))) {
234
+ if (! ($ this ->class ->hasField ($ fieldName ) || $ this ->class ->hasAssociation ($ fieldName ))) {
280
235
throw InvalidMagicMethodCall::becauseFieldNotFoundIn (
281
- $ this ->_entityName ,
236
+ $ this ->entityName ,
282
237
$ fieldName ,
283
238
$ method . $ by
284
239
);
0 commit comments