| Conditions | 6 |
| Paths | 24 |
| Total Lines | 52 |
| Code Lines | 36 |
| 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 |
||
| 43 | public function findExisting(IUser $user, |
||
| 44 | ?string $uid, |
||
| 45 | ?string $email, |
||
| 46 | ?string $cloudId): ?string { |
||
| 47 | $addressbooksQuery = $this->db->getQueryBuilder(); |
||
| 48 | $cardQuery = $this->db->getQueryBuilder(); |
||
| 49 | $propQuery = $this->db->getQueryBuilder(); |
||
| 50 | |||
| 51 | $propOr = $propQuery->expr()->orX(); |
||
| 52 | if ($uid !== null) { |
||
| 53 | $propOr->add($propQuery->expr()->andX( |
||
| 54 | $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('UID')), |
||
| 55 | $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($uid)) |
||
| 56 | )); |
||
| 57 | } |
||
| 58 | if ($email !== null) { |
||
| 59 | $propOr->add($propQuery->expr()->andX( |
||
| 60 | $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('EMAIL')), |
||
| 61 | $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($email)) |
||
| 62 | )); |
||
| 63 | } |
||
| 64 | if ($cloudId !== null) { |
||
| 65 | $propOr->add($propQuery->expr()->andX( |
||
| 66 | $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('CLOUD')), |
||
| 67 | $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($cloudId)) |
||
| 68 | )); |
||
| 69 | } |
||
| 70 | $addressbooksQuery->selectDistinct('id') |
||
| 71 | ->from('addressbooks') |
||
| 72 | ->where($addressbooksQuery->expr()->eq('principaluri', $cardQuery->createNamedParameter("principals/users/" . $user->getUID()))); |
||
| 73 | $propQuery->selectDistinct('cardid') |
||
| 74 | ->from('cards_properties') |
||
| 75 | ->where($propQuery->expr()->in('addressbookid', $propQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY)) |
||
| 76 | ->andWhere($propOr) |
||
| 77 | ->groupBy('cardid'); |
||
| 78 | $cardQuery->select('carddata') |
||
| 79 | ->from('cards') |
||
| 80 | ->where($cardQuery->expr()->in('id', $cardQuery->createFunction($propQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY)) |
||
| 81 | ->andWhere($cardQuery->expr()->in('addressbookid', $cardQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY)) |
||
| 82 | ->setMaxResults(1); |
||
| 83 | $result = $cardQuery->execute(); |
||
| 84 | /** @var string|resource|false $card */ |
||
| 85 | $card = $result->fetchColumn(0); |
||
| 86 | |||
| 87 | if ($card === false) { |
||
|
|
|||
| 88 | return null; |
||
| 89 | } |
||
| 90 | if (is_resource($card)) { |
||
| 91 | return stream_get_contents($card); |
||
| 92 | } |
||
| 93 | |||
| 94 | return $card; |
||
| 95 | } |
||
| 97 |