Completed
Pull Request — master (#22)
by
unknown
02:43
created

Min   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 60
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A field() 6 6 1
A run() 7 7 1
A __invoke() 7 7 1
A __toString() 10 10 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 SimpleCrud\Queries\Mysql;
4
5
use SimpleCrud\Queries\Query;
6
use SimpleCrud\Table;
7
use PDO;
8
9
/**
10
 * Manages a database select min query in Mysql databases.
11
 */
12 View Code Duplication
class Min extends Query
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    use ExtendedSelectionTrait;
15
16
    protected $field;
17
18
    /**
19
     * Set the field name to maximize over.
20
     *
21
     * @param string $field
22
     *
23
     * @return self
24
     */
25
    public function field($field)
26
    {
27
        $this->field = $field;
28
29
        return $this;
30
    }
31
32
    /**
33
     * Run the query and return the value.
34
     * 
35
     * @return int
36
     */
37
    public function run()
38
    {
39
        $result = $this->__invoke()->fetch();
40
        $field = $this->table->{$this->field};
41
        
42
        return $field->dataFromDatabase($result[0]);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function __invoke()
49
    {
50
        $statement = $this->table->getDatabase()->execute((string) $this, $this->marks);
51
        $statement->setFetchMode(PDO::FETCH_NUM);
52
53
        return $statement;
54
    }
55
56
    /**
57
     * Build and return the query.
58
     *
59
     * @return string
60
     */
61
    public function __toString()
62
    {
63
        $query = "SELECT MIN(`{$this->field}`) FROM `{$this->table->getName()}`";
64
65
        $query .= $this->fromToString();
66
        $query .= $this->whereToString();
67
        $query .= $this->limitToString();
68
69
        return $query;
70
    }
71
}
72