1
0
Fork 0
mirror of synced 2024-11-18 06:55:33 -05:00
ultimate-vim/sources_non_forked/ale/ale_linters/php/phpstan.vim

105 lines
3.4 KiB
VimL
Raw Normal View History

2022-08-08 09:45:56 -04:00
" Author: medains <https://github.com/medains>, ardis <https://github.com/ardisdreelath>, Arizard <https://github.com/Arizard>
" Description: phpstan for PHP files
" Set to change the ruleset
let g:ale_php_phpstan_executable = get(g:, 'ale_php_phpstan_executable', 'phpstan')
2019-05-17 10:09:13 -04:00
let g:ale_php_phpstan_level = get(g:, 'ale_php_phpstan_level', '')
let g:ale_php_phpstan_configuration = get(g:, 'ale_php_phpstan_configuration', '')
2019-05-17 10:09:13 -04:00
let g:ale_php_phpstan_autoload = get(g:, 'ale_php_phpstan_autoload', '')
2022-08-08 09:45:56 -04:00
let g:ale_php_phpstan_memory_limit = get(g:, 'ale_php_phpstan_memory_limit', '')
2021-07-30 16:52:54 -04:00
call ale#Set('php_phpstan_use_global', get(g:, 'ale_use_global_executables', 0))
2019-05-17 10:09:13 -04:00
function! ale_linters#php#phpstan#GetCommand(buffer, version) abort
let l:configuration = ale#Var(a:buffer, 'php_phpstan_configuration')
let l:configuration_option = !empty(l:configuration)
\ ? ' -c ' . ale#Escape(l:configuration)
\ : ''
2018-12-17 06:28:27 -05:00
2019-05-17 10:09:13 -04:00
let l:autoload = ale#Var(a:buffer, 'php_phpstan_autoload')
let l:autoload_option = !empty(l:autoload)
\ ? ' -a ' . ale#Escape(l:autoload)
\ : ''
2018-12-17 06:28:27 -05:00
2022-08-08 09:45:56 -04:00
let l:memory_limit = ale#Var(a:buffer, 'php_phpstan_memory_limit')
let l:memory_limit_option = !empty(l:memory_limit)
\ ? ' --memory-limit ' . ale#Escape(l:memory_limit)
\ : ''
2019-05-17 10:09:13 -04:00
let l:level = ale#Var(a:buffer, 'php_phpstan_level')
2018-12-17 06:28:27 -05:00
2023-04-01 16:48:04 -04:00
if empty(l:level) && empty(ale_linters#php#phpstan#FindConfigFile(a:buffer))
2019-05-17 10:09:13 -04:00
" if no configuration file is found, then use 4 as a default level
let l:level = '4'
endif
2018-12-17 06:28:27 -05:00
2019-05-17 10:09:13 -04:00
let l:level_option = !empty(l:level)
\ ? ' -l ' . ale#Escape(l:level)
\ : ''
2019-05-17 10:09:13 -04:00
let l:error_format = ale#semver#GTE(a:version, [0, 10, 3])
2021-07-30 16:52:54 -04:00
\ ? ' --error-format json'
\ : ' --errorFormat json'
2018-12-17 06:28:27 -05:00
2019-05-17 10:09:13 -04:00
return '%e analyze --no-progress'
2018-12-17 06:28:27 -05:00
\ . l:error_format
\ . l:configuration_option
2019-05-17 10:09:13 -04:00
\ . l:autoload_option
\ . l:level_option
2022-08-08 09:45:56 -04:00
\ . l:memory_limit_option
\ . ' %s'
endfunction
function! ale_linters#php#phpstan#Handle(buffer, lines) abort
2021-07-30 16:52:54 -04:00
let l:res = ale#util#FuzzyJSONDecode(a:lines, {'files': []})
let l:output = []
2021-07-30 16:52:54 -04:00
if type(l:res.files) is v:t_list
return l:output
endif
for l:err in l:res.files[expand('#' . a:buffer .':p')].messages
call add(l:output, {
2021-07-30 16:52:54 -04:00
\ 'lnum': l:err.line,
\ 'text': l:err.message,
2019-05-17 10:09:13 -04:00
\ 'type': 'E',
\})
endfor
return l:output
endfunction
2023-04-01 16:48:04 -04:00
function! ale_linters#php#phpstan#GetCwd(buffer) abort
let l:result = ale#path#Dirname(ale_linters#php#phpstan#FindConfigFile(a:buffer))
return empty(l:result) ? v:null : l:result
endfunction
function! ale_linters#php#phpstan#FindConfigFile(buffer) abort
let l:result = ale#path#FindNearestFile(a:buffer, 'phpstan.neon')
if empty(l:result)
let l:result = ale#path#FindNearestFile(a:buffer, 'phpstan.neon.dist')
endif
return l:result
endfunction
call ale#linter#Define('php', {
\ 'name': 'phpstan',
2021-07-30 16:52:54 -04:00
\ 'executable': {buffer -> ale#path#FindExecutable(buffer, 'php_phpstan', [
\ 'vendor/bin/phpstan',
\ 'phpstan'
\ ])},
2019-05-17 10:09:13 -04:00
\ 'command': {buffer -> ale#semver#RunWithVersionCheck(
\ buffer,
2021-07-30 16:52:54 -04:00
\ ale#path#FindExecutable(buffer, 'php_phpstan', [
\ 'vendor/bin/phpstan',
\ 'phpstan'
\ ]),
2019-05-17 10:09:13 -04:00
\ '%e --version',
\ function('ale_linters#php#phpstan#GetCommand'),
\ )},
\ 'callback': 'ale_linters#php#phpstan#Handle',
2023-04-01 16:48:04 -04:00
\ 'cwd': function('ale_linters#php#phpstan#GetCwd'),
\})