PHPStorm/webstorm/PyCharm tips

phpstorm對於使用PHP開發web的人員來講,是一個很是不錯的編輯開發IDE,之前用過sublime,可是相比於storm,sublime在瀏覽legacy代碼,類代碼編輯方面明顯要遜色很多。同時其代碼調試功能是我見過的web開發IDE中是最棒的!傾情推薦。對應的webstorm對於js開發也是很是棒!php

project->setting->Autoscroll to source、from source選中便可實現sublime同樣單點就打開文件瀏覽的工做模式,同時你在editor中的文件對應會在project navigation window中打開對應的文件夾,很方便。html

上述設置沒法保存,若是須要保存,則須要安裝Autoscroll Save pluginpython

http://phpstorm.tips/tips/8-jump-to-matching-bracenginx

alt+home  - 彈出當前目錄以供操做laravel

alt+insert  - 目錄context時, 在選中的目錄中彈出建立...對話框git

alt+insert  - 文件內容context時,彈出建立construct,phpdoc等對話框,這時若是alit+enter則會提示initialize class 的fieldsgithub

ctrl+w       - 持續選擇括號,方括號,tag內容,不斷擴展, 很是好用!!!在sublime中必須經過擴展plugin來實現的web

ctrl+d        - 選擇光標所在詞,持續向下選擇next occurencedocker

ctrl+alt+左箭頭 -直接返回最近前一次光標所在位置(也能夠是多個文件哦) Navigate | Back / Forward.apache

ctrl+alt+右箭頭 -直接返回最近後一次光標所在位置(也能夠是多個文件哦) 

ctrl+shitf+d -取消當前選擇到的occurence

ctrl+shift+o - 瀏覽查找global symbol

ctrl+shift+N - 光標移動到matching brace(Moving caret to the matched brace)

alt+左鍵       - 多光標設置

shift+c     -跳到代碼code start的地方

shift+x   -跳到代碼code end的地方

alt + Up - previous method in current class

alt + Down - Next method in current class

ctrl + shift +F7 - highlight all the symbol usage in current file

alt+V           - vertical split into two display window

alt+C       - close active editor window

ctrl+E        - recent files,能夠快速在瀏覽過的文件中切換

ctrl+shift+E -recently changed files,列出最近修改過的文件

shift+enter -直接到下一行

