Avoid passing `null` to non-nullable arguments

This commit is contained in:
Johannes Meyer 2022-01-19 10:56:19 +01:00
parent dd3ceaa637
commit 3dc384fb58
27 changed files with 72 additions and 63 deletions

View File

@ -41,7 +41,7 @@ class ApplicationStateController extends Controller
setcookie(
'icingaweb2-session',
$now,
null,
0,
$params['path'],
$params['domain'],
$params['secure'],

View File

@ -448,7 +448,7 @@ class RoleForm extends RepositoryForm
$b = array_shift($bParts);
} while ($a === $b);
return strnatcmp($a, $b);
return strnatcmp($a ?? '', $b ?? '');
});
}

View File

@ -232,7 +232,7 @@ class Benchmark
round(($m->timestamp - floor($m->timestamp)) * 1000)
);
$vals = array(
date('H:i:s', $m->timestamp) . '.' . $micro,
date('H:i:s', (int) $m->timestamp) . '.' . $micro,
$m->message
);

View File

@ -119,11 +119,11 @@ class Cli extends ApplicationBootstrap
if ($this->params->shift('autocomplete')) {
$this->params->unshift('autocomplete');
}
$watch = $this->params->shift('watch');
if ($watch === true) {
$watch = 5;
}
if (preg_match('~^\d+$~', $watch)) {
$this->watchTimeout = 5;
} elseif (is_numeric($watch)) {
$this->watchTimeout = (int) $watch;
}

View File

@ -331,7 +331,7 @@ class Auth
setcookie(
'icingaweb2-session',
time(),
null,
0,
$params['path'],
$params['domain'],
$params['secure'],

View File

@ -203,6 +203,10 @@ class DbUserBackend extends DbRepository implements UserBackendInterface, Inspec
$lob = stream_get_contents($lob);
}
if ($lob === null) {
return '';
}
return $this->ds->getDbType() === 'pgsql' ? pg_unescape_bytea($lob) : $lob;
}

View File

