Completed
Pull Request — master (#25)
by
unknown
03:13
created

WriteConcern   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 5
c 3
b 0
f 2
lcom 1
cbo 1
dl 7
loc 56
ccs 14
cts 14
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
setWriteConcern() 0 1 ?
A setWriteConcernFromParameters() 0 6 1
A setWriteConcernFromArray() 7 7 2
A getWriteConcern() 0 12 2

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
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 */
15
16
namespace Alcaeus\MongoDbAdapter\Helper;
17
18
/**
19
 * @internal
20
 */
21
trait WriteConcern
22
{
23
    use WriteConcernRaw;
24
25
    /**
26
     * @var \MongoDB\Driver\WriteConcern
27
     */
28
    protected $writeConcern;
29
30
    /**
31
     * @param $wstring
32
     * @param int $wtimeout
33
     * @return bool
34
     */
35
    abstract public function setWriteConcern($wstring, $wtimeout = 0);
36
37
    /**
38
     * @return array
39
     */
40 120
    public function getWriteConcern()
41
    {
42 120
        if ($this->writeConcern === null) {
43 119
            $this->writeConcern = new \MongoDB\Driver\WriteConcern(1);
44 119
        }
45
46
        return [
47 120
            'w' => $this->writeConcern->getW(),
48 120
            'wtimeout' => $this->writeConcern->getWtimeout(),
49 120
        ];
50
51
    }
52
53
    /**
54
     * @param string|int $wstring
55
     * @param int $wtimeout
56
     * @return bool
57
     */
58 120
    protected function setWriteConcernFromParameters($wstring, $wtimeout = 0)
59
    {
60 120
        $this->writeConcern = $this->createWriteConcernFromParameters($wstring, $wtimeout);
61
62 120
        return true;
63
    }
64
65
    /**
66
     * @param array $writeConcernArray
67
     * @return bool
68
     */
69 119 View Code Duplication
    protected function setWriteConcernFromArray($writeConcernArray)
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...
70
    {
71 119
        $wstring = $writeConcernArray['w'];
72 119
        $wtimeout = isset($writeConcernArray['wtimeout']) ? $writeConcernArray['wtimeout'] : 0;
73
74 119
        return $this->setWriteConcernFromParameters($wstring, $wtimeout);
75
    }
76
}
77