Skip to content

Commit 5f6bbc6

Browse files
committed
First commit
0 parents  commit 5f6bbc6

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Robert Basic
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# vim-phpstan
2+
3+
A Vim plugin for [PHPStan](https://github.com/phpstan/phpstan). It calls `phpstan` to do static analysis of your PHP code and displays the errors in Vim's quickfix list.
4+
5+
See `:help quickfix` for more on how to use the quickfix.
6+
7+
# Usage
8+
9+
Call the `PHPStanAnalyse` command and pass the directories you want analysed as arguments:
10+
11+
``` vim
12+
:PHPStanAnalyse src test
13+
```
14+
15+
And the quickfix list will be populated with something like this:
16+
17+
![vim phpstan quickfix screenshot](https://github.com/robertbasic/vim-phpstan/blob/master/vim-phpstan-qf.png)
18+
19+
# Installation
20+
21+
Using [vim-plug](https://github.com/junegunn/vim-plug):
22+
23+
`Plug 'robertbasic/vim-phpstan'`
24+
25+
# Configuration
26+
27+
You can configure the level PHPStan uses. By default the level is 2.
28+
29+
`let g:phpstan_analyse_level = 4` in your `.vimrc` file to change the level to fit your needs.

autoload/phpstan.vim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function! phpstan#PHPStanAnalyse(...)
2+
let paths = join(a:000, '\ ')
3+
exe "setlocal makeprg=phpstan\\ analyse\\ --no-progress\\ -l" . g:phpstan_analyse_level . "\\ " . paths
4+
" File: path/to/file.php, line: 12, error: error message
5+
setlocal efm=File%.\ %f%.\ line%.\ %l%.\ error%.\ %m
6+
setlocal efm+=%-G " Ignore empty lines
7+
8+
exe "silent make\ \\\|&\ " . g:phpstan_plugin_path . "/phpstan_filter"
9+
exe "cwindow"
10+
endfun

plugin/phpstan.vim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
if exists('g:phpstan_plugin_loaded') || &cp
2+
finish
3+
endif
4+
let g:phpstan_plugin_loaded = 1
5+
6+
let g:phpstan_plugin_path = expand('<sfile>:p:h')
7+
8+
if !exists('g:phpstan_analyse_level')
9+
let g:phpstan_analyse_level = 2
10+
endif
11+
12+
" A command to call
13+
command! -nargs=+ PHPStanAnalyse call phpstan#PHPStanAnalyse(<f-args>)

plugin/phpstan_filter

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#! /usr/bin/php
2+
3+
<?php
4+
$stdin = file_get_contents('php://stdin', 'r');
5+
6+
$lines = explode("\n", $stdin);
7+
8+
$lines = preg_grep('~[a-z]+~', $lines);
9+
10+
$lines = array_map('trim', $lines);
11+
12+
$output = '';
13+
$currentFile = '';
14+
foreach ($lines as $line) {
15+
$file = [];
16+
$lineNumberError = [];
17+
$error = [];
18+
$isFileLine = preg_match('~^Line(.+)~', $line, $file);
19+
$isNumberErrorLine = preg_match('~^(\d+)(.*)~', $line, $lineNumberError);
20+
$isErrorLine = preg_match('~^([a-zA-Z].+)~', $line, $error);
21+
22+
if (!$isFileLine && !$isNumberErrorLine && !$isErrorLine) {
23+
continue;
24+
}
25+
26+
if ($isFileLine) {
27+
$currentFile = trim(array_pop($file));
28+
continue;
29+
} else if ($isNumberErrorLine && !$isErrorLine) {
30+
$errorMessage = trim(array_pop($lineNumberError));
31+
$lineNumber = trim(array_pop($lineNumberError));
32+
} else if ($isErrorLine && !$isNumberErrorLine) {
33+
$errorMessage = trim(array_pop($error));
34+
$lineNumber = 0;
35+
}
36+
37+
$output .= "File: " . $currentFile . ", line: " . $lineNumber . ", error: " . $errorMessage . PHP_EOL;
38+
}
39+
40+
echo trim($output);

vim-phpstan-qf.png

45.2 KB
Loading

0 commit comments

Comments
 (0)