PHPStorm/webstorm tips

phpstorm对于使用PHP开发web的人员来说,是一个非常不错的编辑开发IDE,以前用过sublime,但是相比于storm,sublime在浏览legacy代码,类代码编辑方面明显要逊色不少。同时其代码调试功能是我见过的web开发IDE中是最棒的!倾情推荐。对应的webstorm对于js开发也是非常棒!

project->setting->Autoscroll to source、from source选中即可实现sublime一样单点就打开文件浏览的工作模式,同时你在editor中的文件对应会在project navigation window中打开对应的文件夹,很方便。

PHPStorm/webstorm tips

上述设置无法保存,如果需要保存,则需要安装Autoscroll Save plugin

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

alt+home  - 弹出当前目录以供操作

alt+insert  - 目录context时, 在选中的目录中弹出创建...对话框

alt+insert  - 文件内容context时,弹出创建construct,phpdoc等对话框,这时如果alit+enter则会提示initialize class 的fields

ctrl+w       - 持续选择括号,方括号,tag内容,不断扩展, 非常好用!!!在sublime中必须通过扩展plugin来实现的

ctrl+d        - 选择光标所在词,持续向下选择next occurence

ctrl+alt+左箭头 -直接返回最近前一次光标所在位置(也可以是多个文件哦) Navigate | Back / Forward.

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: is a NameVirtualHost
default server localhost (/etc/httpd/conf.d/zendserver_gui.conf:)
port namevhost localhost (/etc/httpd/conf.d/zendserver_gui.conf:)
wildcard NameVirtualHosts and _default_ servers:
*: is a NameVirtualHost
port namevhost www.xxxx.cn (/usr/local/zend/etc/sites.d/vhost_xxx.conf:)
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配置要点

远程调试的一般知识PHPStorm/webstorm tips

也可以参考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)

PHPStorm/webstorm tipsPHPStorm/webstorm tips

关于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

PHPStorm/webstorm tips

3.可选在phpstorm中选择stop at the first line:在php代码第一行就暂停

3.在phpstorm中增加breakpoint

4.增加一个debug configuration,以方便在PHPStorm中直接快速执行debug命令

PHPStorm/webstorm tips

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()能够主动断掉!

PHPStorm/webstorm tipsPHPStorm/webstorm tips

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/ 分析器

PHPStorm/webstorm tips

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

PHPStorm/webstorm tips

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可以解决这个问题,具体配置方式如下图:

PHPStorm/webstorm tips

一旦配置完成后,后续在IDE中创建新的PHP Class时,IDE自动添加符合PSR-4规范的namespace!

自动插入PHP use statements

在写php代码时,由于有namespace的存在,引用一个类感觉是很啰嗦的,好在phpstorm已经替我们想到了这个pain point,光标在一个类名时,只要alt+enter选择import class就会自动插入use xxxClass到文件头部很方便PHPStorm/webstorm tips

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

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

http://*.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();

PHPStorm/webstorm tips

如何针对文件夹下面特定后缀名的文件来搜索?

PHPStorm/webstorm tips

pycharm配置对应code completion提示

我们选择使用IDE环境而不是python命令行方式开发代码,很重要一点是希望能够高产高效。其中code completion就是很重要的一点,我们希望在知道零星信息或者全然不知任何代码知识的前提下IDE能够激发我们的想象并选择,这时code completion的功能就十分强大了。相关的配置及使用方式,配置上有一个code completion的触发方式,默认是首字母,我们可以选择全场景下应用代码智能补全,设置好后,对应的代码补全快捷键是:

PHPStorm/webstorm tips

Default completion shortcuts:

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

效果图:

PHPStorm/webstorm tips

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. \
&& 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的原理图:

PHPStorm/webstorm tips

  • 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
上一篇:对AutoIt中控件和窗口的理解


下一篇:express框架目录结构