Prevent VSCode auto add empty newline after <?php, and before comments - php

When I write in a php file the following:
<?php
/**
* Comments....
the VSCode autoformat on save like this:
<?php
/**
* Comments....
I don't want this auto empty newline.
The "problem" is detected only after the combination of <?php and /** comments.
Thank you

not sure how to really say this in a completely understandable way, but you need to edit the formatter configuration that you use in vscodes settings. Or you can install a different formatter and use that instead with its own config

Related

Automatic add / update comment header block in source files

I can't believe that I didn't find an already asked question to that topic (maybe I am wrong). However, for example in WordPress-Themes all PHP-files come with a comment header block:
<?php
/**
* Template for header
*
* Description
*
* #Author: Mokorana
* #Date: 2020-11-26 09:19:12
* #Last Modified by: Mokorana
* #Last Modified time: 2020-11-27 10:08:51
*
* #package mokorana
*/
?>
Whats is the best practice to add and maintain these comment blocks? Is it done directly in the text editor (atom) or in a node-workflow or however?
Adding header comments it's usually automated process.
The simplest way is to configure header comment for each new PHP file in your IDE. For example PhpStorm has File templates for this.
Another option these are tools like PHP CS Fixer. You configure header comment template and add it to all files in your project using one simple command.
Tools like PHP CS Fixer it's a better way, because if you want to change something in your comment, you just need to update template in config file and run console command again. It will update all files in your project.

Which script is writing output in PHP?

I'm working on a Laravel application and after some updates I keep getting a comma "," in every http response or console output so I'm crazy trying to find which script file is printing that character.
Is it possible to know which file is writing to the output http response or console?
UPDATE
I've put an echo call in Composer's ClassLoader vendor\composer\ClassLoader.php after an include function for the loaded classes is called, as follows:
/**
* Scope isolated include.
*
* Prevents access to $this/self from included files.
*/
function includeFile($file)
{
include $file; echo $file . "\n";
}
And now the comma appears between the following classes are loaded, is this helpful?
C:\Users\Bla\Bla\trunk\vendor/filp/whoops/src/Whoops/Handler/PrettyPageHandler.php
,C:\Users\Bla\Bla\trunk\vendor/laravel/framework/src/Illuminate/Foundation/AliasLoader.php
C:\Users\Bla\Bla\trunk\vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php
UPDATE 2
Found it! As #vladsch said, it was before an opening tag ,<?php in one of my config files, thanks
Since you have it in HTTP and console then this rules out the views.
The culprit is probably a comma inadvertently inserted before the opening <?php tag. Most likely one of the first ones that is at the top of every PHP file.
Do a search for ,<?php or for regex pattern /\s*,\s*<\?php/
Yes, you can, debugging with xdebug or other debbuger tool, step by step (step into) until you find the script.
See this stackover entry for guiadance.
I strongly recommend use a real debug tool as a good pro practice. But maybe for this particular purpouse its a little overwelming.
You can probably narrow it down with some basic debugging. Add a dd('hello') throughout your application.
I would start in the routes.php file. See if the comma appears before or after the hello. If it is before the hello, then the comma is in one of the bootstrapping files.
If the comma is after the hello, then it is most likely in one of your views etc. Keep moving the dd('hello') deeper into your application until it appears.
No you cannot, that's why you should use a version control system (i.e.: git), so you can easily rollback whenever the system breaks.
In your case you could try to search for comma inside every single files of your working directory (escluding vendor and other unimportant folders such as storage)
If you have a debugger you could try to step into to check every single call from the beggining

Code folding in Notepad++ for PHP

I'm using Notepad++ 6.6.9. In Java I'm able to use code folding as simple as this:
//{ region 1
public Core(){}
//}
I don't even need to save. Just add this sort of code and collapsable itens are added around it.
But in PHP I can't make it work. I've tried the following:
//{#region LOADING
//{START LOADING
//{ region 1
//}
//}END example
//}#endregion LOADING
It simply doesn't work. Could somebody point me any working code, or a tutorial on how to configuring some custom one?
See here: https://stackoverflow.com/a/25954860/3869072
I just looked for the same, so you can simply write this:
#region LOADING
//Your code here
#endregion LOADING
the # works obviously as a comment in php

How to add comments in PHP Eclipse?

