Vim: undefined variables are unmarked - php

Lately I've been making a couple of mistakes when refactoring in Vim, the result was undefined and unused variables. Any decent IDE (like NetBeans) would mark them as such, but I've yet to come across a Vim plugin that does the same.
Can anyone help me out? I'm mainly programming in PHP.

There should be a solution with the Syntastic plugin, on which you would need to put a PHP static code analyzer like PHPLint.
However I never spent some time to test this !
Other PHP programs can be found on this SO answer.

You can run Zend's PHP code analyzer from within VIM. I currently do this. The catch is that Zend Code Analyzer is no longer packaged as a separate binary when installing Zend Studio. I'm not sure which OS you are running. I'm running on OS X. If you don't already have the binary, use steps 1 & 2 on this site to get it - http://michalf.me/blog:zend-code-analyzer-in-textmate. You may have to adjust for your OS.
After getting the binary add the following to your .vimrc and replace the /usr/local/... with the path to your ZendCodeAnalyzer.
if !exists("autocommands_loaded")
let autocommands_loaded = 1
"PHP Make
autocmd BufRead *.inc,*.php set makeprg=/usr/local/bin/ZendCodeAnalyzer\ %
autocmd BufRead *.inc,*.php set errorformat=%f(line\ %l):\ %m
endif
map <F7> :silent lmake<cr>:lwindow <cr>:redraw!<cr>
Now when you enter F7 it will run make which is set to run the ZendCodeAnalyzer. It will put the results into a location list - :help location. You can scroll through the location list and hit enter on a line and it will take you to that line in your file. If it doesn't find anything, then it won't open anything.

Well, this might not be what you are looking for, but if you must have Vim keybindings (I know I need them), then jVi brings this to NetBeans. I don't know if this is a viable option for you, but maybe this will help.

When renaming vars in a whole file type in vi cmd line:
:%s/\$oldName/\$newName/
When renaming the between line 14 and 21 (e.g inside a function) type
:14,21s/\$oldName/\$newName/
When renaming vars recursively in a directory type in vi cmd line:
:!find DIRECTORY_PATH -name "*.php" | xargs sed -ni 's/\$oldName/\$newName/'
Make a backup of the folder before to avoid headaches. ;)

I'm not sure how intelligent this plugin is but it seems to do what you want: https://github.com/vim-scripts/php_localvarcheck.vim

Related

PHP from source - Questions about Extension-Folder and Interaction with existing repo-version

