#author("2021-03-21T17:33:09+09:00","","")
#author("2021-03-21T17:37:33+09:00","","")
#nofollow
#norelated
総数:&counter(total); 今日:&counter(today); 昨日:&counter(yesterday);

* もくじ [#gc5000a8]

#contents

&size(22){&color(blue){Setting up Visual Studio Code for php};};&br;

参考url: &br;
[[VisualStudioCodeのPHP>https://code.visualstudio.com/docs/languages/php]]&br;
[[【PHP】VSCodeでPHP開発環境を整えてみよう(2020年夏)【おすすめ拡張機能】>https://iwasiman.hatenablog.com/entry/20200727-php-on-vscode]]&br;


*リンティング Linting (Ctrl +,)[#bd2cf9d7]

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

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

~/.config/Code/User/settings.json &br;
 {
   "php.validate.executablePath": "/usr/bin/php"
 }



*スニペット Snippets (Ctrl + Space) [#wd77b2e9]


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



*PHP拡張機能 PHP extensions (Ctrl + Shift + X)[#hb553ca2]


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

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

**PHP Intelephense [#b9536408]
コーディング全般をサポートするエクステンションです。&br;
コードの入力補完、文法誤りチェック、マウスオーバー時のドキュメントの表示、親クラスなど違うファイル上のクラスの定義へのジャンプ……などが可能になります。&br;
&br;
VSCode自体もPHPの基本的な補完は行ってくれるので、&br;
バッティングするため設定の以下を変更します。&br;
&br;
|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
 }





*デバッグ PHP Debug extension の利用[#oe25a1dd]



XDebugを使用したPHPデバッグは、PHP Debug extension 機能を介してサポートされます。&br;
VSCodeで動作するようにXDebugを構成するためのPHP Debug extension 機能の指示に従います。&br;
&br;
参考url: &br;
[[PHP Debug Adapter for Visual Studio Code>https://marketplace.visualstudio.com/items?itemName=felixfbecker.php-debug]]&br;


**Installation [#pa40cfa1]


Install the extension: Press F1, type ext install php-debug.&br;
&br;
このextensionは、VS CodeとXdebug ( by Derick Rethans ) の間のdebug adapterです。&br;
Xdebugは、サーバーにインストールする必要のあるPHP extension (.so ファイル)です。
&br;

***1. Install Xdebug [#k5cb6b77]

[[installing xdebug on ubuntu desktop 20.04.2]]を参照&br;

***2. Configure PHP to use Xdebug [#k543d72b]

Configure PHP to use Xdebug by adding zend_extension=path/to/xdebug to your php.ini. &br;
The path of your php.ini is shown in your phpinfo() output under "Loaded Configuration File".&br;
&br;

[[installing xdebug on ubuntu desktop 20.04.2]]を参照&br;
&br;
http://192.168.3.13/info.phpで表示されたphpのsettingsの項目は下記だった。&br;
|Configuration File (php.ini) Path	| /etc/php/7.4/apache2 |
|Loaded Configuration File	| /etc/php/7.4/apache2/php.ini |
&br;
http://localhost/xdebug_info.php で表示されたphpのsettingsの項目は下記だった。&br;

|Configuration File (php.ini) Path	| /etc/php/7.4/cli |
|Loaded Configuration File	| /etc/php/7.4/apache2/php.ini |


***3. Enable remote debugging in your php.ini [#z89961ab]

For Xdebug v3.x.x:

 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. &br;
The extension still defaults to 9000, so make sure your configuration in launch.json and php.ini match.&br;
&size(22){&color(blue){make sure your configuration in launch.json and php.ini match.};};&br;

***4. If you are doing web development, don't forget to restart your webserver to reload the settings. [#tb55ec01]


***5. Verify your installation by checking your phpinfo() output for an Xdebug section. [#had7fdef]


**VS Code Configuration [#i812cf3b]

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


 ~/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 [#l869dbbe]


This setting will simply start listening on the specified port (by default 9000) for Xdebug.&br;
If you configured Xdebug like recommended above, &br;
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.&br;


***Launch currently open script [#xabc280e]


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



-Supported launch.json settings

|setting item|description|h
|request| Always "launch"|
|hostname|The address to bind to when listening for Xdebug (default: all IPv6 connections if available, else all IPv4 connections)|
|port|The port on which to listen for Xdebug (default: 9000)|
|stopOnEntry|Whether to break at the beginning of the script (default: false)|
|pathMappings|A list of server paths mapping to the local source paths on your machine, see "Remote Host Debugging" below|
|log|Whether to log all communication between VS Code and the adapter to the debug console. See Troubleshooting further down.|
|ignore|An optional array of glob patterns that errors should be ignored from (for example **/vendor/**/*.php)|
|xdebugSettings|Allows you to override Xdebug's remote debugging settings to fine tuning Xdebug to your needs. &br;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. &br;This can speed up the debugger on slow machines. &br;For a full list of feature names that can be set please refer to the Xdebug documentation.[[Xdebug documentation>https://xdebug.org/docs-dbgp.php#feature-names]]|


-xdebugSettings
|feature name|description|h
|max_children|max number of array or object children to initially retrieve|
|max_data|max amount of variable data to initially retrieve.|
|max_depth|maximum depth that the debugger engine may return when sending arrays, hashs or object structures to the IDE.|
|show_hidden|This feature can get set by the IDE if it wants to have more detailed internal information on properties (eg. private members of classes, etc.) &br; Zero means that hidden members are not shown to the IDE.|


-Options specific to CLI debugging
|program|Path to the script that should be launched|
|args|Arguments passed to the script|
|cwd|The current working directory to use when launching the script|
|runtimeExecutable|Path to the PHP binary used for launching the script. &br;By default the one on the PATH.|
|runtimeArgs|Additional arguments to pass to the PHP binary|
|externalConsole|Launches the script in an external console window instead of the debug console (default: false)|
|env|Environment variables to pass to the script|



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



**Remote Host Debugging [#f4baed45]


参考url:&br;
[[[PHP] Xdebug のリモートデバッグ、理解していますか?>https://qiita.com/castaneai/items/d5fdf577a348012ed8af]]&br;


[[Xdebug Step Debugging>https://xdebug.org/docs/step_debug]]&br;

***HTTP server 側 [#rd1bc97a]

To debug a running application on a remote host, &br;
you need to tell Xdebug to connect to a different IP than localhost. &br;
This can either be done &br;
-by setting xdebug.client_host to your IP or &br;
-by setting xdebug.discover_client_host to 1 to make Xdebug always connect back to the machine who did the web request.&br;
&br;
The latter is the only setting that supports multiple users debugging the same server and "just works" for web projects.&br;
Xdebug will then use the HTTP headers to find out the IP address of the host that initiated the debugging request, &br;
and use that IP address to connect to. &br;
This is a common way of set-up if you are sharing a development server among you and your team mates.
&br;
Again, please see the [[Xdebug documentation>https://xdebug.org/docs/remote#communcation]] on the subject for more information.&br;
&br;


***HTTP client 側 (Visual Studio Code側) [#ra572b7d]

To make VS Code map the files on the server to the right files on your local machine, &br;
you have to set the pathMappings settings in your launch.json. Example:&br;

 // server -> local
 "pathMappings": {
   "/var/www/html": "${workspaceFolder}/www",
   "/app": "${workspaceFolder}/app"
 }

例:~/test_php/html/配下のphpファイルのデバッグをする場合
launch.jsonファイルの位置

 ~/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,
             "pathMappings": { ←★
                 "/var/www/html": "${workspaceFolder}" ←★
             } ←★
         },
         {
             "name": "Launch currently open script",
             "type": "php",
             "request": "launch",
             "program": "${file}",
             "cwd": "${fileDirname}",
             "port": 9000
         }
     ]
 



* ''次は'' [#dacc7db7]
-php のインストール[[setting up visual studio code for php ubuntu desktop 20.04.2]]へ

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS