Skip to content

Commit 6815106

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 88aa63c + a6d6b4c commit 6815106

File tree

6 files changed

+23
-23
lines changed

6 files changed

+23
-23
lines changed

Encoder/JsonEncoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
3232

3333
public function __construct(JsonEncode $encodingImpl = null, JsonDecode $decodingImpl = null)
3434
{
35-
$this->encodingImpl = null === $encodingImpl ? new JsonEncode() : $encodingImpl;
36-
$this->decodingImpl = null === $decodingImpl ? new JsonDecode(true) : $decodingImpl;
35+
$this->encodingImpl = $encodingImpl ?: new JsonEncode();
36+
$this->decodingImpl = $decodingImpl ?: new JsonDecode(true);
3737
}
3838

3939
/**

Tests/Encoder/JsonEncoderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class JsonEncoderTest extends \PHPUnit_Framework_TestCase
1919
{
2020
protected function setUp()
2121
{
22-
$this->encoder = new JsonEncoder;
22+
$this->encoder = new JsonEncoder();
2323
$this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
2424
}
2525

Tests/Encoder/XmlEncoderTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase
2424

2525
protected function setUp()
2626
{
27-
$this->encoder = new XmlEncoder;
27+
$this->encoder = new XmlEncoder();
2828
$serializer = new Serializer(array(new CustomNormalizer()), array('xml' => new XmlEncoder()));
2929
$this->encoder->setSerializer($serializer);
3030
}
3131

3232
public function testEncodeScalar()
3333
{
34-
$obj = new ScalarDummy;
34+
$obj = new ScalarDummy();
3535
$obj->xmlFoo = "foo";
3636

3737
$expected = '<?xml version="1.0"?>'."\n".
@@ -42,7 +42,7 @@ public function testEncodeScalar()
4242

4343
public function testSetRootNodeName()
4444
{
45-
$obj = new ScalarDummy;
45+
$obj = new ScalarDummy();
4646
$obj->xmlFoo = "foo";
4747

4848
$this->encoder->setRootNodeName('test');
@@ -63,7 +63,7 @@ public function testDocTypeIsNotAllowed()
6363

6464
public function testAttributes()
6565
{
66-
$obj = new ScalarDummy;
66+
$obj = new ScalarDummy();
6767
$obj->xmlFoo = array(
6868
'foo-bar' => array(
6969
'@id' => 1,
@@ -92,7 +92,7 @@ public function testAttributes()
9292

9393
public function testElementNameValid()
9494
{
95-
$obj = new ScalarDummy;
95+
$obj = new ScalarDummy();
9696
$obj->xmlFoo = array(
9797
'foo-bar' => 'a',
9898
'foo_bar' => 'a',
@@ -206,7 +206,7 @@ public function testEncode()
206206
public function testEncodeSerializerXmlRootNodeNameOption()
207207
{
208208
$options = array('xml_root_node_name' => 'test');
209-
$this->encoder = new XmlEncoder;
209+
$this->encoder = new XmlEncoder();
210210
$serializer = new Serializer(array(), array('xml' => new XmlEncoder()));
211211
$this->encoder->setSerializer($serializer);
212212

@@ -289,7 +289,7 @@ public function testDecodeArray()
289289

290290
public function testDecodeWithoutItemHash()
291291
{
292-
$obj = new ScalarDummy;
292+
$obj = new ScalarDummy();
293293
$obj->xmlFoo = array(
294294
'foo-bar' => array(
295295
'@key' => "value",
@@ -362,7 +362,7 @@ protected function getXmlSource()
362362

363363
protected function getObject()
364364
{
365-
$obj = new Dummy;
365+
$obj = new Dummy();
366366
$obj->foo = 'foo';
367367
$obj->bar = array('a', 'b');
368368
$obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', 'item' => array(array('title' => 'title1'), array('title' => 'title2')), 'Barry' => array('FooBar' => array('Baz' => 'Ed', '@id' => 1)));

Tests/Normalizer/CustomNormalizerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ class CustomNormalizerTest extends \PHPUnit_Framework_TestCase
1919
{
2020
protected function setUp()
2121
{
22-
$this->normalizer = new CustomNormalizer;
23-
$this->normalizer->setSerializer(new Serializer);
22+
$this->normalizer = new CustomNormalizer();
23+
$this->normalizer->setSerializer(new Serializer());
2424
}
2525

2626
public function testSerialize()
2727
{
28-
$obj = new ScalarDummy;
28+
$obj = new ScalarDummy();
2929
$obj->foo = 'foo';
3030
$obj->xmlFoo = 'xml';
3131
$this->assertEquals('foo', $this->normalizer->normalize($obj, 'json'));
@@ -34,18 +34,18 @@ public function testSerialize()
3434

3535
public function testDeserialize()
3636
{
37-
$obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy), 'xml');
37+
$obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy()), 'xml');
3838
$this->assertEquals('foo', $obj->xmlFoo);
3939
$this->assertNull($obj->foo);
4040

41-
$obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy), 'json');
41+
$obj = $this->normalizer->denormalize('foo', get_class(new ScalarDummy()), 'json');
4242
$this->assertEquals('foo', $obj->foo);
4343
$this->assertNull($obj->xmlFoo);
4444
}
4545

4646
public function testSupportsNormalization()
4747
{
48-
$this->assertTrue($this->normalizer->supportsNormalization(new ScalarDummy));
48+
$this->assertTrue($this->normalizer->supportsNormalization(new ScalarDummy()));
4949
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass));
5050
}
5151

Tests/Normalizer/GetSetMethodNormalizerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ class GetSetMethodNormalizerTest extends \PHPUnit_Framework_TestCase
1717
{
1818
protected function setUp()
1919
{
20-
$this->normalizer = new GetSetMethodNormalizer;
20+
$this->normalizer = new GetSetMethodNormalizer();
2121
$this->normalizer->setSerializer($this->getMock('Symfony\Component\Serializer\Serializer'));
2222
}
2323

2424
public function testNormalize()
2525
{
26-
$obj = new GetSetDummy;
26+
$obj = new GetSetDummy();
2727
$obj->setFoo('foo');
2828
$obj->setBar('bar');
2929
$obj->setCamelCase('camelcase');
@@ -118,7 +118,7 @@ public function testIgnoredAttributes()
118118
{
119119
$this->normalizer->setIgnoredAttributes(array('foo', 'bar', 'camelCase'));
120120

121-
$obj = new GetSetDummy;
121+
$obj = new GetSetDummy();
122122
$obj->setFoo('foo');
123123
$obj->setBar('bar');
124124

Tests/SerializerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public function testNormalizeNoMatch()
3434
public function testNormalizeTraversable()
3535
{
3636
$this->serializer = new Serializer(array(), array('json' => new JsonEncoder()));
37-
$result = $this->serializer->serialize(new TraversableDummy, 'json');
37+
$result = $this->serializer->serialize(new TraversableDummy(), 'json');
3838
$this->assertEquals('{"foo":"foo","bar":"bar"}', $result);
3939
}
4040

4141
public function testNormalizeGivesPriorityToInterfaceOverTraversable()
4242
{
43-
$this->serializer = new Serializer(array(new CustomNormalizer), array('json' => new JsonEncoder()));
44-
$result = $this->serializer->serialize(new NormalizableTraversableDummy, 'json');
43+
$this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
44+
$result = $this->serializer->serialize(new NormalizableTraversableDummy(), 'json');
4545
$this->assertEquals('{"foo":"normalizedFoo","bar":"normalizedBar"}', $result);
4646
}
4747

0 commit comments

Comments
 (0)