As a Linux beginner I'm currently a bit confused building my own PHP from source.
I have a Ubuntu with a LAMP-Stack where all packages comes from a Repository.
(PHP as FastCGI)
Now I'm trying to set up a second PHP-Version and actually it works well but I have a few questions.
First of all some specs:
Ubuntu 10.04 (I need this older Version)
PHP 5.3.2-1ubuntu4.2.5 (Repo)
For my own PHP-Version I use the following "./configure" parameters (Reduced to the essential):
--prefix=/opt/php5310
--with-config-file-path=/opt/php5310/etc
--with-config-file-scan-dir=/opt/php5310/conf.d
--with-libdir=lib64
--with-mysql=/usr/bin/mysql
...
First of all executing "sudo make test" show up a few failed tests (about 10-14). Unfortunately I can't find any information what I have to do or what excactly goes wrong but "sudo make install" works nevertheless.
Is it normal that not all tests passed?
Then I noticed (phpinfo) the "imagick" extension was not loaded.
Looking at the "extension_dir" shows why.
On the Repo-Version:
extension_dir = /usr/lib/php5/20090626
On my own Version:
extension_dir = /opt/php5310/lib/php/extensions/no-debug-non-zts-20090626
(This folder doesn't even exist)
When I put this inside the php.ini:
extension_dir = /usr/lib/php5/20090626
It's working but is it the right way or just a creepy workaround?
And what about "--with-libdir=lib64" doesn't it belong to "extension_dir"?
On my System /usr/lib64 is a symlink to /usr/lib.
And last but not least my Repo-Version has a folder "/etc/php5/cgi/conf.d" with many ".ini" files e.g. "curl.ini", "gd.ini" and so on.
My Solution was:
"--with-config-file-scan-dir=/opt/php5310/conf.d
And then create the "conf.d" Folder and copy everthing from "/etc/php5/cgi/conf.d" to "/opt/php5310/conf.d" to be not dependent on the PHP Repo-Version.
Would this be a correct solution for multiple PHP-Versions or should it be a symlink or
"--with-config-file-scan-dir=/etc/php5/cgi/conf.d
Thank you for your help
Failing tests generally means failing functionality. Of course, for something as large as PHP you may never touch that functionality. It may be that your best bet here is to search for the names of the tests which failed to see if anyone has had the same issue (and how to fix it or disable the functionality), and to find the source of the tests in PHP and understand why they failed.

Validate PHP syntax in VIM

I would like to know how if it's possible to validate if a PHP file is valid in VIM without closing VIM every time?
Thank you
You can execute shell commands in vim. This is the same as calling php -l filename.php from the shell:
:!php -l %
I have this mapped into my ~/.vim/after/ftplugin/php.vim file so that I only have to press F5:
map <F5> :!php -l %<CR>
Use :make with the following php specific settings:
:set makeprg=php\ -l\ %
:set errorformat=%m\ in\ %f\ on\ line\ %l,%-GErrors\ parsing\ %f,%-G
Your syntax errors will be in the Quickfix window. You can open this buffer with :copen or :cope for short. If you only want to open the window only if their are errors use :cwindow.
You can use :cnext and :cprev to move through the quickfix list to jump to the corresponding errors. I suggest Tim Pope's excellent unimpared.vim plugin to make moving through the list as simple as [q and ]q.
To simplify the workflow I suggest a mapping like this one:
nnoremap <f5> :update<bar>make<bar>cwindow<cr>
Now you can just hit <f5> and the buffer will be updated (if necessary), linted, and any errors will appear in the quickfix window.
To make this a bit more robust, add these commands to ~/.vim/after/ftplugin/php.vim. Example ~/.vim/after/ftplugin/php.vim
setlocal makeprg=php\ -l\ %
setlocal errorformat=%m\ in\ %f\ on\ line\ %l,%-GErrors\ parsing\ %f,%-G
nnoremap <buffer> <silent> <f5> :update<bar>sil! make<bar>cwindow<cr>
For more information:
:h quickfix
:h makeprg
:h errorformat
:w !php -l
The real credit goes to Can I see changes before I save my file in Vim? for the idea so up vote there.
But to explain on this post (mostly taken from above):
The above command works as follows:
The syntax for saving a file in vim is:
:w <filename>
The syntax for executing a shell command in vim is:
:!<command>
Executing the save command without a filename but rather a shell command behind it causes vim to write the files content to stdin of the shell instead of saving it in a physical file. You can verify this by executing
:w !cat
This should always print the files current content (which would have been written to a file instead).
4 You can check code with php -l from STDIN by piping it in
The file is "saved" to stdin, php lint is run with the stdin as input.
To check PHP syntax without having to save first you can use:
map :w !php -l
http://vim.wikia.com/wiki/Runtime_syntax_check_for_php
You should try a plugin like Neomake. It will show you in the gutter error markers for every PHP syntax errors. Go on the line to see the error itself. You can as well link it to PHP Mess detector and PHP Stan to show you errors and possible improvements in your code.
This combo is very powerful!
In case you are interested I wrote an article how to make a Vim PHP IDE. This is basically a list of essential plugins you should try out! Of course Neomake is part of it.
For just syntax highlight (what sometimes gives clues about errors) a non yet saved file (usually black and white on vim) just source the syntax file:
:source $VIMRUNTIME/syntax/[the syntaxfile].vim
Examples
:source $VIMRUNTIME/syntax/sh.vim
:source $VIMRUNTIME/syntax/php.vim
:source $VIMRUNTIME/syntax/javascript.vim
You can manually run the PHP linting commands, but if you're doing full time development in PHP then it's easier to use a generic syntax checking plugin.
Syntastic (as recommended by #lucapette) has long been the main syntax plugin for Vim with fairly relaxed requirements:
Syntastic itself has rather relaxed requirements: it doesn't have any external dependencies, and it needs a version of Vim compiled with a few common features: autocmd, eval, file_in_path, modify_fname, quickfix, reltime, statusline, and user_commands. Not all possible combinations of features that include the ones above make equal sense on all operating systems, but Vim version 7 or later with the "normal", "big", or "huge" feature sets should be fine.
Since Vim 8, which allows for asynchronous syntax checking, there are now two good plugins. These work by continuously linting your code so you don't have to save your code for errors to show up:
Asyncronous Lint Engine (ale)
Neomake (as recommended by #matthieu)
All of these should automatically call php -l for the current file as long as the syntax for that file is set to PHP (:set syntax=php if Vim doesn't automatically recognise the syntax).

Netbeans file debugging

I have netbeans setup with xdebug so it can debug php. However, this only works if I create a php project. It will not work if I try opening a stand alone php file. So my question is, is it possible to debug a stand alone php file which is not part of a netbeans php project?
If that is not possible, how do I debug stand alone php files with netbeans?
No, There is none that I am aware of. As Myrddin mentioned the debugger needs some configurations that is a part of netbeans project.
but the best way you can debug a single file is to copy it on a project folder, and click the debug project, once the debug session is set then you can browse the PHP File that you want to debug and it will actually go through xdebug.
Good Luck!
Each project can have it's own configuration (you can have 1 project that has PHP5.4 interpreter, one the is PHP5.6, one that is a command line and another that is a web project), but if you configure a general PHP 5 Interpreter:
If you work on a windows machine you can use this code (filename is php.cmd)
set XDEBUG_CONFIG="idekey=netbeans-xdebug"
#php.exe %*
If you want to be able to debug, your interpreter should have the XDEBUG_CONFIG system variable and make sure it's connected to netbeans. You should set this to the same value in your Debbugging section of the PHP's config:
Next thing - if you right click inside the editor you will have the Debug File option, and a prompt window will pop:
You don't really need anything here. Just hit the "OK" button.
As you can see, this final result is debug session of the t1.php file within c:\TEMP\ (which is not a working project):
Short answer: CTRL + SHIFT + F5
You can find the answer here:
https://blogs.oracle.com/netbeansphp/entry/run_file_without_project
I'm not entirely sure, but I think it is not possible, because you need some configuration to get the debugging working, and this configuration is part of a project.
You can always use print_r and var_dump to debug a single file. But that is probably not the answer you're looking for.
xdebug is very heavy and old tool you can use Kint php debuger here.
its free, so you can download Here
it's pretty replacement for var_dump(), print_r() and debug_backtrace().
you need to add kint.class.php file using include or require function.
require '/kint/Kint.class.php';
that's it.
and you can use like
########## DUMP VARIABLE ###########################
Kint::dump($GLOBALS, $_SERVER); // pass any number of parameters
// or simply use d() as a shorthand:
d($_SERVER);
########## DEBUG BACKTRACE #########################
Kint::trace();
more help is available on https://github.com/raveren/kint/
Good Luck :)

Unix or VIM command to format PHP code

I know that you can use the indent command to beautify c code. I want to do the same thing with php but the command is giving errors (it is intended for c only I believe) is there a standard way to formatt php code with a command? I would be open to either a unix command that comes with OS X or a VIM command.
If vim has been set up to recognize PHP, type =G at the top of your file and it will reindent your code to the end.
To expand on rkulla's comment, = is the indentation command, while G is the command to go to the end of the document and gg goes to the beginning. If you want to indent the next 5 lines beginning from your current line, you can type =5= or 5==.
If you are using VIM, erjiang's answer is the best method. For reference for yourself or anyone reading this who does not use VIM, there is a PEAR module for beautifying PHP called 'PHP Beautifier'.
http://beautifyphp.sourceforge.net
See command line usage in the documentation.

problem with xdebug vim plugin

I am using xdebug plugin for vim. After making few changes i was able to run debugger but not able to set breakpoints.
So, I enabled xdebug.remote_log and below is the log statements corresponding to setting breakpoint.
<- breakpoint_set -i 5 -t line -f file:///C:\htdocs\testLocal.php -n 36
->
Its issuing request to debugger in proper format only but no idea why debugger is returning "command is not avilable".
Please let me know if anything is wrong.
Make sure you set breakpoints on lines that contain a php expression, not on blank lines or lines containing just a closing bracket.
Setting breakpoints on blank lines doesn't work, and returns the cryptic 'command not found' error you are getting.
I had the same problem. When I checked the xdebug logs, it showed file:///http://myapp.local
As you see my ide (PHP eclipse) seems to insert the extra "http://" treating this as a URL instead of a file.
Luckily, this was the case with just my index.php. Other file breakpoints were being communicated correctly to xdebug and I was able to set and hit breakpoints on php expression as long as they didn't know span multiple lines. The latter for some reason would cause breakpoints to not hit and just pass by.
Hope this helps!
I spotted an extra "/" in "file:///C:\htdocs\testLocal.php", mayby the vim plugin isn't compatible with windows filepaths?
I recently just installed this same thing and found a tutorial by Blake Johnson all about installing and using Xdebug from within Vim.
Also, try clearing any cookies on the page you might have. This often times would cause the plugin to get screwed up in a strange way.

Categories