Conditions | 18 |
Paths | 14 |
Total Lines | 52 |
Code Lines | 29 |
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 | // Unicode chars need 2 or more chars |
||
64 | // The characterCount before this loop already validate if is a single emoji |
||
65 | // This condition is to don't continue if non emoji chars |
||
66 | if (strlen($emoji) >= 2) { |
||
67 | // If the current code-point is an emoji or a modifier (like a skin-tone) |
||
68 | // just continue and check the next character |
||
69 | if ($codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_SYMBOL || |
||
70 | $codePointType === \IntlChar::CHAR_CATEGORY_MODIFIER_LETTER || |
||
71 | $codePointType === \IntlChar::CHAR_CATEGORY_OTHER_SYMBOL || |
||
72 | $codePointType === \IntlChar::CHAR_CATEGORY_FORMAT_CHAR || // i.e. 🏴 🏴 |
||
73 | $codePointType === \IntlChar::CHAR_CATEGORY_OTHER_PUNCTUATION || // i.e. ‼️ ⁉️ #⃣ |
||
74 | $codePointType === \IntlChar::CHAR_CATEGORY_LOWERCASE_LETTER || // i.e. ℹ️ |
||
75 | $codePointType === \IntlChar::CHAR_CATEGORY_MATH_SYMBOL || // i.e. ↔️ ◻️ ⤴️ ⤵️ |
||
76 | $codePointType === \IntlChar::CHAR_CATEGORY_ENCLOSING_MARK || // i.e. 0⃣..9⃣ |
||
77 | $codePointType === \IntlChar::CHAR_CATEGORY_DECIMAL_DIGIT_NUMBER || // i.e. 0⃣..9⃣ |
||
78 | $codePointType === \IntlChar::CHAR_CATEGORY_DASH_PUNCTUATION || // i.e. 〰️ |
||
79 | $codePointType === \IntlChar::CHAR_CATEGORY_GENERAL_OTHER_TYPES |
||
80 | ) { |
||
81 | continue; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | // If it's neither a modifier nor an emoji, we only allow |
||
86 | // a zero-width-joiner or a variation selector 16 |
||
87 | $codePointValue = \IntlChar::ord($codePoint); |
||
88 | if ($codePointValue === 8205 || $codePointValue === 65039) { |
||
89 | continue; |
||
90 | } |
||
91 | |||
92 | return false; |
||
93 | } |
||
94 | |||
95 | return true; |
||
96 | } |
||
98 |