How can I add comments in PHP eclipse? I'm trying by selecting few lines of code, then right click, On menu, "Source" and then I have tried all these options there "general element comment", "toggle comment", "Add block comment". None of them works.
PS. I just downloaded latest eclipse PHP version.
Try Ctrl+Shift+/
The question is similair to How to comment a block in Eclipse?
Why don't you just try adding /** + enter? It will produce something like
/**
* Comment here
*/
Remember to follow the doc order
/**
* Description
* #param argument/parameter
* #return
*/
It`s a bug in latest PHP eclipse. Fix is here.
I was using single line comments on my eclipse php project. When i uploaded it to server ,some obfuscation has been applied automatically.Hence all the line breaks are removed and it treating the whole program as a single line. so i am getting an END OF STATEMENT error on server.So its better to use /--/ instead of //---- while commenting.

Vim PHP omni completion

I'm trying to get PHP autocompletion right in Vim. Right now when I do a $blog = new Blog(); $blog-> and then hit CTRL+X CTRL+O I'd expect omnicompletion to return all the functions in the class Blog.
Instead, it returns all functions for the entire project. I've built ctags for my project like so: ctags -R *
Is there any way to make the autocompletion context-aware?
catchmeifyoutry's answer points out a work-around by adding a comment such as /* #var $myVar myClass */ immediately before the line on which you use omnicomplete, however this is cumbersome and for the time it takes to write the comment, you may as well have written the function name yourself.
Solution: phpComplete
It is a Vim script: phpComplete
You will still need a tags file generated for your classes, but you can then use omni complete within the file, like so (modified from the description on the script's page);
This patch allows for in-file checking so you don't need the comment.
$blog = new Blog;
...
$blog->Blah(); // <-- complete without comment
It also allows support for singleton instantiations:
$instance = Class::getInstance();
$instance->completeMe(); // sweet completion
" Assuming Vim 7 (full version) is installed,
" adding the following to your ~/.vimrc should work.
filetype plugin on
au FileType php set omnifunc=phpcomplete#CompletePHP
" You might also find this useful
" PHP Generated Code Highlights (HTML & SQL)
let php_sql_query=1
let php_htmlInStrings=1
" Hope this helps!
(via http://www.linuxquestions.org/questions/linux-software-2/vim-omin-completion-for-php-621940/#post3155311)
Omnicompletion will only work if the tag file contains both the class definition, and the variable declaration.
Straightforward solution
In general that means that you will need to save and (re)generate the tags file after the class Blog { ... } and $blog = new Blog(); parts, but before trying $blog-> <C-X><C-O>.
This is because the PHP omni-complete function will look for the class declaration of the $blog variable in the tags file.
(BTW if you have opened the tags file in a buffer, reload it after regenerating it.)
Alternative
The vim documentation (:help ft-php-omni) also defines a way which doesn't require the variable to be indexed in the tags file, but uses instead a specific comment on the preceding line:
/* #var $myVar myClass */
$myVar->
However, the class definition still does need to be in the tag file, and the comment is needed every time you want to use omni-complete. So typing away in a new PHP file still won't give you nice omni-completion :(
Just a thought
Maybe it is possible to generate on-the-fly a temporary tags file (like the taglist plugin) of just the unsaved buffer, and allow omni-complete to use it too?? I'm not a great vim hacker though...
This one works as expected:
https://github.com/shawncplus/phpcomplete.vim
I am just missing the function parameters in the pveview!
The following works better. Taken from http://weierophinney.net/matthew/archives/134-exuberant-ctags-with-PHP-in-Vim.html
ctags \
-f ~/.vim/tags \
-h ".php" -R \
--exclude="\.svn" \
--totals=yes \
--tag-relative=yes \
--PHP-kinds=+ivcf \
--regex-PHP='/(abstract)?\s+class\s+([^ ]+)/\2/c/' \
--regex-PHP='/(static|abstract|public|protected|private)\s+function\s+(\&\s+)?([^ (]+)/\3/f/' \
--regex-PHP='/interface\s+([^ ]+)/\1/i/' \
--regex-PHP='/\$([a-zA-Z_][a-zA-Z0-9_]*)/\1/v/' \
Even with the above, there seems to be some issues. e.g. phpcomplete doesn't seem to support methods of instance vars.
$this->objA = new SomeClass();
$this->objA-><do_autocomplete> #fails
However,
$objA = new SomeClass();
$objA-><do_autocomplete> #works
After trying to get phpcomplete working for the last few hours my advice to anyone also trying, is to STOP. It doesn't work well and is not worth the trouble.
In C++, I run the following to get better context sensitivity:
ctags '--c++-kinds=+p' '--fields=+iaS' '--extra=+q'
It's not perfect, but after ctags adds the extra information to the tags file as specified by the above command vim handles completion better.
You can use a pretty powerful combo:
Phpactor
nvim-completion-manager
I tried a lot of stuff: PHPComplete, Padawan and so on. This is the best I could find.
In case you are interested, I wrote as well an article how to do a PHP IDE with Vim.
I've created a vim plugin for my padawan.php completion server. Checkout this video to see how it works.
try
curl -L -s https://git.io/ide | sh
then relaunch your nvim. You might got code completion, and goto definition features.
*Currently, it's only available for neovim

Categories