Completed
Push — master ( 997648...3e9ab2 )
by Oscar
01:59
created

Field   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 10 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 2
cbo 1
dl 11
loc 110
rs 10
c 1
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A dataToDatabase() 0 8 3
A dataFromDatabase() 0 4 1
A getScheme() 0 4 1
A getSelectExpression() 11 11 2
A getValueExpression() 0 4 1
A getConfig() 0 4 2
A setConfig() 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
namespace SimpleCrud\Fields;
4
5
use SimpleCrud\Table;
6
7
/**
8
 * Generic field.
9
 */
10
class Field
11
{
12
    protected $table;
13
    protected $name;
14
    protected $config = [];
15
16
    /**
17
     * @param Table $table
18
     * @param string $name
19
     */
20
    public function __construct(Table $table, $name)
21
    {
22
        $this->table = $table;
23
        $this->name = $name;
24
    }
25
26
    /**
27
     * Converts the data to save in the database
28
     *
29
     * @param mixed $data
30
     *
31
     * @return mixed
32
     */
33
    public function dataToDatabase($data)
34
    {
35
        if ($data === '' && $this->getScheme()['null']) {
36
            return;
37
        }
38
39
        return $data;
40
    }
41
42
    /**
43
     * Converts the data to be used
44
     *
45
     * @param mixed $data
46
     *
47
     * @return mixed
48
     */
49
    public function dataFromDatabase($data)
50
    {
51
        return $data;
52
    }
53
54
    /**
55
     * Returns the field scheme
56
     *
57
     * @return array
58
     */
59
    public function getScheme()
60
    {
61
        return $this->table->getScheme()['fields'][$this->name];
62
    }
63
64
    /**
65
     * Returns the expression used to get the value from the database
66
     *
67
     * @param string|null $as
68
     *
69
     * @return string
70
     */
71 View Code Duplication
    public function getSelectExpression($as = null)
72
    {
73
        $tableName = $this->table->getName();
74
        $fieldName = $this->name;
75
76
        if ($as) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $as of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
77
            return "`{$tableName}`.`{$fieldName}` as `{$as}`";
78
        }
79
80
        return "`{$tableName}`.`{$fieldName}`";
81
    }
82
83
    /**
84
     * Returns the expression used to save the value in the database
85
     *
86
     * @param string $mark
87
     *
88
     * @return string
89
     */
90
    public function getValueExpression($mark)
91
    {
92
        return $mark;
93
    }
94
95
    /**
96
     * Returns a config value
97
     *
98
     * @return mixed
99
     */
100
    public function getConfig($name)
101
    {
102
        return isset($this->config[$name]) ? $this->config[$name] : null;
103
    }
104
105
    /**
106
     * Edit a config value
107
     *
108
     * @param string $name
109
     * @param mixed $value
110
     *
111
     * @return self
112
     */
113
    public function setConfig($name, $value)
114
    {
115
        $this->config[$name] = $value;
116
117
        return $this;
118
    }
119
}
120