SelectStatement   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 18.46 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 12
loc 65
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addFields() 12 12 2
A clearFields() 0 6 1
A getFields() 0 4 1
A build() 0 16 3

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
 * @author Jared King <[email protected]>
5
 *
6
 * @link http://jaredtking.com
7
 *
8
 * @copyright 2015 Jared King
9
 * @license MIT
10
 */
11
namespace JAQB\Statement;
12
13
class SelectStatement extends Statement
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $fields = ['*'];
19
20
    /**
21
     * Adds fields to this statement.
22
     * Supported input styles:
23
     * - addFields('field1,field2')
24
     * - addFields(['field','field2']).
25
     *
26
     * @param string|array $fields
27
     *
28
     * @return self
29
     */
30 View Code Duplication
    public function addFields($fields)
31
    {
32
        if (!is_array($fields)) {
33
            $fields = array_map(function ($f) {
34
                return trim($f);
35
            }, explode(',', $fields));
36
        }
37
38
        $this->fields = array_merge($this->fields, $fields);
39
40
        return $this;
41
    }
42
43
    public function clearFields()
44
    {
45
        $this->fields = [];
46
47
        return $this;
48
    }
49
50
    /**
51
     * Gets the fields associated with this statement.
52
     * If no fields are present then defaults to '*'.
53
     *
54
     * @return array fields
55
     */
56
    public function getFields()
57
    {
58
        return $this->fields;
59
    }
60
61
    public function build()
62
    {
63
        $fields = $this->getFields();
64
        foreach ($fields as &$field) {
65
            $field = $this->escapeIdentifier($field);
66
        }
67
68
        // remove empty values
69
        $fields = array_filter($fields);
70
71
        if (count($fields) === 0) {
72
            return '';
73
        }
74
75
        return 'SELECT '.implode(', ', $fields);
76
    }
77
}
78