Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 94e991b

Browse files
committed
Refactored fixture helper to use PHP7.1 and discard useless properties
1 parent 5a43c19 commit 94e991b

File tree

3 files changed

+91
-116
lines changed

3 files changed

+91
-116
lines changed

AbstractFixture.php

Lines changed: 59 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
/*
3+
/**
44
* This file is part of the Orbitale DoctrineTools package.
55
*
66
* (c) Alexandre Rock Ancelet <[email protected]>
@@ -34,80 +34,54 @@
3434
*/
3535
abstract class AbstractFixture extends BaseAbstractFixture implements OrderedFixtureInterface
3636
{
37-
/**
38-
* @var EntityManager
39-
*/
37+
/** @var ObjectManager */
4038
private $manager;
4139

42-
/**
43-
* @var EntityRepository
44-
*/
40+
/** @var EntityRepository */
4541
private $repo;
4642

47-
/**
48-
* @var int
49-
*/
50-
private $order;
51-
52-
/**
53-
* @var string
54-
*/
55-
private $entityClass;
56-
57-
/**
58-
* @var PropertyAccessor
59-
*/
43+
/** @var PropertyAccessor */
6044
private $propertyAccessor;
6145

62-
/**
63-
* @var null|string
64-
*/
65-
private $referencePrefix;
66-
67-
/**
68-
* @var bool
69-
*/
70-
private $searchForMatchingIds = true;
71-
72-
/**
73-
* @var int
74-
*/
46+
/** @var int */
7547
private $totalNumberOfObjects = 0;
7648

77-
/**
78-
* @var int
79-
*/
49+
/** @var int */
8050
private $numberOfIteratedObjects = 0;
8151

82-
/**
83-
* @var int
84-
*/
85-
private $flushEveryXIterations = 0;
86-
87-
/**
88-
* @var bool
89-
*/
52+
/** @var bool */
9053
private $clearEMOnFlush = true;
9154

9255
public function __construct()
9356
{
94-
$this->order = $this->getOrder();
95-
$this->flushEveryXIterations = $this->flushEveryXIterations();
96-
$this->searchForMatchingIds = $this->searchForMatchingIds();
97-
$this->entityClass = $this->getEntityClass();
98-
$this->referencePrefix = $this->getReferencePrefix();
99-
$this->propertyAccessor = class_exists('Symfony\Component\PropertyAccess\PropertyAccess') ? PropertyAccess::createPropertyAccessor() : null;
100-
$this->clearEMOnFlush = $this->clearEntityManagerOnFlush();
57+
$this->clearEMOnFlush = $this->clearEntityManagerOnFlush();
58+
if (class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
59+
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
60+
}
10161
}
10262

63+
/**
64+
* Returns the class of the entity you're managing.
65+
*
66+
* @return string
67+
*/
68+
abstract protected function getEntityClass(): string;
69+
70+
/**
71+
* Returns a list of objects to insert in the database.
72+
*
73+
* @return ArrayCollection|object[]
74+
*/
75+
abstract protected function getObjects();
76+
10377
/**
10478
* {@inheritdoc}
10579
*/
10680
public function load(ObjectManager $manager)
10781
{
10882
$this->manager = $manager;
10983

110-
if ($this->disableLogger()) {
84+
if ($this->disableLogger() && $this->manager instanceof EntityManager) {
11185
$this->manager->getConnection()->getConfiguration()->setSQLLogger(null);
11286
}
11387

@@ -126,8 +100,8 @@ public function load(ObjectManager $manager)
126100
// Flush if we performed a "whole" fixture load,
127101
// or if we flushed with batches but have not flushed all items.
128102
if (
129-
!$this->flushEveryXIterations
130-
|| ($this->flushEveryXIterations && $this->numberOfIteratedObjects !== $this->totalNumberOfObjects)
103+
!$this->flushEveryXIterations()
104+
|| ($this->flushEveryXIterations() && $this->numberOfIteratedObjects !== $this->totalNumberOfObjects)
131105
) {
132106
$this->manager->flush();
133107
if ($this->clearEMOnFlush) {
@@ -145,7 +119,7 @@ public function load(ObjectManager $manager)
145119
* @param ClassMetadata $metadata
146120
* @param null $id
147121
*/
148-
protected function setGeneratorBasedOnId(ClassMetadata $metadata, $id = null)
122+
protected function setGeneratorBasedOnId(ClassMetadata $metadata, $id = null): void
149123
{
150124
if ($id) {
151125
$metadata->setIdGenerator(new AssignedGenerator());
@@ -157,7 +131,7 @@ protected function setGeneratorBasedOnId(ClassMetadata $metadata, $id = null)
157131
/**
158132
* Creates the object and persist it in database.
159133
*
160-
* @param array|object $data
134+
* @param object $data
161135
*/
162136
private function fixtureObject($data)
163137
{
@@ -168,7 +142,7 @@ private function fixtureObject($data)
168142
$identifier = $metadata->getIdentifier();
169143

170144
// The ID is taken in account to force its use in the database.
171-
$id = array();
145+
$id = [];
172146
foreach ($identifier as $key) {
173147
$id[$key] = $this->getPropertyFromData($data, $key);
174148
}
@@ -183,7 +157,7 @@ private function fixtureObject($data)
183157
$addRef = false;
184158

185159
// If the user specifies an ID and the fixture class wants it to be merged, we search for an object.
186-
if ($id && $this->searchForMatchingIds) {
160+
if ($id && $this->searchForMatchingIds()) {
187161
// Checks that the object ID exists in database.
188162
$obj = $this->repo->findOneBy($id);
189163
if ($obj) {
@@ -203,7 +177,6 @@ private function fixtureObject($data)
203177
if (is_array($data)) {
204178
$obj = $this->createNewInstance($data);
205179
foreach ($data as $field => $value) {
206-
207180
// If the value is a callable we execute it and inject the fixture object and the manager.
208181
if ($value instanceof \Closure) {
209182
$value = $value($obj, $this, $this->manager);
@@ -226,9 +199,9 @@ private function fixtureObject($data)
226199

227200
// If we need to flush it, then we do it too.
228201
if (
229-
$this->flushEveryXIterations
230-
&& $this->numberOfIteratedObjects
231-
&& $this->numberOfIteratedObjects % $this->flushEveryXIterations === 0
202+
$this->numberOfIteratedObjects > 0
203+
&& $this->flushEveryXIterations() > 0
204+
&& $this->numberOfIteratedObjects % $this->flushEveryXIterations() === 0
232205
) {
233206
$this->manager->flush();
234207
if ($this->clearEMOnFlush) {
@@ -239,27 +212,27 @@ private function fixtureObject($data)
239212
}
240213

241214
// If we have to add a reference, we do it
242-
if ($addRef === true && $obj && $this->referencePrefix) {
215+
if ($addRef === true && $obj && $this->getReferencePrefix()) {
243216
if (!$id || !reset($id)) {
244217
// If no id was provided in the object, maybe there was one after data hydration.
245218
// Can be done maybe in entity constructor or in a property callback.
246219
// So let's try to get it.
247220
if ($this->propertyAccessor) {
248221
if ($this->propertyAccessor) {
249222
try {
250-
$id = array('id' => $this->propertyAccessor->getValue($obj, 'id'));
223+
$id = ['id' => $this->propertyAccessor->getValue($obj, 'id')];
251224
} catch (NoSuchIndexException $e) {
252-
$id = array();
225+
$id = [];
253226
}
254227
}
255228
} elseif (method_exists($obj, 'getId')) {
256-
$id = array('id' => $obj->getId());
229+
$id = ['id' => $obj->getId()];
257230
}
258231
}
259232
if (1 === count($id)) {
260233
// Only reference single identifiers.
261234
$id = reset($id);
262-
$this->addReference($this->referencePrefix.($id ?: (string) $obj), $obj);
235+
$this->addReference($this->getReferencePrefix().($id ?: (string) $obj), $obj);
263236
} elseif (count($id) > 1) {
264237
throw new \RuntimeException('Cannot add reference for composite identifiers.');
265238
}
@@ -274,7 +247,7 @@ private function fixtureObject($data)
274247
*
275248
* @return mixed
276249
*/
277-
private function getPropertyFromData($data, $key)
250+
private function getPropertyFromData($data, string $key)
278251
{
279252
if (is_object($data)) {
280253
$method = 'get'.ucfirst($key);
@@ -286,9 +259,7 @@ private function getPropertyFromData($data, $key)
286259
}
287260
}
288261

289-
if (isset($data[$key])) {
290-
return $data[$key];
291-
}
262+
return $data[$key] ?? null;
292263
}
293264

294265
/**
@@ -298,9 +269,9 @@ private function getPropertyFromData($data, $key)
298269
*
299270
* @return int
300271
*/
301-
public function getOrder()
272+
public function getOrder(): int
302273
{
303-
return $this->order;
274+
return 0;
304275
}
305276

306277
/**
@@ -309,7 +280,7 @@ public function getOrder()
309280
*
310281
* @return bool
311282
*/
312-
protected function disableLogger()
283+
protected function disableLogger(): bool
313284
{
314285
return false;
315286
}
@@ -320,35 +291,30 @@ protected function disableLogger()
320291
* NOTE: To create references of an object, it must have an ID, and if not, implement __toString(), because
321292
* each object is referenced BEFORE flushing the database.
322293
* NOTE2: If you specified a "flushEveryXIterations" value, then the object will be provided with an ID every time.
323-
*
324-
* @return string|null
325294
*/
326-
protected function getReferencePrefix()
295+
protected function getReferencePrefix(): ?string
327296
{
328-
return $this->referencePrefix;
297+
return null;
329298
}
330299

331300
/**
332301
* If specified, the entity manager will be flushed every X times, depending on your specified values.
333302
* Default is null, so the database is flushed only at the end of all persists.
334-
*
335-
* @return bool
336303
*/
337-
protected function flushEveryXIterations()
304+
protected function flushEveryXIterations(): int
338305
{
339-
return $this->flushEveryXIterations;
306+
return 0;
340307
}
341308

342309
/**
343-
* If true and an ID is specified, will execute a $manager->find($id) in the database.
344-
* By default this var is true.
310+
* If true and an ID is specified in fixture's $data, will execute a $manager->find($id) in the database.
345311
* Be careful, if you set it to false you may have "duplicate entry" errors if your database is already populated.
346312
*
347313
* @return bool
348314
*/
349-
protected function searchForMatchingIds()
315+
protected function searchForMatchingIds(): bool
350316
{
351-
return $this->searchForMatchingIds;
317+
return true;
352318
}
353319

354320
/**
@@ -357,35 +323,23 @@ protected function searchForMatchingIds()
357323
*
358324
* @return bool
359325
*/
360-
protected function clearEntityManagerOnFlush()
326+
protected function clearEntityManagerOnFlush(): bool
361327
{
362-
return $this->clearEMOnFlush;
328+
return true;
363329
}
364330

365331
/**
366332
* Creates a new instance of the class associated with the fixture.
367-
* Very useful if you have constructor arguments to manage.
333+
* Override this method if you have constructor arguments to manage yourself depending on input data.
368334
*
369335
* @param array $data
370336
*
371337
* @return object
372338
*/
373-
protected function createNewInstance($data)
339+
protected function createNewInstance(array $data)
374340
{
375-
return new $this->entityClass;
376-
}
377-
378-
/**
379-
* Returns the class of the entity you're managing.
380-
*
381-
* @return string
382-
*/
383-
protected abstract function getEntityClass();
341+
$class = $this->getEntityClass();
384342

385-
/**
386-
* Returns a list of objects to insert in the database.
387-
*
388-
* @return ArrayCollection|object[]
389-
*/
390-
protected abstract function getObjects();
343+
return new $class;
344+
}
391345
}

BaseEntityRepository.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Orbitale DoctrineTools package.
5+
*
6+
* (c) Alexandre Rock Ancelet <[email protected]>
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+
namespace Orbitale\Component\DoctrineTools;
13+
14+
use Doctrine\ORM\EntityRepository;
15+
16+
class BaseEntityRepository extends EntityRepository
17+
{
18+
19+
}
20+
21+
@trigger_error(sprintf('Class %s is deprecated, use the %s trait instead.', BaseEntityRepository::class, EntityRepositoryHelperTrait::class), E_USER_DEPRECATED);

0 commit comments

Comments
 (0)