| Conditions | 11 |
| Paths | 9 |
| Total Lines | 34 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 50 | private function initCols(ITypeDictionary $typeDictionary) |
||
| 51 | { |
||
| 52 | $numFields = pg_num_fields($this->handler); |
||
| 53 | if ($numFields < 0 || $numFields === null) { |
||
| 54 | throw new ResultException('Error retrieving number of fields of the result.'); |
||
| 55 | } |
||
| 56 | $this->columns = []; |
||
| 57 | $this->colNameMap = []; |
||
| 58 | for ($i = 0; $i < $numFields; $i++) { |
||
| 59 | /* NOTE: pg_field_type() cannot be used for simplicity - multiple types of the same name might exist in |
||
| 60 | * different schemas. Thus, the only reasonable way to recognize the types is using their OIDs, |
||
| 61 | * returned by pg_field_type_oid(). Up to some extreme cases, within a given database, the same OID |
||
| 62 | * will always refer to the same data type. |
||
| 63 | */ |
||
| 64 | $name = pg_field_name($this->handler, $i); |
||
| 65 | if ($name === false || $name === null) { // NOTE: besides false, pg_field_name() might return NULL on error |
||
| 66 | throw new ResultException("Error retrieving name of result column $i."); |
||
| 67 | } |
||
| 68 | if ($name == '?column?') { |
||
| 69 | $name = null; |
||
| 70 | } |
||
| 71 | $typeOid = pg_field_type_oid($this->handler, $i); |
||
| 72 | if ($typeOid === false || $typeOid === null) { // NOTE: besides false, pg_field_type_oid() might return NULL on error |
||
| 73 | throw new ResultException("Error retrieving type OID of result column $i."); |
||
| 74 | } |
||
| 75 | $type = $typeDictionary->requireTypeByOid($typeOid); |
||
| 76 | |||
| 77 | $this->columns[] = new Column($this, $i, $name, $type); |
||
| 78 | |||
| 79 | if ($name !== null && !isset($this->colNameMap[$name])) { |
||
| 80 | $this->colNameMap[$name] = $i; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 145 |
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.