1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-03 12:12:43 +01:00
phorge-phorge/src/applications/maniphest/xaction/ManiphestTaskPointsTransaction.php
Andre Klapper 538cccc63e Fix PHP 8.1 "strlen(null)" exception setting Story Points on a Task
Summary:
`strlen()` was used in Phabricator to check if a generic value is a non-empty string.
This behavior is deprecated since PHP 8.1. Here we adopt `phutil_string_cast()` to
reply to the question "is this an empty string?".

Note: this may highlight other absurd input values that might be worth correcting
instead of just ignoring. If phutil_string_cast() throws an exception in your
instance, report it to Phorge to evaluate and fix that specific corner case.

Closes T15390

Test Plan:
Enable `maniphest.points` in settings, for example with:

```
./bin/config set maniphest.points --stdin <<< '{"enabled":true}'
```

Then try to create a Task: it does not explode anymore in PHP 8.1+.

Also try to set various possible values from the Conduit API method "maniphest.edit".

Reviewers: O1 Blessed Committers, valerio.bozzolan

Reviewed By: O1 Blessed Committers, valerio.bozzolan

Subscribers: speck, tobiaswiese, valerio.bozzolan, Matthew, Cigaryno

Maniphest Tasks: T15390

Differential Revision: https://we.phorge.it/D25222
2023-05-23 11:57:45 +02:00

131 lines
3.2 KiB
PHP

<?php
final class ManiphestTaskPointsTransaction
extends ManiphestTaskTransactionType {
const TRANSACTIONTYPE = 'points';
public function generateOldValue($object) {
return $this->getValueForPoints($object->getPoints());
}
public function generateNewValue($object, $value) {
return $this->getValueForPoints($value);
}
public function applyInternalEffects($object, $value) {
$object->setPoints($value);
}
public function shouldHide() {
if (!ManiphestTaskPoints::getIsEnabled()) {
return true;
}
return false;
}
public function getTitle() {
$old = $this->getOldValue();
$new = $this->getNewValue();
if ($old === null) {
return pht(
'%s set the point value for this task to %s.',
$this->renderAuthor(),
$this->renderNewValue());
} else if ($new === null) {
return pht(
'%s removed the point value for this task.',
$this->renderAuthor());
} else {
return pht(
'%s changed the point value for this task from %s to %s.',
$this->renderAuthor(),
$this->renderOldValue(),
$this->renderNewValue());
}
}
public function getTitleForFeed() {
$old = $this->getOldValue();
$new = $this->getNewValue();
if ($old === null) {
return pht(
'%s set the point value for %s to %s.',
$this->renderAuthor(),
$this->renderObject(),
$this->renderNewValue());
} else if ($new === null) {
return pht(
'%s removed the point value for %s.',
$this->renderAuthor(),
$this->renderObject());
} else {
return pht(
'%s changed the point value for %s from %s to %s.',
$this->renderAuthor(),
$this->renderObject(),
$this->renderOldValue(),
$this->renderNewValue());
}
}
public function validateTransactions($object, array $xactions) {
$errors = array();
foreach ($xactions as $xaction) {
$new = $xaction->getNewValue();
if (strlen($new) && !is_numeric($new)) {
$errors[] = $this->newInvalidError(
pht('Points value must be numeric or empty.'));
continue;
}
if ((double)$new < 0) {
$errors[] = $this->newInvalidError(
pht('Points value must be nonnegative.'));
continue;
}
}
return $errors;
}
public function getIcon() {
return 'fa-calculator';
}
/**
* Normalize your Story Points from generic stuff to double or null.
* @param mixed $value Your raw Story Points
* @return double|null
*/
private function getValueForPoints($value) {
// The Point can be various types also thanks to Conduit API
// like integers, floats, null, and strings of course.
// Everything meaningful must be printable as a string.
$is_empty = phutil_string_cast($value) === '';
if ($is_empty) {
$value = null;
}
if ($value !== null) {
$value = (double)$value;
}
return $value;
}
public function getTransactionTypeForConduit($xaction) {
return 'points';
}
public function getFieldValuesForConduit($xaction, $data) {
return array(
'old' => $xaction->getOldValue(),
'new' => $xaction->getNewValue(),
);
}
}