summaryrefslogtreecommitdiff
path: root/ale_linters/php/phpstan.vim
blob: b99e4f5821a3f7ac303448e4443de1bd2e2a1368 (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
" 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')

function! ale_linters#php#phpstan#GetExecutable(buffer) abort
    return ale#Var(a:buffer, 'php_phpstan_executable')
endfunction

function! ale_linters#php#phpstan#GetCommand(buffer) abort
    let l:executable = ale_linters#php#phpstan#GetExecutable(a:buffer)

    return ale#Escape(l:executable)
    \   . ' analyze -l'
    \   . ale#Var(a:buffer, 'php_phpstan_level')
    \   . ' --errorFormat raw'
    \   . ' %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',
\   'executable_callback': 'ale_linters#php#phpstan#GetExecutable',
\   'command_callback': 'ale_linters#php#phpstan#GetCommand',
\   'callback': 'ale_linters#php#phpstan#Handle',
\})