Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unneeded code in Transliterator::postProcessText #14

Open
knusperpixel opened this issue Jun 21, 2016 · 2 comments
Open

Unneeded code in Transliterator::postProcessText #14

knusperpixel opened this issue Jun 21, 2016 · 2 comments

Comments

@knusperpixel
Copy link

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
Copy link
Member

stof commented Jun 21, 2016

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
Copy link
Author

@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);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants