HiveBrain v1.2.0
Get Started
← Back to all entries
debugphpMinor

Checking empty object

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
objectcheckingempty

Problem

Is there a better method for covering errors in my case? I am looking for best practice for now and future instances.

Foreseen errors that could arise:

  • No attributes at all



  • Some attributes could be missing



  • Attribute value could be empty



// converting dom into array to use later
$fields = $dom->getElementsByTagName($field);
$arr['field'] = array();

foreach ($fields as $field) {
    $attributes = isset($field->attributes) ? $field->attributes : NULL;

    if (!empty($attributes)) {
        // the method getNamedItem($string) returns NULL if not found
        $name = !empty($attributes->getNamedItem('name')) ? $attributes->getNamedItem('name')->nodeValue : NULL;
        $id = !empty($attributes->getNamedItem('id')) ? $attributes->getNamedItem('id')->nodeValue : NULL;
        $field_name = !empty($attributes->getNamedItem('field_name')) ? $attributes->getNamedItem('field_name')->nodeValue : NULL;

        if (!empty($name) && !empty($id) && !empty($field_name)) {
            $arr['field'][$name][$id][$field_name] = $field->nodeValue;
        }
    }
}

Solution

I think it is cleaner to handle special cases early; your code winds up less indented.

// converting dom into array to use later
$fields = $dom->getElementsByTagName($field);
$arr['field'] = array();

foreach ($fields as $field) {
  $attributes = isset($field->attributes) ? $field->attributes : NULL;

  if (empty($attributes)) {
    continue;
  }

  $name = !empty($attributes->getNamedItem('name')) ? $attributes->getNamedItem('name')->nodeValue : NULL;
  $id = !empty($attributes->getNamedItem('id')) ? $attributes->getNamedItem('id')->nodeValue : NULL;
  $field_name = !empty($attributes->getNamedItem('field_name')) ? $attributes->getNamedItem('field_name')->nodeValue : NULL;

  if (empty($name) || empty($id) || empty($field_name)) {
    continue;
  }

  $arr['field'][$name][$id][$field_name] = $field->nodeValue;
}


This looks less complicated, at least to me, but it does exactly the same work.

You could also introduce a method to get the nodeValue or NULL, given $attributes and a name.

// converting dom into array to use later
$fields = $dom->getElementsByTagName($field);
$arr['field'] = array();

foreach ($fields as $field) {
  $attributes = isset($field->attributes) ? $field->attributes : NULL;

  if (empty($attributes)) {
    continue;
  }

  $name       = nodeIfPresent($attributes, 'name');
  $id         = nodeIfPresent($attributes, 'id');
  $field_name = nodeIfPresent($attributes, 'field_name');

  if (empty($name) || empty($id) || empty($field_name)) {
    continue;
  }

  $arr['field'][$name][$id][$field_name] = $field->nodeValue;
}

Code Snippets

// converting dom into array to use later
$fields = $dom->getElementsByTagName($field);
$arr['field'] = array();

foreach ($fields as $field) {
  $attributes = isset($field->attributes) ? $field->attributes : NULL;

  if (empty($attributes)) {
    continue;
  }

  $name = !empty($attributes->getNamedItem('name')) ? $attributes->getNamedItem('name')->nodeValue : NULL;
  $id = !empty($attributes->getNamedItem('id')) ? $attributes->getNamedItem('id')->nodeValue : NULL;
  $field_name = !empty($attributes->getNamedItem('field_name')) ? $attributes->getNamedItem('field_name')->nodeValue : NULL;

  if (empty($name) || empty($id) || empty($field_name)) {
    continue;
  }

  $arr['field'][$name][$id][$field_name] = $field->nodeValue;
}
// converting dom into array to use later
$fields = $dom->getElementsByTagName($field);
$arr['field'] = array();

foreach ($fields as $field) {
  $attributes = isset($field->attributes) ? $field->attributes : NULL;

  if (empty($attributes)) {
    continue;
  }

  $name       = nodeIfPresent($attributes, 'name');
  $id         = nodeIfPresent($attributes, 'id');
  $field_name = nodeIfPresent($attributes, 'field_name');

  if (empty($name) || empty($id) || empty($field_name)) {
    continue;
  }

  $arr['field'][$name][$id][$field_name] = $field->nodeValue;
}

Context

StackExchange Code Review Q#55167, answer score: 3

Revisions (0)

No revisions yet.