Skip to content

Commit 57ff6d6

Browse files
committed
Merge pull request symfony#508 from Seldaek/cs
Add code sample and some more details to the CS page
2 parents 6419194 + 5487093 commit 57ff6d6

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

contributing/code/conventions.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ Conventions
44
The :doc:`standards` document describes the coding standards for the Symfony2
55
projects and the internal and third-party bundles. This document describes
66
coding standards and conventions used in the core framework to make it more
7-
consistent and predictable. You can follow them in your own code, but you
8-
don't need to.
7+
consistent and predictable. You are encouraged to follow them in your own
8+
code, but you don't need to.
99

1010
Method Names
1111
------------

contributing/code/standards.rst

+65-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,60 @@ Coding Standards
22
================
33

44
When contributing code to Symfony2, you must follow its coding standards. To
5-
make a long story short, here is the golden rule: *Imitate the existing
6-
Symfony2 code*.
5+
make a long story short, here is the golden rule: **Imitate the existing
6+
Symfony2 code**. Most open-source Bundles and libraries used by Symfony2 also
7+
follow the same guidelines, and you should too.
8+
9+
Remember that the main advantage of standards is that every piece of code
10+
looks and feels familiar, it's not about this or that being more readable.
11+
12+
Since a picture - or some code - is worth a thousand words, here's a short
13+
example containing most features described below:
14+
15+
.. code-block:: php
16+
17+
<?php
18+
19+
/*
20+
* This file is part of the Symfony package.
21+
*
22+
* (c) Fabien Potencier <fabien@symfony.com>
23+
*
24+
* For the full copyright and license information, please view the LICENSE
25+
* file that was distributed with this source code.
26+
*/
27+
28+
namespace Acme;
29+
30+
class Foo
31+
{
32+
const SOME_CONST = 42;
33+
34+
private $foo;
35+
36+
/**
37+
* @param string $dummy Some argument description
38+
*/
39+
public function __construct($dummy)
40+
{
41+
$this->foo = $this->transform($dummy);
42+
}
43+
44+
/**
45+
* @param string $dummy Some argument description
46+
* @return string|null Transformed input
47+
*/
48+
private function transform($dummy)
49+
{
50+
if (true === $dummy) {
51+
return;
52+
} elseif ('string' === $dummy) {
53+
$dummy = substr($dummy, 0, 5);
54+
}
55+
56+
return $dummy;
57+
}
58+
}
759
860
Structure
961
---------
@@ -35,8 +87,8 @@ Structure
3587
* Put braces on their own line for classes, methods, and functions
3688
declaration;
3789

38-
* Separate the conditional statement and the opening brace with a single
39-
space and no blank line;
90+
* Separate the conditional statements (`if`, `else`, ...) and the opening
91+
brace with a single space and no blank line;
4092

4193
* Declare visibility explicitly for class, methods, and properties (usage of
4294
`var` is prohibited);
@@ -68,9 +120,18 @@ Naming Conventions
68120

69121
* Use alphanumeric characters and underscores for file names;
70122

123+
* Don't forget to look at the more verbose :doc:`conventions` document for
124+
more subjective naming considerations.
125+
71126
Documentation
72127
-------------
73128

74129
* Add PHPDoc blocks for all classes, methods, and functions;
75130

76131
* The `@package` and `@subpackage` annotations are not used.
132+
133+
License
134+
-------
135+
136+
* Symfony is released under the MIT license, and the license block has to be
137+
present at the top of every PHP file, before the namespace.

0 commit comments

Comments
 (0)