Skip to content

Commit 6f868a2

Browse files
Fixed PropertyFormatter to be compatible with \ReflectionProperty
1 parent c9f9516 commit 6f868a2

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

src/Formatter/ClassFormatter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ public function format(bool $ignoreSubElements = false): string
128128
}
129129

130130
foreach ($this->class->getProperties() as $property) {
131-
$result .= (new PropertyFormatter($this->class->getName(), $property))->format();
131+
$defaultValue = $this->class->getDefaultProperties()[$property->getName()] ?? null;
132+
$result .= (new PropertyFormatter($this->class->getName(), $property, $defaultValue))->format();
132133
}
133134

134135
foreach ($this->class->getMethods() as $method) {

src/Formatter/PropertyFormatter.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ class PropertyFormatter
1919
*/
2020
protected $property;
2121

22-
public function __construct(string $className, ReflectionProperty $property)
22+
/**
23+
* @var mixed
24+
*/
25+
protected $defaultValue;
26+
27+
public function __construct(string $className, ReflectionProperty $property, $defaultValue)
2328
{
2429
$this->className = $className;
2530
$this->property = $property;
31+
$this->defaultValue = $defaultValue;
2632
}
2733

2834
public function format(): string
@@ -56,7 +62,7 @@ public function format(): string
5662
}
5763

5864
$result .= '$' . $this->property->getName();
59-
$formattedValue = FormatHelper::formatValue($this->property->getValue());
65+
$formattedValue = FormatHelper::formatValue($this->defaultValue);
6066
if ($formattedValue !== 'null') {
6167
$result .= ' = ' . $formattedValue;
6268
}

tests/unit/Formatter/PropertyFormatterTest.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ protected function createReflectionPropertyMock(
2727
'isPublic',
2828
'isProtected',
2929
'isPrivate',
30-
'isStatic',
31-
'getValue'
30+
'isStatic'
3231
])
3332
->disableOriginalConstructor()
3433
->getMock();
@@ -52,13 +51,12 @@ public function testSimpleProperty()
5251
$property->method('isProtected')->willReturn(false);
5352
$property->method('isPrivate')->willReturn(false);
5453
$property->method('isStatic')->willReturn(false);
55-
$property->method('getValue')->willReturn(123);
5654

5755
/**
5856
* @var \ReflectionProperty $property
5957
*/
6058
$expectedOutput = $t . $t . 'public $test = 123;' . $n . $n;
61-
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property))->format());
59+
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property, 123))->format());
6260
}
6361

6462
public function testProtectedProperty()
@@ -75,13 +73,12 @@ public function testProtectedProperty()
7573
$property->method('isProtected')->willReturn(true);
7674
$property->method('isPrivate')->willReturn(false);
7775
$property->method('isStatic')->willReturn(false);
78-
$property->method('getValue')->willReturn(123);
7976

8077
/**
8178
* @var \ReflectionProperty $property
8279
*/
8380
$expectedOutput = $t . $t . 'protected $test = 123;' . $n . $n;
84-
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property))->format());
81+
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property, 123))->format());
8582
}
8683

8784
public function testPrivateProperty()
@@ -98,13 +95,12 @@ public function testPrivateProperty()
9895
$property->method('isProtected')->willReturn(false);
9996
$property->method('isPrivate')->willReturn(true);
10097
$property->method('isStatic')->willReturn(false);
101-
$property->method('getValue')->willReturn(123);
10298

10399
/**
104100
* @var \ReflectionProperty $property
105101
*/
106102
$expectedOutput = $t . $t . 'private $test = 123;' . $n . $n;
107-
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property))->format());
103+
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property, 123))->format());
108104
}
109105

110106
public function testStaticProperty()
@@ -121,13 +117,12 @@ public function testStaticProperty()
121117
$property->method('isProtected')->willReturn(false);
122118
$property->method('isPrivate')->willReturn(false);
123119
$property->method('isStatic')->willReturn(true);
124-
$property->method('getValue')->willReturn(123);
125120

126121
/**
127122
* @var \ReflectionProperty $property
128123
*/
129124
$expectedOutput = $t . $t . 'public static $test = 123;' . $n . $n;
130-
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property))->format());
125+
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property, 123))->format());
131126
}
132127

133128
public function testIsNotDefaultProperty()
@@ -141,13 +136,12 @@ public function testIsNotDefaultProperty()
141136
$property->method('isProtected')->willReturn(false);
142137
$property->method('isPrivate')->willReturn(false);
143138
$property->method('isStatic')->willReturn(false);
144-
$property->method('getValue')->willReturn(123);
145139

146140
/**
147141
* @var \ReflectionProperty $property
148142
*/
149143
$expectedOutput = '';
150-
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property))->format());
144+
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property, 123))->format());
151145
}
152146

153147
public function testPropertyWithoutValue()
@@ -164,13 +158,12 @@ public function testPropertyWithoutValue()
164158
$property->method('isProtected')->willReturn(false);
165159
$property->method('isPrivate')->willReturn(false);
166160
$property->method('isStatic')->willReturn(false);
167-
$property->method('getValue')->willReturn(null);
168161

169162
/**
170163
* @var \ReflectionProperty $property
171164
*/
172165
$expectedOutput = $t . $t . 'public $test;' . $n . $n;
173-
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property))->format());
166+
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property, null))->format());
174167
}
175168

176169
public function testPropertyFromParent()
@@ -184,13 +177,12 @@ public function testPropertyFromParent()
184177
$property->method('isProtected')->willReturn(false);
185178
$property->method('isPrivate')->willReturn(false);
186179
$property->method('isStatic')->willReturn(false);
187-
$property->method('getValue')->willReturn(123);
188180

189181
/**
190182
* @var \ReflectionProperty $property
191183
*/
192184
$expectedOutput = '';
193-
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property))->format());
185+
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property, 123))->format());
194186
}
195187

196188
public function testPropertyWithDoc()
@@ -216,7 +208,6 @@ public function testPropertyWithDoc()
216208
$property->method('isProtected')->willReturn(false);
217209
$property->method('isPrivate')->willReturn(false);
218210
$property->method('isStatic')->willReturn(false);
219-
$property->method('getValue')->willReturn(123);
220211

221212
/**
222213
* @var \ReflectionProperty $property
@@ -230,6 +221,6 @@ public function testPropertyWithDoc()
230221
. $t . $t . ' * @var int' . $n
231222
. $t . $t . ' */' . $n
232223
. $t . $t . 'public $test = 123;' . $n . $n;
233-
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property))->format());
224+
$this->assertSame($expectedOutput, (new PropertyFormatter('TestClass', $property, 123))->format());
234225
}
235226
}

0 commit comments

Comments
 (0)