首页 > 解决方案 > Visual Studio Code 将如何使用像 PhpStorm 这样的 PHP 格式?

问题描述

我已经使用phpfmt扩展来缩进像这样格式化代码的代码

'cms' => [
    'class' => 'yii2mod\cms\Module',
    'controllerNamespace' => 'backend\controllers',
    'defaultRoute' => 'cms',
],

当我合并它时返回代码意图错误。我需要像 PhpStorm 那样格式化代码

'cms'   => [
    'class'               => 'yii2mod\cms\Module',
    'controllerNamespace' => 'backend\controllers',
    'defaultRoute'        => 'cms',
],

哪个扩展以及如何在 Visual Studio Code 中使用它来消除 Visual Studio Code 中的 PHP 预期错误?

标签: phpvisual-studio-codephpstorm

解决方案


您可以使用我用于我的开发环境的以下设置,这将最接近您正在寻找的内容。

将以下内容添加到您settings.json的 VSCode 中。

//phpfmt
"phpfmt.php_bin": "php",
"phpfmt.passes": [
    "AlignPHPCode",
    "AlignTypeHint",
    "AddMissingParentheses",
    "ConvertOpenTagWithEcho",
    "DocBlockToComment",
    "IndentTernaryConditions",
    "JoinToImplode",
    "PSR2KeywordsLowerCase",
    "PSR2LnAfterNamespace",
    "PSR2CurlyOpenNextLine",
    "PSR2ModifierVisibilityStaticOrder",
    "PSR2SingleEmptyLineAndStripClosingTag",
    "ReindentSwitchBlocks",
    "RemoveUseLeadingSlash",
    "StripExtraCommaInArray",
    "SpaceBetweenMethods",
],
"phpfmt.exclude": [
    "ReindentComments",
    "StripNewlineWithinClassBody"
],
"phpfmt.psr2": false,

推荐阅读