summaryrefslogtreecommitdiff
path: root/ale_linters/php/phpstan.vim
blob: 4dce5d5fa2a8986ae539fdf6b5186426513173ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
" 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')
let g:ale_php_phpstan_level = get(g:, 'ale_php_phpstan_level', '')
let g:ale_php_phpstan_configuration = get(g:, 'ale_php_phpstan_configuration', '')
let g:ale_php_phpstan_autoload = get(g:, 'ale_php_phpstan_autoload', '')
let g:ale_php_phpstan_memory_limit = get(g:, 'ale_php_phpstan_memory_limit', '')
call ale#Set('php_phpstan_use_global', get(g:, 'ale_use_global_executables', 0))

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)
    \   : ''

    let l:autoload = ale#Var(a:buffer, 'php_phpstan_autoload')
    let l:autoload_option = !empty(l:autoload)
    \   ? ' -a ' . ale#Escape(l:autoload)
    \   : ''

    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)
    \   : ''

    let l:level =  ale#Var(a:buffer, 'php_phpstan_level')
    let l:config_file_exists = ale#path#FindNearestFile(a:buffer, 'phpstan.neon')
    let l:dist_config_file_exists = ale#path#FindNearestFile(a:buffer, 'phpstan.neon.dist')

    if empty(l:level) && empty(l:config_file_exists) && empty(l:dist_config_file_exists)
        " if no configuration file is found, then use 4 as a default level
        let l:level = '4'
    endif

    let l:level_option = !empty(l:level)
    \   ? ' -l ' . ale#Escape(l:level)
    \   : ''

    let l:error_format = ale#semver#GTE(a:version, [0, 10, 3])
    \   ? ' --error-format json'
    \   : ' --errorFormat json'

    return '%e analyze --no-progress'
    \   . l:error_format
    \   . l:configuration_option
    \   . l:autoload_option
    \   . l:level_option
    \   . l:memory_limit_option
    \   . ' %s'
endfunction

function! ale_linters#php#phpstan#Handle(buffer, lines) abort
    let l:res = ale#util#FuzzyJSONDecode(a:lines, {'files': []})
    let l:output = []

    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, {
        \   'lnum': l:err.line,
        \   'text': l:err.message,
        \   'type': 'E',
        \})
    endfor

    return l:output
endfunction

call ale#linter#Define('php', {
\   'name': 'phpstan',
\   'executable': {buffer -> ale#path#FindExecutable(buffer, 'php_phpstan', [
\       'vendor/bin/phpstan',
\       'phpstan'
\   ])},
\   'command': {buffer -> ale#semver#RunWithVersionCheck(
\       buffer,
\       ale#path#FindExecutable(buffer, 'php_phpstan', [
\           'vendor/bin/phpstan',
\           'phpstan'
\       ]),
\       '%e --version',
\       function('ale_linters#php#phpstan#GetCommand'),
\   )},
\   'callback': 'ale_linters#php#phpstan#Handle',
\})