総数:19 今日:1 昨日:0

もくじ

Setting up Visual Studio Code for php

参考url:
VisualStudioCodeのPHP
【PHP】VSCodeでPHP開発環境を整えてみよう(2020年夏)【おすすめ拡張機能】

リンティング Linting (Ctrl +,)

PHP設定を変更するには、ユーザー設定またはワークスペース設定(Ctrl +,)を開き、「php」と入力して、使用可能な設定のリストをフィルタリングします。

PHPリンターを制御するための3つの設定があります。

設定項目説明設定手順
php.validate.enablePHPリンティングを有効にするかどうかを制御します。デフォルトで有効になっています。
php.validate.executablePathディスク上のPHP実行可能ファイルを指します。PHP実行可能ファイルがシステムパス上にない場合は、これを設定します。PHP実行可能パスを設定するには、[ PHP]> [検証:実行可能パス]の下の[ settings.jsonで編集]リンクを選択します。これにより、ユーザーファイルが開きます。PHPインストールへのパスを含む設定を追加します。
php.validate.run検証が保存時(値:)"onSave"またはタイプ(値:)のどちらでトリガーされるかを制御します"onType"。デフォルトは保存時です。

/.config/Code/User/settings.json

{
  "php.validate.executablePath": "/usr/bin/php"
}

スニペット Snippets (Ctrl + Space)

Visual Studio Codeには、PHPの一般的なスニペットのセットが含まれています。
これらにアクセスするには、Ctrl + Spaceを押して、コンテキスト固有のリストを取得します。

PHP拡張機能 PHP extensions (Ctrl + Shift + X)

VS Code Marketplaceには多くのPHP言語拡張機能があり、さらに多くのものが作成されています。
拡張機能ビュー(Ctrl + Shift + X)でVS Code内からPHP拡張機能を検索し、
「php」と入力して拡張機能のドロップダウンリストをフィルタリングできます。

PHP Debug

PHPをインストールするときにほぼ必ず一緒に入れるデバッグツール
Xdebugのインストールが必須です。
VSCode上で再生ボタンや停止ボタン的なUIを操作しながら、1行ずつデバッグ実行ができるようになります。

PHP Intelephense

コーディング全般をサポートするエクステンションです。
コードの入力補完、文法誤りチェック、マウスオーバー時のドキュメントの表示、親クラスなど違うファイル上のクラスの定義へのジャンプ……などが可能になります。

VSCode自体もPHPの基本的な補完は行ってくれるので、
バッティングするため設定の以下を変更します。

PHP > Suggest: Basic組み込みの言語候補機能。Ctrl+, でSettings画面開き、Extensions->PHPで表示させて、チェックを外す(false)
PHP > Validate: Enable組み込みのPHP検証。Ctrl+, でSettings画面開き、Extensions->PHPで表示させて、チェックを外す(false)

/.config/Code/User/settings.json

{
    "php.validate.executablePath": "/usr/bin/php",
    "php.suggest.basic": false,
    "php.validate.enable": false
}

デバッグ

XDebugを使用したPHPデバッグは、PHP Debug extension 機能を介してサポートされます。
VSCodeで動作するようにXDebugを構成するためのPHP Debug extension 機能の指示に従います。

参考url:
PHP Debug Adapter for Visual Studio Code

Enable remote debugging in your php.ini:

sudo gvim -f /etc/php/7.4/apache2/php.ini
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.client_port = 9000

下記のファイルも追記する。

sudo gvim -f /etc/php/7.4/cli/php.ini

Please note that the default Xdebug port changed between Xdebug v2 to v3 from 9000 to 9003.
The extension still defaults to 9000, so make sure your configuration in launch.json and php.ini match.
make sure your configuration in launch.json and php.ini match.

VS Code Configuration

In your project, go to the debugger and hit the little gear icon and choose PHP.
A new launch configuration will be created for you with two configurations:

~/test_php/html/.vscode/launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

Listen for Xdebug

This setting will simply start listening on the specified port (by default 9000) for Xdebug.
If you configured Xdebug like recommended above,
everytime you make a request with a browser to your webserver or launch a CLI script Xdebug will connect and you can stop on breakpoints, exceptions etc.

Launch currently open script

This setting is an example of CLI debugging.
It will launch the currently opened script as a CLI,
show all stdout/stderr output in the debug console
and end the debug session once the script exits.

Supported launch.json settings

setting itemdescription
requestAlways "launch"
hostnameThe address to bind to when listening for Xdebug (default: all IPv6 connections if available, else all IPv4 connections)
portThe port on which to listen for Xdebug (default: 9000)
stopOnEntryWhether to break at the beginning of the script (default: false)
pathMappingsA list of server paths mapping to the local source paths on your machine, see "Remote Host Debugging" below
logWhether to log all communication between VS Code and the adapter to the debug console. See Troubleshooting further down.
ignoreAn optional array of glob patterns that errors should be ignored from (for example **/vendor/**/*.php)
xdebugSettingsAllows you to override Xdebug's remote debugging settings to fine tuning Xdebug to your needs.
For example, you can play with max_children and max_depth to change the max number of array and object children that are retrieved and the max depth in structures like arrays and objects.
This can speed up the debugger on slow machines.
For a full list of feature names that can be set please refer to the Xdebug documentation.Xdebug documentation

xdebugSettings

feature namedescription
max_childrenmax number of array or object children to initially retrieve
max_datamax amount of variable data to initially retrieve.
max_depthmaximum depth that the debugger engine may return when sending arrays, hashs or object structures to the IDE.
show_hiddenThis feature can get set by the IDE if it wants to have more detailed internal information on properties (eg. private members of classes, etc.)
Zero means that hidden members are not shown to the IDE.

Options specific to CLI debugging

programPath to the script that should be launched
argsArguments passed to the script
cwdThe current working directory to use when launching the script
runtimeExecutablePath to the PHP binary used for launching the script.
By default the one on the PATH.
runtimeArgsAdditional arguments to pass to the PHP binary
externalConsoleLaunches the script in an external console window instead of the debug console (default: false)
envEnvironment variables to pass to the script

Features

Line breakpoints
Conditional breakpoints
Function breakpoints
Step over, step in, step out
Break on entry
Breaking on uncaught exceptions and errors / warnings / notices
Multiple, parallel requests
Stack traces, scope variables, superglobals, user defined constants
Arrays & objects (including classname, private and static properties)
Debug console
Watches
Run as CLI
Run without debugging

Remote Host Debugging

[PHP] Xdebug のリモートデバッグ、理解していますか?

Xdebug Step Debugging

次は


トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS