Php syntax that understands indentation

Consider this php script:

<?php                       // 1
                            // 2
function foo() {            // 3
                            // 4
    $foo = 1;               // 5
                            // 6
    if("something") {       // 7
        $bar = 2;           // 8
                            // 9 = note the missing brace
                            // 10
    $baz = 4;               // 11
                            // 12
}                           // 13
?>                          // 14

      

When you run it you will get a parse error

 Parse error: syntax error, unexpected end of file in ... on line 14

      

Is there a php tool that is smart enough to parse my indentation and tell me the actual problem is on line 9 (or 11, for that matter) and not at the end of the script.

+3


source to share


4 answers


PHP_CodeSniffer is a little helpful here. This is what it prints for the script in question:

--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 4 LINE(S)
--------------------------------------------------------------------------------
  2 | ERROR | Missing file doc comment
  3 | ERROR | Missing function doc comment
  8 | ERROR | Line indented incorrectly; expected 0 spaces, found 4
 22 | ERROR | Closing brace indented incorrectly; expected 4 spaces, found 0
--------------------------------------------------------------------------------

      



The third message is confusing, but still closer to the source of the problem than the default.

0


source


This problem is largely insoluble. The compiler will do its best to tell you where the error occurred, but it cannot do it accurately. Basically, it will keep scanning the file until it reaches something that should be invalid at that point; but that doesn't mean that where you squinted. It simply means that the moment you made your mistake, more technical syntax followed (even if it was completely stupid) until the compiler gave up in disgust.

So in your case, the syntax is technically good up to line 14, although the code deviates from what you specified on line 9. The parser cannot know this.



Since indentation is optional and ignored by the parser, it will almost certainly cause more problems than it would cost to account for the indentation. If it did, it would keep showing up with errors where there were no errors. After all, one of the syntactically meaningless consequences of indenting is that there is more than one sensible way to indent.

Your best bet, I think, would be to use an IDE that will automatically add padding for you. (Eclipse can certainly be configured to sort the indentation for you.) If you do this, you will find that when you write line 11, you want to indent more than you expected, and it will let you know that you forgot to close the bracket.

+2


source


Technically it would be possible and not as difficult to implement as a plugin for some IDEs (Netbeans, PHP Storm, Eclipse, ...) But I don't know of any plugins or IDEs that currently offer such a feature. Perhaps you can implement it yourself.

A simple implementation would be like

  • split the code into lines
  • define indentation for each line
  • when changing the indentation level find {

  • case 1 no {

    • => if there are multiple level n + 1 identifiers in the following lines - enter a warning
  • case 2: {

    • move forward to the next level idention level and seek }

    • if theres no }

      => throw a warning

this procedure should be called recursive

+1


source


I know this doesn't technically answer the question, but I found that syntax highlighting in netbeans helps doesn't end with this problem.

Now I could not live without him.

enter image description here

0


source







All Articles