| Conditions | 7 |
| Paths | 72 |
| Total Lines | 76 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 40 | public function existsByUsers($users){ |
||
| 41 | $usersCount = count($users); |
||
| 42 | $sql = <<<SQL |
||
| 43 | SELECT |
||
| 44 | DISTINCT c1.conversation_id AS conv_id |
||
| 45 | FROM |
||
| 46 | *PREFIX*chat_och_users_in_conversation c1 |
||
| 47 | WHERE EXISTS ( |
||
| 48 | SELECT |
||
| 49 | 1 |
||
| 50 | FROM |
||
| 51 | *PREFIX*chat_och_users_in_conversation c2 |
||
| 52 | WHERE |
||
| 53 | c1.conversation_id = c2.conversation_id |
||
| 54 | AND |
||
| 55 | c2.user = ? |
||
| 56 | ) |
||
| 57 | SQL; |
||
| 58 | |||
| 59 | for($i = 0; $i < ($usersCount -1); $i++){ |
||
| 60 | $sql .= <<<SQL |
||
| 61 | AND EXISTS ( |
||
| 62 | SELECT |
||
| 63 | 1 |
||
| 64 | FROM |
||
| 65 | *PREFIX*chat_och_users_in_conversation c2 |
||
| 66 | WHERE |
||
| 67 | c1.conversation_id = c2.conversation_id |
||
| 68 | AND |
||
| 69 | c2.user = ? |
||
| 70 | ) |
||
| 71 | SQL; |
||
| 72 | } |
||
| 73 | |||
| 74 | $sql .= <<<SQL |
||
| 75 | AND NOT EXISTS ( |
||
| 76 | SELECT |
||
| 77 | 1 |
||
| 78 | FROM |
||
| 79 | *PREFIX*chat_och_users_in_conversation c2 |
||
| 80 | WHERE |
||
| 81 | c1.conversation_id = c2.conversation_id |
||
| 82 | AND |
||
| 83 | c2.user NOT IN ( |
||
| 84 | SQL; |
||
| 85 | |||
| 86 | foreach($users as $key=>$user){ |
||
| 87 | if($key === $usersCount-1){ |
||
| 88 | $sql .= " ?"; |
||
| 89 | } else { |
||
| 90 | $sql .= " ?,"; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | $sql .= <<<SQL |
||
| 94 | ) |
||
| 95 | ) |
||
| 96 | SQL; |
||
| 97 | |||
| 98 | $params = array(); |
||
| 99 | |||
| 100 | foreach($users as $user){ |
||
| 101 | $params[] = $user; |
||
| 102 | } |
||
| 103 | |||
| 104 | foreach($users as $user){ |
||
| 105 | $params[] = $user; |
||
| 106 | } |
||
| 107 | |||
| 108 | try{ |
||
| 109 | $result = $this->execute($sql, $params); |
||
| 110 | $row = $result->fetch(); |
||
| 111 | return $row; |
||
| 112 | } catch (DoesNotExistException $exception) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |