Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance conditions #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

peter279k
Copy link
Contributor

Changed log

  • To reduce nested condition, using the ?: operator to do that.
  • Using the ?? syntax to reduce conditions.
  • Using the error Exception first to enhance condition logic.

@coveralls
Copy link

coveralls commented Dec 1, 2018

Coverage Status

Coverage decreased (-0.1%) to 94.427% when pulling 7fee5ac on peter279k:enhance_condition into b8acd4b on brick:master.

$this->handle = (is_resource($file) ? $file : @ fopen($file, 'rb'));

if ($this->handle === false) {
throw new \InvalidArgumentException('Cannot open file for reading: ' . $file);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean about this comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the null coalesce operator here, as it also allows for undefined variable without notice. This operator is equivalent to isset($values) ? $values : [], not strictly equivalent to the original code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is not related to the null coalesce operator.

I think this change is proper approach.

@@ -49,7 +49,7 @@ public function get($object) : array
{
$values = $this->storage->get($object);

return ($values === null) ? [] : $values;
return $values ?? [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the null coalesce operator here, as it also allows for undefined variable without notice. This operator is equivalent to isset($values) ? $values : [], not strictly equivalent to the original code.

}

throw new \UnexpectedValueException('Object not found.');
return $this->data[$hash];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change brings no value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach is about error exception first.

It's about change the order of conditions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got that, but what's the point? Please don't spent time on nitpick like this :)

Copy link
Contributor Author

@peter279k peter279k Dec 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I will not spend time about this change.

}

return null;
return $this->data[$hash] ?? null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can keep this change alone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about reverting this due to your previous comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an isset($x) ? $x : null, so conceptually equivalent to $x ?? null. I can accept this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants