├── .gitignore ├── .travis.yml ├── README.md ├── autoload └── php_cs_fixer.vim ├── doc └── vim-php-cs-fixer.txt ├── plugin └── php_cs_fixer.vim └── tests ├── fixtures └── misc │ ├── class_definition,no_trailing_whitespace.php │ ├── imports.php │ ├── no_closing_tag,full_opening_tag.php │ ├── phpdoc_to_comment,phpdoc_var_without_name.php │ ├── phpdocs.test.php │ ├── semicolon_after_instruction,no_unneeded_control_parentheses.php │ ├── simplified_null_return,no_useless_return,no_whitespace_in_blank_line,blank_line_before_return,no_extra_consecutive_blank_lines.php │ └── single_import_per_statement,ordered_imports.php ├── misc.vim └── results └── misc ├── class_definition,no_trailing_whitespace.php ├── imports.php ├── no_closing_tag,full_opening_tag.php ├── phpdoc_to_comment,phpdoc_var_without_name.php ├── phpdocs.test.php ├── semicolon_after_instruction,no_unneeded_control_parentheses.php ├── simplified_null_return,no_useless_return,no_whitespace_in_blank_line,blank_line_before_return,no_extra_consecutive_blank_lines.php └── single_import_per_statement,ordered_imports.php /.gitignore: -------------------------------------------------------------------------------- 1 | tags 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | sudo: false 4 | 5 | php: 6 | - 7.0 7 | - 7.1 8 | 9 | matrix: 10 | fast_finish: true 11 | include: 12 | - php: 5.3 13 | - php: 5.4 14 | - php: 5.5 15 | - php: 5.6 16 | - php: hhvm 17 | sudo: required 18 | dist: trusty 19 | group: edge 20 | allow_failures: 21 | - php: 7.0 22 | - php: 7.1 23 | 24 | cache: 25 | directories: 26 | - $HOME/.composer/cache 27 | 28 | env: 29 | - PATH="$HOME/.composer/vendor/bin:$HOME/vim/bin:$PATH" 30 | 31 | install: 32 | - composer self-update --no-progress --no-ansi --no-interaction --profile 33 | - composer global require "friendsofphp/php-cs-fixer" 34 | 35 | before_script: 36 | - git clone https://github.com/vim/vim --depth 1 /tmp/vim && cd /tmp/vim && ./configure --prefix=$HOME/vim --with-features=huge && make && make install && cd - 37 | - git clone https://github.com/thinca/vim-themis --branch v1.5.2.1 --single-branch --depth 1 /tmp/vim-themis 38 | 39 | script: 40 | - echo $PATH 41 | - composer --version 42 | - php-cs-fixer --version 43 | - vim --cmd version --cmd quit 44 | - /tmp/vim-themis/bin/themis tests/ -r --reporter dot 45 | 46 | notifications: 47 | email: false 48 | 49 | # vim:fdl=0:sts=2:sw=2:ts=2 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Vim-php-cs-fixer 2 | ================ 3 | 4 | [![](https://img.shields.io/travis/stephpy/vim-php-cs-fixer.svg)](https://travis-ci.org/stephpy/vim-php-cs-fixer) 5 | [![](https://img.shields.io/github/issues/stephpy/vim-php-cs-fixer.svg)](https://github.com/stephpy/vim-php-cs-fixer/issues) 6 | [![](https://img.shields.io/badge/doc-%3Ah%20vim--php--cs--fixer-blue.svg)](doc/vim-php-cs-fixer.txt) 7 | [![](https://img.shields.io/badge/license-MIT-blue.svg)](doc/vim-php-cs-fixer.txt) 8 | 9 | Integrate [php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer). 10 | 11 | This plugin will execute the `php-cs-fixer` command on the directory or file (depends on which command you call). See options to know how to customize that. 12 | 13 | **Options available**: 14 | 15 | ```viml 16 | " If php-cs-fixer is in $PATH, you don't need to define line below 17 | " let g:php_cs_fixer_path = "~/php-cs-fixer.phar" " define the path to the php-cs-fixer.phar 18 | 19 | " If you use php-cs-fixer version 1.x 20 | let g:php_cs_fixer_level = "symfony" " options: --level (default:symfony) 21 | let g:php_cs_fixer_config = "default" " options: --config 22 | " If you want to define specific fixers: 23 | "let g:php_cs_fixer_fixers_list = "linefeed,short_tag" " options: --fixers 24 | "let g:php_cs_fixer_config_file = '.php_cs' " options: --config-file 25 | " End of php-cs-fixer version 1 config params 26 | 27 | " If you use php-cs-fixer version 2.x 28 | let g:php_cs_fixer_rules = "@PSR2" " options: --rules (default:@PSR2) 29 | "let g:php_cs_fixer_cache = ".php_cs.cache" " options: --cache-file 30 | "let g:php_cs_fixer_config_file = '.php_cs' " options: --config 31 | let g:php_cs_fixer_allow_risky = "yes" " options: --allow-risky 32 | " End of php-cs-fixer version 2 config params 33 | 34 | let g:php_cs_fixer_php_path = "php" " Path to PHP 35 | let g:php_cs_fixer_enable_default_mapping = 1 " Enable the mapping by default (pcd) 36 | let g:php_cs_fixer_dry_run = 0 " Call command with dry-run option 37 | let g:php_cs_fixer_verbose = 0 " Return the output of command if 1, else an inline information. 38 | " let g:php_cs_fixer_ignore_env = 1 " Ignoring any environment requirements 39 | ``` 40 | 41 | Default mapping is `pcd` 42 | 43 | If you want to change it: 44 | 45 | ```viml 46 | nnoremap pcd :call PhpCsFixerFixDirectory() 47 | nnoremap pcf :call PhpCsFixerFixFile() 48 | ``` 49 | 50 | If you want to add **fix on save** functionality, add this string to the end of ~/.vimrc: 51 | ```viml 52 | autocmd BufWritePost *.php silent! call PhpCsFixerFixFile() 53 | ``` 54 | 55 | # Installation 56 | 57 | Via **[Vundle](https://github.com/gmarik/vundle)**, add: 58 | 59 | ```viml 60 | Bundle 'stephpy/vim-php-cs-fixer' 61 | ``` 62 | 63 | Via **[Pathogen](https://github.com/tpope/vim-pathogen)**, do: 64 | 65 | ```bash 66 | cd ~/.vim/bundle 67 | git clone git@github.com:stephpy/vim-php-cs-fixer.git 68 | ``` 69 | 70 | To see how to install `php-cs-fixer`, look at [php-cs-fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer) repository. 71 | 72 | If you see any improvement or question, contribute or create an issue 73 | 74 | -------------------------------------------------------------------------------- /autoload/php_cs_fixer.vim: -------------------------------------------------------------------------------- 1 | "============================================================================= 2 | " File: autoload/php_cs_fixer.vim 3 | " Author: Stéphane PY 4 | 5 | " Global options definition."{{{ 6 | let g:php_cs_fixer_path = get(g:, 'php_cs_fixer_path', '') 7 | let g:php_cs_fixer_php_path = get(g:, 'php_cs_fixer_php_path', 'php') 8 | 9 | if exists('g:php_cs_fixer_path') && g:php_cs_fixer_path != "" 10 | let g:php_cs_fixer_command = g:php_cs_fixer_php_path.' '.g:php_cs_fixer_path.' fix' 11 | let g:php_cs_fixer_version_command = 'PHP_CS_FIXER_IGNORE_ENV=1 '.g:php_cs_fixer_php_path.' '.g:php_cs_fixer_path.' --version' 12 | else 13 | if executable('php-cs-fixer') 14 | let g:php_cs_fixer_command = 'php-cs-fixer fix' 15 | let g:php_cs_fixer_version_command = 'PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer --version' 16 | else 17 | echoerr('php-cs-fixer not found and g:php_cs_fixer_path not set') 18 | finish 19 | end 20 | end 21 | 22 | " Check the php-cs-fixer version 23 | if (has('win32') || has('win64')) 24 | let sxq_save = &shellxquote 25 | set shellxquote& 26 | let g:php_cs_fixer_version = strpart(matchstr(system(g:php_cs_fixer_version_command), '\d\+\.\d\+\.\d\+'), 0, 1) 27 | let &shellxquote = sxq_save 28 | else 29 | let g:php_cs_fixer_version = system(g:php_cs_fixer_version_command . " | sed -e 's/[^0-9.]*\\([0-9.]*\\).*/\\1/'") 30 | endif 31 | 32 | if g:php_cs_fixer_version >= 2 33 | let g:php_cs_fixer_rules = get(g:, 'php_cs_fixer_rules', '@PSR2') 34 | else 35 | let g:php_cs_fixer_level = get(g:, 'php_cs_fixer_level', 'symfony') 36 | endif 37 | 38 | if exists('g:php_cs_fixer_config') && filereadable(expand(g:php_cs_fixer_config)) 39 | if g:php_cs_fixer_version == 1 40 | let g:php_cs_fixer_command = g:php_cs_fixer_command . ' --config-file=' . g:php_cs_fixer_config 41 | else 42 | let g:php_cs_fixer_command = g:php_cs_fixer_command . ' --config=' . g:php_cs_fixer_config 43 | endif 44 | endif 45 | 46 | if exists('g:php_cs_fixer_config_file') && filereadable(expand(g:php_cs_fixer_config_file)) 47 | if g:php_cs_fixer_version == 1 48 | let g:php_cs_fixer_command = g:php_cs_fixer_command . ' --config-file=' . g:php_cs_fixer_config_file 49 | else 50 | let g:php_cs_fixer_command = g:php_cs_fixer_command . ' --config=' . g:php_cs_fixer_config_file 51 | endif 52 | endif 53 | 54 | if exists('g:php_cs_fixer_cache') 55 | let g:php_cs_fixer_command = g:php_cs_fixer_command . ' --cache-file=' . g:php_cs_fixer_cache 56 | endif 57 | 58 | if exists('g:php_cs_fixer_allow_risky') && g:php_cs_fixer_version >= 2 59 | let g:php_cs_fixer_command = g:php_cs_fixer_command . ' --allow-risky=' . g:php_cs_fixer_allow_risky 60 | endif 61 | 62 | if exists('g:php_cs_fixer_ignore_env') && g:php_cs_fixer_version >= 2 63 | let g:php_cs_fixer_command = 'PHP_CS_FIXER_IGNORE_ENV=' . g:php_cs_fixer_ignore_env . ' ' . g:php_cs_fixer_command 64 | endif 65 | "}}} 66 | 67 | fun! php_cs_fixer#Fix(path, dry_run) 68 | 69 | if !executable('php-cs-fixer') 70 | if !filereadable(expand(g:php_cs_fixer_path)) 71 | echoerr(g:php_cs_fixer_path.' is not found') 72 | endif 73 | endif 74 | 75 | if &mod 76 | echohl Title | echo "There are unsaved changes" | echohl None 77 | return 78 | endif 79 | 80 | let command = g:php_cs_fixer_command.' '.a:path 81 | 82 | if a:dry_run == 1 83 | echohl Title | echo "[DRY RUN MODE]" | echohl None 84 | let command = command.' --dry-run' 85 | endif 86 | 87 | if g:php_cs_fixer_version >= 2 88 | if exists('g:php_cs_fixer_rules') && g:php_cs_fixer_rules != '@PSR2' 89 | let command = command." --rules='".g:php_cs_fixer_rules."'" 90 | endif 91 | if exists('g:php_cs_fixer_using_cache') && g:php_cs_fixer_rules != '@PSR2' 92 | let command = command." --using-cache='".g:php_cs_fixer_using_cache."'" 93 | endif 94 | else 95 | if exists('g:php_cs_fixer_level') && g:php_cs_fixer_level != 'all' 96 | let command = command.' --level='.g:php_cs_fixer_level 97 | endif 98 | if exists('g:php_cs_fixer_fixers_list') 99 | let command = command.' --fixers='.g:php_cs_fixer_fixers_list 100 | endif 101 | endif 102 | 103 | let s:output = system(command) 104 | 105 | if a:dry_run != 1 106 | exec 'edit!' 107 | endif 108 | 109 | let fix_num = 0 110 | let errors_report = 0 111 | let error_num = 0 112 | for line in split(s:output, '\n') 113 | if match(line, 'Files that were not fixed due to errors reported during linting before fixing:') != -1 114 | let errors_report = 1 115 | endif 116 | 117 | if match(line, '^\s\+\d\+)') != -1 118 | if errors_report == 0 119 | let fix_num = fix_num + 1 120 | else 121 | let error_num = error_num + 1 122 | endif 123 | endif 124 | endfor 125 | 126 | if !(v:shell_error == 0 || v:shell_error == 8 || (v:shell_error == 1 && fix_num > 0)) 127 | echohl Error | echo s:output | echohl None 128 | else 129 | 130 | if g:php_cs_fixer_verbose == 1 131 | echohl Title | echo s:output | echohl None 132 | else 133 | if fix_num > 0 134 | echohl Title | echo fix_num." file(s) modified(s)" | echohl None 135 | else 136 | echohl Title | echo "There is no cs to fix" | echohl None 137 | endif 138 | if error_num > 0 139 | echohl Error | echo error_num." error(s)" | echohl None 140 | endif 141 | endif 142 | 143 | " if there is no cs to fix, we have not to ask for remove dry run 144 | if a:dry_run == 1 && fix_num > 0 145 | let l:confirmed = confirm("Do you want to launch command without dry-run option ?", "&Yes\n&No", 2) 146 | if l:confirmed == 1 147 | call PhpCsFixerFix(a:path, 0) 148 | endif 149 | endif 150 | endif 151 | endfun 152 | -------------------------------------------------------------------------------- /doc/vim-php-cs-fixer.txt: -------------------------------------------------------------------------------- 1 | *vim-php-cs-fixer* 2 | 3 | License |vim-php-cs-fixer-license| 4 | Mappings |vim-php-cs-fixer-mappings| 5 | 6 | Integrate PHP CS Fixer (https://github.com/FriendsOfPHP/PHP-CS-Fixer). 7 | 8 | This plugin will execute the php-cs-fixer command 9 | on the directory or file (depends on which command you call). 10 | 11 | ============================================================================== 12 | MAPPINGS *vim-php-cs-fixer-mappings* 13 | 14 | pcd Calls php-cs-fixer on the current directory of 15 | the file. 16 | 17 | pcf Calls php-cs-fixer on the current file. 18 | 19 | 20 | To change default mappings, there are two commands available to you. 21 | `PhpCsFixerFixDirectory()` 22 | `PhpCsFixerFixFile()` 23 | 24 | A possible remapping could be 25 | `nnoremap d :call PhpCsFixerFixDirectory()` 26 | `nnoremap f :call PhpCsFixerFixFile()` 27 | 28 | ============================================================================== 29 | 30 | LICENSE *vim-php-cs-fixer-license* 31 | 32 | Copyright 2017 by Stéphane PY 33 | 34 | Permission is hereby granted, free of charge, to any person obtaining 35 | a copy of this software and associated documentation files (the 36 | "Software"), to deal in the Software without restriction, including 37 | without limitation the rights to use, copy, modify, merge, publish, 38 | distribute, sublicense, and/or sell copies of the Software, and to 39 | permit persons to whom the Software is furnished to do so, subject to 40 | the following conditions: 41 | 42 | The above copyright notice and this permission notice shall be included 43 | in all copies or substantial portions of the Software. 44 | 45 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 46 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 47 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 48 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 49 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 50 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 51 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 52 | 53 | ============================================================================== 54 | vim:tw=78:ts=8:ft=help:norl:noet:fen: 55 | -------------------------------------------------------------------------------- /plugin/php_cs_fixer.vim: -------------------------------------------------------------------------------- 1 | "============================================================================= 2 | " File: plugin/php_cs_fixer.vim 3 | " Author: Stéphane PY 4 | 5 | if exists("g:vim_php_cs_fixer") || &cp 6 | finish 7 | endif 8 | let g:vim_php_cs_fixer = 1 9 | 10 | let g:php_cs_fixer_enable_default_mapping = get(g:, 'php_cs_fixer_enable_default_mapping', '1') 11 | let g:php_cs_fixer_dry_run = get(g:, 'php_cs_fixer_dry_run', 0) 12 | let g:php_cs_fixer_verbose = get(g:, 'php_cs_fixer_verbose', 0) 13 | 14 | " Backwards compatibility 15 | fun! PhpCsFixerFix(path, dry_run) 16 | call php_cs_fixer#Fix(a:path, a:dry_run) 17 | endfun 18 | 19 | fun! PhpCsFixerFixDirectory() 20 | call php_cs_fixer#Fix(expand('%:p:h'), g:php_cs_fixer_dry_run) 21 | endfun 22 | 23 | fun! PhpCsFixerFixFile() 24 | call php_cs_fixer#Fix(expand('%:p'), g:php_cs_fixer_dry_run) 25 | endfun 26 | 27 | if(g:php_cs_fixer_enable_default_mapping == 1) 28 | nnoremap pcd :call PhpCsFixerFixDirectory() 29 | nnoremap pcf :call PhpCsFixerFixFile() 30 | endif 31 | 32 | " vim: foldmethod=marker 33 | -------------------------------------------------------------------------------- /tests/fixtures/misc/class_definition,no_trailing_whitespace.php: -------------------------------------------------------------------------------- 1 | 5 | -------------------------------------------------------------------------------- /tests/fixtures/misc/phpdoc_to_comment,phpdoc_var_without_name.php: -------------------------------------------------------------------------------- 1 | 3 | -------------------------------------------------------------------------------- /tests/fixtures/misc/simplified_null_return,no_useless_return,no_whitespace_in_blank_line,blank_line_before_return,no_extra_consecutive_blank_lines.php: -------------------------------------------------------------------------------- 1 |