起因:
因為sublime 的 Xdebug 插件已經很久沒在更新了https://github.com/martomo/SublimeTextXdebug
所以在新版的php和xdebug中會有不兼容的情況
https://github.com/martomo/SublimeTextXdebug/issues/172#issuecomment-363770737
而且workerman 的events.php 裡面因為xdebug版本太低斷不到,而xdebug版本太高sublime又不兼容。所以轉向有在維護更新的phpstorm
熱鍵
Settings => ctrl + alt + sSettings => Keymap => 選Sublime Text
搜尋某個熱鍵榜定了什麼功能(有無被使用)
Keymap => 點擊搜尋圖示 => 輸入熱鍵。ex. ctrl+shift+p 查出已經榜訂了Find Action
打開Version Control 面板(看git log) => alt + 9
Sublime: ( set_mark + select_to_mark ) => ( Find Action )Toggle Sticky Selection
Sublime: alignment ( ctrl+alt+a ) => ( Find Action ) Reformat Code
Sublime: Emmet - Go to Matching Pair => ctrl + m ( Move Caret to Matching Brace )
快速加入try / catch: 選擇代碼範圍 => ( Find Action ) Surround With => try / catch ... finally 要手動加
對某段(HTML)代碼縮排 => 選擇後 => ( Find Action ) Auto-Indent Lines - Ctrl+Alt+I
複製純文本(貼上word時不帶樣式)
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206176309-Disable-rich-text-copying-?page=1#community_comment_360000433359 Disable rich-text copying?
ctrl + shift + p (Find Action) => Copy as Plain Text
找檔案中下一個error
https://intellij-support.jetbrains.com/hc/en-us/community/posts/207031535-sometimes-that-is-difficult-to-find-place-of-error?page=1#community_comment_207369639 sometimes that is difficult to find place of error
ctrl + shift + p (Find Action) => Next Highlighted Error
自定義熱鍵
Resume Program(XDebug Run,跳到下一個breakpoint):F9https://stackoverflow.com/questions/14223631/in-phpstorm-how-do-you-untab-to-the-previous-tabstop/14746035
Next Line Bookmark in Editor
原本2021.2.2原本 F2 是在同一個文件中跳到下一個書籤,2022.1.1 是會跨文件跳書籤。如果要維持在同一個文件中跳書籤:
Next Line Bookmark:F2 => Next Line Bookmark in Editor:F2
Toggle Sticky Selection: ctrl+alt+shift+s
=> 避免使用shift + s,因為中文輸入時按shift + s打不出S
Path with line number
在某個新版後,在Find in Files(Ctrl+Shift+F)中Copy Reference 不再帶行號
Copy Reference:Ctrl+Alt+Shift+C => Path with line number:Ctrl+Alt+Shift+C
樣式:
配色方案(color scheme)File => Settings => Editor => Color Scheme
因為弱報錯提示不明顯,所以不使用 Monokai ,如圖:
使用PHPStorm預設的Darcula?
斷點時string和int的顏色區別不明顯,所以不使用Darcula
使用Monokai Pro Theme
Settings => Plugins => Marketplace 下載安裝 Monokai Pro Theme
Appearance & Behavior => Appearance => Theme 改回Darcular
Editor => Color Scheme 選擇 Monokai Pro (非 Monokai Pro(Classic))
字型大小:
Settings => Editor => Font
字型大小(外觀Appearance):
改變編輯器(Editor)以外的字型和大小
Settings => Appearance & Behavior => Appearance
改變前
改變後
版本控制(以github 為例)
1. 安裝git bash => PHPStorm 會自動偵測到git路徑,因為git也加Windows PowerShell 的環境變量上了2. git bash 新增公鑰和私鑰
$ ssh-keygen -C "username@email.com" -t rsa
然後把公鑰 .ssh/id_rsa.pub 上傳到github上
ps. 現在github可以開私人project了
3. 拉取代碼
Find Action.. => Checkout from version control => Git => 設定路徑
ps. 效果等同在 git bash上直接 git clone下來
Live Template
如同sublime 的 sinppet,
例:
epc
Applicable in ... => 該template 在什麼語言的什麼地方會提示
echo "<pre>\$VARIABLE$:";
print_r($VARIABLE$);
echo "</pre>";
epc
Applicable in ... => 該template 在什麼語言的什麼地方會提示
echo "<pre>\$VARIABLE$:";
print_r($VARIABLE$);
echo "</pre>";
Editor
General
https://stackoverflow.com/questions/20486542/selecting-entire-variable-with-double-click Selecting entire variable with double click點兩下變數不選$:Settings => Editor => General => Smart Keys => Select variable name without '$' sign on double click (預設沒勾,勾了就不選$)
避免打開太多tab使得PHPstorm把tab關掉
Editor | General | Editor Tabs | Tab closing policy | tab limit 設一個大一點的數
增加剪貼板(clipboard )歷史記錄
https://stackoverflow.com/a/10337122 How to increase PHPStorm clipboard history size?
Settings (Preferences on Mac) | Editor | Maximum number of contents to keep in clipboard.
自動在頭部use命名空間
https://blog.csdn.net/vcit102/article/details/117081161 phpstorm 自动在头部 use 命名空间
Editor | General | Auto Import | PHP | (勾選)Enable auto-import in file scope
Code Style
註解comment 不要從第一行開始
https://stackoverflow.com/a/48694397 How to set the style of line comment in phpstorm
Preferences > Editor > Code Style > PHP > (tab) Code Generation > Comment Code > (uncheck) Line comment at first column + 勾選"Add a space at comment start"
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206332339-Tabs-and-Indents-are-2-spaces-despite-setting-at-4-spaces?page=1#community_comment_206876035 Tabs and Indents are 2 spaces despite setting at 4 spaces
如果 Reformat Code 不生效,請嘗試
Settings => Editor => Code Style => 取消勾選 "Detect and use existing file indents for editing"
Settings => Editor => Code Style => 取消勾選 "Detect and use existing file indents for editing"
讓 array 的key和value對齊
https://stackoverflow.com/a/29795404 Stop PhpStorm from aligning associative arrays
Settings | Editor | Code Style | PHP | Wrapping and Braces --> Array initializer | Align key-value pairs
讓 class的 const 和value 對齊
Settings | Editor | Code Style | PHP | Wrapping and Braces --> Class field/constant groups | Align constants
Diff
選擇文件與當前文件比較
ctrl + shift + p => compare with
剪貼簿與當前文件比較
ctrl + shift + p => compare with Clipboard
underscore.js template
在Settings 中的 Plugins => Marketplace => 搜尋EJS => install 安裝,裝完須重啟IDE
設置特定文件為EJS格式:Settings => Editor => File Types => EJS => 新增文件名
.env - Plugins下載 .env files support
Deployment
Settings => Deployment => Connection
配置SFTP
Root path: 專案路徑
Encoding for client-server communication: UTF-8
Mappings:
Deployment path: /
Excluded Paths:
本地的 .idea、vendor (PHPStorm 資料夾)不要上傳
Settings => Deployment => Options
Upload changed files automatically to default server: Always
ps. 所有git 操作都在Windows上 PHPStorm的git 或 ConEmu64 + git bash,如果是從Server上Deployment下載下來的話,會下載完又馬上同步上Server(有點坑)
ps2. Server 分支不用管,全看Windows上 git bash的分支。除非弄亂了再兩邊切到同一個分支的同一個commit再繼續工作
ps3. 如果項目沒git,Upload changed files automatically to default server等先 Deployment => Download from x.x.x.x 下載完之後再改Always,第一次下載完就不會馬上同步上去
Deployment path: /
Excluded Paths:
本地的 .idea、vendor (PHPStorm 資料夾)不要上傳
Upload changed files automatically to default server: Always
ps. 所有git 操作都在Windows上 PHPStorm的git 或 ConEmu64 + git bash,如果是從Server上Deployment下載下來的話,會下載完又馬上同步上Server(有點坑)
ps2. Server 分支不用管,全看Windows上 git bash的分支。除非弄亂了再兩邊切到同一個分支的同一個commit再繼續工作
ps3. 如果項目沒git,Upload changed files automatically to default server等先 Deployment => Download from x.x.x.x 下載完之後再改Always,第一次下載完就不會馬上同步上去
上傳時報錯:Failed to change timestamp of the file 'your.php'
Settings => Deployment => Options
Preserve files timestamps:取消勾選
在Find in Files (Ctrl+Shift+F)中換行
直接點圖Soft-Wrap Preview Editor
在所選裡面搜尋search in selection
Ctrl+f搜尋逗號(,)=>選擇範圍 => Search In Selection => Select All Occurrences
XDebug
遠程調試
適用於:本機http調試(xampp、phpstudy)、虛擬機或內網測試機http調試+cli調試
如果忽然不能斷點,檢查本機IP是否變動,更改php.ini的 xdebug.client_host 為本機IP
Add New Configuration -> PHP Remote Debug
IDE key(session id): PHPSTORM (搭配 chrome 的 Xdebug helper)
Server 不需要配,打開 Start Listening for PHP Debug Connections 和 Xdebug helper 會自動import
如果server 上裝了兩個PHP(5.5 + 7.3 ) 都裝了XDebug,但是只有 5.5的能進斷點,7.3進不了 => 解法:不要裝在同一台,hyper-v 開虛擬機裝PHP 7.3環境。或把php 7.3 + XDebug 跑在laradock上
(可選)Settings => Languages & Framworks => PHP => 配置 PHP language level 和 CLI Interpreter (本機php環境選擇 Local Path to Interpreter...)Settings => Languages & Framworks => PHP => Debug => External connections => Break at first line in PHP scripts 勿勾選,以免workerman 調試時一直斷在新進程開始處
laradock 中找不到路徑
解法:
在servers中配置laradock的server
對物件斷點
以laravel passport 登錄時
vendor/league/oauth2-server/src/AuthorizationServer.php:192
為例,$this->enabledGrantTypes跑foreach時,只需要在 personal_access 時斷點
strpos("\Laravel\Passport\Bridge\PersonalAccessGrant", get_class($grantType)) !== false
註:
get_class($grantType) => 因為foreach沒指定index,這邊用get_class去取出類命名空間
Laravel\Passport\Bridge\PersonalAccessGrant
strpos => 判斷是否子字串
false => 0也是判斷成功,false才是失敗
\Laravel\Passport\Bridge\PersonalAccessGrant 怎麼快速複製出來?
展開 $this->enabledGrantTypes => 右鍵 => Copy Type更多:
Jump To Type Source => 打開 vendor/laravel/passport/src/Bridge/PersonalAccessGrant.php
More (Ctrl + Shift + F8) => 顯示所有斷點
本地調試
適用於:本機cli(腳本、爬蟲),勿使用git bash(斷不到)Add configuration ...
Add New Configuration => PHP Script
設定 Name, File, Interpreter 設定 CLI Interpreter
PHPStorm的XDebug也會有bug
https://www.reddit.com/r/phpstorm/comments/un5e97/debug_console_output_only_shows_array_item_count/ Debug console output only shows array item count instead of contents
reddit 上的網友遇到了這個問題,斷點時在console打印array或object只會顯示array的長度,估計是使用最新版本2022.1.1,如圖:
有網友回復他到YouTrack(jetbrains的jira)回報問題Debug console output only shows array item count instead of contents
官方回復會在 2022.2 修正這個問題
Debug console cannot evaluate arrays or objects
不降版本可以用print_r($a, true)
https://youtrack.jetbrains.com/issue/WI-66067/Debug-console-cannot-evaluate-arrays-or-objects#focus=Comments-27-6090147.0-0 PhpStorm 2021.3.2 是最後正常的版本,2021.3.3後就會有這個問題
Docker
.idea
jet brains 系列編輯器會把IDE配置存在項目的.idea 目錄下如果有人把.idea 加到git 且推到master 分支上,你拉下來用PHPStorm 打開項目時會吃到他的設定。
解法:
關掉PHPStorm ,切到開發分支dev(別的分支 .idea/ 有被gitignore),刪除.idea/ ,然後打開PHPStorm解決
composer
在windows上的PHPStorm直接執行composer而不用 terminal 連上服務器執行在windows上安裝composer
參考 https://getcomposer.org/download/ 打開 XAMPP for Windows 的終端安裝# php -v
PHP 7.4.1 (cli) (built: Dec 17 2019 19:24:02) ( ZTS Visual C++ 2017 x64 )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
...
放在 C:\Users\bear\Downloads 下是不行的
bear@BEAR-PC C:\Users\bear\Downloads
# php composer-setup.php
All settings correct for using Composer
The installation directory "C:\Users\bear\Downloads" is not writable
bear@BEAR-PC C:\bear
# php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
bear@BEAR-PC C:\bear
# php -r "if (hash_file('sha384', 'composer-setup.php') === 'e0012edf3e80b6978849f5eff0d4b4e4c79ff1609dd1e613307e16318854d24ae64f26d17af3ef0bf7cfb710ca74755a') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Installer verified
bear@BEAR-PC C:\bear
# php composer-setup.php
All settings correct for using Composer
Downloading...
Composer (version 1.10.1) successfully installed to: C:\bear\composer.phar
Use it: php composer.phar
bear@BEAR-PC C:\bear
# php -r "unlink('composer-setup.php');"
在PHPStorm中設定composer
Settings => Tools => Command Line Tool Support => Add(+) => Choose tool: ComposerPath to PHP executable: C:\xampp\php\php.exe => 即使和server php版本不一樣,用XAMPP 的 PHP 7.4 沒事
Path to composer.phar or composer: C:\bear\composer.phar => 官網上下載
Alias:
使用方法(會自動 auto-complete):
Run Command: composer dump-autoload
Windows 的 git bash 一樣要設 git config --global core.autocrlf false 避免 composer dump-autoload 後會有 CRLF will be replaced by LF in xxx 錯誤
windows + PHPStorm + git bash 避免CRLF will be replaced by LF in xxx 錯誤
Settings => Editor => Code Style => General => Line Separator: Unix and macOS(\n)composer install 或 composer require 安裝插件時只在Linux 上執行
就是確保所有新建文件的Line Separator是\n
artisan
Settings => Tools => Command Line Tool Support => Add(+) => Choose tool: Tool based on Symfony Console.Path to PHP executable: C:\xampp\php\php.exe
Path to script: C:\PhpstormProjects\project\artisan
Alias: php artisan
如果報錯:
Problem
Failed to parse output as xml: ParseError at [row,col]:[2,1]
Message: 前言中不允许有内容。.
Command
d2564ee6-4324-47ee-b9f2-6bb9789fff37 D:\path\laravel\artisan list --format=xml
Output
Warning: require(D:\path\laravel/vendor/autoload.php): Failed to open stream: No such file or directory in D:\path\laravel\artisan on line 18
Fatal error: Uncaught Error: Failed opening required 'D:\path\laravel/vendor/autoload.php' (include_path='.;C:\php\pear') in D:\path\laravel\artisan:18
Stack trace:
#0 {main}
thrown in D:\path\laravel\artisan on line 18
檢查下vendor目錄,是不是沒有composer install
Plugins
Case conversion
不用這個,改用String Manipulation
.env files support
EJS
Mongo Plugin
Quick File Preview
SQL Formatter
Sublime的 SqlBeautifier 替代品,但還是覺得Navicat查詢的美化SQL做的比較好String Manipulation
熱鍵:alt+m
https://stackoverflow.com/a/36477843 Auto increment number for multiple lines in PhpStormSublime的Text Pastry替代品,但用起來沒那麼順手,還是開sublime好。也有 Case conversion功能
.ignore
Ideolog
Rainbow Brackets
Php Inspections (EA Extended)
熱鍵:alt+enter - show context action
GitToolBox
每一行後面顯示git commit(時間 作者 commit內容)
laravel idea
自動生成Model
ctrl+shift+p => laravel =>Code Generation => Create Model
自動提示DB字段
新增fruits表
CREATE TABLE `fruits` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID', `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '名字', `class` tinyint(1) NOT NULL DEFAULT '1' COMMENT '等級', `quantity` tinyint(2) NOT NULL DEFAULT '0' COMMENT '數量', `created_at` datetime NOT NULL COMMENT '創建時間', `updated_at` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改時間', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
s
設定Database
Database(View=>Tool Window) => + => Data Source => MySQL
第一次打開需要安裝Driver顯示你專案的資料庫
Helper Code Parameters...
使用laravel idea Create Model 後(Models/Fruit.php)
先Laravel => Generate Helper Code,然後在 $fillable 中 alt+insert (Generate)
新增字段後。Database先Refresh,然後Generate Helper Code,這樣新增的字段才生效
Languages & Framworks
避免composer.json改變專案的PHP版本
https://stackoverflow.com/a/51222031 How can I set the PHP version in PHPStorm?
PHP => Composer => 取消勾選:Synchronize IDE settings with composer.json
和sublime比較
列模式貼上比sublime強
將剪貼板中的 2 行的內容:
Title1
Title2
取代目標內容的title
- title: Example Title 1
color: '#03fccf'
profile: CMD (clink)
commands:
- ls
- cd ..
- title: Example Title 2
color: '#fc036b'
profile: 1
- title: Example Title 3
color: '#302a57'
當剪貼板中的行數(2) 不等於取代目標內容(3)
Sublime
PHPStorm
選擇偶數行
https://stackoverflow.com/a/16273692 How can I select every other line with multiple cursors in Sublime Text?
正則搜索:.*\n.*\n ,alt+enter 全選, 左鍵 ← ,下鍵 ↓
WebStorm
避免切視窗時自動保存文件
不然vue 會自動重新編譯,等手動ctrl+s 保存時再編譯
https://stackoverflow.com/a/15605487 How to Turn off automatic saving on web storm
File | Settings | Appearance & Behavior | System Settings > 取消勾選:
Save files when switching to a different application or a built-in terminal