Add phan as a linter for php files (#1026)

Add phan for checking PHP code
This commit is contained in:
Diego Oliveira 2017-10-24 19:25:02 -02:00 committed by w0rp
parent c248885e57
commit b172cd8b17
5 changed files with 78 additions and 2 deletions

View File

@ -118,7 +118,7 @@ formatting.
| Objective-C++ | [clang](http://clang.llvm.org/) | | Objective-C++ | [clang](http://clang.llvm.org/) |
| OCaml | [merlin](https://github.com/the-lambda-church/merlin) see `:help ale-ocaml-merlin` for configuration instructions | | OCaml | [merlin](https://github.com/the-lambda-church/merlin) see `:help ale-ocaml-merlin` for configuration instructions |
| Perl | [perl -c](https://perl.org/), [perl-critic](https://metacpan.org/pod/Perl::Critic) | | Perl | [perl -c](https://perl.org/), [perl-critic](https://metacpan.org/pod/Perl::Critic) |
| PHP | [hack](http://hacklang.org/), [langserver](https://github.com/felixfbecker/php-language-server), [php -l](https://secure.php.net/), [phpcs](https://github.com/squizlabs/PHP_CodeSniffer), [phpmd](https://phpmd.org), [phpstan](https://github.com/phpstan/phpstan), [phpcbf](https://github.com/squizlabs/PHP_CodeSniffer) | | PHP | [hack](http://hacklang.org/), [langserver](https://github.com/felixfbecker/php-language-server), [phan](https://github.com/phan/phan) see `:help ale-php-phan` to instructions, [php -l](https://secure.php.net/), [phpcs](https://github.com/squizlabs/PHP_CodeSniffer), [phpmd](https://phpmd.org), [phpstan](https://github.com/phpstan/phpstan), [phpcbf](https://github.com/squizlabs/PHP_CodeSniffer) |
| Pod | [proselint](http://proselint.com/)| | Pod | [proselint](http://proselint.com/)|
| Pug | [pug-lint](https://github.com/pugjs/pug-lint) | | Pug | [pug-lint](https://github.com/pugjs/pug-lint) |
| Puppet | [puppet](https://puppet.com), [puppet-lint](https://puppet-lint.com) | | Puppet | [puppet](https://puppet.com), [puppet-lint](https://puppet-lint.com) |

36
ale_linters/php/phan.vim Normal file
View File

@ -0,0 +1,36 @@
" Author: diegoholiveira <https://github.com/diegoholiveira>
" Description: static analyzer for PHP
" Define the minimum severity
let g:ale_php_phan_minimum_severity = get(g:, 'ale_php_phan_minimum_severity', 0)
function! ale_linters#php#phan#GetCommand(buffer) abort
return 'phan -y '
\ . ale#Var(a:buffer, 'php_phan_minimum_severity')
\ . ' %s'
endfunction
function! ale_linters#php#phan#Handle(buffer, lines) abort
" Matches against lines like the following:
"
" /path/to/some-filename.php:18 ERRORTYPE message
let l:pattern = '^.*:\(\d\+\)\s\(\w\+\)\s\(.\+\)$'
let l:output = []
for l:match in ale#util#GetMatches(a:lines, l:pattern)
call add(l:output, {
\ 'lnum': l:match[1] + 0,
\ 'text': l:match[3],
\ 'type': 'W',
\})
endfor
return l:output
endfunction
call ale#linter#Define('php', {
\ 'name': 'phan',
\ 'executable': 'phan',
\ 'command_callback': 'ale_linters#php#phan#GetCommand',
\ 'callback': 'ale_linters#php#phan#Handle',
\})

View File

@ -34,6 +34,21 @@ g:ale_php_langserver_use_global *g:ale_php_langserver_use_global*
See: |ale-integrations-local-executables| See: |ale-integrations-local-executables|
===============================================================================
phan *ale-php-phan*
WARNING: please do not use this linter if you have an configuration file
for your project because the phan will look into your entirely project and
ale will display in the current buffer warnings that may belong to other file.
g:ale_php_phan_minimum_severity *g:ale_php_phan_minimum_severity*
*b:ale_php_phan_minimum_severity*
Type: |Number|
Default: `0`
This variable defines the minimum severity level
=============================================================================== ===============================================================================
phpcbf *ale-php-phpcbf* phpcbf *ale-php-phpcbf*

View File

@ -115,6 +115,7 @@ CONTENTS *ale-contents*
php...................................|ale-php-options| php...................................|ale-php-options|
hack................................|ale-php-hack| hack................................|ale-php-hack|
langserver..........................|ale-php-langserver| langserver..........................|ale-php-langserver|
phan................................|ale-php-phan|
phpcbf..............................|ale-php-phpcbf| phpcbf..............................|ale-php-phpcbf|
phpcs...............................|ale-php-phpcs| phpcs...............................|ale-php-phpcs|
phpmd...............................|ale-php-phpmd| phpmd...............................|ale-php-phpmd|
@ -273,7 +274,7 @@ Notes:
* Objective-C++: `clang` * Objective-C++: `clang`
* OCaml: `merlin` (see |ale-ocaml-merlin|) * OCaml: `merlin` (see |ale-ocaml-merlin|)
* Perl: `perl -c`, `perl-critic` * Perl: `perl -c`, `perl-critic`
* PHP: `hack`, `langserver`, `php -l`, `phpcs`, `phpmd`, `phpstan`, `phpcbf` * PHP: `hack`, `langserver`, `phan`, `php -l`, `phpcs`, `phpmd`, `phpstan`, `phpcbf`
* Pod: `proselint` * Pod: `proselint`
* Pug: `pug-lint` * Pug: `pug-lint`
* Puppet: `puppet`, `puppet-lint` * Puppet: `puppet`, `puppet-lint`

View File

@ -0,0 +1,24 @@
Before:
runtime ale_linters/php/phan.vim
Execute(The php static analyzer handler should parse errors from phan):
AssertEqual
\ [
\ {
\ 'lnum': 25,
\ 'type': 'W',
\ 'text': 'Return type of getValidator is undeclared type \Respect\Validation\Validator',
\ },
\ {
\ 'lnum': 66,
\ 'type': 'W',
\ 'text': 'Call to method string from undeclared class \Respect\Validation\Validator',
\ },
\ ],
\ ale_linters#php#phan#Handle(347, [
\ "example.php:25 PhanUndeclaredTypeReturnType Return type of getValidator is undeclared type \\Respect\\Validation\\Validator",
\ "example.php:66 PhanUndeclaredClassMethod Call to method string from undeclared class \\Respect\\Validation\\Validator",
\ ])
After:
call ale#linter#Reset()