|
| 1 | +-- Tested using PHP 7.x and PHP 8.x. |
| 2 | +local h = require("null-ls.helpers") |
| 3 | +local methods = require("null-ls.methods") |
| 4 | + |
| 5 | +local DIAGNOSTICS = methods.internal.DIAGNOSTICS |
| 6 | + |
| 7 | +return h.make_builtin({ |
| 8 | + name = "php", |
| 9 | + meta = { |
| 10 | + url = "https://www.php.net/", |
| 11 | + description = "Uses the php command-line tool's built in `-l` flag to check for syntax errors.", |
| 12 | + }, |
| 13 | + method = DIAGNOSTICS, |
| 14 | + filetypes = { "php" }, |
| 15 | + generator_opts = { |
| 16 | + command = "php", |
| 17 | + -- Send file to stdin otherwise checking is only done when the file is saved. |
| 18 | + to_stdin = true, |
| 19 | + to_temp_file = false, |
| 20 | + -- -d display_errors=STDERR ensures errors are reported to stderr. |
| 21 | + -- -d log_errors=Off Disables logging of errors. |
| 22 | + -- |
| 23 | + -- Without these, a setting in php.ini can turn off error reporting, or |
| 24 | + -- change where errors are reported. |
| 25 | + args = { "-l", "-d", "display_errors=STDERR", "-d", " log_errors=Off" }, |
| 26 | + from_stderr = true, |
| 27 | + format = "line", |
| 28 | + check_exit_code = function(code) |
| 29 | + -- Code 0 means no syntax errors. |
| 30 | + -- Code 255 means syntax errors. |
| 31 | + -- Other codes mean something went wrong. |
| 32 | + return code == 0 or code == 255 |
| 33 | + end, |
| 34 | + on_output = h.diagnostics.from_patterns({ |
| 35 | + { |
| 36 | + -- Example of an error when checking a file: |
| 37 | + -- Parse error: syntax error, unexpected '$appends' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST) in app/Config.php on line 16 |
| 38 | + |
| 39 | + -- Example of an error when checking stdin: |
| 40 | + -- Parse error: syntax error, unexpected token "=>", expecting "," or ";" in Standard input code on line 21 |
| 41 | + |
| 42 | + -- This pattern should match both. |
| 43 | + pattern = [[Parse error: (.*) in (.*) on line (%d+)]], |
| 44 | + groups = { "message", "junk", "row" }, |
| 45 | + overrides = { |
| 46 | + diagnostic = { severity = h.diagnostics.severities["error"] }, |
| 47 | + offsets = { col = 1, end_col = 1 }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }), |
| 51 | + }, |
| 52 | + factory = h.generator_factory, |
| 53 | +}) |
0 commit comments