| Conditions | 10 |
| Paths | 10 |
| Total Lines | 39 |
| Code Lines | 21 |
| 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 |
||
| 44 | public function isValidSingleEmoji(string $emoji): bool { |
||
| 45 | $intlBreakIterator = \IntlBreakIterator::createCharacterInstance(); |
||
| 46 | $intlBreakIterator->setText($emoji); |
||
| 47 | |||
| 48 | $characterCount = 0; |
||
| 49 | while ($intlBreakIterator->next() !== \IntlBreakIterator::DONE) { |
||
| 50 | $characterCount++; |
||
| 51 | } |
||
| 52 | |||
| 53 | if ($characterCount !== 1) { |
||
| 54 | return false; |
||
| 55 | } |
||
| 56 | |||
| 57 | $codePointIterator = \IntlBreakIterator::createCodePointInstance(); |
||
| 58 | $codePointIterator->setText($emoji); |
||
| 59 | |||
| 60 | foreach ($codePointIterator->getPartsIterator() as $codePoint) { |
||
| 61 | $codePointType = \IntlChar::charType($codePoint); |
||
| 62 | |||
| 63 | // If the current code-point is an emoji or a modifier (like a skin-tone) |
||
| 64 | // just continue and check the next character |
||
| 65 | if ($codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_SYMBOL || |
||
| 66 | $codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_LETTER || |
||
| 67 | $codePointType === \IntlChar::CHAR_CATEGORY_OTHER_SYMBOL || |
||
| 68 | $codePointType === \IntlChar::CHAR_CATEGORY_GENERAL_OTHER_TYPES) { |
||
| 69 | continue; |
||
| 70 | } |
||
| 71 | |||
| 72 | // If it's neither a modifier nor an emoji, we only allow |
||
| 73 | // a zero-width-joiner or a variation selector 16 |
||
| 74 | $codePointValue = \IntlChar::ord($codePoint); |
||
| 75 | if ($codePointValue === 8205 || $codePointValue === 65039) { |
||
| 76 | continue; |
||
| 77 | } |
||
| 78 | |||
| 79 | return false; |
||
| 80 | } |
||
| 81 | |||
| 82 | return true; |
||
| 83 | } |
||
| 85 |