PersistentViewCollection   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 19.51 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 5
dl 8
loc 41
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 1
A initialize() 0 25 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Doctrine\ODM\CouchDB;
4
5
use Doctrine\Common\Collections\Collection;
6
7
class PersistentViewCollection extends PersistentCollection
8
{
9
    private $dm;
10
    private $owningDocumentId;
11
    private $assoc;
12
13 View Code Duplication
    public function __construct(Collection $collection, DocumentManager $dm, $owningDocumentId, $assoc)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
    {
15
        $this->col = $collection;
16
        $this->dm = $dm;
17
        $this->owningDocumentId = $owningDocumentId;
18
        $this->assoc = $assoc;
19
        $this->isInitialized = false;
20
    }
21
22
    public function initialize()
23
    {
24
        if (!$this->isInitialized) {
25
            $this->isInitialized = true;
26
27
            $elements = $this->col->toArray();
28
            $this->col->clear();
29
30
            $relatedObjects = $this->dm->createNativeQuery('doctrine_associations', 'inverse_associations')
31
                                  ->setStartKey(array($this->owningDocumentId, $this->assoc['mappedBy']))
0 ignored issues
show
Documentation introduced by
array($this->owningDocum...his->assoc['mappedBy']) is of type array<integer,?,{"0":"?","1":"?"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
32
                                  ->setEndKey(array($this->owningDocumentId, $this->assoc['mappedBy'], 'z'))
0 ignored issues
show
Documentation introduced by
array($this->owningDocum...assoc['mappedBy'], 'z') is of type array<integer,?,{"0":"?","1":"?","2":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
33
                                  ->setIncludeDocs(true)
34
                                  ->execute();
35
36
            $uow = $this->dm->getUnitOfWork();
37
            foreach ($relatedObjects AS $relatedRow) {
38
                $this->col->add($uow->createDocument($this->assoc['targetDocument'], $relatedRow['doc']));
39
            }
40
41
            // append old elements
42
            foreach ($elements AS $object) {
43
                $this->col->add($object);
44
            }
45
        }
46
    }
47
}
48