Skip to content

Conversation

@bensinclair
Copy link
Member

Note: This change was made by Claude Code.

Summary

This builds off of the #31 Enhancement: Added replaceEmoji() method with proper validation based on reviewer feedback.

Key Improvements Made

  1. Implemented replaceEmoji() method with proper validation
  • Uses preg_replace_callback() instead of simple preg_replace()
  • Only replaces known emoji shortcodes by checking against the supported shortcodes list
  • Preserves non-emoji :word: patterns unchanged
  • Follows the reviewer's suggestion to validate shortcodes before replacement
  1. Refactored removeEmoji() method
  • Now uses the new replaceEmoji() method internally with empty string replacement
  • Removes commented code as requested in the review
  • Maintains backward compatibility
  1. Comprehensive test coverage
  • testReplaceEmoji(): Tests basic replacement functionality
  • testReplaceEmojiPreservesNonEmojiShortcodes(): Ensures non-emoji shortcodes are preserved
  • testRemoveEmojiUsesReplaceEmoji(): Verifies the refactored relationship
  • All tests passing (17/17)
  1. Updated documentation
  • Added README examples showing the new replaceEmoji() method
  • Included a dedicated section explaining the functionality
  • Shows how only valid emoji are replaced while preserving other patterns

Key Features

✅ Smart emoji detection: Only replaces actual emoji shortcodes
✅ Preserves custom shortcodes: Non-emoji :word: patterns remain unchanged
✅ Flexible replacement: Any string can be used as replacement text
✅ Backward compatible: Existing removeEmoji() functionality unchanged
✅ Well tested: Comprehensive test coverage with edge cases
✅ Well documented: Clear examples and usage patterns

The implementation addresses all the review feedback from PR #31 and provides a robust, validated emoji replacement feature that only affects actual emoji while preserving other shortcode-like patterns in the text.

@bensinclair bensinclair requested review from Copilot and joshmcrae July 28, 2025 23:56
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new replaceEmoji() method to the LitEmoji library that allows replacing emoji with custom text while preserving non-emoji shortcode patterns. The implementation includes proper validation to only replace known emoji shortcodes.

  • Added replaceEmoji() method with callback-based validation using supported shortcodes
  • Refactored removeEmoji() to use the new replaceEmoji() method internally
  • Added comprehensive test coverage for the new functionality and edge cases

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/LitEmoji.php Implements the new replaceEmoji() method and refactors removeEmoji() to use it
tests/LitEmojiTest.php Adds comprehensive test coverage for the new method and its edge cases
README.md Documents the new replaceEmoji() method with usage examples

Comment on lines +217 to +223
$supportedShortcodes = array_keys(self::getShortcodes());

return preg_replace_callback('/:([\w_-]+):/', function($matches) use ($replacement, $supportedShortcodes) {
$shortcode = $matches[1];

// Only replace if this is a known emoji shortcode
if (in_array($shortcode, $supportedShortcodes)) {
Copy link

Copilot AI Jul 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The array_keys(self::getShortcodes()) call on line 217 and the in_array() check on line 223 could be inefficient for large shortcode lists. Consider using array_key_exists($shortcode, self::getShortcodes()) instead to avoid creating the keys array and use O(1) lookup.

Suggested change
$supportedShortcodes = array_keys(self::getShortcodes());
return preg_replace_callback('/:([\w_-]+):/', function($matches) use ($replacement, $supportedShortcodes) {
$shortcode = $matches[1];
// Only replace if this is a known emoji shortcode
if (in_array($shortcode, $supportedShortcodes)) {
$shortcodes = self::getShortcodes();
return preg_replace_callback('/:([\w_-]+):/', function($matches) use ($replacement, $shortcodes) {
$shortcode = $matches[1];
// Only replace if this is a known emoji shortcode
if (array_key_exists($shortcode, $shortcodes)) {

Copilot uses AI. Check for mistakes.
Copy link
Member

@joshmcrae joshmcrae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, however for efficiency I would follow the recommendation Copilot made for checking whether a shortcode is valid before replacement.

Checking whether a key is in an array is faster than checking if an element is in an array - it's a constant time check vs linear time (taking longer as the list of shortcodes increases).

You could still put the shortcodes in a variable at line 217 instead of calling self::getShortcodes() for every encountered shortcode, otherwise the change it recommends is good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants