In some PHP frameworks there exist ways to check quality of code. Laravel uses the PSR-1 and PSR-2 code. Is there way for checking PSR-1 and PSR-2, that checks code via terminal or before commit to git?
PHP CodeSniffer is a useful tool for this. You can also opt in on Scrutinizer or StyleCI, though they may cost money for private projects.
You could run CodeSniffer like this in a pre-commit hook:
"vendor/bin/phpcs --extensions=php --warning-severity=0 --report=full --standard=phpcs.xml app/" which will check for all code in the App folder.
If you are using PHPStorm there is an option to reformat your code with PSR-2. Otherwise you can always use tools like StyleCI.
In 2017 you have 3 options:
already mentioned PHP_CodeSniffer with PSR2, stable, but slowly evolving
PHP-CS-Fixer with PSR2 and Symfony standards (much extra checks, closer to Laravel than PSR2)
vendor/bin/php-cs-fixer fix src --rules=#Symfony
or combine the best of both above in EasyCodingStandard - super easy to use and setup
Related
I guess, it's an off-topic question, but let me try to ask it. I've just started learing about Continuous Integration and now I know that for Java language there are tools like Checkstyle and PMD that enable to enforce coding standards and to report any lines of code that are not meeting these standards.
And now I wonder if there are similar tools for PHP language, that I could incorporate to my Continuous Integration system. What If want all my codes to follow PSR-1 and PSR-2 specification. Are there any tools for PHP that can automate this process - check the whole code base and find files which do not meet the requirements etc.?
Try CodeSniffer.
For example, to verify code for PSR-2 compliance use phpcs --standard=PSR2 src.
Also, I recommend to check out this PHP package boilerplate. It has some basic CI setup with Travis CI and Codeclimate that might be helpful to you.
I've already spent two days trying to make a good work environment with VIM for a framework, in this case, laravel.
All is perfect as always, but there is a very important issue:
I can't get omnicomplete properly working
I've tried all that I found via google:
-phpcomplete: despite in other of my projects works well, it seems that gets mad with composer. Doesn't recognize facades nor common methods for the framework.
-ctags: helps with some methods, but still a mess with omnicompletion.
-phpcomplete-extended and phpcomplete-extended for laravel: author doesn't maintain this plugin anymore (logical since frameworks change so quick) so does not work anymore.
-PIV, uses standart phpcomplete, so same issue.
-padawan.php I couldn't get it to work, IMHO poorly documented
Is there any vim user who could manage to get omnicompletion functionality properly?
I'm starting to think I should move from vim since it's not ready for these new technologies :'(
Grep AppServiceProvider against tags file:
AppServiceProvider app/Providers/AppServiceProvider.php /^class
AppServiceProvider extends
ServiceProvider$/;" c namespace:Furbook\Providers
Furbook\Providers app/Providers/AppServiceProvider.php /^namespace
Furbook\Providers;$/;" n boot app/Providers/AppServiceProvider.php /^
public function
boot()$/;" f class:Furbook\Providers::AppServiceProvider
register app/Providers/AppServiceProvider.php /^ public function
register()$/;" f class:Furbook\Providers::AppServiceProvider
This has been one of my top concerns, here are my preferred options:
Phpactor. My current choice, works pretty well and it's main dev is really active. Fast and has A LOT of refactoring tools. Great for composer projects.
ctags
I use phpcomplete and ctags (patched for php), but still no autocompletion for facades, I resolved this with the Laravel 5 IDE Helper Generator. The idea is to generate a _ide_helper.php file with classes and methods for facades first, and then create the tags.
I also created a function in my vimrc, so I can automatically generate the tags.
function! GenTags()
if isdirectory("./vendor")
echo '(re)Generating framework tags'
execute "!php artisan ide-helper:generate"
echo '(re)Generating tags'
execute "!ctags -R --filter-terminator=php"
else
echo 'Not in a framework project'
if filereadable("tags")
echo "Regenerating tags..."
execute "!ctags -R --filter-terminator=php"
else
let choice = confirm("Create tags?", "&Yes\n&No", 2)
if choice == 1
echo "Generating tags..."
execute "!ctags -R --filter-terminator=php"
endif
endif
endif
:endfunction
command! -nargs=* GenTags call GenTags()
GenTags()
eclim
-- install eclipse, don't use eclipse installer, better untar directly
-- install eclim, use the eclim installer.
-- For complete code completion change your models to extend Eloquentinstead of Model
-- and the plugin YouCompleteMe (you can try any other)
PHP Language Server
It's better and more automated than ctags.
Despite autocompletion is still a bit worse than eclipse (eclim), the Php Language server is developed on PHP, which means a lot of PHP users can contribute to the project and it's pretty active and improving.
Plug 'roxma/nvim-completion-manager'
" (optional) javascript
Plug 'roxma/nvim-cm-tern', {'do': 'npm install'} "
"(optional) language server protocol framework
Plug 'autozimu/LanguageClient-neovim', { 'do': ':UpdateRemotePlugins' } "
"(optional) php completion via LanguageClient-neovim
Plug 'roxma/LanguageServer-php-neovim', {'do': 'composer install &&
composer run-script parse-stubs'}
If anyone wants to know more can check my vimrc at github
Talking about padawan.php that's true about documentation, I haven't spent enough time to make it more or less useful, but installation of padawan shouldn't be that hard:
Install padawan.php via composer global require mkusher/padawan
Configure your $PATH
Install padawan.vim
Generate index for your project
I'm not sure whether Laravel projects will work out of the box or you still will need ide helper, but general php things should work well.
Disclaimer: I am not a PHP programmer and I know nothing about PHP.
First of all, if an auto-completion mechanism is essential for your work, then you should probably choose a proper IDE for the programming language you're working with. Vim includes a framework for auto-completion, which does not mean that it is implemented for all supported languages. And even if there is an omni-completion function available you mileage may vary, depending on how well developed it is.
From what I can gather in other questions (in particular Vim PHP omni completion), the best PGP completion plugin is phpcomplete, so I've installed it.
I then downloaded laraval from here and ran ctags 5.9~svn20110310 (available in Debian Jessie) using the following command:
ctags -R --filter-terminator=php
Using the following test file:
<?php
$example = new AppServiceProvider();
$example->
?>
when I press i_ctrl-x_ctrl-o I get the following suggestions:
boot( f )
register( f )
As it can be seen, there is no long list of methods.
I've also tried to use Universal Ctags that includes the patches required by phpcomplete plugin, but in order to make it work I had to use the following syntax:
ctags -R --fields=+aimS-s --filter-terminator=php .
That is, the extra -s was required.
Maybe you can work from here and test this in more detail in your environment.
I would only like to run an inspection (phpcs) on files that are not ignored by git as the core code of Magento (which is ignored) does not really have code style so the inspector goes wild.
So I see in Settings | Scopes you need to match as a pattern, is it possible to define the .gitignore files somehow.
I think this is not possible, so any other solutions of keeping files out of PHPCodeSniffer would be appreciated.
You can configure JetBrains product integration with phpcs. Integration will bring you even more flexibility.
Please check documentation http://www.jetbrains.com/idea/webhelp/using-php-code-sniffer-tool.html
We're about to look at implementing some PHP Coding Standards in our workplace to add some consistency between all of our developers.
I've read around and seen Zend and PEAR standards etc, but what's the best way to enforce these?
I've found a PHP Codesniffer plugin for netbeans, but are there any other ways I could enforce a standard, possibly CI (Continuous Integration) / Hudson or even when committing to SVN?
I was just wondering If anyone had experience or any other tools/methods I could look into?
Thanks
CodeSniffer is indeed the best tool for this.
There is a number of ways to use it:
integrating into Eclipse
integrate as pre-commit hook in you VCS
integrating into your CI Server
Disclaimer: the linked pages are just random picks from Google on that topic. They are not to suggest to use Eclipse, SVN or Hudson. Use what you think is appropriate for your development environment.
Also see http://www.qatools.org for additional tools.
I always wanted to perform code beautification on each SVN commit via SVN hooks.
Still belive it's the best & most efficient way.
Currently all team members just know that XXX is approved code beautifier and everyone supposed to use it.
At 2017 you have basically 3 options:
From oldest to newest (by features and codebase):
PHP_CodeSniffer
PHP CS Fixer
EasyCodingStandard - connects them both in super easy to understand way
I normally work on Python projects and we have a good tool called virtualenv for setting up project environments. Does something similar exist for PHP? I have not been able to find one.
Hmm, maybe PEAR does what you want. PEAR is a repository of various useful classes. You can download them, but you also use the Command Line to manage packages. Apart from this, there is nothing I know that would do this. There is phing to automate build processes. It's like Java's Ant. And some frameworks have RAD tools to setup projects, but those are usually aimed at the framework specific parts.
eclipse helios for php developers
As far as I know there isn't really a way of doing this in PHP without having to install loads of tools. My best guess would be to set up a VCS, such as Git, and use branches to mimic environments. It's ugly, but it works.