发布于 2026-01-05 3 阅读
0

使用 Visual Studio Code 进行 PHP 开发的最佳 setting.json 文件

使用 Visual Studio Code 进行 PHP 开发的最佳 setting.json 文件

概述

PHP 是一种动态的面向对象语言,它为开发者带来了面向对象编程 (OOP) 的强大功能以及动态类型的灵活性。PHP支持类型注解,允许您定义函数参数的运行时类型。

集成开发环境 (IDE) 可以提高工作效率,因为大多数 IDE 都内置了自动补全和其他实用功能,例如“跳转到定义”和“查找所有引用”。PHPStorm
一款流行的 IDE ,但今天我们将重点介绍哪些 Visual Studio Code 扩展可以将 Visual Studio Code 变成功能齐全的 PHP IDE 替代方案。

能力越大,责任越大。Visual Studio Code 是一款功能强大的文本编辑器,并提供了极其强大的扩展 API,因此很容易导致 settings.json 文件出现混乱。本文最后,您的 Visual Studio Code 将具备以下提升效率的功能:

  • 错误、警告和其他自省,例如未使用的变量。
  • 代码补全
  • 函数/方法签名参数提示。
  • 转到定义
  • DotEnv 语法高亮
  • 查找所有引用
  • 鼠标悬停在方法上时会显示文档
  • 路径自动补全,适用于字符串字面量文件路径。
  • PSR 代码嗅探器实时发出错误和警告。
  • 自动 PHP 文档块。

这些功能将通过安装以下扩展程序来实现:

简而言之:完整的设置文件在 GitHub 上。

第一步。

首先,我们需要禁用默认的 PHP 支持。这可以通过修改 .vscode/settings.json 文件中的两个配置项来实现。本文中的代码片段摘自完整的文件

{
 "php.validate.enable": false,
 "php.suggest.basic": false,
}

这将使我们的扩展程序能够发挥其最大效用,并防止任何功能冲突。

第二步。

默认情况下,php-redis 扩展未包含在默认配置中。这意味着您将无法使用Redis类的自动补全功能。现在,让我们来配置 Intelephense 的最佳设置。一个小技巧:在 VS Code 中编辑 setting.json 文件时,您可以通过输入冒号 (:) 并单击工具提示来查看配置项的默认值。

"intelephense.stubs": [
    "apache",
    "redis",
    "bcmath",
    "bz2",
    "calendar",
    "com_dotnet",
    "Core",
    "ctype",
    "curl",
    "date",
    "dba",
    "dom",
    "enchant",
    "exif",
    "fileinfo",
    "filter",
    "fpm",
    "ftp",
    "gd",
    "hash",
    "iconv",
    "imap",
    "interbase",
    "intl",
    "json",
    "ldap",
    "libxml",
    "mbstring",
    "mcrypt",
    "meta",
    "mssql",
    "mysqli",
    "oci8",
    "odbc",
    "openssl",
    "pcntl",
    "pcre",
    "PDO",
    "pdo_ibm",
    "pdo_mysql",
    "pdo_pgsql",
    "pdo_sqlite",
    "pgsql",
    "Phar",
    "posix",
    "pspell",
    "readline",
    "recode",
    "Reflection",
    "regex",
    "session",
    "shmop",
    "SimpleXML",
    "snmp",
    "soap",
    "sockets",
    "sodium",
    "SPL",
    "sqlite3",
    "standard",
    "superglobals",
    "sybase",
    "sysvmsg",
    "sysvsem",
    "sysvshm",
    "tidy",
    "tokenizer",
    "wddx",
    "xml",
    "xmlreader",
    "xmlrpc",
    "xmlwriter",
    "Zend OPcache",
    "zip",
    "zlib"
],
"intelephense.telemetry.enabled": false,
"intelephense.format.enable": true,
"intelephense.completion.triggerParameterHints": true,
"intelephense.completion.insertUseDeclaration": true,
"intelephense.completion.fullyQualifyGlobalConstantsAndFunctions": true,
"intelephense.trace.server": "messages",
"intelephense.files.exclude": [
    "**/.git/**",
    "**/.svn/**",
    "**/.hg/**",
    "**/CVS/**",
    "**/.DS_Store/**",
    "**/node_modules/**",
    "**/bower_components/**",
    "**/storage",
    "**/storage/**",
    "**/tests/**",
    "**/resources/views/**",
    "**/database/migrations/**",
    "**/storage/framework/views/**",
    "_ide_helper.php",
    "_ide_helper_models"
]

步骤 3 PHP 代码嗅探器设置

"phpcs.executablePath": "vendor/bin/phpcs",
"phpcs.enable": true,
"phpcs.autoConfigSearch": true,
"phpcs.standard": "./phpcs.xml",
"phpcs.showWarnings": true,
"phpcs.showSources": true,
"phpcs.ignorePatterns": [
    "**/.git/**",
    "**/.svn/**",
    "**/.hg/**",
    "**/CVS/**",
    "**/.DS_Store/**",
    "**/node_modules/**",
    "**/bower_components/**",
    "**/storage/**",
    "**/resources/views/**",
    "**/database/migrations/**",
    "**/tests/**",
    "**/storage/framework/views/**",
    "**/vendor",
    "_ide_helper.php",
    "_ide_helper_models"
]

步骤 4. PHPDoc 代码块设置

"php-docblocker.returnGap": true,
"php-docblocker.qualifyClassNames": true,
"php-docblocker.author": {
    "name": "Ryan McCullagh", 
    "email": "ryan@amezmo.com"
}

步骤 5. 可选设置。

这些设置更偏向个人喜好,但我在这里分享它们,是因为我认为它们很棒。

"search.collapseResults": "alwaysCollapse",
"search.exclude": {
    "**/node_modules": true,
    "**/bower_components": true,
    "**/.git": true,
    "**/storage": true,
    "**/tests": true,
    "_ide_helper.php": true,
    "_ide_helper_models.php": true,
    "package-lock.json": true
},
"files.eol": "\n",
"files.insertFinalNewline": true,
"files.trimTrailingWhitespace": true,
"files.associations": {
    "*.blade.php": "html",
    "*.php": "php",
    "*.vue": "vue",
    "*.php-cs": "php"
},
"editor.fontSize": 14,
"editor.letterSpacing": 0.5,
"editor.lineHeight": 24,
"editor.cursorBlinking": "smooth",
"editor.fontLigatures": false,
"editor.formatOnType": false,
"editor.formatOnPaste": false,
"editor.autoIndent": "advanced",
"git.showPushSuccessNotification": true,
"editor.gotoLocation.multipleDefinitions": "goto",
"editor.snippetSuggestions": "bottom",
"workbench.editor.enablePreview": false,
"workbench.editor.enablePreviewFromQuickOpen": false,

最后两个功能很棒,因为当您通过“查找所有引用”导航到文件或单击“转到定义”时,编辑器将保持打开状态。

文章来源:https://dev.to/ryan1/the-best-setting-json-for-php-development-with-visual-studio-code-4agc