@ -88,7 +88,7 @@ class LdapUserBackend extends LdapRepository implements UserBackendInterface, Do
*/
public function setBaseDn($baseDn)
{
if (($baseDn = trim($baseDn))) {
if ($baseDn && ($baseDn = trim($baseDn))) {
$this->baseDn = $baseDn;
}
@ -160,7 +160,7 @@ class LdapUserBackend extends LdapRepository implements UserBackendInterface, Do
*/
public function setFilter($filter)
{
if (($filter = trim($filter))) {
if ($filter && ($filter = trim($filter))) {
if ($filter[0] === '(') {
$filter = substr($filter, 1, -1);
}
@ -195,9 +195,7 @@ class LdapUserBackend extends LdapRepository implements UserBackendInterface, Do
*/
public function setDomain($domain)
{
$domain = trim($domain);
if (strlen($domain)) {
if ($domain && ($domain = trim($domain))) {
$this->domain = $domain;
}

View File

@ -169,7 +169,7 @@ class LdapUserGroupBackend extends LdapRepository implements Inspectable, UserGr
*/
public function setUserBaseDn($baseDn)
{
if (($baseDn = trim($baseDn))) {
if ($baseDn && ($baseDn = trim($baseDn))) {
$this->userBaseDn = $baseDn;
}
@ -195,7 +195,7 @@ class LdapUserGroupBackend extends LdapRepository implements Inspectable, UserGr
*/
public function setGroupBaseDn($baseDn)
{
if (($baseDn = trim($baseDn))) {
if ($baseDn && ($baseDn = trim($baseDn))) {
$this->groupBaseDn = $baseDn;
}
@ -336,7 +336,7 @@ class LdapUserGroupBackend extends LdapRepository implements Inspectable, UserGr
*/
public function setUserFilter($filter)
{
if (($filter = trim($filter))) {
if ($filter && ($filter = trim($filter))) {
if ($filter[0] === '(') {
$filter = substr($filter, 1, -1);
}
@ -366,7 +366,7 @@ class LdapUserGroupBackend extends LdapRepository implements Inspectable, UserGr
*/
public function setGroupFilter($filter)
{
if (($filter = trim($filter))) {
if ($filter && ($filter = trim($filter))) {
$this->groupFilter = $filter;
}
@ -431,9 +431,7 @@ class LdapUserGroupBackend extends LdapRepository implements Inspectable, UserGr
*/
public function setDomain($domain)
{
$domain = trim($domain);
if (strlen($domain)) {
if ($domain && ($domain = trim($domain))) {
$this->domain = $domain;
}

View File

@ -131,7 +131,7 @@ class SVGRenderer
'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'
);
$this->document = $implementation->createDocument(null, null, $docType);
$this->document = $implementation->createDocument(null, '', $docType);
$this->svg = $this->createOuterBox();
$this->document->appendChild($this->svg);
}

View File

@ -358,7 +358,7 @@ class Loader
} elseif (! empty($step['object'])) {
$object = (string) $step['object'] . $step['type'];
}
if (is_array($step['args'])) {
if (isset($step['args']) && is_array($step['args'])) {
foreach ($step['args'] as & $arg) {
if (is_object($arg)) {
$arg = sprintf('[%s]', get_class($arg));

View File

@ -263,7 +263,7 @@ class FilterQueryString
$this->string = $string;
$this->length = strlen($string);
$this->length = $string ? strlen($string) : 0;
if ($this->length === 0) {
return Filter::matchAll();

View File

@ -106,7 +106,7 @@ class DateFormatter
*/
public static function formatDateTime($dateTime)
{
return date('Y-m-d H:i:s', (float) $dateTime);
return date('Y-m-d H:i:s', (int) $dateTime);
}
/**

View File

@ -61,31 +61,11 @@ abstract class LdapRepository extends Repository
*/
protected function getNormedAttribute($name)
{
$loweredName = strtolower($name);
$loweredName = strtolower($name ?? '');
if (array_key_exists($loweredName, $this->normedAttributes)) {
return $this->normedAttributes[$loweredName];
}
return $name;
}
/**
* Return whether the given object DN is related to the given base DN
*
* Will use the current connection's root DN if $baseDn is not given.
*
* @deprecated This was only used by LdapUserGroupBackend::isMemberAttributeAmbiguous
* It will be removed with 2.6.0!
*
* @param string $dn The object DN to check
* @param string $baseDn The base DN to compare the object DN with
*
* @return bool
*/
protected function isRelatedDn($dn, $baseDn = null)
{
$normalizedDn = strtolower(join(',', array_map('trim', explode(',', $dn))));
$normalizedBaseDn = strtolower(join(',', array_map('trim', explode(',', $baseDn ?: $this->ds->getDn()))));
return strpos($normalizedDn, $normalizedBaseDn) !== false;
}
}

View File

@ -344,7 +344,9 @@ class RepositoryQuery implements QueryInterface, SortRules, FilterColumns, Itera
}
}
$baseDirection = strtoupper($sortColumns['order']) === static::SORT_DESC ? static::SORT_DESC : static::SORT_ASC;
$baseDirection = isset($sortColumns['order']) && strtoupper($sortColumns['order']) === static::SORT_DESC
? static::SORT_DESC
: static::SORT_ASC;
foreach ($sortColumns['columns'] as $column) {
list($column, $specificDirection) = $this->splitOrder($column);

View File

@ -432,9 +432,7 @@ class User
*/
public function setDomain($domain)
{
$domain = trim($domain);
if (strlen($domain)) {
if ($domain && ($domain = trim($domain))) {
$this->domain = $domain;
}

View File

@ -19,6 +19,10 @@ class StringHelper
*/
public static function trimSplit($value, $delimiter = ',', $limit = null)
{
if ($value === null) {
return [];
}
if ($limit !== null) {
$exploded = explode($delimiter, $value, $limit);
} else {
@ -40,6 +44,10 @@ class StringHelper
*/
public static function cname($name, $separator = '_')
{
if ($name === null) {
return '';
}
return str_replace(' ', '', ucwords(str_replace($separator, ' ', strtolower($name))));
}
@ -54,6 +62,10 @@ class StringHelper
*/
public static function ellipsis($string, $maxLength, $ellipsis = '...')
{
if ($string === null) {
return '';
}
if (strlen($string) > $maxLength) {
return substr($string, 0, $maxLength - strlen($ellipsis)) . $ellipsis;
}
@ -72,6 +84,10 @@ class StringHelper
*/
public static function ellipsisCenter($string, $maxLength, $ellipsis = '...')
{
if ($string === null) {
return '';
}
$start = ceil($maxLength / 2.0);
$end = floor($maxLength / 2.0);
if (strlen($string) > $maxLength) {
@ -117,6 +133,10 @@ class StringHelper
*/
public static function endsWith($string, $suffix)
{
if ($string === null) {
return false;
}
$stringSuffix = substr($string, -strlen($suffix));
return $stringSuffix !== false ? $stringSuffix === $suffix : false;
}

View File

@ -389,12 +389,12 @@ class Response extends Zend_Controller_Response_Http
/** @var Cookie $cookie */
setcookie(
$cookie->getName(),
$cookie->getValue(),
$cookie->getExpire(),
$cookie->getValue() ?? '',
$cookie->getExpire() ?? 0,
$cookie->getPath(),
$cookie->getDomain(),
$cookie->getDomain() ?? '',
$cookie->isSecure(),
$cookie->isHttpOnly()
$cookie->isHttpOnly() ?? true
);
}
}

View File

@ -104,7 +104,7 @@ class View extends Zend_View_Abstract
*/
public function escape($value)
{
return htmlspecialchars($value, ENT_COMPAT | ENT_SUBSTITUTE | ENT_HTML5, self::CHARSET, true);
return htmlspecialchars($value ?: '', ENT_COMPAT | ENT_SUBSTITUTE | ENT_HTML5, self::CHARSET, true);
}
/**

View File

@ -225,7 +225,7 @@ class SortBox extends AbstractWidget
// TODO(el): ToggleButton :)
$toggle = array('asc' => 'sort-name-down', 'desc' => 'sort-name-up');
unset($toggle[strtolower($direction) ?: 'asc']);
unset($toggle[isset($direction) ? strtolower($direction) : 'asc']);
$newDirection = key($toggle);
$icon = current($toggle);

View File

@ -271,11 +271,12 @@ class Zend_Controller_Router_Route extends Zend_Controller_Router_Route_Abstract
// Translate value if required
$part = $this->_parts[$pos];
if ($this->_isTranslated
&& $part !== null
&& (substr($part, 0, 1) === '@' && substr($part, 1, 1) !== '@'
&& $name === null)
|| $name !== null && in_array($name, $this->_translatable)
) {
if (substr($part, 0, 1) === '@') {
if ($part && substr($part, 0, 1) === '@') {
$part = substr($part, 1);
}
@ -284,7 +285,7 @@ class Zend_Controller_Router_Route extends Zend_Controller_Router_Route_Abstract
}
}
if (substr($part, 0, 2) === '@@') {
if ($part && substr($part, 0, 2) === '@@') {
$part = substr($part, 1);
}

View File

@ -286,7 +286,9 @@ abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
*/
protected function _quote($value)
{
if (is_int($value) || is_float($value)) {
if ($value === null) {
$value = '';
} elseif (is_int($value) || is_float($value)) {
return $value;
}
$this->_connect();

View File

@ -240,6 +240,12 @@ class Zend_Db_Statement_Pdo extends Zend_Db_Statement implements IteratorAggrega
if ($style === null) {
$style = $this->_fetchMode;
}
if ($cursor === null) {
$cursor = PDO::FETCH_ORI_NEXT;
}
if ($offset === null) {
$offset = 0;
}
try {
return $this->_stmt->fetch($style, $cursor, $offset);
} catch (PDOException $e) {

View File

@ -158,7 +158,7 @@ class Zend_Form_Decorator_Description extends Zend_Form_Decorator_Abstract
}
$description = $element->getDescription();
$description = trim($description);
$description = trim($description ?: '');
if (!empty($description) && (null !== ($translator = $element->getTranslator()))) {
$description = $translator->translate($description);

View File

@ -91,7 +91,7 @@ class Zend_Form_Decorator_HtmlTag extends Zend_Form_Decorator_Abstract
$val = implode(' ', $val);
}
}
$val = htmlspecialchars($val, ENT_COMPAT, $enc);
$val = htmlspecialchars($val ?? '', ENT_COMPAT, $enc);
$xhtml .= " $key=\"$val\"";
}
return $xhtml;

View File

@ -298,7 +298,7 @@ class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
}
$label = $element->getLabel();
$label = trim($label);
$label = trim($label ?? '');
if (empty($label)) {
return '';

View File

@ -275,7 +275,7 @@ class TimelineController extends Controller
{
$startTime = new DateTime();
$startParam = $this->_request->getParam('start');
$startTimestamp = is_numeric($startParam) ? intval($startParam) : strtotime($startParam);
$startTimestamp = is_numeric($startParam) ? intval($startParam) : strtotime($startParam ?? '');
if ($startTimestamp !== false) {
$startTime->setTimestamp($startTimestamp);
} else {
@ -284,7 +284,7 @@ class TimelineController extends Controller
$endTime = clone $startTime;
$endParam = $this->_request->getParam('end');
$endTimestamp = is_numeric($endParam) ? intval($endParam) : strtotime($endParam);
$endTimestamp = is_numeric($endParam) ? intval($endParam) : strtotime($endParam ?? '');
if ($endTimestamp !== false) {
$endTime->setTimestamp($endTimestamp);
} else {

View File

@ -37,7 +37,7 @@ class PerfdataSet implements IteratorAggregate
*/
protected function __construct($perfdataStr)
{
if (($perfdataStr = trim($perfdataStr)) !== '') {
if ($perfdataStr && ($perfdataStr = trim($perfdataStr))) {
$this->perfdataStr = $perfdataStr;
$this->parse();
}