ODMQuery   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 63
Duplicated Lines 7.94 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 4
dl 5
loc 63
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 5 20 7
A setDocumentManager() 0 4 1
A onlyDocs() 0 8 2
A toArray() 0 5 1

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
4
namespace Doctrine\ODM\CouchDB\View;
5
6
use Doctrine\CouchDB\View\Query;
7
use Doctrine\ODM\CouchDB\DocumentManager;
8
9
class ODMQuery extends Query
10
{
11
    /**
12
     * @var DocumentManager
13
     */
14
    private $dm;
15
16
    /**
17
     * @var bool
18
     */
19
    private $onlyDocs = false;
20
21
    private $toArray = false;
22
23
    public function execute()
24
    {
25
        $response = $this->doExecute();
26
        $data = array();
27
        if ($this->dm && $this->getParameter('include_docs') === true) {
28
            $uow = $this->dm->getUnitOfWork();
29
            foreach ($response->body['rows'] AS $k => $v) {
30
                $doc = $uow->createDocument(null, $v['doc']);
31
                if ($this->toArray) {
32
                    $data[] = $doc;
33 View Code Duplication
                } else if ($this->onlyDocs) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
34
                    $response->body['rows'][$k] = $doc;
35
                } else {
36
                    $response->body['rows'][$k]['doc'] = $doc;
37
                }
38
            }
39
        }
40
41
        return ($this->toArray) ? $data : $this->createResult($response);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->toArray ? $data :...reateResult($response); of type array|Doctrine\CouchDB\View\Result adds the type array to the return on line 41 which is incompatible with the return type of the parent method Doctrine\CouchDB\View\AbstractQuery::execute of type Doctrine\CouchDB\View\Result.
Loading history...
42
    }
43
44
45
    /**
46
     * @param DocumentManager $dm
47
     */
48
    public function setDocumentManager(DocumentManager $dm)
49
    {
50
        $this->dm = $dm;
51
    }
52
53
    /**
54
     * @param  bool $flag
55
     * @return Query
56
     */
57
    public function onlyDocs($flag)
58
    {
59
        if ($flag) {
60
            $this->setIncludeDocs(true);
61
        }
62
        $this->onlyDocs = $flag;
63
        return $this;
64
    }
65
66
    public function toArray($flag)
67
    {
68
        $this->toArray = $flag;
69
        return $this;
70
    }
71
}
72