ExtensibleSet: short array syntax, readability

This commit is contained in:
Thomas Gelf 2022-09-20 20:28:05 +02:00
parent b6bfe913f8
commit c91ff3303c

View File

@ -11,15 +11,15 @@ class ExtensibleSet
{ {
protected $ownValues; protected $ownValues;
protected $plusValues = array(); protected $plusValues = [];
protected $minusValues = array(); protected $minusValues = [];
protected $resolvedValues; protected $resolvedValues;
protected $allowedValues; protected $allowedValues;
protected $inheritedValues = array(); protected $inheritedValues = [];
protected $fromDb; protected $fromDb;
@ -69,7 +69,7 @@ class ExtensibleSet
} elseif (is_object($set)) { } elseif (is_object($set)) {
$this->reset(); $this->reset();
foreach (array('override', 'extend', 'blacklist') as $method) { foreach (['override', 'extend', 'blacklist'] as $method) {
if (property_exists($set, $method)) { if (property_exists($set, $method)) {
$this->$method($set->$method); $this->$method($set->$method);
} }
@ -98,7 +98,7 @@ class ExtensibleSet
} }
} }
$plain = (object) array(); $plain = (object) [];
if ($this->ownValues !== null) { if ($this->ownValues !== null) {
$plain->override = $this->ownValues; $plain->override = $this->ownValues;
@ -127,7 +127,7 @@ class ExtensibleSet
} }
} }
$plain = (object) array(); $plain = (object) [];
if ($old['override'] !== null) { if ($old['override'] !== null) {
$plain->override = $old['override']; $plain->override = $old['override'];
@ -179,19 +179,16 @@ class ExtensibleSet
{ {
$db = $this->object->getDb(); $db = $this->object->getDb();
$query = $db->select()->from( $query = $db->select()->from($this->tableName(), [
$this->tableName(), 'property',
array( 'merge_behaviour'
'property', ])->where($this->foreignKey() . ' = ?', $this->object->get('id'));
'merge_behaviour'
)
)->where($this->foreignKey() . ' = ?', $this->object->get('id'));
$byBehaviour = array( $byBehaviour = [
'override' => array(), 'override' => [],
'extend' => array(), 'extend' => [],
'blacklist' => array(), 'blacklist' => [],
); ];
foreach ($db->fetchAll($query) as $row) { foreach ($db->fetchAll($query) as $row) {
if (! array_key_exists($row->merge_behaviour, $byBehaviour)) { if (! array_key_exists($row->merge_behaviour, $byBehaviour)) {
@ -229,11 +226,11 @@ class ExtensibleSet
protected function tableName() protected function tableName()
{ {
return implode('_', array( return implode('_', [
$this->object->getTableName(), $this->object->getTableName(),
$this->propertyName, $this->propertyName,
'set' 'set'
)); ]);
} }
public function getObject() public function getObject()
@ -268,9 +265,9 @@ class ExtensibleSet
} }
$table = $this->tableName(); $table = $this->tableName();
$props = array( $props = [
$this->foreignKey() => $this->object->get('id') $this->foreignKey() => $this->object->get('id')
); ];
$db->delete( $db->delete(
$this->tableName(), $this->tableName(),
@ -285,7 +282,7 @@ class ExtensibleSet
foreach ($this->ownValues as $value) { foreach ($this->ownValues as $value) {
$db->insert( $db->insert(
$table, $table,
array_merge($props, array('property' => $value)) array_merge($props, ['property' => $value])
); );
} }
} }
@ -295,7 +292,7 @@ class ExtensibleSet
foreach ($this->plusValues as $value) { foreach ($this->plusValues as $value) {
$db->insert( $db->insert(
$table, $table,
array_merge($props, array('property' => $value)) array_merge($props, ['property' => $value])
); );
} }
} }
@ -305,7 +302,7 @@ class ExtensibleSet
foreach ($this->minusValues as $value) { foreach ($this->minusValues as $value) {
$db->insert( $db->insert(
$table, $table,
array_merge($props, array('property' => $value)) array_merge($props, ['property' => $value])
); );
} }
} }
@ -324,8 +321,8 @@ class ExtensibleSet
public function override($values) public function override($values)
{ {
$this->ownValues = array(); $this->ownValues = [];
$this->inheritedValues = array(); $this->inheritedValues = [];
$this->addValuesTo($this->ownValues, $values); $this->addValuesTo($this->ownValues, $values);
@ -370,7 +367,7 @@ class ExtensibleSet
$this->resolvedValues = null; $this->resolvedValues = null;
} }
$this->inheritedValues = array(); $this->inheritedValues = [];
$this->addValuesTo( $this->addValuesTo(
$this->inheritedValues, $this->inheritedValues,
@ -382,13 +379,13 @@ class ExtensibleSet
public function forgetInheritedValues() public function forgetInheritedValues()
{ {
$this->inheritedValues = array(); $this->inheritedValues = [];
return $this; return $this;
} }
protected function renderArray($array) protected function renderArray($array)
{ {
$safe = array(); $safe = [];
foreach ($array as $value) { foreach ($array as $value) {
$safe[] = c::alreadyRendered($value); $safe[] = c::alreadyRendered($value);
} }
@ -398,7 +395,7 @@ class ExtensibleSet
public function renderAs($key, $prefix = ' ') public function renderAs($key, $prefix = ' ')
{ {
$parts = array(); $parts = [];
// TODO: It would be nice if we could use empty arrays to override // TODO: It would be nice if we could use empty arrays to override
// inherited ones // inherited ones
@ -446,7 +443,7 @@ class ExtensibleSet
} }
if (empty($this->allowedValues)) { if (empty($this->allowedValues)) {
return array(); return [];
} }
return array_combine($this->allowedValues, $this->allowedValues); return array_combine($this->allowedValues, $this->allowedValues);
@ -498,7 +495,7 @@ class ExtensibleSet
protected function addResolvedValues($values) protected function addResolvedValues($values)
{ {
if (! $this->hasBeenResolved()) { if (! $this->hasBeenResolved()) {
$this->resolvedValues = array(); $this->resolvedValues = [];
} }
return $this->addValuesTo( return $this->addValuesTo(
@ -537,7 +534,7 @@ class ExtensibleSet
protected function recalculate() protected function recalculate()
{ {
$this->resolvedValues = array(); $this->resolvedValues = [];
if ($this->ownValues === null) { if ($this->ownValues === null) {
$this->addValuesTo($this->resolvedValues, $this->inheritedValues); $this->addValuesTo($this->resolvedValues, $this->inheritedValues);
@ -553,10 +550,10 @@ class ExtensibleSet
protected function reset() protected function reset()
{ {
$this->ownValues = null; $this->ownValues = null;
$this->plusValues = array(); $this->plusValues = [];
$this->minusValues = array(); $this->minusValues = [];
$this->resolvedValues = null; $this->resolvedValues = null;
$this->inheritedValues = array(); $this->inheritedValues = [];
return $this; return $this;
} }
@ -572,6 +569,6 @@ class ExtensibleSet
return $values; return $values;
} }
return array($values); return [$values];
} }
} }