Completed
Pull Request — master (#30)
by Matthew
13:27 queued 10:46
created

DoctrineJobManagerTest::testResetStalledJobs()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 100
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 75
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Dtc\QueueBundle\Tests\Doctrine;
4
5
use Doctrine\ODM\MongoDB\DocumentManager;
6
use Doctrine\ORM\EntityManager;
7
use Dtc\QueueBundle\Doctrine\DoctrineJobManager;
8
use Dtc\QueueBundle\Doctrine\DtcQueueListener;
9
use Dtc\QueueBundle\Model\BaseJob;
10
use Dtc\QueueBundle\Model\Job;
11
use Dtc\QueueBundle\Model\RetryableJob;
12
use Dtc\QueueBundle\Tests\Manager\AutoRetryTrait;
13
use Dtc\QueueBundle\Tests\Manager\PriorityTestTrait;
14
use Dtc\QueueBundle\Model\StallableJob;
15
use Dtc\QueueBundle\Tests\FibonacciWorker;
16
use Dtc\QueueBundle\Tests\Manager\BaseJobManagerTest;
17
use Dtc\QueueBundle\ODM\JobManager;
18
use Dtc\QueueBundle\Tests\ORM\JobManagerTest;
19
use Symfony\Component\DependencyInjection\Container;
20
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
21
22
/**
23
 * @author David
24
 *
25
 * This test requires local mongodb running
26
 */
27
abstract class DoctrineJobManagerTest extends BaseJobManagerTest
28
{
29
    use PriorityTestTrait;
30
    use AutoRetryTrait;
31
32
    protected static $dtcQueueListener;
33
34
    /** @var DocumentManager|EntityManager */
35
    protected static $objectManager;
36
    protected static $objectName;
37
    protected static $archiveObjectName;
38
    protected static $runClass;
39
    protected static $runArchiveClass;
40
    protected static $jobTimingClass;
41
    protected static $jobManagerClass;
42
    protected static $runManagerClass;
43
    protected static $jobTimingManagerClass;
44
    public static $runManager;
45
46
    public static function setUpBeforeClass()
47
    {
48
        self::$jobTimingManager = new self::$jobTimingManagerClass(self::$objectManager, self::$jobTimingClass, true);
49
        self::$runManager = new self::$runManagerClass(self::$objectManager, self::$runClass, self::$runArchiveClass);
50
        self::$jobManager = new self::$jobManagerClass(self::$runManager, self::$jobTimingManager, self::$objectManager, self::$objectName, self::$archiveObjectName);
51
        self::$jobManager->setMaxPriority(255);
52
53
        self::assertEquals(255, self::$jobManager->getMaxPriority());
54
        self::assertEquals(JobManager::PRIORITY_DESC, self::$jobManager->getPriorityDirection());
55
        self::$jobManager->setPriorityDirection(JobManager::PRIORITY_ASC);
56
        self::assertEquals(JobManager::PRIORITY_ASC, self::$jobManager->getPriorityDirection());
57
        self::$jobManager->setPriorityDirection(JobManager::PRIORITY_DESC);
58
59
        /** @var DoctrineJobManager $jobManager */
60
        $jobManager = self::$jobManager;
61
62
        $parameters = new ParameterBag();
63
64
        $container = new Container($parameters);
65
        $container->set('dtc_queue.job_manager', $jobManager);
66
        $container->set('dtc_queue.run_manager', self::$runManager);
67
68
        self::$dtcQueueListener = new DtcQueueListener(self::$jobManager->getJobArchiveClass(), self::$runManager->getRunArchiveClass());
69
        self::$objectManager->getEventManager()->addEventListener('preUpdate', self::$dtcQueueListener);
70
        self::$objectManager->getEventManager()->addEventListener('prePersist', self::$dtcQueueListener);
71
        self::$objectManager->getEventManager()->addEventListener('preRemove', self::$dtcQueueListener);
72
73
        self::$worker = new FibonacciWorker();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Dtc\QueueBundle\Tests\FibonacciWorker() of type object<Dtc\QueueBundle\Tests\FibonacciWorker> is incompatible with the declared type object<Dtc\QueueBundle\Manager\Worker> of property $worker.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
74
75
        parent::setUpBeforeClass();
76
    }
77
78
    public static function tearDownAfterClass()
79
    {
80
        self::$objectManager->getEventManager()->removeEventListener('preUpdate', self::$dtcQueueListener);
81
        self::$objectManager->getEventManager()->removeEventListener('prePersist', self::$dtcQueueListener);
82
        self::$objectManager->getEventManager()->removeEventListener('preRemove', self::$dtcQueueListener);
83
        parent::tearDownAfterClass();
84
    }
85
86
    public function testOrdering()
87
    {
88
        // priority when at
89
        /** @var DoctrineJobManager $jobManager */
90
        $jobManager = self::$jobManager;
91
92
        $time1 = time() - 2;
93
        $dateTime1 = new \DateTime("@$time1");
94
95
        $time2 = time();
96
        $dateTime2 = new \DateTime("@$time2");
97
98
        /** @var Job $job */
99
        $job = new static::$jobClass(static::$worker, false, null);
100
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
101
        $job->setWhenAt($dateTime1);
102
        $job->setPriority(3);
103
        $id = $job->getId();
104
105
        $job2 = new static::$jobClass(static::$worker, false, null);
106
        $job2->setPriority(1);
107
        $job2->setWhenAt($dateTime2);
108
        $job2->fibonacci(1);
109
        $id2 = $job2->getId();
110
111
        $job3 = new static::$jobClass(static::$worker, false, null);
112
        $job3->setPriority(1);
113
        $job3->setWhenAt($dateTime1);
114
        $job3->fibonacci(1);
115
        $id3 = $job3->getId();
116
117
        $job4 = new static::$jobClass(static::$worker, false, null);
118
        $job4->setPriority(1);
119
        $job4->setWhenAt($dateTime2);
120
        $job4->fibonacci(1);
121
        $id4 = $job4->getId();
122
123
        $nextJob = $jobManager->getJob();
124
        static::assertEquals($id3, $nextJob->getId());
125
        $nextNextJob = $jobManager->getJob();
126
        $nextNextId = $nextNextJob->getId();
127
        static::assertTrue($id4 == $nextNextId || $id2 == $nextNextId, "$nextNextId not equals $id4 or $id2, could be $id or $id3");
128
129
        static::assertNotNull($jobManager->getJob());
130
        static::assertNotNull($jobManager->getJob());
131
132
        // non-priority when at
133
        $time1 = time() - 2;
134
        $dateTime1 = new \DateTime("@$time1");
135
136
        $time2 = time();
137
        $dateTime2 = new \DateTime("@$time2");
138
139
        /** @var Job $job */
140
        $job = new static::$jobClass(static::$worker, false, null);
141
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
142
        $job->setWhenAt($dateTime1);
143
        $job->setPriority(3);
144
        $id = $job->getId();
145
146
        $job2 = new static::$jobClass(static::$worker, false, null);
147
        $job2->setPriority(1);
148
        $job2->setWhenAt($dateTime2);
149
        $job2->fibonacci(1);
150
151
        $job3 = new static::$jobClass(static::$worker, false, null);
152
        $job3->setPriority(1);
153
        $job3->setWhenAt($dateTime2);
154
        $job3->fibonacci(1);
155
156
        $job4 = new static::$jobClass(static::$worker, false, null);
157
        $job4->setPriority(1);
158
        $job4->setWhenAt($dateTime2);
159
        $job4->fibonacci(1);
160
161
        $nextJob = $jobManager->getJob(null, null, false);
162
        static::assertEquals($id, $nextJob->getId());
163
        static::assertNotNull($jobManager->getJob());
164
        static::assertNotNull($jobManager->getJob());
165
        static::assertNotNull($jobManager->getJob());
166
    }
167
168
    public function getJobBy()
169
    {
170
        /** @var DoctrineJobManager $jobManager */
171
        $jobManager = self::$jobManager;
172
173
        /** @var Job $job */
174
        $job = new static::$jobClass(static::$worker, false, null);
175
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
176
        $id = $job->getId();
177
        $nextJob = $jobManager->getJob('fibonacci', null);
178
        static::assertNotNull($nextJob);
179
        static::assertEquals($id, $nextJob->getId());
180
181
        /** @var Job $job */
182
        $job = new static::$jobClass(static::$worker, false, null);
183
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
184
        $id = $job->getId();
185
        $nextJob = $jobManager->getJob('fibonacci', 'fibonacci');
186
        static::assertNotNull($nextJob);
187
        static::assertEquals($id, $nextJob->getId());
188
189
        /** @var Job $job */
190
        $job = new static::$jobClass(static::$worker, false, null);
191
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
192
        $id = $job->getId();
193
        $nextJob = $jobManager->getJob(null, 'fibonacci');
194
        static::assertNotNull($nextJob);
195
        static::assertEquals($id, $nextJob->getId());
196
197
        /** @var Job $job */
198
        $job = new static::$jobClass(static::$worker, false, null);
199
        $job->fibonacci(1);
0 ignored issues
show
Documentation Bug introduced by
The method fibonacci does not exist on object<Dtc\QueueBundle\Model\Job>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
200
        $id = $job->getId();
201
        $nextJob = $jobManager->getJob(null, 'fibonaccia');
202
        static::assertNull($nextJob);
203
        $nextJob = $jobManager->getJob('fibonacci', 'fibonaccia');
204
        static::assertNull($nextJob);
205
        $nextJob = $jobManager->getJob('fibonaccii', 'fibonacci');
206
        static::assertNull($nextJob);
207
        $nextJob = $jobManager->getJob();
208
        static::assertNotNull($nextJob);
209
        static::assertEquals($id, $nextJob->getId());
210
    }
211
212
    public function testDeleteJob()
213
    {
214
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
215
        $jobManager = self::$jobManager;
216
217
        /** @var Job $job */
218
        $job = $this->getJob();
219
        $id = $job->getId();
220
        $jobManager->deleteJob($job);
221
222
        $nextJob = $jobManager->getJob(null, null, true, 123);
223
        self::assertNull($nextJob, "Shouldn't be any jobs left in queue");
224
225
        $archiveObjectName = $jobManager->getJobArchiveClass();
226
227
        self::assertNotNull($id);
228
        $archiveRepository = $jobManager->getObjectManager()->getRepository($archiveObjectName);
229
        $result = $archiveRepository->find($id);
230
        self::assertNotNull($result);
231
        self::assertEquals($id, $result->getId());
232
    }
233
234
    public function testCountLiveJobs()
235
    {
236
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
237
        $jobManager = self::$jobManager;
238
239
        while ($job = $jobManager->getJob()) {
240
            $jobManager->deleteJob($job);
241
        }
242
243
        $this->getJob();
244
245
        $count = $jobManager->countLiveJobs();
246
        self::assertEquals(1, $count);
247
248
        $this->getJob();
249
250
        $count = $jobManager->countLiveJobs();
251
        self::assertEquals(2, $count);
252
253
        $this->getJob();
254
255
        $count = $jobManager->countLiveJobs();
256
        self::assertEquals(3, $count);
257
258
        $count = $jobManager->countLiveJobs('asdf');
259
        self::assertEquals(0, $count);
260
261
        $count = $jobManager->countLiveJobs('fibonacci');
262
        self::assertEquals(3, $count);
263
264
        $count = $jobManager->countLiveJobs('fibonacci', 'test');
265
        self::assertEquals(0, $count);
266
267
        $count = $jobManager->countLiveJobs('fibonacci', 'fibonacci');
268
        self::assertEquals(3, $count);
269
270
        while ($job = $jobManager->getJob()) {
271
            $jobManager->deleteJob($job);
272
        }
273
    }
274
275
    public function testArchiveAllJobs()
276
    {
277
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
278
        $jobManager = self::$jobManager;
279
280
        while ($job = $jobManager->getJob()) {
281
            $jobManager->deleteJob($job);
282
        }
283
284
        $this->getJob();
285
286
        $count = $jobManager->countLiveJobs();
287
        $archiveCount = $this->runCountQuery($jobManager->getJobArchiveClass());
288
        self::assertEquals(1, $count);
289
        $allCount = $this->runCountQuery($jobManager->getJobClass());
290
        $counter = 0;
291
        $countJobs = function($count) use (&$counter) {
292
            $counter += $count;
293
        };
294
        $jobManager->archiveAllJobs(null, null, $countJobs);
295
        self::assertEquals(0, $jobManager->countLiveJobs());
296
        self::assertEquals($allCount - 1, $this->runCountQuery($jobManager->getJobClass()));
297
        self::assertEquals($archiveCount + 1, $this->runCountQuery($jobManager->getJobArchiveClass()));
298
        self::assertEquals(1, $counter);
299
300
        $this->getJob();
301
        $this->getJob();
302
303
        $count = $jobManager->countLiveJobs();
304
        self::assertEquals(2, $count);
305
        $archiveCount = $this->runCountQuery($jobManager->getJobArchiveClass());
306
        $counter = 0;
307
        $jobManager->archiveAllJobs('fibonacci', null, $countJobs);
308
        self::assertEquals(0, $jobManager->countLiveJobs());
309
        self::assertEquals(2, $counter);
310
        self::assertEquals($archiveCount + 2, $this->runCountQuery($jobManager->getJobArchiveClass()));
311
312
        $this->getJob();
313
        $this->getJob();
314
315
        $count = $jobManager->countLiveJobs();
316
        self::assertEquals(2, $count);
317
318
        $jobManager->archiveAllJobs('fibonacc', null, $countJobs);
319
        self::assertEquals(2, $jobManager->countLiveJobs());
320
321
        $jobManager->archiveAllJobs('fibonacci', 'fibo', $countJobs);
322
        self::assertEquals(2, $jobManager->countLiveJobs());
323
324
        $jobManager->archiveAllJobs('fibonacci', 'fibonacci', $countJobs);
325
        self::assertEquals(0, $jobManager->countLiveJobs());
326
327
        while ($job = $jobManager->getJob()) {
328
            $jobManager->deleteJob($job);
329
        }
330
    }
331
332
    abstract protected function runCountQuery($class);
333
334
    public function testResetExceptionJobs()
335
    {
336
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
337
        $jobManager = self::$jobManager;
338
339
        $id = $this->createExceptionJob();
340
        $archiveObjectName = $jobManager->getJobArchiveClass();
341
        $objectManager = $jobManager->getObjectManager();
342
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
343
        $result = $archiveRepository->find($id);
344
        self::assertNotNull($result);
345
        self::assertEquals(BaseJob::STATUS_EXCEPTION, $result->getStatus());
346
        if ($objectManager instanceof EntityManager) {
347
            JobManagerTest::createObjectManager();
348
            $jobManager = new self::$jobManagerClass(self::$runManager, self::$jobTimingManager, self::$objectManager, self::$objectName, self::$archiveObjectName);
349
            $jobManager->getObjectManager()->clear();
350
            $objectManager = $jobManager->getObjectManager();
351
        }
352
353
        $count = $jobManager->resetExceptionJobs();
354
355
        self::assertEquals(1, $count);
356
        $repository = $jobManager->getRepository();
357
        $job = $repository->find($id);
358
359
        self::assertNotNull($job);
360
        self::assertEquals(BaseJob::STATUS_NEW, $job->getStatus());
361
        self::assertNull($job->getStartedAt());
362
        self::assertNull($job->getFinishedAt());
363
        self::assertNull($job->getElapsed());
364
        self::assertNull($job->getMessage());
365
366
        $objectManager->remove($job);
367
        $objectManager->flush();
368
369
        $id = $this->createExceptionJob();
370
        $archiveObjectName = $jobManager->getJobArchiveClass();
371
        $objectManager = $jobManager->getObjectManager();
372
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
373
        $result = $archiveRepository->find($id);
374
        $result->setMaxRetries(10);
375
        $result->setRetries(10);
376
        $objectManager->persist($result);
377
        $objectManager->flush();
378
        $count = $jobManager->resetExceptionJobs();
379
        self::assertEquals(0, $count);
380
        $job = $repository->find($id);
381
        self::assertNull($job);
382
        $job = $archiveRepository->find($id);
383
        self::assertNotNull($job);
384
        $objectManager->remove($job);
385
        $objectManager->flush();
386
    }
387
388
    protected function createExceptionJob()
389
    {
390
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
391
        $jobManager = self::$jobManager;
392
393
        /** @var Job $job */
394
        $job = $this->getJob();
395
        $id = $job->getId();
396
        $jobManager->deleteJob($job);
397
398
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
399
        $archiveObjectName = $jobManager->getJobArchiveClass();
400
401
        $objectManager = $jobManager->getObjectManager();
402
403
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
404
        $result = $archiveRepository->find($id);
405
        self::assertNotNull($result);
406
        self::assertEquals($id, $result->getId());
407
408
        $result->setStatus(BaseJob::STATUS_EXCEPTION);
409
        $result->setStartedAt(new \DateTime());
410
        $result->setFinishedAt(new \DateTime());
411
        $result->setElapsed(12345);
412
        $result->setMessage('soomething');
413
        $objectManager->persist($result);
414
        $objectManager->flush();
415
416
        return $id;
417
    }
418
419
    /**
420
     * @param bool $endRun
421
     * @param bool $setId
422
     *
423
     * @return mixed
424
     */
425
    public function createStalledJob($endRun, $setId)
426
    {
427
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
428
        $jobManager = self::$jobManager;
429
430
        $job = new self::$jobClass(self::$worker, false, null);
431
        $job->fibonacci(1);
432
        self::assertNotNull($job->getId(), 'Job id should be generated');
433
        $job->setStatus(BaseJob::STATUS_RUNNING);
434
        $time = time();
435
        $date = new \DateTime("@$time");
436
        $job->setStartedAt($date);
437
        $id = $job->getId();
438
        $job = $jobManager->getRepository()->find($id);
439
440
        self::assertNotNull($job);
441
442
        $runClass = self::$runManager->getRunClass();
443
444
        $objectManager = $jobManager->getObjectManager();
445
        $run = new $runClass();
446
        $run->setLastHeartbeatAt(new \DateTime());
447
        if ($setId) {
448
            $run->setCurrentJobId($job->getId());
449
        }
450
        $objectManager->persist($run);
451
        $objectManager->flush();
452
        $runId = $run->getId();
453
        self::assertNotNull($runId);
454
        $job->setRunId($runId);
455
        $objectManager->persist($job);
456
        $objectManager->flush();
457
        if ($endRun) {
458
            $objectManager->remove($run);
459
            $objectManager->flush();
460
        }
461
        $id = $job->getId();
462
        $job = $jobManager->getRepository()->find($id);
463
464
        self::assertNotNull($job);
465
466
        if ($endRun) {
467
            $archivedRun = $objectManager->getRepository(self::$runManager->getRunArchiveClass())->find($runId);
468
469
            $minusTime = $time - (DoctrineJobManager::STALLED_SECONDS + 1);
470
            $archivedRun->setEndedAt(new \DateTime("@$minusTime"));
471
472
            $objectManager->persist($archivedRun);
473
            $objectManager->flush();
474
        }
475
        $id = $job->getId();
476
477
        return $id;
478
    }
479
480
    public function testResetStalledJobs()
481
    {
482
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
483
        $jobManager = self::$jobManager;
484
        $id = $this->createStalledJob(true, false);
485
486
        $objectManager = $jobManager->getObjectManager();
487
        $count = $jobManager->resetStalledJobs();
488
        self::assertEquals(1, $count);
489
490
        $job = $jobManager->getRepository()->find($id);
491
492
        self::assertNotNull($job);
493
        self::assertEquals(BaseJob::STATUS_NEW, $job->getStatus());
494
        self::assertNull($job->getStartedAt());
495
        self::assertNull($job->getFinishedAt());
496
        self::assertNull($job->getElapsed());
497
        self::assertNull($job->getMessage());
498
        self::assertEquals(1, $job->getStalls());
499
500
        $objectManager->remove($job);
501
        $objectManager->flush();
502
503
        $jobManager = self::$jobManager;
504
        $id = $this->createStalledJob(true, true);
505
506
        $objectManager = $jobManager->getObjectManager();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
507
        $count = $jobManager->resetStalledJobs();
508
        self::assertEquals(1, $count);
509
510
        $job = $jobManager->getRepository()->find($id);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
511
512
        self::assertNotNull($job);
513
        $objectManager->remove($job);
514
        $objectManager->flush();
515
516
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
517
        $id = $this->createStalledJob(false, false);
518
519
        $objectManager = $jobManager->getObjectManager();
520
        $count = $jobManager->resetStalledJobs();
521
        self::assertEquals(1, $count);
522
523
        $job = $jobManager->getRepository()->find($id);
524
525
        self::assertNotNull($job);
526
        self::assertEquals(BaseJob::STATUS_NEW, $job->getStatus());
527
        self::assertNull($job->getStartedAt());
528
        self::assertNull($job->getFinishedAt());
529
        self::assertNull($job->getElapsed());
530
        self::assertNull($job->getMessage());
531
        self::assertEquals(1, $job->getStalls());
532
533
        $objectManager->remove($job);
534
        $objectManager->flush();
535
536
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
537
        $jobManager = self::$jobManager;
538
        $id = $this->createStalledJob(false, true);
539
540
        $job = $jobManager->getRepository()->find($id);
541
        $objectManager = $jobManager->getObjectManager();
542
        $count = $jobManager->resetStalledJobs();
543
        self::assertEquals(0, $count);
544
545
        $objectManager->remove($job);
546
        $objectManager->flush();
547
548
        $id = $this->createStalledJob(true, false);
549
        $job = $jobManager->getRepository()->find($id);
550
        $job->setMaxRetries(10);
551
        $job->setRetries(10);
552
        $objectManager->persist($job);
553
        $objectManager->flush();
554
555
        $count = $jobManager->resetStalledJobs();
556
        self::assertEquals(0, $count);
557
        $job = $jobManager->getRepository()->find($id);
558
        self::assertNull($job);
559
        $job = $objectManager->getRepository($jobManager->getJobArchiveClass())->find($id);
560
        self::assertNotNull($job);
561
        $objectManager->remove($job);
562
        $objectManager->flush();
563
564
        $id = $this->createStalledJob(true, false);
565
        $job = $jobManager->getRepository()->find($id);
566
        $job->setMaxStalls(10);
567
        $job->setStalls(10);
568
        $objectManager->persist($job);
569
        $objectManager->flush();
570
571
        $count = $jobManager->resetStalledJobs();
572
        self::assertEquals(0, $count);
573
        $job = $jobManager->getRepository()->find($id);
574
        self::assertNull($job);
575
        $job = $objectManager->getRepository($jobManager->getJobArchiveClass())->find($id);
576
        self::assertNotNull($job);
577
        $objectManager->remove($job);
578
        $objectManager->flush();
579
    }
580
581
    public function testpruneExceptionJobs()
582
    {
583
        $job = $this->getJob();
584
        $id = $job->getId();
585
586
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
587
        $jobManager = self::$jobManager;
588
        $jobManager->deleteJob($job);
589
        $archiveObjectName = $jobManager->getJobArchiveClass();
590
591
        $objectManager = $jobManager->getObjectManager();
592
593
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
594
        $result = $archiveRepository->find($id);
595
        self::assertNotNull($result);
596
        self::assertEquals($id, $result->getId());
597
598
        $result->setStatus(BaseJob::STATUS_EXCEPTION);
599
        $result->setStartedAt(new \DateTime());
600
        $result->setFinishedAt(new \DateTime());
601
        $result->setElapsed(12345);
602
        $result->setMessage('soomething');
603
        $objectManager->persist($result);
604
        $objectManager->flush();
605
606
        $count = $jobManager->pruneExceptionJobs('asdf');
607
        self::assertEquals(0, $count);
608
        $count = $jobManager->pruneExceptionJobs(null, 'asdf');
609
        self::assertEquals(0, $count);
610
        $count = $jobManager->pruneExceptionJobs('fibonacci', 'asdf');
611
        self::assertEquals(0, $count);
612
        $count = $jobManager->pruneExceptionJobs('fibonacci', 'asdf');
613
        self::assertEquals(0, $count);
614
        $count = $jobManager->pruneExceptionJobs('fibonacci', 'fibonacci');
615
        self::assertEquals(1, $count);
616
        $repository = $jobManager->getRepository();
617
        $job = $repository->find($id);
618
        $objectManager->clear();
619
        self::assertNull($job);
620
        $archiveJob = $archiveRepository->find($id);
621
        self::assertNull($archiveJob);
622
623
        $job = $this->getJob();
624
        $id = $job->getId();
625
        $objectManager->remove($job);
626
        $objectManager->flush();
627
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
628
        $jobManager = self::$jobManager;
629
        $archiveObjectName = $jobManager->getJobArchiveClass();
630
631
        $objectManager = $jobManager->getObjectManager();
632
633
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
634
        $result = $archiveRepository->find($id);
635
        self::assertNotNull($result);
636
        self::assertEquals($id, $result->getId());
637
638
        $result->setStatus(BaseJob::STATUS_EXCEPTION);
639
        $result->setStartedAt(new \DateTime());
640
        $result->setFinishedAt(new \DateTime());
641
        $result->setElapsed(12345);
642
        $result->setMessage('soomething');
643
        $objectManager->persist($result);
644
        $objectManager->flush();
645
646
        $job = $this->getJob();
647
        $id = $job->getId();
648
        $objectManager->remove($job);
649
        $objectManager->flush();
650
651
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
652
        $jobManager = self::$jobManager;
653
        $archiveObjectName = $jobManager->getJobArchiveClass();
654
        $objectManager = $jobManager->getObjectManager();
655
656
        $archiveRepository = $objectManager->getRepository($archiveObjectName);
657
        $result = $archiveRepository->find($id);
658
        self::assertNotNull($result);
659
        self::assertEquals($id, $result->getId());
660
661
        $result->setStatus(BaseJob::STATUS_EXCEPTION);
662
        $result->setStartedAt(new \DateTime());
663
        $result->setFinishedAt(new \DateTime());
664
        $result->setElapsed(12345);
665
        $result->setMessage('soomething');
666
        $objectManager->persist($result);
667
        $objectManager->flush();
668
        $count = $jobManager->pruneExceptionJobs();
669
        self::assertEquals(2, $count);
670
    }
671
672
    public function testPruneStalledJobs()
673
    {
674
        static::setUpBeforeClass();
675
676
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
677
        $jobManager = self::$jobManager;
678
679
        $job = new self::$jobClass(self::$worker, false, null);
680
        $job->fibonacci(1);
681
        self::assertNotNull($job->getId(), 'Job id should be generated');
682
        $job->setStatus(BaseJob::STATUS_RUNNING);
683
        $time = time();
684
        $date = new \DateTime("@$time");
685
        $job->setStartedAt($date);
686
        $id = $job->getId();
687
        $job = $jobManager->getRepository()->find($id);
688
689
        self::assertNotNull($job);
690
691
        $runClass = self::$runManager->getRunClass();
692
693
        $objectManager = $jobManager->getObjectManager();
694
        $run = new $runClass();
695
        $run->setLastHeartbeatAt(new \DateTime());
696
        $objectManager->persist($run);
697
        $objectManager->flush();
698
        $runId = $run->getId();
699
        self::assertNotNull($runId);
700
        $job->setRunId($runId);
701
        $objectManager->persist($job);
702
        $objectManager->flush();
703
        $objectManager->remove($run);
704
        $objectManager->flush();
705
        $id = $job->getId();
706
        $job = $jobManager->getRepository()->find($id);
707
708
        self::assertNotNull($job);
709
710
        $archivedRun = $objectManager->getRepository(self::$runManager->getRunArchiveClass())->find($runId);
711
712
        $minusTime = $time - (DoctrineJobManager::STALLED_SECONDS + 1);
713
        $archivedRun->setEndedAt(new \DateTime("@$minusTime"));
714
715
        $objectManager->persist($archivedRun);
716
        $objectManager->flush();
717
718
        $count = $jobManager->pruneStalledJobs('asdf');
719
        self::assertEquals(0, $count);
720
        $count = $jobManager->pruneStalledJobs(null, 'asdf');
721
        self::assertEquals(0, $count);
722
        $count = $jobManager->pruneStalledJobs('fibonacci', 'asdf');
723
        self::assertEquals(0, $count);
724
        $count = $jobManager->pruneStalledJobs('fibonacci', 'fibonacci');
725
        self::assertEquals(1, $count);
726
727
        $job = $jobManager->getRepository()->find($id);
728
729
        self::assertNull($job);
730
731
        $archivedJob = $jobManager->getObjectManager()->getRepository($jobManager->getJobArchiveClass())->find($id);
732
733
        self::assertNotNull($archivedJob);
734
        self::assertEquals(StallableJob::STATUS_STALLED, $archivedJob->getStatus());
735
        self::assertEquals(1, $archivedJob->getStalls());
736
        $objectManager->remove($archivedJob);
737
        $objectManager->flush();
738
739
        // multiple
740
741
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
742
        $jobManager = self::$jobManager;
743
744
        $job = new self::$jobClass(self::$worker, false, null);
745
        $job->fibonacci(1);
746
        self::assertNotNull($job->getId(), 'Job id should be generated');
747
        $job->setStatus(BaseJob::STATUS_RUNNING);
748
        $time = time();
749
        $date = new \DateTime("@$time");
750
        $job->setStartedAt($date);
751
        $id = $job->getId();
752
        $job = $jobManager->getRepository()->find($id);
753
754
        self::assertNotNull($job);
755
756
        $runClass = self::$runManager->getRunClass();
757
758
        $objectManager = $jobManager->getObjectManager();
759
        $run = new $runClass();
760
        $run->setLastHeartbeatAt(new \DateTime());
761
        $objectManager->persist($run);
762
        $objectManager->flush();
763
        $runId = $run->getId();
764
        self::assertNotNull($runId);
765
        $job->setRunId($runId);
766
        $objectManager->persist($job);
767
        $objectManager->flush();
768
        $objectManager->remove($run);
769
        $objectManager->flush();
770
        $id = $job->getId();
771
        $job = $jobManager->getRepository()->find($id);
772
773
        self::assertNotNull($job);
774
775
        $archivedRun = $objectManager->getRepository(self::$runManager->getRunArchiveClass())->find($runId);
776
777
        $minusTime = $time - (DoctrineJobManager::STALLED_SECONDS + 1);
778
        $archivedRun->setEndedAt(new \DateTime("@$minusTime"));
779
780
        $objectManager->persist($archivedRun);
781
        $objectManager->flush();
782
783
        $job = new self::$jobClass(self::$worker, false, null);
784
        $job->fibonacci(1);
785
        self::assertNotNull($job->getId(), 'Job id should be generated');
786
        $job->setStatus(BaseJob::STATUS_RUNNING);
787
        $time = time();
788
        $date = new \DateTime("@$time");
789
        $job->setStartedAt($date);
790
        $id = $job->getId();
791
        $job = $jobManager->getRepository()->find($id);
792
793
        self::assertNotNull($job);
794
795
        $runClass = self::$runManager->getRunClass();
796
797
        $objectManager = $jobManager->getObjectManager();
798
        $run = new $runClass();
799
        $run->setLastHeartbeatAt(new \DateTime());
800
        $objectManager->persist($run);
801
        $objectManager->flush();
802
        $runId = $run->getId();
803
        self::assertNotNull($runId);
804
        $job->setRunId($runId);
805
        $objectManager->persist($job);
806
        $objectManager->flush();
807
        $objectManager->remove($run);
808
        $objectManager->flush();
809
        $id = $job->getId();
810
        $job = $jobManager->getRepository()->find($id);
811
812
        self::assertNotNull($job);
813
814
        $archivedRun = $objectManager->getRepository(self::$runManager->getRunArchiveClass())->find($runId);
815
816
        $minusTime = $time - (DoctrineJobManager::STALLED_SECONDS + 1);
817
        $archivedRun->setEndedAt(new \DateTime("@$minusTime"));
818
819
        $objectManager->persist($archivedRun);
820
        $objectManager->flush();
821
        $count = $jobManager->pruneStalledJobs();
822
        self::assertEquals(2, $count);
823
    }
824
825
    public function testBatchJobs()
826
    {
827
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
828
        foreach ($jobs as $job) {
829
            self::$jobManager->getObjectManager()->remove($job);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
830
        }
831
        self::$jobManager->getObjectManager()->flush();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
832
        self::$jobManager->getObjectManager()->clear();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
833
834
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
835
        $worker = self::$worker;
836
        $job1 = $worker->later()->fibonacci(1);
837
        $job2 = $worker->batchLater()->fibonacci(1);
838
        self::assertEquals($job1, $job2);
839
840
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
841
        self::assertCount(1, $jobs);
842
        self::assertEquals($job1, $jobs[0]);
843
        self::assertNull($jobs[0]->getPriority());
844
        self::$jobManager->getObjectManager()->remove($jobs[0]);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
845
        self::$jobManager->getObjectManager()->flush();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
846
        self::$jobManager->getObjectManager()->clear();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
847
848
        $job1 = $worker->later()->fibonacci(1);
849
        self::assertNull($job1->getPriority());
850
        $job2 = $worker->batchLater()->setPriority(3)->fibonacci(1);
851
        self::assertEquals($job1, $job2);
852
        self::assertNotNull($job2->getPriority());
853
854
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
855
        self::assertCount(1, $jobs);
856
        self::assertEquals($job1, $jobs[0]);
857
        self::assertNotNull($jobs[0]->getPriority());
858
859
        // Not
860
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
861
        foreach ($jobs as $job) {
862
            self::$jobManager->getObjectManager()->remove($job);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
863
        }
864
        self::$jobManager->getObjectManager()->remove($jobs[0]);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
865
        self::$jobManager->getObjectManager()->flush();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
866
        self::$jobManager->getObjectManager()->clear();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
867
868
        $job1 = $worker->later(100)->fibonacci(1);
869
870
        $time1 = new \DateTime('@'.time());
871
        $job2 = $worker->batchLater(0)->fibonacci(1);
872
        $time2 = new \DateTime();
873
874
        self::assertEquals($job1, $job2);
875
        self::assertGreaterThanOrEqual($time1, $job2->getWhenAt());
876
        self::assertLessThanOrEqual($time2, $job2->getWhenAt());
877
878
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
879
        self::assertCount(1, $jobs);
880
        self::assertEquals($job1, $jobs[0]);
881
        self::assertGreaterThanOrEqual($time1, $jobs[0]->getWhenAt());
882
        self::assertLessThanOrEqual($time2, $jobs[0]->getWhenAt());
883
        self::$jobManager->getObjectManager()->remove($jobs[0]);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
884
        self::$jobManager->getObjectManager()->flush();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
885
        self::$jobManager->getObjectManager()->clear();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
886
887
        $job1 = $worker->later(100)->setPriority(3)->fibonacci(1);
888
        $priority1 = $job1->getPriority();
889
        $time1 = new \DateTime('@'.time());
890
        $job2 = $worker->batchLater(0)->setPriority(1)->fibonacci(1);
891
        $time2 = new \DateTime();
892
        self::assertEquals($job1, $job2);
893
        self::assertNotEquals($priority1, $job2->getPriority());
894
895
        self::assertEquals($job1, $job2);
896
        self::assertGreaterThanOrEqual($time1, $job2->getWhenAt());
897
        self::assertLessThanOrEqual($time2, $job2->getWhenAt());
898
    }
899
900
    public function testPruneExpiredJobs()
901
    {
902
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
903
        $jobManager = self::$jobManager;
904
        $objectManager = $jobManager->getObjectManager();
905
906
        $job = new self::$jobClass(self::$worker, false, null);
907
        $job->fibonacci(1);
908
        self::assertNotNull($job->getId(), 'Job id should be generated');
909
        $time = time() - 1;
910
        $date = new \DateTime("@$time");
911
        $job->setExpiresAt($date);
912
        $objectManager->persist($job);
913
        $objectManager->flush();
914
915
        $count = $jobManager->pruneExpiredJobs('asdf');
916
        self::assertEquals(0, $count);
917
        $count = $jobManager->pruneExpiredJobs(null, 'asdf');
918
        self::assertEquals(0, $count);
919
        $count = $jobManager->pruneExpiredJobs(null, 'fibonacci');
920
        self::assertEquals(1, $count);
921
922
        $job = new self::$jobClass(self::$worker, false, null);
923
        $job->fibonacci(1);
924
        self::assertNotNull($job->getId(), 'Job id should be generated');
925
        $time = time() - 1;
926
        $date = new \DateTime("@$time");
927
        $job->setExpiresAt($date);
928
        $objectManager->persist($job);
929
        $objectManager->flush();
930
931
        $job = new self::$jobClass(self::$worker, false, null);
932
        $job->fibonacci(1);
933
        self::assertNotNull($job->getId(), 'Job id should be generated');
934
        $time = time() - 1;
935
        $date = new \DateTime("@$time");
936
        $job->setExpiresAt($date);
937
        $objectManager->persist($job);
938
        $objectManager->flush();
939
940
        $count = $jobManager->pruneExpiredJobs(null, 'fibonacci');
941
        self::assertEquals(2, $count);
942
943
        $job = new self::$jobClass(self::$worker, false, null);
944
        $job->fibonacci(1);
945
        self::assertNotNull($job->getId(), 'Job id should be generated');
946
        $time = time() - 1;
947
        $date = new \DateTime("@$time");
948
        $job->setExpiresAt($date);
949
        $objectManager->persist($job);
950
        $objectManager->flush();
951
952
        $job = new self::$jobClass(self::$worker, false, null);
953
        $job->fibonacci(1);
954
        self::assertNotNull($job->getId(), 'Job id should be generated');
955
        $time = time() - 1;
956
        $date = new \DateTime("@$time");
957
        $job->setExpiresAt($date);
958
        $objectManager->persist($job);
959
        $objectManager->flush();
960
961
        $count = $jobManager->pruneExpiredJobs('fibonacci', 'fibonacci');
962
        self::assertEquals(2, $count);
963
964
        $job = new self::$jobClass(self::$worker, false, null);
965
        $job->fibonacci(1);
966
        self::assertNotNull($job->getId(), 'Job id should be generated');
967
        $time = time() - 1;
968
        $date = new \DateTime("@$time");
969
        $job->setExpiresAt($date);
970
        $objectManager->persist($job);
971
        $objectManager->flush();
972
973
        $job = new self::$jobClass(self::$worker, false, null);
974
        $job->fibonacci(1);
975
        self::assertNotNull($job->getId(), 'Job id should be generated');
976
        $time = time() - 1;
977
        $date = new \DateTime("@$time");
978
        $job->setExpiresAt($date);
979
        $objectManager->persist($job);
980
        $objectManager->flush();
981
982
        $count = $jobManager->pruneExpiredJobs('fibonacci');
983
        self::assertEquals(2, $count);
984
985
        $job = new self::$jobClass(self::$worker, false, null);
986
        $job->fibonacci(1);
987
        self::assertNotNull($job->getId(), 'Job id should be generated');
988
        $time = time() - 1;
989
        $date = new \DateTime("@$time");
990
        $job->setExpiresAt($date);
991
        $objectManager->persist($job);
992
        $objectManager->flush();
993
994
        $jobId1 = $job->getId();
995
996
        $job = new self::$jobClass(self::$worker, false, null);
997
        $job->fibonacci(1);
998
        self::assertNotNull($job->getId(), 'Job id should be generated');
999
        $time = time() - 1;
1000
        $date = new \DateTime("@$time");
1001
        $job->setExpiresAt($date);
1002
        $objectManager->persist($job);
1003
        $objectManager->flush();
1004
1005
        $jobId2 = $job->getId();
1006
1007
        $count = $jobManager->pruneExpiredJobs();
1008
        self::assertEquals(2, $count);
1009
1010
        $archiveRepository = $jobManager->getObjectManager()->getRepository($jobManager->getJobArchiveClass());
1011
1012
        $job = $archiveRepository->find($jobId1);
1013
        self::assertNotNull($job);
1014
        self::assertEquals(Job::STATUS_EXPIRED, $job->getStatus());
1015
1016
        $job = $archiveRepository->find($jobId2);
1017
        self::assertNotNull($job);
1018
        self::assertEquals(Job::STATUS_EXPIRED, $job->getStatus());
1019
    }
1020
1021
    public function testPruneArchivedJobs()
1022
    {
1023
        /** @var JobManager|\Dtc\QueueBundle\ORM\JobManager $jobManager */
1024
        $jobManager = self::$jobManager;
1025
        $objectManager = $jobManager->getObjectManager();
1026
        $jobArchiveClass = $jobManager->getJobArchiveClass();
1027
        $jobArchiveRepository = $objectManager->getRepository($jobArchiveClass);
1028
1029
        self::$objectManager->getEventManager()->removeEventListener('preUpdate', self::$dtcQueueListener);
1030
1031
        $job = new self::$jobClass(self::$worker, false, null);
1032
        $job->fibonacci(1);
1033
        $id = $job->getId();
1034
        $objectManager->remove($job);
1035
        $objectManager->flush();
1036
1037
        $jobArchive = $jobArchiveRepository->find($id);
1038
        self::assertNotNull($jobArchive);
1039
        $time = time() - 86401;
1040
        $jobArchive->setUpdatedAt(new \DateTime("@$time"));
1041
        $objectManager->persist($jobArchive);
1042
        $objectManager->flush();
1043
1044
        $older = $time + 1;
1045
        $count = $jobManager->pruneArchivedJobs(new \DateTime("@$time"));
1046
        self::assertEquals(0, $count);
1047
        $count = $jobManager->pruneArchivedJobs(new \DateTime("@$older"));
1048
        self::assertEquals(1, $count);
1049
1050
        $job = new self::$jobClass(self::$worker, false, null);
1051
        $job->fibonacci(1);
1052
        $id = $job->getId();
1053
        $objectManager->remove($job);
1054
        $objectManager->flush();
1055
1056
        $jobArchive = $jobArchiveRepository->find($id);
1057
        self::assertNotNull($jobArchive);
1058
        $time = time() - 86401;
1059
        $jobArchive->setUpdatedAt(new \DateTime("@$time"));
1060
        $objectManager->persist($jobArchive);
1061
        $objectManager->flush();
1062
1063
        $job = new self::$jobClass(self::$worker, false, null);
1064
        $job->fibonacci(1);
1065
        $id = $job->getId();
1066
        $objectManager->remove($job);
1067
        $objectManager->flush();
1068
1069
        $jobArchive = $jobArchiveRepository->find($id);
1070
        self::assertNotNull($jobArchive);
1071
        $jobArchive->setUpdatedAt(new \DateTime("@$time"));
1072
        $objectManager->persist($jobArchive);
1073
        $objectManager->flush();
1074
        $older = $time + 1;
1075
        $count = $jobManager->pruneArchivedJobs(new \DateTime("@$time"));
1076
        self::assertEquals(0, $count);
1077
        $count = $jobManager->pruneArchivedJobs(new \DateTime("@$older"));
1078
        self::assertEquals(2, $count);
1079
1080
        self::$objectManager->getEventManager()->addEventListener('preUpdate', self::$dtcQueueListener);
1081
    }
1082
1083
    public function testPerformance()
1084
    {
1085
        $jobs = self::$jobManager->getRepository()->findAll();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getRepository() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1086
        foreach ($jobs as $job) {
1087
            self::$jobManager->getObjectManager()->remove($job);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1088
        }
1089
        self::$jobManager->getObjectManager()->flush();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1090
1091
        self::$jobManager->getObjectManager()->clear();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1092
        parent::testPerformance();
1093
    }
1094
1095
    protected function getBaseStatus()
1096
    {
1097
        /** @var DoctrineJobManager $jobManager */
1098
        $jobManager = self::$jobManager;
1099
        $job = new self::$jobClass(self::$worker, false, null);
1100
        $job->fibonacci(1);
1101
        $status = $jobManager->getStatus();
1102
        self::assertArrayHasKey('fibonacci->fibonacci()', $status);
1103
        $fibonacciStatus = $status['fibonacci->fibonacci()'];
1104
1105
        self::assertArrayHasKey(BaseJob::STATUS_NEW, $fibonacciStatus);
1106
        self::assertArrayHasKey(BaseJob::STATUS_EXCEPTION, $fibonacciStatus);
1107
        self::assertArrayHasKey(BaseJob::STATUS_RUNNING, $fibonacciStatus);
1108
        self::assertArrayHasKey(BaseJob::STATUS_SUCCESS, $fibonacciStatus);
1109
        self::assertArrayHasKey(Job::STATUS_EXPIRED, $fibonacciStatus);
1110
        self::assertArrayHasKey(StallableJob::STATUS_MAX_STALLS, $fibonacciStatus);
1111
        self::assertArrayHasKey(RetryableJob::STATUS_MAX_EXCEPTIONS, $fibonacciStatus);
1112
        self::assertArrayHasKey(RetryableJob::STATUS_MAX_FAILURES, $fibonacciStatus);
1113
        self::assertArrayHasKey(RetryableJob::STATUS_MAX_RETRIES, $fibonacciStatus);
1114
1115
        return [$job, $status];
1116
    }
1117
1118
    public function testGetStatus()
1119
    {
1120
        list($job1, $status1) = $this->getBaseStatus();
1121
        list($job2, $status2) = $this->getBaseStatus();
1122
        $fibonacciStatus1 = $status1['fibonacci->fibonacci()'];
1123
        $fibonacciStatus2 = $status2['fibonacci->fibonacci()'];
1124
1125
        self::assertEquals($fibonacciStatus1[BaseJob::STATUS_NEW] + 1, $fibonacciStatus2[BaseJob::STATUS_NEW]);
1126
        $jobManager = self::$jobManager;
1127
        $objectManager = $jobManager->getObjectManager();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Dtc\QueueBundle\Manager\JobManagerInterface as the method getObjectManager() does only exist in the following implementations of said interface: Dtc\QueueBundle\Doctrine\DoctrineJobManager, Dtc\QueueBundle\ODM\JobManager, Dtc\QueueBundle\ORM\JobManager.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
1128
        $objectManager->remove($job1);
1129
        $objectManager->remove($job2);
1130
    }
1131
}
1132