phppsr-2psr-12

How many new lines before namespace declaration in PSR?


PSR has a fairly clear stance regarding new lines after namespace declaration:

When present, there MUST be one blank line after the namespace declaration.

But what about blank lines before namespaces? Are there any rules or limitations about it? I believe I've seen something somewhere, but cannot find now.


Solution

  • The section 3. Declare Statements, Namespace, and Import Statements in PSR-12 talks about the various parts of the start of the script and states

    The header of a PHP file may consist of a number of different blocks. If present, each of the blocks below MUST be separated by a single blank line, and MUST NOT contain a blank line. Each block MUST be in the order listed below, although blocks that are not relevant may be omitted.

    (emphasis mine).

    Also following is the example

    <?php
    
    /**
     * This file contains an example of coding styles.
     */
    
    declare(strict_types=1);
    
    namespace Vendor\Package;
    
    use Vendor\Package\{ClassA as A, ClassB, ClassC as C};
    

    which shows the spacing.

    The main thing I read from this is not about defining the number of blank lines before but more defining that any of the components must have a single blank line between them.

    Just a comment about PSR-2, is does state

    Deprecated - As of 2019-08-10 PSR-2 has been marked as deprecated. PSR-12 is now recommended as an alternative.