Skip to content

Commit 601cf86

Browse files
committed
Throw an exception for invalid "resultForIdenticals"
Signed-off-by: Jack Cherng <[email protected]>
1 parent 2fa5c42 commit 601cf86

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ $rendererOptions = [
9292
// internally, ops (tags) are all int type but this is not good for human reading.
9393
// set this to "true" to convert them into string form before outputting.
9494
'outputTagAsString' => true,
95-
// change this value to a non-null one as the the returned diff
95+
// change this value to a string as the the returned diff
9696
// if the two input strings are identical
9797
'resultForIdenticals' => null,
9898
// extra HTML classes added to the DOM of the diff container

example/demo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
// internally, ops (tags) are all int type but this is not good for human reading.
4949
// set this to "true" to convert them into string form before outputting.
5050
'outputTagAsString' => false,
51-
// change this value to a non-null one as the the returned diff
51+
// change this value to a string as the the returned diff
5252
// if the two input strings are identical
5353
'resultForIdenticals' => null,
5454
// extra HTML classes added to the DOM of the diff container

src/Renderer/AbstractRenderer.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ abstract class AbstractRenderer implements RendererInterface
4949
// internally, ops (tags) are all int type but this is not good for human reading.
5050
// set this to "true" to convert them into string form before outputting.
5151
'outputTagAsString' => false,
52-
// change this value to a non-null one as the the returned diff
52+
// change this value to a string as the the returned diff
5353
// if the two input strings are identical
5454
'resultForIdenticals' => null,
5555
// extra HTML classes added to the DOM of the diff container
@@ -111,10 +111,20 @@ public function getOptions(): array
111111
* @final
112112
*
113113
* @todo mark this method with "final" in the next major release
114+
*
115+
* @throws \InvalidArgumentException
114116
*/
115117
public function getResultForIdenticals(): string
116118
{
117-
return $this->options['resultForIdenticals'] ?? $this->getResultForIdenticalsDefault();
119+
$custom = $this->options['resultForIdenticals'];
120+
121+
if (isset($custom) && !\is_string($custom)) {
122+
throw new \InvalidArgumentException(
123+
'renderer option `resultForIdenticals` must be null or string.'
124+
);
125+
}
126+
127+
return $custom ?? $this->getResultForIdenticalsDefault();
118128
}
119129

120130
/**

tests/Renderer/RendererTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,22 @@ public function testSetOptionsWithResultForIdenticals(): void
6868
'Rederer options: result for identicals should work.'
6969
);
7070
}
71+
72+
/**
73+
* Test the AbstractRenderer::setOptions with an invalid result for identicals.
74+
*
75+
* @covers \Jfcherng\Diff\Renderer\AbstractRenderer::setOptions
76+
*/
77+
public function testSetOptionsWithInvalidResultForIdenticals(): void
78+
{
79+
static::expectException(\InvalidArgumentException::class);
80+
81+
$diffResult = DiffHelper::calculate(
82+
'we are the same',
83+
'we are the same',
84+
'Inline',
85+
[],
86+
['resultForIdenticals' => 50]
87+
);
88+
}
7189
}

0 commit comments

Comments
 (0)