/** + enter -爲下面的代碼加註釋doc

ctrl+R        -瀏覽類的成員,再次ctrl+R列出包含繼承過來的成員

shift +s - toggle status bar

live template - 在本身的代碼窗口中選中但願重用的代碼,ctrl+shift+p, search live template, 選擇save as live template便可使用

Mark changed file:    file >> settings >> Editor >> Editor Tabs. Check Mark modified tabs with asterisk Click ok

phpStorm的auto save功能有時比較煩人,可是又沒有好的方法來關閉,比較可行的方式是首先打開修改後mark爲*的功能,隨後能夠在local history中選擇變動

  • Settings / Appearance & Behavior / System Settings / Save file on frame deactivation: unchecked
  • Settings / Appearance & Behavior / System Settings / Save files automatically if...: unchecked
  • Settings / Editor / General / Editor tabs / Mark modified tabs with asterisk :checked

alt+ H: local history 

dependency injection自動添加成員變量到

php --ini :列出本機php所使用的ini文件

http -S: 列出appache所使用conf文件

 

[root@AY130628135803343949Z]# httpd -S
httpd: Could not reliably determine the server's fully qualified domain name, using 10.160.37.203 for ServerName
VirtualHost configuration:
127.0.0.1:10083        is a NameVirtualHost
         default server localhost (/etc/httpd/conf.d/zendserver_gui.conf:6)
         port 10083 namevhost localhost (/etc/httpd/conf.d/zendserver_gui.conf:6)
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         port 80 namevhost www.xxxx.cn (/usr/local/zend/etc/sites.d/vhost_xxx.conf:1)
Syntax OK

 

 

 

https://confluence.jetbrains.com/display/PhpStorm/Remote+debugging+in+PhpStorm+via+SSH+tunnel

https://confluence.jetbrains.com/display/PhpStorm/Working+with+Remote+PHP+Interpreters+in+PhpStorm

https://confluence.jetbrains.com/display/PhpStorm/Working+with+Advanced+Vagrant+features+in+PhpStorm

下面的資源是適用於laravel的Live templates

https://github.com/koomai/phpstorm-laravel-live-templates

debugging PHP with phpstorm

http://blog.jetbrains.com/phpstorm/2013/07/webinar-recording-debugging-php-with-phpstorm/

https://confluence.jetbrains.com/display/PhpStorm/Zero-configuration+Web+Application+Debugging+with+Xdebug+and+PhpStorm

Homestead+phpstorm remote debugging配置要點

遠程調試的通常知識

也能夠參考http://www.cnblogs.com/JeffreySun/p/4877395.html

 

1. 安裝bookmark  https://www.jetbrains.com/phpstorm/marklets/

2. Start Listening for PHP Debug Connections in PHPSTORM以便開始偵聽debug請求

2.a: project->settings(ctrl+alt+s)->deployment增長一個描述如何deploy你的server的profile,配置sftp信息,配置path mapping(IDE to remote server)

關於path mapping,這篇文章有很詳細的描述: http://blog.jetbrains.com/webide/2011/03/configure-php-debugging-in-phpstorm-2-0/

2.b: project->settings(ctrl+alt+s)->PHP->Servers選擇上面的profile

3.可選在phpstorm中選擇stop at the first line:在php代碼第一行就暫停

3.在phpstorm中增長breakpoint

4.增長一個debug configuration,以方便在PHPStorm中直接快速執行debug命令

4.a運行run->debug xxx將啓動一個默認瀏覽器,而且傳遞http://homestead.app:8000/?XDEBUG_SESSION_START=17769參數給homestead server,由xdebug捕捉到而且方向鏈接到PHPSTORM監聽的調試端口9000,若是首次調試,則須要再PHPSTORM中點擊接受調試請求,啓動調試雙向鏈接

4.b若是在bookmarklet安裝的狀況下,能夠直接在瀏覽器中點擊start debug this page按鈕,瀏覽器就能夠向phpstorm的debug監聽接口發起請求

可是注意若是咱們在file->settings->php->debug配置選項中勾選ignore external connections through unregistered server configuations,那麼因爲咱們的homestead住在防火牆後面(使用8000端口),在 4.a模式下咱們向homestead.app:8000端口經過get參數發起debug引擎啓動請求,而隨後homestead的web server又反向向phpstorm請求調試鏈接(注意這個回來的server_host參數是homestead.app:80端口!!!!)時,server host是不同的,所以在上述debug配置選項的狀況下,PHPStorm是會忽略debug鏈接請求的!!(9000端口)。

相反,若是咱們沒有勾選上面的選項,則phpstorm會提示咱們是否接受來自homestead.app:80服務器來的調試請求,一旦咱們接受,咱們的phpstorm中就將增長了另一個php server,而咱們需要在這個server中配置path mapping,不然將可能沒法hit breakpoint!(即使是經過xdebug_break()主動斷點是工做的!),在debug界面會一直報file path is not mapped to any file path on server的錯誤(其緣由就是無註冊的server相匹配)。

IDE正常中斷點的前提是當IDE偵聽調試鏈接時,在php程序中的xdeubg_break()可以主動斷掉!

 

5.reload這個page

5.a爲了驗證debug環境是否已經ok,能夠經過在Php代碼中人爲增長一條xdebug代碼: xdebug_break(); 隨後瀏覽器訪問服務器運行該代碼,那麼就應該觸發IDE的breakpoint!!

有時你在php配置使能xdebug功能時,可能找不到相應的php.ini配置文件,方法是php --ini,這個命令就會列出系統的php到底使用的是哪個php.ini文件

6.點擊start debug將由browser初始化一個debug session而且向phpstorm發起請求

xdebug.ini中 remote_host = 192.168.56.1  這條能夠由xdebug.remote_connect_back = 1來替代,增長一條 xdebug.idekey = "PHPSTORM", 這個信息是phpstorm中所須要的(在須要支持多個調試session時由debug proxy所須要)

隨後重啓vagrant中的web服務: sudo service php5-fpm restart

 

http://www.sitepoint.com/install-xdebug-phpstorm-vagrant/

若是在debug過程當中,瀏覽器60s就會timeout,能夠在/etc/nginx/nginx.conf中增長fastcgi_read_timeout 3600s;另一個參數:

fastcgi_read_timeout 3600s;
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;

對於apache,咱們直接在httpd.conf文件中加入:

Timeout 5000 

即刻解決30秒左右http請求就被kill掉的問題,隨意調試吧!

參考如下連接:xdebug配置

https://netbeans.org/kb/docs/php/configure-php-environment-windows.html

如何使用profiler功能? 

PHPStorm自己並不具有profiler功能,它主要完成對xdebug輸出的profiler文件作分析。如何打開profiler功能呢?在php.ini文件中增長兩行(通常在xdebug.ini中定義):

xdebug.profiler_enable = 1
xdebug.profiler_output_dir = "/home/vagrant/Code"

隨後執行sudo service php5-fpm restart便可。這時一旦執行任何一個url,則會在/home/vagrant/Code目錄下生成相應的文件,這個文件在phpstorm的tools->analyze profiler file中分析,或者你也可使用QCachegrind(windows),KCachegrind來作專業分析

http://sourceforge.net/projects/wincachegrind/ 分析器

execution trace(function trace)

若是你是項目的新加入者,對於系統是如何工做的沒有清晰地瞭解,但願經過研究系統整個運做流程,那麼使用xdebug的execution trace功能很是有幫助

xdebug.collect_params = 4 (最全的信息)

xdebug.collect_return = 1

xdebug.trace_format = 2 (2爲html, 0爲通常可讀,1爲機讀)

xdebug_start_trace() xdebug_stop_trace()

function trace文件分析工具:

https://github.com/beberlei/xdebug-trace-gui

 

Category
Setting
Values
Description






Log file
xdebug.trace_output_dir




xdebug.trace_output_name




xdebug.trace_options

1 = append to trace file if it exists. 0 (default) = overwrite






Select data per call
xdebug.collect_params

Non-zero = collect parameters passed to functions
  • 0 = None.
  • 1 = Type and number of elements (f.e. string(6), array(8)).
  • 2 = Type and number of elements, with a tool tip for the full information
  • 3 = Full variable contents (with limits set by xdebug.var_display_max_children, xdebug.var_display_max_data and xdebug.var_display_max_depth.)
  • 4 = Full variable contents and variable name.


xdebug.collect_return

1 = write return values to trace file. Default 0


xdebug.collect_vars

1 = analyze variables used in scope (slow).


xdebug.collect_assignments

1 = include variable assignments in execution traces






Format
xdebug.trace_format

  • 0 = human readable indented format. time index, memory usage, memory delta (xdebug.show_mem_delta=1), level, function name, function parameters (xdebug.collect_params=1), filename and line number.
  • 1 = computer readable format with different record type for enter stack frame and exit. See docs for fields.
    • File line 2 says "File format: 2", which seems incorrect
  • 2 = HTML format


xdebug.show_mem_delta

1 = show mem delta for each call






Behavior
xdebug.auto_trace

1 = enable trace just before script runs. Includes the auto_prepend_file


xdebug.trace_enable_trigger

1 = trigger trace using XDEBUG_TRACE GET/POST parameter, or set cookie XDEBUG_TRACE. To prevent Xdebug generating trace files for every request, set xdebug.auto_trace to 0.
(Could not get this to work. This setting doesn't seem to be used in the XDebug source code.)
Doh! See: [xdebug-general] Re: Is trace_enable_trigger defunct? Not implemented in Xdebug 2.1.2!






Limits
xdebug.var_display_max_depth

Max levels of array elements and object properties that are displayed in trace output. Default 3.


xdebug.var_display_max_data

Max string length for output of variables. Default 512.


xdebug.var_display_max_children

Max number of array children and object properties that are displayed in trace output. Default 128


























Functions to call in the PHP subject code

Category
Function
Description

ini_set()
Sets the value of a configuration option. I.e., substitute or override for settings in php.ini file. http://www.php.net/manual/en/function.ini-set.php

void xdebug_start_trace(
string trace_file_path
[, integer options] )
Start tracing at this line of code.
Trace file path is absolute, or relative to cwd. If no path given (empty string? No arg?) then xdebug uses setting xdebug.trace_output_dir.
options is a bitmap:
  • XDEBUG_TRACE_APPEND: 1 = append, 0 = overwrite
  • XDEBUG_TRACE_COMPUTERIZED:
    • 2 = Act as though xdebug.trace_format=1 (more elaborate format, but less human readable).
    • 0 = Simpler format
  • XDEBUG_TRACE_HTML: 4 = Output HTML table. 0 = output ordinary text




void xdebug_stop_trace()
Stop tracing and close output file.




string xdebug_get_tracefile_name()
Get name of output file. Useful in conjunction with xdebug.auto_trace, apparently.



Data display functions

Are these relevant? Where does their output go?

xdebug_var_dump()


xdebug.show_local_vars

 auto namespacing

在編寫PHP類代碼過程當中,若是使用了namespace,那麼每次新建立類文件時須要指定namespace,同時爲了使用composer的autoload功能,你又必須遵循PSR-4的目錄佈置及命名規範,這一點是比較繁瑣的。PhpStorm能夠解決這個問題,具體配置方式以下圖:

一旦配置完成後,後續在IDE中建立新的PHP Class時,IDE自動添加符合PSR-4規範的namespace!

自動插入PHP use statements

在寫php代碼時,因爲有namespace的存在,引用一個類感受是很囉嗦的,好在phpstorm已經替咱們想到了這個pain point,光標在一個類名時,只要alt+enter選擇import class就會自動插入use xxxClass到文件頭部很方便

https://www.youtube.com/watch?v=LUTolQw8K9A

https://www.jetbrains.com/phpstorm/documentation/phpstorm-video-tutorials.jsp#10

http://stackoverflow.com/questions/15339165/cannot-debug-with-phpstorm-vagrant-xdebug

https://confluence.jetbrains.com/display/PhpStorm/Remote+debugging+in+PhpStorm+via+SSH+tunnel 

PHPStorm XMX out of memory問題:

在大型項目中,因爲構建index須要使用大量內存,這時頗有可能會發生JVM heap內存不夠的狀況,解決辦法:

修改文件C:\Program Files (x86)\JetBrains\PhpStorm 4.0.2\bin\PhpStorm.exe.vmoptions

-Xms128m
-Xmx1024m -XX:MaxPermSize=250m
-XX:ReservedCodeCacheSize=64m
-ea

使用typehinting讓phpstorm更加聰明

在代碼編寫中,若是咱們本身知道一個variable是什麼class,這樣咱們若是再經過如下方法作下type hinting, PHPSTORM就可以學習到這個變量的全部方法和屬性,這樣當你使用這個變量時便可以自動補充代碼,這樣就可以大大提升編碼速度和準確度

        /* @var \App\User $usertocheck */
        $usertocheck = $user?$user:$this->app->auth->user();

