PHP-Parser icon indicating copy to clipboard operation
PHP-Parser copied to clipboard

Error recovery with method parameters with missing names

Open Gert-dev opened this issue 7 years ago • 4 comments

A situation developers often encounter is when typing out function and method signatures, the code is invalid for a time:

<?php
// Still have to type the name of the parameter here.
// Currently produces "Syntax error, unexpected ')', expecting T_VARIABLE on line 3"
function test(Request) {

}

One that may be a bit tougher to handle:

<?php

// Still have to type the name of the parameter, complete the closing paranthesis as well as the body.
// Currently produces "Syntax error, unexpected EOF, expecting T_VARIABLE on line 4"
function test(Request

The same likely applies to methods inside a class.

Due there not being any AST currently, autocompletion in Serenata fails to work properly. Would it be possible to add (somewhat sensible) error recovery for these so that some measure of static analysis can be performed?

Thanks for your time and any assistance you can offer.

Gert-dev avatar May 10 '18 11:05 Gert-dev

The first case is implemented in https://github.com/nikic/PHP-Parser/commit/01e85a26c663acb20472bb5cf17ae39743da338a. The variable will be an Expr\Error in this case. Also works for ... and &, though I guess those are not important in practice.

I don't think I can do the second one at this point though.

nikic avatar May 12 '18 17:05 nikic

Thanks! That already helps, especially when using snippets that generate an empty method body, in which case the second case doesn't happen.

Is the second case something you want to do at some point in the future or too difficult to solve?

Gert-dev avatar May 13 '18 13:05 Gert-dev

@Gert-dev I've pushed https://github.com/nikic/PHP-Parser/commit/7b201b63d2cef4ec795d52044a31d20e79e871fb which makes the function (Test) case work. As code editors usually insert () pairs, this will probably handle the common cases.

nikic avatar May 13 '18 14:05 nikic

@nikic That is true and this change also takes away some of the pain, upping (the illusion of) professional autocompletion in Serenata.

Feel free to close this or leave it open if you want to revise it at some point in the future.

Gert-dev avatar May 14 '18 19:05 Gert-dev

Going to close this, as I think it's unlikely I'm ever going to implement the remaining bit here...

nikic avatar May 21 '23 16:05 nikic