如何重新格式化代码,在行分隔符之前添加新行" \ n在PhpStorm中?

时间:2017-07-29 10:25:21

标签: php phpstorm

我有这个功能:

情形1:

public function searchArticle($keyword)
{
    $keyword = sanitize($keyword, 15);

    $sql = "SELECT a.*, c.id as cid, c.name" . Lang::$lang . " as catname, a.title" . Lang::$lang . " as atitle, c.slug as catslug, u.username,"
        . "\n (SELECT COUNT(artid) FROM " . self::cmTable . " WHERE artid = a.id) as totalcomments, YEAR(a.created) as year, MONTH(a.created) as month, DATE_FORMAT(a.created, '%d') as day,"
        . "\n (SELECT GROUP_CONCAT(DISTINCT participant) FROM " . self::partTable . " WHERE FIND_IN_SET(id,a.participants) > 0)as participants"
        . "\n FROM " . self::mTable . " as a"
        . "\n LEFT JOIN " . self::ctTable . " as c ON c.id = a.cid" . "\n LEFT JOIN users as u ON u.id = a.uid"
        . "\n WHERE MATCH (title" . Lang::$lang . ", body" . Lang::$lang . ") AGAINST ('" . self::$db->escape($keyword) . "*' IN BOOLEAN MODE)"
        . "\n AND a.created <= NOW()" . "\n AND (a.expire = '0000-00-00 00:00:00' OR a.expire >= NOW())"
        . "\n AND a.active = 1"
        . "\n ORDER BY a.created DESC LIMIT 20";
    $row = self::$db->fetch_all($sql);

    return ($row) ? $row : 0;
}

每一行都是应有的。

现在,当我使用代码重新格式化选项时,此代码样式如下所示:

情况2:

public function searchArticle($keyword)
{
    $keyword = sanitize($keyword, 15);

    $sql = "SELECT a.*, c.id as cid, c.name" . Lang::$lang . " as catname, a.title" . Lang::$lang . " as atitle, c.slug as catslug, u.username," . "\n (SELECT COUNT(artid) FROM " . self::cmTable . " WHERE artid = a.id) as totalcomments, YEAR(a.created) as year, MONTH(a.created) as month, DATE_FORMAT(a.created, '%d') as day," . "\n (SELECT GROUP_CONCAT(DISTINCT participant) FROM " . self::partTable . " WHERE FIND_IN_SET(id,a.participants) > 0)as participants" . "\n FROM " . self::mTable . " as a" . "\n LEFT JOIN " . self::ctTable . " as c ON c.id = a.cid" . "\n LEFT JOIN users as u ON u.id = a.uid" . "\n WHERE MATCH (title" . Lang::$lang . ", body" . Lang::$lang . ") AGAINST ('" . self::$db->escape($keyword) . "*' IN BOOLEAN MODE)" . "\n AND a.created <= NOW()" . "\n AND (a.expire = '0000-00-00 00:00:00' OR a.expire >= NOW())" . "\n AND a.active = 1" . "\n ORDER BY a.created DESC LIMIT 20";
    $row = self::$db->fetch_all($sql);

    return ($row) ? $row : 0;
}

我能得到的最接近的是设置&#34;二进制表达式&#34; to&#34;总是包装&#34; in&#34;首选项/代码风格/ PHP / Wapping和Braces&#34;,但它仍然不是应该的,并且它的可读性不是很好。

情形3:

public function searchArticle($keyword)
{
    $keyword = sanitize($keyword, 15);

    $sql =
        "SELECT a.*, c.id as cid, c.name" .
        Lang::$lang .
        " as catname, a.title" .
        Lang::$lang .
        " as atitle, c.slug as catslug, u.username," .
        "\n (SELECT COUNT(artid) FROM " .
        self::cmTable .
        " WHERE artid = a.id) as totalcomments, YEAR(a.created) as year, MONTH(a.created) as month, DATE_FORMAT(a.created, '%d') as day," .
        "\n (SELECT GROUP_CONCAT(DISTINCT participant) FROM " .
        self::partTable .
        " WHERE FIND_IN_SET(id,a.participants) > 0)as participants" .
        "\n FROM " .
        self::mTable .
        " as a" .
        "\n LEFT JOIN " .
        self::ctTable .
        " as c ON c.id = a.cid" .
        "\n LEFT JOIN users as u ON u.id = a.uid" .
        "\n WHERE MATCH (title" .
        Lang::$lang .
        ", body" .
        Lang::$lang .
        ") AGAINST ('" .
        self::$db->escape($keyword) .
        "*' IN BOOLEAN MODE)" .
        "\n AND a.created <= NOW()" .
        "\n AND (a.expire = '0000-00-00 00:00:00' OR a.expire >= NOW())" .
        "\n AND a.active = 1" .
        "\n ORDER BY a.created DESC LIMIT 20";
    $row = self::$db->fetch_all($sql);

    return ($row) ? $row : 0;
}

如何在PhpStorm中设置代码重新格式设置,以便仅在之前强制换行。 &#34; \ n? (正如在案例1中可以看到的)

1 个答案:

答案 0 :(得分:2)

heredoc syntax用于包含查询的字符串。

您可以将变量直接嵌入到字符串中(又名variables interpolation)。我看到你也使用类静态成员和类常量来组成查询,并且在字符串解析期间无法识别它们。但您仍然可以使用sprintf()嵌入它们:

$sql = <<< END_QUERY
    SELECT a.*, c.id as cid, c.name%s as catname, a.title%s as atitle, c.slug as catslug, u.username,
        (SELECT COUNT(artid) FROM %s WHERE artid = a.id) as totalcomments, YEAR(a.created) as year, MONTH(a.created) as month, DATE_FORMAT(a.created, '%%d') as day,
        (SELECT GROUP_CONCAT(DISTINCT participant) FROM %s WHERE FIND_IN_SET(id,a.participants) > 0)as participants
    FROM %s as a
        LEFT JOIN %s as c ON c.id = a.cid
        LEFT JOIN users as u ON u.id = a.uid
    WHERE MATCH (title%s, body%s) AGAINST ('%s*' IN BOOLEAN MODE)
        AND a.created <= NOW()
        AND (a.expire = '0000-00-00 00:00:00' OR a.expire >= NOW())
        AND a.active = 1
    ORDER BY a.created DESC
    LIMIT 20
END_QUERY;

$query = sprintf($query, Lang::$lang, Lang::$lang, self::cmTable, self::partTable, self::mTable, self::ctTable, Lang::$lang, Lang::$lang, self::$db->escape($keyword));

因为百分号(%)是sprintf()的特殊字符,所以您必须加倍才能让它自己代表(DATE_FORMAT(a.created, '%d')成为DATE_FORMAT(a.created, '%%d')

或者,如果在字符串之前将类静态属性,类常量和函数调用提取到局部变量中,则可以避免使用sprintf()

$lang    = Lang::$lang;
$cmTable = Lang::cmTable;
$kword   = self::$db->escape($keyword);
// ...
$sql = <<< END_QUERY
    SELECT a.*, c.id as cid, c.name{$lang} as catname, a.title{$lang} as atitle, c.slug as catslug, u.username,
// ...

这样代码格式化工具将不再触及查询。作为奖励,查询更容易阅读,因为它是所有文本,没有更多的引号和字符串连接。