| Conditions | 4 |
| Paths | 5 |
| Total Lines | 73 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 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 namespace Felixkiss\UniqueWithValidator; |
||
| 32 | public function validateUniqueWith($attribute, $value, $parameters) |
||
| 33 | { |
||
| 34 | // cleaning: trim whitespace |
||
| 35 | $parameters = array_map('trim', $parameters); |
||
| 36 | |||
| 37 | // first item equals table name |
||
| 38 | list($connection, $table) = $this->parseTable(array_shift($parameters)); |
||
| 39 | |||
| 40 | // The second parameter position holds the name of the column that |
||
| 41 | // needs to be verified as unique. If this parameter isn't specified |
||
| 42 | // we will just assume that this column to be verified shares the |
||
| 43 | // attribute's name. |
||
| 44 | $column = $attribute; |
||
| 45 | |||
| 46 | // Create $extra array with all other columns, so getCount() will |
||
| 47 | // include them as where clauses as well |
||
| 48 | $extra = array(); |
||
| 49 | |||
| 50 | // Check if last parameter is an integer. If it is, then it will |
||
| 51 | // ignore the row with the specified id - useful when updating a row |
||
| 52 | list($ignore_id, $ignore_column) = $this->getIgnore($parameters); |
||
| 53 | |||
| 54 | // Figure out whether field_name is the same as column_name |
||
| 55 | // or column_name is explicitly specified. |
||
| 56 | // |
||
| 57 | // case 1: |
||
| 58 | // $parameter = 'last_name' |
||
| 59 | // => field_name = column_name = 'last_name' |
||
| 60 | // case 2: |
||
| 61 | // $parameter = 'last_name=sur_name' |
||
| 62 | // => field_name = 'last_name', column_name = 'sur_name' |
||
| 63 | foreach ($parameters as $parameter) |
||
| 64 | { |
||
| 65 | $parameter = array_map('trim', explode('=', $parameter, 2)); |
||
| 66 | $field_name = $parameter[0]; |
||
| 67 | |||
| 68 | if (count($parameter) > 1) |
||
| 69 | { |
||
| 70 | $column_name = $parameter[1]; |
||
| 71 | } |
||
| 72 | else |
||
| 73 | { |
||
| 74 | $column_name = $field_name; |
||
| 75 | } |
||
| 76 | |||
| 77 | // Figure out whether main field_name has an explicitly specified |
||
| 78 | // column_name |
||
| 79 | if ($field_name == $column) |
||
| 80 | { |
||
| 81 | $column = $column_name; |
||
| 82 | } |
||
| 83 | else |
||
| 84 | { |
||
| 85 | $extra[$column_name] = array_get($this->data, $field_name); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | // The presence verifier is responsible for counting rows within this |
||
| 90 | // store mechanism which might be a relational database or any other |
||
| 91 | // permanent data store like Redis, etc. We will use it to determine |
||
| 92 | // uniqueness. |
||
| 93 | $verifier = $this->getPresenceVerifier(); |
||
| 94 | $verifier->setConnection($connection); |
||
| 95 | |||
| 96 | return $verifier->getCount( |
||
| 97 | $table, |
||
| 98 | $column, |
||
| 99 | $value, |
||
| 100 | $ignore_id, |
||
| 101 | $ignore_column, |
||
| 102 | $extra |
||
| 103 | ) == 0; |
||
| 104 | } |
||
| 105 | |||
| 167 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.