ale
ale copied to clipboard
php shell / cli script and namespaces
Discussed in https://github.com/dense-analysis/ale/discussions/4232
Originally posted by Nickwiz June 14, 2022
When using ALE with PHP as cgi, shell script ... and using namespace
the first lines can be something like:
#! /usr/bin/env php
<?php
namespace CLI;
echo "OK\n";
This produces ALE error:
Namespace declaration statement has to be the very first statement or after any declare call in the script
It complains about the shebang and note that PHP does so if the file is redirected instead of used as an argument, i.e:
OK:
$ php -l -d error_reporting=E_ALL -d display_errors=1 -d log_errors=0 test.php
No syntax errors detected in test.php
$ php test.php
OK
$ ./test.php
OK
BAD:
$ php -l -d error_reporting=E_ALL -d display_errors=1 -d log_errors=0 <test.php
Fatal error: Namespace declaration statement has to be the very first statement or
after any declare call in the script in Standard input code on line 3
Errors parsing Standard input code
$ php< test.php
PHP Fatal error: Namespace declaration statement has to be the very first statement or
after any declare call in the script in Standard input code on line 3
Is there an option to rectify for this somewhere? If not, it should perhaps be reported as a bug, but not sure.
I do not know the ALE code and not sure why it redirects the file instead of using it as an argument.
Have not found any options in the Command line options section that could help in this regard. https://www.php.net/manual/en/features.commandline.options.php
You can see the command that ALE uses at php.vim#L37
call ale#linter#Define('php', {
\ 'name': 'php',
\ 'executable': {b -> ale#Var(b, 'php_php_executable')},
\ 'output_stream': 'stdout',
\ 'command': '%e -l -d error_reporting=E_ALL -d display_errors=1 -d log_errors=0 --',
\ 'callback': 'ale_linters#php#php#Handle',
\})
By default ALE uses stdin (redirection) for passing the data to the linters but this can be changed by setting the lint_file option to "1". See :h ale#linter#Define
. Unfortunatelly this change would require changes to ALEs code as this cannot be changed via config and also reading files from disk disables linting while typing. Read the docs in lint_file option for more details.