IcingaObject: readability, style

This commit is contained in:
Thomas Gelf 2021-05-20 15:11:12 +02:00
parent 80965053cf
commit 7de5b03877

View File

@ -161,9 +161,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($key = $this->stripIdSuffix($property)) {
return $this->hasRelation($key);
} else {
return false;
}
return false;
}
protected function stripIdSuffix($key)
@ -263,7 +263,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
protected function relatedSets()
{
$sets = array();
$sets = [];
foreach ($this->relatedSets as $key => $class) {
$sets[$key] = $this->getRelatedSet($key);
}
@ -663,9 +663,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$var = $this->vars()->get(substr($key, 5));
if ($var === null) {
return $var;
} else {
return $var->getValue();
}
return $var->getValue();
}
// e.g. zone_id
@ -695,7 +695,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
if (array_key_exists('object_type', $props) && key($props) !== 'object_type') {
$type = $props['object_type'];
unset($props['object_type']);
$props = array('object_type' => $type) + $props;
$props = ['object_type' => $type] + $props;
}
}
return parent::setProperties($props);
@ -705,7 +705,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($key === 'vars') {
$value = (array) $value;
$unset = array();
$unset = [];
foreach ($this->vars() as $k => $f) {
if (! array_key_exists($k, $value)) {
$unset[] = $k;
@ -718,13 +718,16 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$this->vars()->set($k, $v);
}
return $this;
} elseif (substr($key, 0, 5) === 'vars.') {
}
if (substr($key, 0, 5) === 'vars.') {
//TODO: allow for deep keys
$this->vars()->set(substr($key, 5), $value);
return $this;
} elseif ($this instanceof ObjectWithArguments
&& substr($key, 0, 10) === 'arguments.'
) {
}
if ($this instanceof ObjectWithArguments
&& substr($key, 0, 10) === 'arguments.') {
$this->arguments()->set(substr($key, 10), $value);
return $this;
}
@ -794,16 +797,18 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($value === 'y' || $value === '1' || $value === true || $value === 1) {
return 'y';
} elseif ($value === 'n' || $value === '0' || $value === false || $value === 0) {
return 'n';
} elseif ($value === '' || $value === null) {
return null;
} else {
throw new InvalidArgumentException(sprintf(
'Got invalid boolean: %s',
var_export($value, 1)
));
}
if ($value === 'n' || $value === '0' || $value === false || $value === 0) {
return 'n';
}
if ($value === '' || $value === null) {
return null;
}
throw new InvalidArgumentException(sprintf(
'Got invalid boolean: %s',
var_export($value, 1)
));
}
protected function setDisabled($disabled)
@ -946,7 +951,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
public function setImports($imports)
{
if (! is_array($imports) && $imports !== null) {
$imports = array($imports);
$imports = [$imports];
}
try {
@ -1020,12 +1025,12 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
if (property_exists($vars, $varname)) {
return $vars->$varname;
} else {
return null;
}
return null;
}
public function getResolvedVar($varname)
public function getResolvedVar($varName)
{
try {
$vars = $this->getResolvedVars();
@ -1033,14 +1038,14 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return null;
}
if (property_exists($vars, $varname)) {
return $vars->$varname;
} else {
return null;
if (property_exists($vars, $varName)) {
return $vars->$varName;
}
return null;
}
public function getOriginForVar($varname)
public function getOriginForVar($varName)
{
try {
$origins = $this->getOriginsVars();
@ -1048,11 +1053,11 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
return null;
}
if (property_exists($origins, $varname)) {
return $origins->$varname;
} else {
return null;
if (property_exists($origins, $varName)) {
return $origins->$varName;
}
return null;
}
public function getResolvedProperties()
@ -1209,7 +1214,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
public function invalidateResolveCache()
{
$this->resolveCache = array();
$this->resolveCache = [];
return $this;
}
@ -1220,8 +1225,8 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$type = $this->getShortTableName();
$query = $db->select()->from(
array('oi' => $table . '_inheritance'),
array('cnt' => 'COUNT(*)')
['oi' => $table . '_inheritance'],
['cnt' => 'COUNT(*)']
)->where('oi.parent_' . $type . '_id = ?', (int) $this->get('id'));
return $db->fetchOne($query);
@ -1276,10 +1281,10 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$this->triggerLoopDetection();
}
$vals = array();
$vals['_MERGED_'] = (object) array();
$vals['_INHERITED_'] = (object) array();
$vals['_ORIGINS_'] = (object) array();
$vals = [];
$vals['_MERGED_'] = (object) [];
$vals['_INHERITED_'] = (object) [];
$vals['_ORIGINS_'] = (object) [];
// $objects = $this->imports()->getObjects();
$objects = IcingaTemplateRepository::instanceByObject($this)
->getTemplatesIndexedByNameFor($this, true);
@ -1288,7 +1293,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$getInherited = 'getInherited' . $what;
$getOrigins = 'getOrigins' . $what;
$blacklist = array('id', 'object_type', 'object_name', 'disabled');
$blacklist = ['id', 'object_type', 'object_name', 'disabled'];
foreach ($objects as $name => $object) {
$origins = $object->$getOrigins();
@ -1774,10 +1779,10 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$zoneId,
$this->getConnection()
)->getObjectName();
} else {
// Config has a lookup cache, is faster:
return $config->getZoneName($zoneId);
}
// Config has a lookup cache, is faster:
return $config->getZoneName($zoneId);
}
public function getRenderingZone(IcingaConfig $config = null)
@ -1811,9 +1816,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($this->prefersGlobalZone()) {
return $this->connection->getDefaultGlobalZoneName();
} else {
return $this->connection->getMasterZoneName();
}
return $this->connection->getMasterZoneName();
}
protected function prefersGlobalZone()
@ -1843,9 +1848,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($this->supportsImports()) {
return $this->imports()->toLegacyConfigString();
} else {
return '';
}
return '';
}
protected function renderLegacyRelationProperty($propertyName, $id, $renderKey = null)
@ -1869,13 +1874,13 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if (is_array($value)) {
return c1::renderKeyValue('host_name', c1::renderArray($value));
} else {
return $this->renderLegacyRelationProperty(
'host',
$this->get('host_id'),
'host_name'
);
}
return $this->renderLegacyRelationProperty(
'host',
$this->get('host_id'),
'host_name'
);
}
/**
@ -1978,7 +1983,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$out = '';
$blacklist = array_merge(
$this->propertiesNotForRendering,
array() /* $this->prioritizedProperties */
[] /* $this->prioritizedProperties */
);
foreach ($this->properties as $key => $value) {
@ -2027,12 +2032,12 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
if ($this->propertyIsBoolean($key)) {
if ($value === $this->defaultProperties[$key]) {
return '';
} else {
return c::renderKeyValue(
$this->booleans[$key],
c::renderBoolean($value)
);
}
return c::renderKeyValue(
$this->booleans[$key],
c::renderBoolean($value)
);
}
if ($this->propertyIsInterval($key)) {
@ -2085,12 +2090,12 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
if ($this->propertyIsBoolean($key)) {
if ($value === $this->defaultProperties[$key]) {
return '';
} else {
return c1::renderKeyValue(
$this->booleans[$key],
c1::renderBoolean($value)
);
}
return c1::renderKeyValue(
$this->booleans[$key],
c1::renderBoolean($value)
);
}
if ($this->propertyIsInterval($key)) {
@ -2136,9 +2141,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($this->supportsCustomVars()) {
return $this->vars()->toConfigString($this->isApplyRule());
} else {
return '';
}
return '';
}
/**
@ -2148,9 +2153,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($this->supportsCustomVars()) {
return $this->vars()->toLegacyConfigString();
} else {
return '';
}
return '';
}
/**
@ -2160,9 +2165,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($this->supportsGroups()) {
return $this->groups()->toConfigString();
} else {
return '';
}
return '';
}
/**
@ -2171,14 +2176,14 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
protected function renderLegacyGroups()
{
if ($this->supportsGroups() && $this->hasBeenLoadedFromDb()) {
$applied = array();
$applied = [];
if ($this instanceof IcingaHost) {
$applied = $this->getAppliedGroups();
}
return $this->groups()->toLegacyConfigString($applied);
} else {
return '';
}
return '';
}
/**
@ -2214,9 +2219,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($this->supportsRanges()) {
return $this->ranges()->toConfigString();
} else {
return '';
}
return '';
}
/**
@ -2226,9 +2231,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($this->supportsRanges()) {
return $this->ranges()->toLegacyConfigString();
} else {
return '';
}
return '';
}
/**
@ -2272,15 +2277,15 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
protected function renderLegacyCheck_command($value)
{
// @codingStandardsIgnoreEnd
$args = array();
$args = [];
foreach ($this->vars() as $k => $v) {
if (substr($k, 0, 3) == 'ARG') {
if (substr($k, 0, 3) === 'ARG') {
$args[] = $v->getValue();
}
}
array_unshift($args, $value);
return c1::renderKeyValue('check_command', join('!', $args));
return c1::renderKeyValue('check_command', implode('!', $args));
}
/**
@ -2328,8 +2333,8 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
// force rendering of check_command when ARG1 is set
if ($this->supportsCustomVars() && array_key_exists('check_command_id', $this->defaultProperties)) {
if ($this->vars()->get('ARG1') !== null
&& $this->get('check_command') === null
if ($this->get('check_command') === null
&& $this->vars()->get('ARG1') !== null
) {
$command = $this->getResolvedRelated('check_command');
$str .= $this->renderLegacyCheck_command($command->getObjectName());
@ -2382,9 +2387,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($this->isTemplate()) {
return 'name';
} else {
return $this->getLegacyObjectType() . '_name';
}
return $this->getLegacyObjectType() . '_name';
}
/**
@ -2415,7 +2420,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
public function toLegacyConfigString()
{
$str = implode(array(
$str = implode([
$this->renderLegacyObjectHeader(),
$this->renderLegacyImports(),
$this->renderLegacyProperties(),
@ -2427,7 +2432,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$this->renderLegacyCustomExtensions(),
$this->renderLegacyCustomVars(),
$this->renderLegacySuffix()
));
]);
$str = $this->alignLegacyProperties($str);
@ -2436,14 +2441,14 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
"# --- This object has been disabled ---\n"
. preg_replace('~^~m', '# ', trim($str))
. "\n\n";
} else {
return $str;
}
return $str;
}
protected function alignLegacyProperties($configString)
{
$lines = preg_split('/\n/', $configString);
$lines = explode("\n", $configString);
$len = 24;
foreach ($lines as &$line) {
@ -2463,7 +2468,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
public function toConfigString()
{
$str = implode(array(
$str = implode([
$this->renderObjectHeader(),
$this->renderPrioritizedProperties(),
$this->renderImports(),
@ -2476,15 +2481,15 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$this->renderCustomExtensions(),
$this->renderCustomVars(),
$this->renderSuffix()
));
]);
if ($this->isDisabled()) {
return "/* --- This object has been disabled ---\n"
// Do not allow strings to break our comment
. str_replace('*/', "* /", $str) . "*/\n";
} else {
return $str;
}
return $str;
}
public function isGroup()
@ -2512,11 +2517,12 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($this->isTemplate()) {
return 'template';
} elseif ($this->isApplyRule()) {
return 'apply';
} else {
return 'object';
}
if ($this->isApplyRule()) {
return 'apply';
}
return 'object';
}
public function getObjectName()
@ -2524,13 +2530,13 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$property = static::getKeyColumnName();
if ($this->hasProperty($property)) {
return $this->get($property);
} else {
throw new LogicException(sprintf(
'Trying to access "%s" for an instance of "%s"',
$property,
get_class($this)
));
}
throw new LogicException(sprintf(
'Trying to access "%s" for an instance of "%s"',
$property,
get_class($this)
));
}
public static function classByType($type)
@ -2578,7 +2584,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
*
* @return IcingaObject
*/
public static function createByType($type, $properties = array(), Db $db = null)
public static function createByType($type, $properties = [], Db $db = null)
{
/** @var IcingaObject $class */
$class = self::classByType($type);
@ -2632,21 +2638,21 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
if (is_array($class::create()->getKeyName())) {
return $class::loadAll($db, $query);
} else {
if (PrefetchCache::shouldBeUsed()
&& $query === null
&& $keyColumn === static::getKeyColumnName()
) {
$result = array();
foreach ($class::prefetchAll($db) as $row) {
$result[$row->$keyColumn] = $row;
}
return $result;
} else {
return $class::loadAll($db, $query, $keyColumn);
}
}
if (PrefetchCache::shouldBeUsed()
&& $query === null
&& $keyColumn === static::getKeyColumnName()
) {
$result = [];
foreach ($class::prefetchAll($db) as $row) {
$result[$row->$keyColumn] = $row;
}
return $result;
}
return $class::loadAll($db, $query, $keyColumn);
}
/**
@ -2665,14 +2671,14 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
'There is no support for loading external objects of type "%s"',
$type
));
} else {
$query = $db->getDbAdapter()
->select()
->from($dummy->getTableName())
->where('object_type = ?', 'external_object');
return $class::loadAll($db, $query, 'object_name');
}
$query = $db->getDbAdapter()
->select()
->from($dummy->getTableName())
->where('object_type = ?', 'external_object');
return $class::loadAll($db, $query, 'object_name');
}
public static function fromJson($json, Db $connection = null)
@ -2790,7 +2796,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$resolveIds = true,
$keepId = false
) {
$props = array();
$props = [];
if ($resolved) {
$p = $this->getInheritedProperties();
@ -2892,7 +2898,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
if ($skipDefaults) {
foreach (array('imports', 'ranges', 'arguments') as $key) {
foreach (['imports', 'ranges', 'arguments'] as $key) {
if (empty($props[$key])) {
unset($props[$key]);
}
@ -2955,9 +2961,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if ($this->gotImports()) {
return $this->imports()->listImportNames();
} else {
return $this->templateTree()->listParentNamesFor($this);
}
return $this->templateTree()->listParentNamesFor($this);
}
public function listFlatResolvedImportNames()
@ -2989,14 +2995,14 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
{
if (array_key_exists($key, $this->defaultProperties)) {
return $value !== $this->defaultProperties[$key];
} else {
return $value !== null;
}
return $value !== null;
}
protected function mapHostsToZones($names)
{
$map = array();
$map = [];
foreach ($names as $hostname) {
/** @var IcingaHost $host */
@ -3004,7 +3010,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
$zone = $host->getRenderingZone();
if (! array_key_exists($zone, $map)) {
$map[$zone] = array();
$map[$zone] = [];
}
$map[$zone][] = $hostname;
@ -3017,12 +3023,12 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
public function getUrlParams()
{
$params = array();
$params = [];
if ($this->isApplyRule() && ! $this instanceof IcingaScheduledDowntime) {
$params['id'] = $this->get('id');
} else {
$params = array('name' => $this->getObjectName());
$params = ['name' => $this->getObjectName()];
if ($this->hasProperty('host_id') && $this->get('host_id')) {
$params['host'] = $this->get('host');
@ -3064,7 +3070,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
public function getPlainUnmodifiedObject()
{
$props = array();
$props = [];
foreach ($this->getOriginalProperties() as $k => $v) {
// Do not ship ids for IcingaObjects:
@ -3090,7 +3096,7 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
}
if ($this->supportsCustomVars()) {
$props['vars'] = (object) array();
$props['vars'] = (object) [];
foreach ($this->vars()->getOriginalVars() as $name => $var) {
$props['vars']->$name = $var->getValue();
}
@ -3154,9 +3160,9 @@ abstract class IcingaObject extends DbObject implements IcingaConfigRenderer
if ($previousHandler !== null) {
call_user_func($previousHandler, $e);
die();
} else {
die($e->getMessage());
}
die($e->getMessage());
}
}