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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/Iterator/CsvFileIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,10 @@ class CsvFileIterator implements \Iterator
*/
public function __construct($file, bool $headerRow = false, string $delimiter = ',', string $enclosure = '"', string $escape = '\\')
{
if (is_resource($file)) {
$this->handle = $file;
} else {
$this->handle = @ fopen($file, 'rb');
$this->handle = (is_resource($file) ? $file : @ fopen($file, 'rb'));

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

$this->headerRow = $headerRow;
Expand Down
12 changes: 4 additions & 8 deletions src/Iterator/CsvJsonFileIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,10 @@ class CsvJsonFileIterator implements \Iterator
*/
public function __construct($file)
{
if (is_resource($file)) {
$this->handle = $file;
} else {
$this->handle = @ fopen($file, 'rb');

if ($this->handle === false) {
throw new \InvalidArgumentException('Cannot open file for reading: ' . $file);
}
$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.

}

$this->current = $this->readRow();
Expand Down
2 changes: 1 addition & 1 deletion src/ObjectArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function get($object) : array
{
$values = $this->storage->get($object);

return ($values === null) ? [] : $values;
return isset($values) ? $values : [];
}

/**
Expand Down
12 changes: 4 additions & 8 deletions src/ObjectStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ public function get($object)
{
$hash = spl_object_hash($object);

if (isset($this->data[$hash])) {
return $this->data[$hash];
}

return null;
return isset($this->data[$hash]) ? $this->data[$hash] : null;
}

/**
Expand Down Expand Up @@ -142,11 +138,11 @@ public function offsetGet($object)
{
$hash = spl_object_hash($object);

if (isset($this->objects[$hash])) {
return $this->data[$hash];
if (! isset($this->objects[$hash])) {
throw new \UnexpectedValueException('Object not found.');
}

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.

}

/**
Expand Down