Skip to content

Commit

Permalink
Use null coalescing operator instead of isset (#545)
Browse files Browse the repository at this point in the history
* Use null coalescing operator instead of isset

* Fix tests
  • Loading branch information
mvorisek authored Apr 8, 2020
1 parent 27027ee commit 21c689b
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 75 deletions.
54 changes: 17 additions & 37 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,20 +187,20 @@ class Field implements Expressionable
/**
* DateTime class used for type = 'data', 'datetime', 'time' fields.
*
* For example, 'DateTime', 'Carbon' etc.
* For example, 'DateTime', 'Carbon\Carbon' etc.
*
* @var string
*/
public $dateTimeClass = 'DateTime';
public $dateTimeClass = \DateTime::class;

/**
* Timezone class used for type = 'data', 'datetime', 'time' fields.
*
* For example, 'DateTimeZone', 'Carbon' etc.
* For example, 'DateTimeZone', 'Carbon\CarbonTimeZone' etc.
*
* @var string
*/
public $dateTimeZoneClass = 'DateTimeZone';
public $dateTimeZoneClass = \DateTimeZone::class;

// }}}

Expand Down Expand Up @@ -334,7 +334,7 @@ public function normalize($value)
case 'datetime':
case 'time':
// we allow http://php.net/manual/en/datetime.formats.relative.php
$class = isset($f->dateTimeClass) ? $f->dateTimeClass : 'DateTime';
$class = $f->dateTimeClass ?? \DateTime::class;

if (is_numeric($value)) {
$value = new $class('@'.$value);
Expand Down Expand Up @@ -403,10 +403,8 @@ public function normalize($value)
* Casts field value to string.
*
* @param mixed $value Optional value
*
* @return string
*/
public function toString($value = null)
public function toString($value = null): string
{
$v = ($value === null ? $this->get() : $this->normalize($value));

Expand Down Expand Up @@ -462,10 +460,8 @@ public function get()
* Sets field value.
*
* @param mixed $value
*
* @return $this
*/
public function set($value)
public function set($value): self
{
$this->owner->set($this->short_name, $value);

Expand All @@ -477,20 +473,16 @@ public function set($value)
* use examples.
*
* @param mixed $value
*
* @return bool
*/
public function compare($value)
public function compare($value): bool
{
return $this->owner[$this->short_name] == $value;
}

/**
* Should this field use alias?
*
* @return bool
*/
public function useAlias()
public function useAlias(): bool
{
return isset($this->actual);
}
Expand All @@ -501,42 +493,32 @@ public function useAlias()

/**
* Returns if field should be editable in UI.
*
* @return bool
*/
public function isEditable()
public function isEditable(): bool
{
return isset($this->ui['editable']) ? $this->ui['editable']
: (($this->read_only || $this->never_persist) ? false
: !$this->system);
return $this->ui['editable'] ?? !$this->read_only && !$this->never_persist && !$this->system;
}

/**
* Returns if field should be visible in UI.
*
* @return bool
*/
public function isVisible()
public function isVisible(): bool
{
return isset($this->ui['visible']) ? $this->ui['visible'] : !$this->system;
return $this->ui['visible'] ?? !$this->system;
}

/**
* Returns if field should be hidden in UI.
*
* @return bool
*/
public function isHidden()
public function isHidden(): bool
{
return isset($this->ui['hidden']) ? $this->ui['hidden'] : false;
return $this->ui['hidden'] ?? false;
}

/**
* Returns field caption for use in UI.
*
* @return string
*/
public function getCaption()
public function getCaption(): string
{
return $this->caption ?? $this->ui['caption'] ?? $this->readableCaption($this->short_name);
}
Expand Down Expand Up @@ -567,10 +549,8 @@ public function getDSQLExpression($expression)

/**
* Returns array with useful debug info for var_dump.
*
* @return array
*/
public function __debugInfo()
public function __debugInfo(): array
{
$arr = [
'short_name' => $this->short_name,
Expand Down
4 changes: 1 addition & 3 deletions src/Field/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ public function normalize($value)
* Casts field value to string.
*
* @param mixed $value Optional value
*
* @return string
*/
public function toString($value = null)
public function toString($value = null): string
{
$v = ($value === null ? $this->get() : $this->normalize($value));

Expand Down
4 changes: 1 addition & 3 deletions src/Field_SQL_Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@ public function afterSave($m)
/**
* Should this field use alias?
* Expression fields always need alias.
*
* @return bool
*/
public function useAlias()
public function useAlias(): bool
{
return true;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -2121,7 +2121,7 @@ public function getIterator()
foreach ($this->rawIterator() as $data) {
$this->data = $this->persistence->typecastLoadRow($this, $data);
if ($this->id_field) {
$this->id = isset($data[$this->id_field]) ? $data[$this->id_field] : null;
$this->id = $data[$this->id_field] ?? null;
}

// you can return false in afterLoad hook to prevent to yield this data row
Expand Down Expand Up @@ -2626,10 +2626,8 @@ public function lastInsertID()

/**
* Returns array with useful debug info for var_dump.
*
* @return array
*/
public function __debugInfo()
public function __debugInfo(): array
{
$arr = [
'id' => $this->id,
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function connect($dsn, $user = null, $password = null, $args = [])
// Process DSN string
$dsn = \atk4\dsql\Connection::normalizeDSN($dsn, $user, $password);

$driver = isset($args['driver']) ? strtolower($args['driver']) : $dsn['driver'];
$driver = strtolower($args['driver'] ?? $dsn['driver']);

switch ($driver) {
case 'mysql':
Expand Down
10 changes: 5 additions & 5 deletions src/Persistence/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function update(Model $m, $id, $data, $table = null)

$this->data[$table][$id] =
array_merge(
isset($this->data[$table][$id]) ? $this->data[$table][$id] : [],
$this->data[$table][$id] ?? [],
$data
);

Expand Down Expand Up @@ -330,8 +330,8 @@ protected function setLimitOrder($m, &$action)

// then set limit
if ($m->limit && ($m->limit[0] || $m->limit[1])) {
$cnt = isset($m->limit[0]) ? $m->limit[0] : 0;
$shift = isset($m->limit[1]) ? $m->limit[1] : 0;
$cnt = $m->limit[0] ?? 0;
$shift = $m->limit[1] ?? 0;

$action->limit($cnt, $shift);
}
Expand Down Expand Up @@ -425,14 +425,14 @@ public function action($m, $type, $args = [])

switch ($type) {
case 'select':
$action = $this->initAction($m, isset($args[0]) ? $args[0] : null);
$action = $this->initAction($m, $args[0] ?? null);
$this->applyConditions($m, $action);
$this->setLimitOrder($m, $action);

return $action;

case 'count':
$action = $this->initAction($m, isset($args[0]) ? $args[0] : null);
$action = $this->initAction($m, $args[0] ?? null);
$this->applyConditions($m, $action);
$this->setLimitOrder($m, $action);

Expand Down
10 changes: 5 additions & 5 deletions src/Persistence/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ public function _typecastSaveField(Field $f, $value)
case 'date':
case 'datetime':
case 'time':
$dt_class = isset($f->dateTimeClass) ? $f->dateTimeClass : 'DateTime';
$tz_class = isset($f->dateTimeZoneClass) ? $f->dateTimeZoneClass : 'DateTimeZone';
$dt_class = $f->dateTimeClass ?? \DateTime::class;
$tz_class = $f->dateTimeZoneClass ?? \DateTimeZone::class;

if ($v instanceof $dt_class || $v instanceof \DateTimeInterface) {
$format = ['date' => 'Y-m-d', 'datetime' => 'Y-m-d H:i:s.u', 'time' => 'H:i:s.u'];
Expand Down Expand Up @@ -563,8 +563,8 @@ public function _typecastLoadField(Field $f, $value)
case 'date':
case 'datetime':
case 'time':
$dt_class = isset($f->dateTimeClass) ? $f->dateTimeClass : 'DateTime';
$tz_class = isset($f->dateTimeZoneClass) ? $f->dateTimeZoneClass : 'DateTimeZone';
$dt_class = $f->dateTimeClass ?? \DateTime::class;
$tz_class = $f->dateTimeZoneClass ?? \DateTimeZone::class;

if (is_numeric($v)) {
$v = new $dt_class('@'.$v);
Expand Down Expand Up @@ -650,7 +650,7 @@ public function action(Model $m, $type, $args = [])
return $q;

case 'select':
$this->initQueryFields($m, $q, isset($args[0]) ? $args[0] : null);
$this->initQueryFields($m, $q, $args[0] ?? null);
break;

case 'count':
Expand Down
4 changes: 1 addition & 3 deletions src/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,8 @@ public function refModel($defaults = []): Model

/**
* Returns array with useful debug info for var_dump.
*
* @return array
*/
public function __debugInfo()
public function __debugInfo(): array
{
$arr = [];
foreach ($this->__debug_fields as $k => $v) {
Expand Down
14 changes: 3 additions & 11 deletions src/Reference/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ protected function getOurValue()

/**
* Returns our field or id field.
*
* @return Field
*/
protected function referenceOurValue(): Field
{
Expand All @@ -54,8 +52,6 @@ protected function referenceOurValue(): Field
* @param array $defaults Properties
*
* @throws Exception
*
* @return Model
*/
public function ref($defaults = []): Model
{
Expand All @@ -72,8 +68,6 @@ public function ref($defaults = []): Model
* @param array $defaults Properties
*
* @throws Exception
*
* @return Model
*/
public function refLink($defaults = []): Model
{
Expand All @@ -92,8 +86,6 @@ public function refLink($defaults = []): Model
* @param array $defaults Properties
*
* @throws Exception
*
* @return Field
*/
public function addField($n, $defaults = []): Field
{
Expand All @@ -107,8 +99,8 @@ public function addField($n, $defaults = []): Field

$defaults['aggregate_relation'] = $this;

$field_n = isset($defaults['field']) ? $defaults['field'] : $n;
$field = isset($defaults['field']) ? $defaults['field'] : null;
$field_n = $defaults['field'] ?? $n;
$field = $defaults['field'] ?? null;

if (isset($defaults['concat'])) {
$defaults['aggregate'] = $this->owner->dsql()->groupConcat($field_n, $defaults['concat']);
Expand All @@ -122,7 +114,7 @@ public function addField($n, $defaults = []): Field

return $r->action('field', [$r->expr(
$defaults['expr'],
isset($defaults['args']) ? $defaults['args'] : null
$defaults['args'] ?? null
), 'alias'=>$field]);
};
unset($defaults['args']);
Expand Down
4 changes: 2 additions & 2 deletions tests/ContainsManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,13 @@ public function testNestedContainsMany()
$this->assertEquals(
json_encode([
'1' => [
'id' => 1, 'vat_rate_id' => 1, 'price' => '10', 'qty' => '2', 'add_date' => (new \DateTime('2019-06-01'))->format('Y-m-d\TH:i:sP'), 'discounts' => json_encode([
'id' => 1, 'vat_rate_id' => '1', 'price' => '10', 'qty' => '2', 'add_date' => (new \DateTime('2019-06-01'))->format('Y-m-d\TH:i:sP'), 'discounts' => json_encode([
'1' => ['id' => 1, 'percent' => '5', 'valid_till' => (new \DateTime('2019-07-15'))->format('Y-m-d\TH:i:sP')],
'2' => ['id' => 2, 'percent' => '10', 'valid_till' => (new \DateTime('2019-07-30'))->format('Y-m-d\TH:i:sP')],
]),
],
'2' => [
'id' => 2, 'vat_rate_id' => 2, 'price' => '15', 'qty' => '5', 'add_date' => (new \DateTime('2019-07-01'))->format('Y-m-d\TH:i:sP'), 'discounts' => json_encode([
'id' => 2, 'vat_rate_id' => '2', 'price' => '15', 'qty' => '5', 'add_date' => (new \DateTime('2019-07-01'))->format('Y-m-d\TH:i:sP'), 'discounts' => json_encode([
'1' => ['id' => 1, 'percent' => '20', 'valid_till' => (new \DateTime('2019-12-31'))->format('Y-m-d\TH:i:sP')],
]),
],
Expand Down
2 changes: 1 addition & 1 deletion tests/ContainsOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function testContainsOne()
// let's test how it all looks in persistence without typecasting
$exp_addr = $i->export(null, null, false)[0]['addr'];
$this->assertEquals(
'{"country_id":2,"address":"bar","built_date":"2019-01-01T00:00:00+00:00","tags":"[\"foo\",\"bar\"]","door_code":"{\"code\":\"DEF\",\"valid_till\":\"2019-07-01T00:00:00+00:00\"}"}',
'{"country_id":"2","address":"bar","built_date":"2019-01-01T00:00:00+00:00","tags":"[\"foo\",\"bar\"]","door_code":"{\"code\":\"DEF\",\"valid_till\":\"2019-07-01T00:00:00+00:00\"}"}',
$exp_addr
);

Expand Down

0 comments on commit 21c689b

Please sign in to comment.