如何針對文件夾下面特定後綴名的文件來搜索?

pycharm配置對應code completion提示

咱們選擇使用IDE環境而不是python命令行方式開發代碼,很重要一點是但願可以高產高效。其中code completion就是很重要的一點,咱們但願在知道零星信息或者全然不知任何代碼知識的前提下IDE可以激發咱們的想象並選擇,這時code completion的功能就十分強大了。相關的配置及使用方式,配置上有一個code completion的觸發方式,默認是首字母,咱們能夠選擇全場景下應用代碼智能補全,設置好後,對應的代碼補全快捷鍵是:

Default completion shortcuts:

  • Basic: Ctrl+Space
  • SmartType: Ctrl+Shift+Space
  • Class Name: Ctrl+Alt+Space

效果圖:

phpstorm debug PHP running in the docker

如今運維容器化已是一個不可逆轉的趨勢,咱們開發測試的產品最終每每都是在docker容器中運行,這時,咱們的開發環境最好也是在docker中運行,這樣就能確保開發和部署環境的高度一致。

這樣雖然很美好,可是也是必定會有代價的,其中的一個代價就是開發調測的環境就沒有那麼簡單了。這裏就記錄一下我使用phpstorm調試運行在docker容器化後的nginx+php-fpm如何本地調試php代碼的。

