Behat / Transliterator

Behat Transliterator library inherited from Doctrine1 and used in Behat for snippet generation
Other
2.05k stars 30 forks source link

Unneeded code in Transliterator::postProcessText #14

Open knusperpixel opened 8 years ago

knusperpixel commented 8 years ago

If I understand the code correctly, there is unneccessary code in Transliterator::postProcessText (https://github.com/Behat/Transliterator/blob/master/src/Behat/Transliterator/Transliterator.php#L568)

The first thing the function does is lowercase the input. After that, in line 580 there is a lot of preg_replacegoing on, checking uppercase chars and finally lowercasing the whole string again.

I was wondering why that is there. I think, the code could be simplified

before

$text = strtolower(preg_replace('/[^A-Za-z0-9\/]+/', $separator,
            preg_replace('/([a-z\d])([A-Z])/', '\1_\2',
                preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2',
                    preg_replace('/::/', '/', $text)))));

after

$text = preg_replace('/[^a-z0-9\/]+/', $separator, preg_replace('/::/', '/', $text));
stof commented 8 years ago

you don't understand it right. Lowercasing the string is the last thing it does. The other thing it does is converting camelCase to underscore-separated words

knusperpixel commented 8 years ago

@stof Sorry, but the first thing the code does is lowercasing it. So this whole camelCase to underscore_seperated words thing can't happen, I think.

   /**
     * Cleans up the text and adds separator.
     *
     * @param string $text
     * @param string $separator
     *
     * @return string
     */
    private static function postProcessText($text, $separator)
    {
        if (function_exists('mb_strtolower')) {
            $text = mb_strtolower($text);
        } else {
            $text = strtolower($text);
        }
        // Remove all none word characters
        $text = preg_replace('/\W/', ' ', $text);
        // More stripping. Replace spaces with dashes
        $text = strtolower(preg_replace('/[^A-Za-z0-9\/]+/', $separator,
            preg_replace('/([a-z\d])([A-Z])/', '\1_\2',
                preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2',
                    preg_replace('/::/', '/', $text)))));
        return trim($text, $separator);
    }