| Conditions | 6 |
| Paths | 6 |
| Total Lines | 53 |
| Code Lines | 46 |
| 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 |
||
| 51 | function process_result($res) |
||
| 52 | { |
||
| 53 | $statCodes = [ |
||
| 54 | PGSQL_EMPTY_QUERY => 'empty query', |
||
| 55 | PGSQL_COMMAND_OK => 'command ok', |
||
| 56 | PGSQL_TUPLES_OK => 'tuples ok', |
||
| 57 | PGSQL_COPY_IN => 'copy in', |
||
| 58 | PGSQL_COPY_OUT => 'copy out', |
||
| 59 | PGSQL_BAD_RESPONSE => 'bad response', |
||
| 60 | PGSQL_NONFATAL_ERROR => 'non-fatal error', // reported as impossible to get this status returned from php pgsql |
||
| 61 | PGSQL_FATAL_ERROR => 'fatal error', |
||
| 62 | ]; |
||
| 63 | $statCode = pg_result_status($res); |
||
| 64 | $statStr = pg_result_status($res, PGSQL_STATUS_STRING); |
||
| 65 | echo "Result status: $statCode ({$statCodes[$statCode]}); $statStr\n"; |
||
| 66 | |||
| 67 | switch ($statCode){ |
||
| 68 | case PGSQL_TUPLES_OK: |
||
| 69 | $numRows = pg_num_rows($res); |
||
| 70 | $numFields = pg_num_fields($res); |
||
| 71 | echo "The result was fetched, having $numRows rows, each having $numFields fields\n"; |
||
| 72 | break; |
||
| 73 | case PGSQL_COMMAND_OK: |
||
| 74 | echo "Command OK\n"; |
||
| 75 | break; |
||
| 76 | case PGSQL_COPY_IN: |
||
| 77 | echo "COPY IN started\n"; |
||
| 78 | break; |
||
| 79 | case PGSQL_COPY_OUT: |
||
| 80 | echo "COPY OUT started\n"; |
||
| 81 | break; |
||
| 82 | default: |
||
| 83 | echo "Error occurred.\n"; |
||
| 84 | $fields = [ |
||
| 85 | 'SQL state' => PGSQL_DIAG_SQLSTATE, |
||
| 86 | 'Severity' => PGSQL_DIAG_SEVERITY, |
||
| 87 | 'Message' => PGSQL_DIAG_MESSAGE_PRIMARY, |
||
| 88 | 'Detail' => PGSQL_DIAG_MESSAGE_DETAIL, |
||
| 89 | 'Hint' => PGSQL_DIAG_MESSAGE_HINT, |
||
| 90 | 'Statement position' => PGSQL_DIAG_STATEMENT_POSITION, |
||
| 91 | 'Internal position' => PGSQL_DIAG_INTERNAL_POSITION, |
||
| 92 | 'Internal query' => PGSQL_DIAG_INTERNAL_QUERY, |
||
| 93 | 'Context' => PGSQL_DIAG_CONTEXT, |
||
| 94 | 'Source file' => PGSQL_DIAG_SOURCE_FILE, |
||
| 95 | 'Source line' => PGSQL_DIAG_SOURCE_LINE, |
||
| 96 | 'Source function' => PGSQL_DIAG_SOURCE_FUNCTION, |
||
| 97 | ]; |
||
| 98 | foreach ($fields as $desc => $field) { |
||
| 99 | echo "$desc: " . pg_result_error_field($res, $field) . "\n"; |
||
| 100 | } |
||
| 101 | echo "\n"; |
||
| 102 | } |
||
| 103 | } |
||
| 104 |