When contributing code to Symfony2, you must follow its coding standards. Tomake a long story short, here is the golden rule: Imitate the existingSymfony2 code. Most open-source Bundles and libraries used by Symfony2 alsofollow the same guidelines, and you should too.

Remember that the main advantage of standards is that every piece of codelooks and feels familiar, it's not about this or that being more readable.

Symfony follows the standards defined in the PSR-0, PSR-1 and PSR-2documents.

Since a picture - or some code - is worth a thousand words, here's a shortexample containing most features described below:


 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Acme;
/**
 * Coding standards demonstration.
 */
class FooBar
{
    const SOME_CONST = 42;
    private $fooBar;
    /**
     * @param string $dummy Some argument description
     */
    public function __construct($dummy)
    {
        $this->fooBar = $this->transformText($dummy);
    }
    /**
     * @param string $dummy Some argument description
     * @param array  $options
     *
     * @return string|null Transformed input
     */
    private function transformText($dummy, array $options = array())
    {
        $mergedOptions = array_merge(
            $options,
            array(
                'some_default' => 'values',
                'another_default' => 'more values',
            )
        );
        if (true === $dummy) {
            return;
        }
        if ('string' === $dummy) {
            if ('values' === $mergedOptions['some_default']) {
                return substr($dummy, 0, 5);
            }
            return ucwords($dummy);
        }
        throw new \RuntimeException(sprintf('Unrecognized dummy option "%s"', $dummy));
    }
}

Structure

  • Add a single space after each comma delimiter;

  • Add a single space around operators (==, &&, ...);

  • Add a comma after each array item in a multi-line array, even after thelast one;

  • Add a blank line before return statements, unless the return is aloneinside a statement-group (like an if statement);

  • Use braces to indicate control structure body regardless of the number ofstatements it contains;

  • Define one class per file - this does not apply to private helper classesthat are not intended to be instantiated from the outside and thus are notconcerned by the PSR-0 standard;

  • Declare class properties before methods;

  • Declare public methods first, then protected ones and finally private ones;

  • Use parentheses when instantiating classes regardless of the number ofarguments the constructor has;

  • Exception message strings should be concatenated using sprintf.

Naming Conventions

  • Use camelCase, not underscores, for variable, function and methodnames, arguments;

  • Use underscores for option names and parameter names;

  • Use namespaces for all classes;

  • Prefix abstract classes with Abstract. Please note some early Symfony2 classesdo not follow this convention and have not been renamed for backward compatibilityreasons. However all new abstract classes must follow this naming convention;

  • Suffix interfaces with Interface;

  • Suffix traits with Trait;

  • Suffix exceptions with Exception;

  • Use alphanumeric characters and underscores for file names;

  • Don't forget to look at the more verbose Conventions document formore subjective naming considerations.

Service Naming Conventions

  • A service name contains groups, separated by dots;

  • The DI alias of the bundle is the first group (e.g. fos_user);

  • Use lowercase letters for service and parameter names;

  • A group name uses the underscore notation;

  • Each service has a corresponding parameter containing the class name,following the SERVICE NAME.class convention.

Documentation

  • Add PHPDoc blocks for all classes, methods, and functions;

  • Omit the @return tag if the method does not return anything;

  • The @package and @subpackage annotations are not used.

License

  • Symfony is released under the MIT license, and the license block has to bepresent at the top of every PHP file, before the namespace.