]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/ale_linters/php/phpstan.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge commit 'a39f715c13be3352193ffd9c5b7536b8786eff64' as '.vim/bundle/vim-lsp'
[etc/vim.git] / .vim / bundle / ale / ale_linters / php / phpstan.vim
1 " Author: medains <https://github.com/medains>, ardis <https://github.com/ardisdreelath>, Arizard <https://github.com/Arizard>
2 " Description: phpstan for PHP files
3
4 " Set to change the ruleset
5 let g:ale_php_phpstan_executable = get(g:, 'ale_php_phpstan_executable', 'phpstan')
6 let g:ale_php_phpstan_level = get(g:, 'ale_php_phpstan_level', '')
7 let g:ale_php_phpstan_configuration = get(g:, 'ale_php_phpstan_configuration', '')
8 let g:ale_php_phpstan_autoload = get(g:, 'ale_php_phpstan_autoload', '')
9 let g:ale_php_phpstan_memory_limit = get(g:, 'ale_php_phpstan_memory_limit', '')
10 call ale#Set('php_phpstan_use_global', get(g:, 'ale_use_global_executables', 0))
11
12 function! ale_linters#php#phpstan#GetCommand(buffer, version) abort
13     let l:configuration = ale#Var(a:buffer, 'php_phpstan_configuration')
14     let l:configuration_option = !empty(l:configuration)
15     \   ? ' -c ' . ale#Escape(l:configuration)
16     \   : ''
17
18     let l:autoload = ale#Var(a:buffer, 'php_phpstan_autoload')
19     let l:autoload_option = !empty(l:autoload)
20     \   ? ' -a ' . ale#Escape(l:autoload)
21     \   : ''
22
23     let l:memory_limit = ale#Var(a:buffer, 'php_phpstan_memory_limit')
24     let l:memory_limit_option = !empty(l:memory_limit)
25     \   ? ' --memory-limit=' . ale#Escape(l:memory_limit)
26     \   : ''
27
28     let l:level =  ale#Var(a:buffer, 'php_phpstan_level')
29
30     if empty(l:level) && empty(ale_linters#php#phpstan#FindConfigFile(a:buffer))
31         " if no configuration file is found, then use 4 as a default level
32         let l:level = '4'
33     endif
34
35     let l:level_option = !empty(l:level)
36     \   ? ' -l ' . ale#Escape(l:level)
37     \   : ''
38
39     let l:error_format = ale#semver#GTE(a:version, [0, 10, 3])
40     \   ? ' --error-format json'
41     \   : ' --errorFormat json'
42
43     return '%e analyze --no-progress'
44     \   . l:error_format
45     \   . l:configuration_option
46     \   . l:autoload_option
47     \   . l:level_option
48     \   . l:memory_limit_option
49     \   . ' %s'
50 endfunction
51
52 function! ale_linters#php#phpstan#Handle(buffer, lines) abort
53     let l:res = ale#util#FuzzyJSONDecode(a:lines, {'files': []})
54     let l:output = []
55
56     if type(l:res.files) is v:t_list
57         return l:output
58     endif
59
60     for l:key in keys(l:res.files)
61         for l:err in l:res.files[l:key].messages
62             call add(l:output, {
63             \   'lnum': l:err.line,
64             \   'text': l:err.message,
65             \   'type': 'E',
66             \})
67         endfor
68     endfor
69
70     return l:output
71 endfunction
72
73 function! ale_linters#php#phpstan#GetCwd(buffer) abort
74     let l:result = ale#path#Dirname(ale_linters#php#phpstan#FindConfigFile(a:buffer))
75
76     return empty(l:result) ? v:null : l:result
77 endfunction
78
79 function! ale_linters#php#phpstan#FindConfigFile(buffer) abort
80     let l:result = ale#path#FindNearestFile(a:buffer, 'phpstan.neon')
81
82     if empty(l:result)
83         let l:result = ale#path#FindNearestFile(a:buffer, 'phpstan.neon.dist')
84     endif
85
86     return l:result
87 endfunction
88
89 call ale#linter#Define('php', {
90 \   'name': 'phpstan',
91 \   'executable': {buffer -> ale#path#FindExecutable(buffer, 'php_phpstan', [
92 \       'vendor/bin/phpstan',
93 \       'phpstan'
94 \   ])},
95 \   'command': {buffer -> ale#semver#RunWithVersionCheck(
96 \       buffer,
97 \       ale#path#FindExecutable(buffer, 'php_phpstan', [
98 \           'vendor/bin/phpstan',
99 \           'phpstan'
100 \       ]),
101 \       '%e --version',
102 \       function('ale_linters#php#phpstan#GetCommand'),
103 \   )},
104 \   'callback': 'ale_linters#php#phpstan#Handle',
105 \   'cwd': function('ale_linters#php#phpstan#GetCwd'),
106 \})