Completed
Push — master ( 9f57de...bf9e4f )
by Nikola
04:54
created

DoctrineDbalExecutorResult   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 99
Duplicated Lines 28.28 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 28
loc 99
c 1
b 0
f 0
wmc 19
lcom 1
cbo 3
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSingleScalarResult() 14 14 3
A getSingleScalarResultOrDefault() 0 8 2
A getSingleScalarResultOrNull() 0 4 1
A getScalarResult() 0 10 2
A getScalarResultOrDefault() 0 10 2
A getScalarResultOrNull() 0 4 1
A getSingleRowResult() 14 14 3
A getSingleRowOrDefault() 0 8 2
A getSingleRowOrNull() 0 4 1
A __call() 0 4 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
namespace RunOpenCode\Bundle\QueryResourcesLoader\Executor;
4
5
use Doctrine\DBAL\Driver\Statement;
6
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\NonUniqueResultException;
7
use RunOpenCode\Bundle\QueryResourcesLoader\Exception\NoResultException;
8
9
final class DoctrineDbalExecutorResult
10
{
11
    private $statement;
12
13
    public function __construct(Statement $statement)
14
    {
15
        $this->statement = $statement;
16
    }
17
18 View Code Duplication
    public function getSingleScalarResult()
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...
19
    {
20
        $scalar = $this->statement->fetchColumn(0);
21
22
        if (false === $scalar) {
23
            throw new NoResultException('Expected on result for given query.');
24
        }
25
26
        if (false !== $this->statement->fetch()) {
27
            throw new NonUniqueResultException('Expected only ine result for given query.');
28
        }
29
30
        return $scalar;
31
    }
32
33
    public function getSingleScalarResultOrDefault($default)
34
    {
35
        try {
36
            return $this->getSingleScalarResult();
37
        } catch (NoResultException $e) {
38
            return $default;
39
        }
40
    }
41
42
    public function getSingleScalarResultOrNull()
43
    {
44
        return $this->getSingleScalarResultOrDefault(null);
45
    }
46
47
    public function getScalarResult()
48
    {
49
        $result = [];
50
51
        while ($val = $this->statement->fetchColumn()) {
52
            $result[] = $val;
53
        }
54
55
        return $result;
56
    }
57
58
    public function getScalarResultOrDefault($default)
59
    {
60
        $result = $this->getScalarResult();
61
62
        if (count($result) < 0) {
63
            return $default;
64
        }
65
66
        return $result;
67
    }
68
69
    public function getScalarResultOrNull()
70
    {
71
        return $this->getScalarResultOrDefault(null);
72
    }
73
74 View Code Duplication
    public function getSingleRowResult()
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...
75
    {
76
        $row = $this->statement->fetch(\PDO::FETCH_BOTH);
77
78
        if (false === $row) {
79
            throw new NoResultException('Expected on result for given query.');
80
        }
81
82
        if (false !== $this->statement->fetch()) {
83
            throw new NonUniqueResultException('Expected only ine result for given query.');
84
        }
85
86
        return $row;
87
    }
88
89
    public function getSingleRowOrDefault($default)
90
    {
91
        try {
92
            return $this->getSingleRowResult();
93
        } catch (NoResultException $e) {
94
            return $default;
95
        }
96
    }
97
98
    public function getSingleRowOrNull()
99
    {
100
        $this->getSingleRowOrDefault(null);
101
    }
102
103
    public function __call($name, $arguments)
104
    {
105
        return call_user_func_array(array($this->statement, $name), $arguments);
106
    }
107
}
108