简体   繁体   中英

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 设置

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.

错误高亮

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.

@axiac provided a different point of view.

HTML is in itself basically full of hardcoded strings - tags et cetera.

PHP requires many strings to work. $_GET[] , $_POST[] and array indices like my $config['strings'][$tag] are a few examples.

If PhpStorm were to highlight all of these strings, all of them would have to be excluded from inspection manually.

As @axiac said:

You can use the "Find in Files" command to find the strings in the PHP scripts (search for ['"] and check "RegExp") but you'll probably be overwhelmed by the big number of strings, many of them being array keys or other strings (separators, SQL queries) that are part of the code, not of the output.

您可以使用 'cmd + f' 并搜索 >[a-z0-9] ,它可以很好地找到 HTML 标记结束和字符串开始的任何实例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM