Skip to content

Add code sample and some more details to the CS page #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 6, 2011
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions contributing/code/standards.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,53 @@ Coding Standards
================

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

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

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

.. code-block:: php

<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Acme;

class Foo
{
private $foo;

const SOME_CONST = 42;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, constants are defined first in the Sf2 code.


public function __construct($dummy)
{
$this->foo = $this->init($dummy);
}

private function init($dummy)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should add some phpdoc. documentign the code is better too :)

{
if (true === $dummy) {
return;
} elseif ('string' === $dummy) {
$var = substr($dummy, 0, 5);
}

return $var;
}
}

Structure
---------
Expand Down Expand Up @@ -35,8 +80,8 @@ Structure
* Put braces on their own line for classes, methods, and functions
declaration;

* Separate the conditional statement and the opening brace with a single
space and no blank line;
* Separate the conditional statements (`if`, `else`, ...) and the opening
brace with a single space and no blank line;

* Declare visibility explicitly for class, methods, and properties (usage of
`var` is prohibited);
Expand Down Expand Up @@ -74,3 +119,9 @@ Documentation
* Add PHPDoc blocks for all classes, methods, and functions;

* The `@package` and `@subpackage` annotations are not used.

License
-------

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