Skip to content

one-declaration-per-file

Discurage including more than one declaration per file

Examples

  • Examples of correct code for this rule using default options

One class per file

php
<?php

class Connect {
	public function __construct() {
		echo 'Connect';
	}
}

One interface per file

php
<?php

interface Connect {
	public function __construct();
}

One trait per file

php
<?php

trait Connect {
	function log() {
		echo 'Connect';
	}
}

One enum per file

php
<?php

enum MyExceptionCase {
    case InvalidMethod;
    case InvalidProperty;
    case Timeout;
}

One namespace per file

php
<?php
namespace App;

class Connect {
	public function __construct() {
		echo 'Connect';
	}
}
  • Examples of incorrect code for this rule using default options

Multiple object structures per file

php
<?php

class Connect {
	public function __construct() {
		echo 'Connect';
	}
}

class Logger {
	public function __construct() {
		echo 'Logger';
	}
}

interface Connect {
	public function __construct();
}

function logError() {
	echo 'log';
}

Multiple unbracketed namespaces per file

php
<?php

namespace App;

class Connect {
	public function __construct() {
		echo 'Connect';
	}
}

namespace App2;

class Connect2 {
	public function __construct() {
		echo 'Connect2';
	}
}

Multiple bracketed namespaces per file

php
<?php

namespace App{
	class Connect {
		public function __construct() {
			echo 'Connect';
		}
	}
}

namespace App2 {
	class Connect2 {
		public function __construct() {
			echo 'Connect2';
		}
	}
}

Multiple enums per file

php
<?php

enum MyExceptionCase {
    case InvalidMethod;
    case InvalidProperty;
    case Timeout;
}

enum Months {
    case January;
    case February;
    case March;
    case April;
    case May;
    case June;
    case July;
    case August;
    case September;
    case October;
    case November;
    case December;
}

enum Status {
    case Pending;
    case Processing;
    case Completed;
    case Failed;
}

Released under the MIT License.