Skip to content

Commit ed0c9a0

Browse files
authored
Merge pull request #3124 from kwizer15/fix/cmd-format-value
Fix PHP 8+ TypeError in cmd::formatValue() when round() receives non-…
2 parents 2906e09 + 60778c4 commit ed0c9a0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

core/class/cmd.class.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,10 @@ public function formatValue($_value, $_quote = false) {
10421042
return intval($binary xor boolval($this->getConfiguration('invertBinary', false)));
10431043
case 'numeric':
10441044
if ($this->getConfiguration('historizeRound') !== '' && is_numeric($this->getConfiguration('historizeRound')) && $this->getConfiguration('historizeRound') >= 0) {
1045+
if (!is_numeric($_value)) {
1046+
log::add('cmd', 'error', __('La formule de calcul doit retourner une valeur numérique uniquement : ', __FILE__) . $this->getHumanName() . ' => ' . $_value);
1047+
$_value = (float) (str_replace(',', '.', $_value));
1048+
}
10451049
$_value = round($_value, $this->getConfiguration('historizeRound'));
10461050
}
10471051
if ($_value > $this->getConfiguration('maxValue', $_value)) {

tests/class/cmdTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/* This file is part of Jeedom.
4+
*
5+
* Jeedom is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* Jeedom is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with Jeedom. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
use PHPUnit\Framework\TestCase;
19+
20+
class cmdTest extends TestCase
21+
{
22+
public function testFormatValueNumericRoundWithNonNumericCalculResult()
23+
{
24+
$cmd = new cmd();
25+
$cmd->setType('info');
26+
$cmd->setSubType('numeric');
27+
$cmd->setConfiguration('historizeRound', 1);
28+
$cmd->setConfiguration('calculValueOffset', '#value# * 2 ~ \'mm\'');
29+
30+
$actualResult = $cmd->formatValue('1,2');
31+
32+
self::assertSame(2.4, $actualResult);
33+
}
34+
}

0 commit comments

Comments
 (0)