Completed
Push — master ( 38310f...b83e6e )
by Matthew
08:14
created

CommonTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A persist() 0 13 4
1
<?php
2
3
namespace Dtc\QueueBundle\Doctrine;
4
5
use Dtc\QueueBundle\Model\Job;
6
use Dtc\QueueBundle\Model\JobTiming;
7
use Dtc\QueueBundle\Model\Run;
8
use Dtc\QueueBundle\Util\Util;
9
10
trait CommonTrait
11
{
12
    /**
13
     * @param Run|Job|JobTiming $object
14
     * @param string            $action
15
     */
16 45
    protected function persist($object, $action = 'persist')
17
    {
18 45
        $objectManager = $this->getObjectManager();
0 ignored issues
show
Bug introduced by
It seems like getObjectManager() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
19 45
        if ($objId = $object->getId()) {
0 ignored issues
show
Bug introduced by
The method getId does only exist in Dtc\QueueBundle\Model\Jo...c\QueueBundle\Model\Run, but not in Dtc\QueueBundle\Model\JobTiming.

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...
20 22
            $newObj = $objectManager->getRepository(get_class($object))->find($objId);
21 22
            if (null !== $newObj && spl_object_hash($newObj) !== spl_object_hash($object)) {
22 2
                Util::copy($object, $newObj);
23 2
                $object = $newObj;
24
            }
25
        }
26 45
        $objectManager->$action($object);
27 45
        $objectManager->flush();
28 45
    }
29
}
30