Add psalm linter for PHP (#1893)

This commit is contained in:
Richard Marmorstein 2018-09-27 11:48:47 -04:00 committed by w0rp
parent 58ceb21cbc
commit 947360f714
6 changed files with 77 additions and 2 deletions

View File

@ -155,7 +155,7 @@ formatting.
| OCaml | [merlin](https://github.com/the-lambda-church/merlin) see `:help ale-ocaml-merlin` for configuration instructions, [ols](https://github.com/freebroccolo/ocaml-language-server), [ocamlformat](https://github.com/ocaml-ppx/ocamlformat) |
| Pawn | [uncrustify](https://github.com/uncrustify/uncrustify) |
| Perl | [perl -c](https://perl.org/), [perl-critic](https://metacpan.org/pod/Perl::Critic), [perltidy](https://metacpan.org/pod/distribution/Perl-Tidy/bin/perltidy) |
| PHP | [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), [php-cs-fixer](http://cs.sensiolabs.org/) |
| PHP | [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), [php-cs-fixer](http://cs.sensiolabs.org/), [psalm](https://getpsalm.org) !! |
| PO | [alex](https://github.com/wooorm/alex) !!, [msgfmt](https://www.gnu.org/software/gettext/manual/html_node/msgfmt-Invocation.html), [proselint](http://proselint.com/), [write-good](https://github.com/btford/write-good) |
| Pod | [alex](https://github.com/wooorm/alex) !!, [proselint](http://proselint.com/), [write-good](https://github.com/btford/write-good) |
| Pony | [ponyc](https://github.com/ponylang/ponyc) |

28
ale_linters/php/psalm.vim Normal file
View File

@ -0,0 +1,28 @@
" Author: richard marmorstein <https://github.com/twitchard>
" Description: plugin for Psalm, static analyzer for PHP
call ale#Set('php_psalm_executable', 'psalm')
function! ale_linters#php#psalm#Handle(buffer, lines) abort
" Matches patterns like the following:
let l:pattern = '^.*:\(\d\+\):\(\d\+\):\(\w\+\) - \(.*\)$'
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[4],
\ 'type': l:match[3][:0] is# 'e' ? 'E' : 'W',
\})
endfor
return l:output
endfunction
call ale#linter#Define('php', {
\ 'name': 'psalm',
\ 'command': '%e --diff --output-format=emacs %s',
\ 'executable_callback': ale#VarFunc('php_psalm_executable'),
\ 'callback': 'ale_linters#php#psalm#Handle',
\ 'lint_file': 1,
\})

View File

@ -169,6 +169,16 @@ g:ale_php_phpstan_configuration *g:ale_php_phpstan_configuration*
This variable sets path to phpstan configuration file.
===============================================================================
psalm *ale-php-psalm*
g:ale_php_psalm_executable *g:ale_php_psalm_executable*
*b:ale_php_psalm_executable*
Type: |String|
Default: `'psalm'`
This variable sets the executable used for psalm.
===============================================================================
php-cs-fixer *ale-php-php-cs-fixer*

View File

@ -206,6 +206,7 @@ CONTENTS *ale-contents*
phpcs...............................|ale-php-phpcs|
phpmd...............................|ale-php-phpmd|
phpstan.............................|ale-php-phpstan|
psalm...............................|ale-php-psalm|
php-cs-fixer........................|ale-php-php-cs-fixer|
po....................................|ale-po-options|
write-good..........................|ale-po-write-good|
@ -433,7 +434,7 @@ Notes:
* OCaml: `merlin` (see |ale-ocaml-merlin|), `ols`, `ocamlformat`
* Pawn: `uncrustify`
* Perl: `perl -c`, `perl-critic`, `perltidy`
* PHP: `langserver`, `phan`, `php -l`, `phpcs`, `phpmd`, `phpstan`, `phpcbf`, `php-cs-fixer`
* PHP: `langserver`, `phan`, `php -l`, `phpcs`, `phpmd`, `phpstan`, `phpcbf`, `php-cs-fixer`, `psalm`!!
* PO: `alex`!!, `msgfmt`, `proselint`, `write-good`
* Pod: `alex`!!, `proselint`, `write-good`
* Pony: `ponyc`

View File

@ -0,0 +1,12 @@
Before:
call ale#assert#SetUpLinterTest('php', 'psalm')
After:
call ale#assert#TearDownLinterTest()
Execute(Custom executables should be used for the executable and command):
let g:ale_php_psalm_executable = 'psalm_test'
AssertLinter 'psalm_test',
\ ale#Escape('psalm_test') . ' --diff --output-format=emacs %s'

View File

@ -0,0 +1,24 @@
Before:
runtime ale_linters/php/psalm.vim
After:
call ale#linter#Reset()
Execute(The php static analyzer handler should parse errors from psalm):
AssertEqual
\ [
\ {
\ 'lnum': 1,
\ 'type': 'W',
\ 'text': 'somewarning',
\ },
\ {
\ 'lnum': 11,
\ 'type': 'E',
\ 'text': 'someerror',
\ },
\ ],
\ ale_linters#php#psalm#Handle(347, [
\ "/file:1:3:warning - somewarning",
\ "/file:11:33:error - someerror",
\ ])