phpnetbeans-8psr-1

Why NetBeans complains about PSR-1 violation when assigning returned value?


I have this code:

<?php

function f() {
    return 5;
}

$a = 5;

And I am using NetBeans 8.2 with PSR-1 compatibility check turned on. And I am getting this error:

enter image description here

Why is that and how can I fix it?


Solution

  • The PSR-1 standard states that

    Files SHOULD either declare symbols (classes, functions, constants, etc.) or cause side-effects (e.g. generate output, change .ini settings, etc.) but SHOULD NOT do both.

    This means if you have function or class definitions in a file, you shouldn't have code with side-effects (like a variable assignment) outside these definitions within the same file.

    So

    function f() {
        return 5;
    }
    

    and

    $a = 5;
    

    should be separated into different files.