简体   繁体   中英

PSR: How many new lines before namespace declaration

Ok, so PSR stands clearly 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM