2017-07-16 14:04:25 +00:00
|
|
|
" Author: medains <https://github.com/medains>, ardis <https://github.com/ardisdreelath>
|
|
|
|
" Description: phpstan for PHP files
|
|
|
|
|
|
|
|
" Set to change the ruleset
|
|
|
|
let g:ale_php_phpstan_executable = get(g:, 'ale_php_phpstan_executable', 'phpstan')
|
|
|
|
let g:ale_php_phpstan_level = get(g:, 'ale_php_phpstan_level', '4')
|
2017-09-30 16:18:20 +00:00
|
|
|
let g:ale_php_phpstan_configuration = get(g:, 'ale_php_phpstan_configuration', '')
|
2017-07-16 14:04:25 +00:00
|
|
|
|
2018-11-23 09:39:50 +00:00
|
|
|
function! ale_linters#php#phpstan#GetExecutable(buffer) abort
|
|
|
|
return ale#Var(a:buffer, 'php_phpstan_executable')
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale_linters#php#phpstan#VersionCheck(buffer) abort
|
|
|
|
let l:executable = ale_linters#php#phpstan#GetExecutable(a:buffer)
|
|
|
|
|
|
|
|
" If we have previously stored the version number in a cache, then
|
|
|
|
" don't look it up again.
|
|
|
|
if ale#semver#HasVersion(l:executable)
|
|
|
|
" Returning an empty string skips this command.
|
|
|
|
return ''
|
|
|
|
endif
|
|
|
|
|
|
|
|
let l:executable = ale#Escape(l:executable)
|
|
|
|
|
|
|
|
return l:executable . ' --version'
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale_linters#php#phpstan#GetCommand(buffer, version_output) abort
|
2017-09-30 16:18:20 +00:00
|
|
|
let l:configuration = ale#Var(a:buffer, 'php_phpstan_configuration')
|
|
|
|
let l:configuration_option = !empty(l:configuration)
|
|
|
|
\ ? ' -c ' . l:configuration
|
|
|
|
\ : ''
|
|
|
|
|
2018-11-23 09:39:50 +00:00
|
|
|
let l:executable = ale_linters#php#phpstan#GetExecutable(a:buffer)
|
|
|
|
let l:version = ale#semver#GetVersion(l:executable, a:version_output)
|
|
|
|
let l:error_format = ale#semver#GTE(l:version, [0, 10, 3])
|
|
|
|
\ ? ' --error-format raw'
|
|
|
|
\ : ' --errorFormat raw'
|
|
|
|
|
2018-08-02 22:44:12 +00:00
|
|
|
return '%e analyze -l'
|
2017-07-16 14:04:25 +00:00
|
|
|
\ . ale#Var(a:buffer, 'php_phpstan_level')
|
2018-11-23 09:39:50 +00:00
|
|
|
\ . l:error_format
|
2017-09-30 16:18:20 +00:00
|
|
|
\ . l:configuration_option
|
2017-07-16 14:04:25 +00:00
|
|
|
\ . ' %s'
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale_linters#php#phpstan#Handle(buffer, lines) abort
|
|
|
|
" Matches against lines like the following:
|
|
|
|
"
|
|
|
|
" filename.php:15:message
|
|
|
|
" C:\folder\filename.php:15:message
|
|
|
|
let l:pattern = '^\([a-zA-Z]:\)\?[^:]\+:\(\d\+\):\(.*\)$'
|
|
|
|
let l:output = []
|
|
|
|
|
|
|
|
for l:match in ale#util#GetMatches(a:lines, l:pattern)
|
|
|
|
call add(l:output, {
|
|
|
|
\ 'lnum': l:match[2] + 0,
|
|
|
|
\ 'text': l:match[3],
|
|
|
|
\ 'type': 'W',
|
|
|
|
\})
|
|
|
|
endfor
|
|
|
|
|
|
|
|
return l:output
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
call ale#linter#Define('php', {
|
|
|
|
\ 'name': 'phpstan',
|
2019-02-22 18:05:04 +00:00
|
|
|
\ 'executable': function('ale_linters#php#phpstan#GetExecutable'),
|
2018-11-23 09:39:50 +00:00
|
|
|
\ 'command_chain': [
|
|
|
|
\ {'callback': 'ale_linters#php#phpstan#VersionCheck'},
|
|
|
|
\ {'callback': 'ale_linters#php#phpstan#GetCommand'},
|
|
|
|
\ ],
|
2017-07-16 14:04:25 +00:00
|
|
|
\ 'callback': 'ale_linters#php#phpstan#Handle',
|
|
|
|
\})
|