1. 確保docker容器中應該安裝好xdebug, 注意哦:xdebug的版本也很是重要,我以前遇到過用最新的2.7.2版本的xdebug沒法中斷點的問題,這裏記錄下我用的xdebug: 2.6.0/2.6.1都是Ok的,php版本是7.1.14/7.1.27

dockerfile以下:

FROM prodenv:phpfpm71
LABEL maintainer="zhang@xxcom"

# Installing xdebug
RUN yes | pecl install xdebug-2.6.0 \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.idekey = PHPSTORM" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.default_enable = 0" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_enable = 1" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_autostart = 0" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_connect_back = 0" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.profiler_enable = 0" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_host = host.docker.internal" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_handler = dbgp" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_log = /tmp/xdebug.log" >baozh> /usr/local/etc/php/conf.d/xdebug.ini

須要說明的是:上面的remote_host選擇了host.docker.internal,由於我是在win10環境下的docker,本質上docker engine是一個win10的hyper-v虛擬機,它會自動在win10主機的hosts中增長這個dns的別名,指向到hyper-v主機的ip地址。

2. 配置好phpstorm的pathmapping,保證root路徑的匹配

3. 設置好phpstorm的DBGP監聽9000端口

如下兩張圖是xdebug支持remote debugging的原理圖:

 

  • The IP of the server is 10.0.1.2 with HTTP on port 80
  • The IDE is on IP 10.0.1.42, so xdebug.remote_host is set to 10.0.1.42
  • The IDE listens on port 9000, so xdebug.remote_port is set to 9000
  • The HTTP request is started on the machine running the IDE
  • Xdebug connects to 10.0.1.42:9000
  • Debugging runs, HTTP Response provided
相關文章
相關標籤/搜索