ODMLuceneQuery   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 72
Duplicated Lines 6.94 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 3
dl 5
loc 72
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDocumentName() 0 4 1
A setDocumentName() 0 5 1
B execute() 5 26 8
A setDocumentManager() 0 4 1
A onlyDocs() 0 6 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\LuceneQuery;
7
use Doctrine\ODM\CouchDB\DocumentManager;
8
9
class ODMLuceneQuery extends LuceneQuery
10
{
11
    /**
12
     * @var DocumentManager
13
     */
14
    private $dm;
15
16
    /**
17
     * @var bool
18
     */
19
    private $onlyDocs = false;
20
21
    private $documentName;
22
23
    public function getDocumentName()
24
    {
25
        return $this->documentName;
26
    }
27
28
    public function setDocumentName($documentName)
29
    {
30
        $this->documentName = $documentName;
31
        return $this;
32
    }
33
34
    public function execute()
35
    {
36
        $response = $this->doExecute();
37
        if ($this->dm && $this->getParameter('include_docs') === true) {
38
            $uow = $this->dm->getUnitOfWork();
0 ignored issues
show
Unused Code introduced by
$uow is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
39
            foreach ($response->body['rows'] AS $k => $v) {
40
                if (!isset($v['type']) && !$this->documentName) {
41
                    throw new \InvalidArgumentException(
42
                        "Cannot query " . $this->getHttpQuery() . " lucene and convert to document instances, ".
43
                        "the type of document " . $v['id'] . " is not stored in Lucene. You can query without " .
44
                        "include_docs and pass the ids to findMany() of the repository you know this document is " .
45
                        "a type of.");
46
                }
47
                $v['type'] = isset($v['type']) ? $v['type'] : $this->documentName;
48
49
                $doc = $this->dm->find(str_replace(".", "\\", $v['type']), $v['id']);
50 View Code Duplication
                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...
51
                    $response->body['rows'][$k] = $doc;
52
                } else {
53
                    $response->body['rows'][$k]['doc'] = $doc;
54
                }
55
            }
56
        }
57
58
        return $this->createResult($response);
59
    }
60
61
62
    /**
63
     * @param DocumentManager $dm
64
     */
65
    public function setDocumentManager(DocumentManager $dm)
66
    {
67
        $this->dm = $dm;
68
    }
69
70
    /**
71
     * @param  bool $flag
72
     * @return LuceneQuery
73
     */
74
    public function onlyDocs($flag)
75
    {
76
        $this->setIncludeDocs(true);
77
        $this->onlyDocs = $flag;
78
        return $this;
79
    }
80
}
81