Completed
Push — develop ( 9659b8...659b85 )
by Mathias
13:02
created

LoadDependendEntities   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 35
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 10 2
A onDelete() 0 11 3
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2018 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Applications\Listener;
12
13
use Core\Service\EntityEraser\DependencyResultEvent;
14
use Jobs\Entity\Job;
15
16
/**
17
 * ${CARET}
18
 * 
19
 * @author Mathias Gelhausen <[email protected]>
20
 * @todo write test 
21
 */
22
class LoadDependendEntities 
23
{
24
25
    private $repository;
26
27
    public function __construct($repository)
28
    {
29
        $this->repository = $repository;
30
    }
31
32
    public function __invoke(DependencyResultEvent $event)
33
    {
34
        $entity = $event->getEntity();
35
36
        if ($entity instanceOf Job) {
37
            $entities = $event->getRepository('Applications')->findBy(['isDraft' => null, 'job' => new \MongoId($entity->getId())]);
0 ignored issues
show
Bug introduced by
The method findBy does only exist in Doctrine\Common\Persistence\ObjectRepository, but not in Core\Repository\RepositoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
38
39
            return ['Applications', $entities, 'These applications references the job and will also be removed:' ];
40
        }
41
    }
42
43
    public function onDelete(DependencyResultEvent $event)
44
    {
45
        $entity = $event->getEntity();
46
47
        if ($entity instanceOf Job) {
48
            $repository = $event->getRepository('Applications');
49
            $entities = $repository->findBy(['isDraft' => null, 'job' => new \MongoId($entity->getId())]);
0 ignored issues
show
Bug introduced by
The method findBy does only exist in Doctrine\Common\Persistence\ObjectRepository, but not in Core\Repository\RepositoryInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
50
            foreach ($entities as $ent) { $repository->remove($ent); }
51
            return ['Applications', $entities, 'were removed.' ];
52
        }
53
    }
54
55
56
}
57