Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to improve PHP makers #2471

Open
vaga opened this issue Feb 12, 2020 · 1 comment
Open

Proposal to improve PHP makers #2471

vaga opened this issue Feb 12, 2020 · 1 comment

Comments

@vaga
Copy link

vaga commented Feb 12, 2020

I made some changes to have more convenient with PHP makers.

My config allow to use:

  • a global executable
  • a composer executable when file has a project root
  • a global configuration (with args)
  • a composer configuration when file has a project root

I add a project root file for PHP and use InitForJob to generate a custom maker.

.vim/plugin/neomake.vim:

" General {{{
function! s:FindProjectExecutable(name) abort
    let project_root = neomake#utils#get_project_root()
    if empty(project_root)
        return ''
    endif

    let exe = project_root.a:name
    if !executable(exe)
        return ''
    endif

    return exe
endfunction

function! s:FindProjectFile(glob) abort
    let project_root = neomake#utils#get_project_root()
    if empty(project_root)
        return ''
    endif

    let fileFound = globpath(project_root, a:glob)
    if empty(fileFound)
        return ''
    endif

    return fileFound
endfunction
" }}}

" PHP {{{
let neomake#makers#ft#php#project_root_files = ['composer.json']
let g:neomake_php_enabled_makers = ['phpcs', 'phpstan']

"   PHPCS {{{
let g:neomake_php_phpcs_maker = {
    \ 'args': ['--report=csv', '-q'],
    \ 'errorformat':
    \ '%-GFile\,Line\,Column\,Type\,Message\,Source\,Severity%.%#,'.
    \ '"%f"\,%l\,%c\,%t%*[a-zA-Z]\,"%m"\,%*[a-zA-Z0-9_.-]\,%*[0-9]%.%#',
    \ }

function! g:neomake_php_phpcs_maker.InitForJob(jobinfo) abort
    let maker = deepcopy(self)

    let exe = s:FindProjectExecutable('/vendor/bin/'.(maker.name))
    if !empty(exe)
        " Use phpcs from composer instead of global executable
        let maker.exe = exe
    endif

    let config = s:FindProjectFile('{phpcs.xml,phpcs.xml.dist}')
    if !empty(config)
        " Use project root path for working directory to load PHPStan configuration
        let maker.cwd = fnamemodify(config, ':p:h')
    else
        if exists('g:neomake_php_phpcs_args_standard')
            call add(args, '--standard=' . expand(g:neomake_php_phpcs_args_standard))
        endif
    endif

    return maker
endfunction
"   }}}

"   PHPStan {{{
let g:neomake_php_phpstan_maker = {
    \ 'args': ['analyse', '--error-format', 'raw', '--no-progress'],
    \ 'errorformat': '%E%f:%l:%m',
    \ }

function! g:neomake_php_phpstan_maker.InitForJob(jobinfo) abort
    let maker = deepcopy(self)

    let exe = s:FindProjectExecutable('/vendor/bin/'.(maker.name))
    if !empty(exe)
        " Use phpstan from composer instead of global executable
        let maker.exe = exe
    endif

    let config = s:FindProjectFile('{phpstan.neon,phpstan.neon.dist}')
    if !empty(config)
        " Use project root path for working directory to load PHPStan configuration
        let maker.cwd = fnamemodify(config, ':p:h')
    else
        call extend(maker.args, ['--level', get(g:, 'neomake_phpstan_level', 0)])
    endif

    return maker
endfunction
"   }}}
" }}}

Before to suggest a PR, I want to know if it can be suitable for others.

@blueyed
Copy link
Member

blueyed commented Feb 20, 2020

I'm not using any php makers, so cannot really say.

Typically I find it more flexible to configure projects based on $PATH (e.g. with virtualenvs for Python), and .lvimrc files (https://github.com/embear/vim-localvimrc) for custom config (including Neomake).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants