Search code examples
phphtmlstringintellij-ideaphpstorm

Does PhpStorm have a way to highlight hardcoded strings?


I have a config file where I store all my strings for easy reference and to make translation easier - just in case I ever need to roll out my application into one of our international locations.

I would like to remove my hardcoded strings from both HTML and PHP code.

This site shows that IntelliJ IDEA is capable of highlighting hardcoded strings. However, I see no such option in my PhpStorm 2017.2 installation.

PhpStorm Settings

This is one example in HTML that I'd like to purge and replace with non-hardcoded strings:

<thead>
<tr>
    <th class="text-center">#</th>
    <th>Benutzername</th>
    <th>Nachname</th>
    <th>Vorname</th>
    <th>E-Mail</th>
    <th>Rollen</th>
    <th></th>
</tr>
</thead>

However, PhpStorm does not highlight the hardcoded strings or suggests any fix. Same goes for PHP code.

error highlighting

If it's of any interest, here's how I usually retrieve strings from my config array.

/**
 * Extract a string from the config file depending on its tag.
 *
 * @param string $tag The string to extract.
 *
 * @return mixed The string that was extracted. At least it should usually be a string, but it could also be
 *               something different, depending on the config.
 */
function get_string($tag)
{
    // Check the config file first.
    global $config;
    $string = $config['strings'][$tag];
    if (!isset($string)) {
        // The faulty tag can be found in the logfile.
        logfile("String Tag not found: " . $tag);
        $string = $tag;
    }

    return $string;
}

I simply give the array index to the function and retrieve the string from the config array. If I ever need internationalisation, I can modify this function.


Solution

  • You can use 'cmd + f' and search for >[a-z0-9] which does a decent job finding any instances where an HTML tag ends and a string begins