phpvimctagscscope

More accurate alternative/workaround to ctags/Cscope for PHP?


I know that it is possible to use Ctrl+] to jump to a definition in Vim and this can work in conjunction whith either ctags or Cscope. I am looking for a more accurate alternative to both ctags and Cscope when working PHP. Sometimes there are multiple possible results to choose from or false positives. I only want to jump to the actual definition of whatever is under the cursor. Ideally this should work for variables, functions, constants, and classes.

I don't see why this can't be done by analyzing the files. I have finally overcome just about every other annoyance/misunderstanding I have with Vim by learning and customizing, so if I could nail this one it would be awesome.

Also, do other's agree that Cscope and ctags are not accurate enough for PHP or am I doing something wrong?

UPDATE

4 years later, I am still using Vim with PHP, and still having this problem. I have tried eclim, ctags, exubarant-ctags, universal-ctags, and cscope. I have tried passing various arguments to these programs to get them to generate better tags. The experience is very poor for all of these options.

But I understand the problem much better now. There might be nothing wrong with the tags generated by these programs. The problem seems to be that when you press Ctrl + ] in Vim or Neovim, it just looks for a tag by that name. It is not looking at the context of the file you are editing to see which tag by that name it should use. It does not even understand what language you are editing, and look for tags from code in that language.

Is there a way to make vim search through the tags file more intelligently, based on the context, and then jump to the most likely location? You know, like what would happen inside a good IDE?


Solution

  • PHP is not a well designed language, and it is not statically typed. As such, the foundation is just not there for any good tools of this kind to be made that would be practical to use in real world projects. IDEs, language servers, and Vim and Neovim plugins can only know as much about the code as the language allows. It is possible to write very consistent code in PHP with as much types as possible and that only utilizes a limited subset of the language, such that tools could understand it. However if your team is capable of writing that good of PHP code, they are good enough to rewrite the code in a more well designed language that can support even better tooling, something like Kotlin which also has great IDE support.