Conditions | 18 |
Paths | 360 |
Total Lines | 87 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 15 | ||
Bugs | 2 | Features | 11 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
68 | { |
||
69 | $pdo = $this->getDatabaseDriver(); |
||
70 | |||
71 | if ($single === false) { |
||
72 | return $pdo->fetchAll($query->getStatement(), $query->getBindValues()); |
||
73 | } |
||
74 | |||
75 | return $pdo->fetchOne($query->getStatement(), $query->getBindValues()); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Reads a single record from the database |
||
80 | * |
||
81 | * @param string $id |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | public function readSinglebyId($id, $keyType = 'primary') |
||
|
|||
86 | { |
||
87 | $query = $this->newQuery(); |
||
88 | $key = $this->returnKeyType($keyType); |
||
89 | |||
90 | $query->cols(['*']) |
||
91 | ->where("`{$key}` = '{$id}'"); |
||
92 | |||
93 | return $this->fireStatementAndReturn($query, true); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Reads all related records from the database |
||
98 | * |
||
99 | * @param string $id |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | public function readAllById($id, $keyType = 'primary') |
||
104 | { |
||
105 | $query = $this->newQuery(); |
||
106 | $key = $this->returnKeyType($keyType); |
||
107 | |||
108 | $query->cols(['*']) |
||
109 | ->where("`{$key}` = '{$id}'"); |
||
110 | |||
111 | return $this->fireStatementAndReturn($query); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Reads all records based off a simple where statement |
||
116 | * |
||
117 | * @param array $fields |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | public function readAllByFields($fields) |
||
122 | { |
||
123 | $query = $this->newQuery(); |
||
124 | |||
125 | $query->cols(['*']); |
||
126 | |||
127 | foreach ($fields as $field => $value) { |
||
128 | $query->where("`{$field}` = '{$value}'"); |
||
129 | } |
||
130 | |||
131 | return $this->fireStatementAndReturn($query); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Reads the count of records based off a where statement |
||
136 | * |
||
137 | * @param array $fields |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | public function readCountByFields($fields) |
||
142 | { |
||
143 | $query = $this->newQuery(); |
||
144 | $key = $this->returnKeyType('primary'); |
||
145 | |||
146 | $query->cols(["COUNT({$key}) as COUNT"]); |
||
147 | |||
148 | foreach ($fields as $field => $value) { |
||
149 | $query->where("`{$field}` = '{$value}'"); |
||
150 | } |
||
151 | |||
152 | $result = $this->fireStatementAndReturn($query); |
||
153 | |||
154 | // Done this to prevent the need for clients to also do this. Returns a single number this way. |
||
155 | return $result[0]["COUNT"]; |
||
176 |
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.