Skip to content

Commit bf95ffc

Browse files
authored
php8.3: fix deprecated passing null to str_replace() (#4525)
* php8.3: fix passing null to `str_replace()` * fix
1 parent 7c8a3e5 commit bf95ffc

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

lib/Varien/Object.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,8 @@ public function toString($format = '')
589589
} else {
590590
preg_match_all('/\{\{([a-z0-9_]+)\}\}/is', $format, $matches);
591591
foreach ($matches[1] as $var) {
592-
$format = str_replace('{{' . $var . '}}', $this->getData($var), $format);
592+
$replace = is_null($this->getData($var)) ? '' : $this->getData($var);
593+
$format = str_replace('{{' . $var . '}}', $replace, $format);
593594
}
594595
$str = $format;
595596
}

tests/unit/Varien/ObjectTest.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,33 @@ public function provideGetDataData(): Generator
166166
}
167167

168168
/**
169+
* @dataProvider provideToString
169170
* @group Varien_Object
170171
*/
171-
public function testToString(): void
172+
public function testToString(string $expectedResult, string $format): void
172173
{
173-
$this->subject->setString1('open');
174-
$this->subject->setString2('mage');
175-
$this->assertSame('open, mage', $this->subject->toString());
176-
$this->assertSame('openmage', $this->subject->toString('{{string1}}{{string2}}'));
177-
$this->assertSame('open', $this->subject->toString('{{string1}}{{string_not_exists}}'));
174+
$this->subject->setString0('0');
175+
$this->subject->setString1('one');
176+
$this->subject->setString2('two');
177+
$this->subject->setString3('three');
178+
179+
$this->assertSame($expectedResult, $this->subject->toString($format));
180+
}
181+
182+
public function provideToString(): Generator
183+
{
184+
yield 'no format' => [
185+
'0, one, two, three',
186+
'',
187+
];
188+
yield 'valid' => [
189+
'0 one two',
190+
'{{string0}} {{string1}} {{string2}}',
191+
];
192+
yield 'invalid' => [
193+
'three 0',
194+
'{{string3}} {{string_not_exists}} {{string0}}',
195+
];
178196
}
179197

180198
/**

0 commit comments

Comments
 (0)