Skip to content
Fustrate edited this page Aug 6, 2012 · 11 revisions

Internal smCore Coding Standards

...

PHP

Standards

  • No trailing ?> in PHP files.
  • Never use spaces in place of tab indentation.
  • Always use spaces for alignment.
  • Always use the Allman style.
  • Echo statements should use commas instead of concatenation (periods) wherever possible.
  • DocBlock comments should be used for all methods, classes, and functions.
  • Class methods, properties, and constants have visibility explicitly declared, even if it's public.
  • Order of visibility/type for methods: public|protected|private [static|abstract] function.
  • Protected and private methods, properties, and constants begin with an underscore, public non-magic methods do not.
  • Non-logical language constructs such as include and require should not use parentheses. if, while, for, foreach, and other logical constructs should, however.
  • There must be a space after control structures: if ($condition) is correct, while($condition) is incorrect.
  • All control structures should use curly braces.
  • use statements should be grouped by vendor, one statement per vendor.

Naming

Type Format
Properties & Array Indexes underscore_lower
Private and Protected Variables _underscore_lower
Constants UNDERSCORE_UPPER
Methods camelCaseLower
Functions underscore_lower
Classes & Interfaces CamelCaseUpper
Namespaces Camel\Case\Upper

Example

<?php

namespace smCore\Example;

use smCore\Filesystem\Directory, smCore\Another\Thing;
use Symfony\Component\Yaml\Yaml;
use Exception;

class Person
{
	const MIN_AGE = 18;

	public $name;
	protected $_age;
	private $_social_security_id;

	public function __construct($name, $age, $social_security_id)
	{
		if (empty($name))
		{
			throw new Exception('You have to have a name!');
		}

		if (!ctype_digit($age) || self::MIN_AGE > (int) $age)
		{
			throw new Exception(sprintf('The person\'s age must be a number greater than %d', self::MIN_AGE));
		}

		if (!preg_match('/^[0-9]{3}-?[0-9]{2}-?[0-9]{4}$/', $social_security_id))
		{
			throw new Exception('Social security ID must be in the format ###-##-####.');
		}

		$this->name = $name;
		$this->_age = (int) $age;
		$this->_social_security_id = $social_security_id;
	}

	protected function _eatBagel()
	{
		// Nom nom nom
		return $this;
	}
}

CSS

  • Hexadecimal colors should be lowercase.
  • Try to keep selectors and rules in alphabetical order when possible.

Example

#martha,
#mickey,
#rose,
#screamer {
	background: red;
	color: #fff;
	font-weight: bold;
	}

.enemies {
	display: none;
	visibility: hidden;
	}

Javascript

(todo)

Example

// TWO Amy Ponds! TWO!
var dalek = function()
{
	$("#doctor").exterminate();

	message = {
		"recipient": "Dalek Caan",
		"message": "The Doctor has been exterminated.",
		"next_targets": [
			"Donna the Screamer",
			"Donna Again",
			"Exterminate Donna"
		]
	};

	sendHome(message);
};