Skip to content

Commit 9260eaf

Browse files
committed
minor #9487 unify constructor initialization style throughout symfony (Tobion)
This PR was merged into the master branch. Discussion ---------- unify constructor initialization style throughout symfony | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | n/a In almost all classes symfony uses property initialization when the value is static. Constructor initialization is only used for things that actually have logic, like passed parameters or dynamic values. IMHO it makes the code much more readable because property definition, phpdoc and default value is in one place. Also one can easily see what the constructor implements for logic like overridden default value of a parent class. Otherwise the real deal is just hidden behind 10 property initializations. One more advantage is that it requires less code. As you can see, the code was almost cut in half (210 additions and 395 deletions). I unified it accordingly across symfony. Sometimes it was [not even consistent within one class](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Config/Definition/BaseNode.php#L32). At the same time I recognized some errors like missing parent constructor call, or undefined properties or private properties that are not even used. I then realized that a few Kernel tests were not passing because they were deeply implementation specific like modifying booted flag with a custom `KernelForTest->setIsBooted();`. I improved and refactored the kernel tests in the __second commit__. __Third commit__ unifies short ternary operator, e.g. `$foo ?: new Foo()`. __Forth commit__ unifies missing parentheses, e.g. `new Foo()`. Commits ------- 077a089 unify missing parentheses 2888594 unify short ternary operator 2a9daff [HttpKernel] better written kernel tests 111ac18 unify constructor initialization style throughout symfony
2 parents 71d2c38 + f75c693 commit 9260eaf

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

Authentication/Token/AbstractToken.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
abstract class AbstractToken implements TokenInterface
2727
{
2828
private $user;
29-
private $roles;
30-
private $authenticated;
31-
private $attributes;
29+
private $roles = array();
30+
private $authenticated = false;
31+
private $attributes = array();
3232

3333
/**
3434
* Constructor.
@@ -39,10 +39,6 @@ abstract class AbstractToken implements TokenInterface
3939
*/
4040
public function __construct(array $roles = array())
4141
{
42-
$this->authenticated = false;
43-
$this->attributes = array();
44-
45-
$this->roles = array();
4642
foreach ($roles as $role) {
4743
if (is_string($role)) {
4844
$role = new Role($role);

0 commit comments

Comments
 (0)