Skip to content

Commit f694dcd

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 745be19 + e3e6690 commit f694dcd

File tree

5 files changed

+9
-10
lines changed

5 files changed

+9
-10
lines changed

Helper/HelperSet.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
class HelperSet implements \IteratorAggregate
2222
{
23-
private $helpers;
23+
private $helpers = array();
2424
private $command;
2525

2626
/**
@@ -30,7 +30,6 @@ class HelperSet implements \IteratorAggregate
3030
*/
3131
public function __construct(array $helpers = array())
3232
{
33-
$this->helpers = array();
3433
foreach ($helpers as $alias => $helper) {
3534
$this->set($helper, is_int($alias) ? null : $alias);
3635
}

Input/Input.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@
2424
*/
2525
abstract class Input implements InputInterface
2626
{
27+
/**
28+
* @var InputDefinition
29+
*/
2730
protected $definition;
28-
protected $options;
29-
protected $arguments;
31+
protected $options = array();
32+
protected $arguments = array();
3033
protected $interactive = true;
3134

3235
/**
@@ -37,8 +40,6 @@ abstract class Input implements InputInterface
3740
public function __construct(InputDefinition $definition = null)
3841
{
3942
if (null === $definition) {
40-
$this->arguments = array();
41-
$this->options = array();
4243
$this->definition = new InputDefinition();
4344
} else {
4445
$this->bind($definition);

Output/Output.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ abstract class Output implements OutputInterface
4646
public function __construct($verbosity = self::VERBOSITY_NORMAL, $decorated = false, OutputFormatterInterface $formatter = null)
4747
{
4848
$this->verbosity = null === $verbosity ? self::VERBOSITY_NORMAL : $verbosity;
49-
$this->formatter = null === $formatter ? new OutputFormatter() : $formatter;
49+
$this->formatter = $formatter ?: new OutputFormatter();
5050
$this->formatter->setDecorated($decorated);
5151
}
5252

Shell.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Shell
3232
private $history;
3333
private $output;
3434
private $hasReadline;
35-
private $processIsolation;
35+
private $processIsolation = false;
3636

3737
/**
3838
* Constructor.
@@ -48,7 +48,6 @@ public function __construct(Application $application)
4848
$this->application = $application;
4949
$this->history = getenv('HOME').'/.history_'.$application->getName();
5050
$this->output = new ConsoleOutput();
51-
$this->processIsolation = false;
5251
}
5352

5453
/**

Tests/ApplicationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ public function testTerminalDimensions()
862862

863863
protected function getDispatcher()
864864
{
865-
$dispatcher = new EventDispatcher;
865+
$dispatcher = new EventDispatcher();
866866
$dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) {
867867
$event->getOutput()->write('before.');
868868
});

0 commit comments

Comments
 (0)