Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Small optimizations #267

Closed

Conversation

snapshotpl
Copy link
Contributor

No description provided.

@@ -172,10 +172,10 @@ public function getMetadata($key = null)
return $metadata;
}

if (! array_key_exists($key, $metadata)) {
return null;
if (isset($metadata[$key])) {
Copy link
Member

Choose a reason for hiding this comment

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

return $metadata[$key] ?? null;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea but after drop php5 ;(

Copy link
Member

Choose a reason for hiding this comment

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

Wha?! This is for an improvement: just drop it already

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But is it not better to merge it as it to current php5 supported line and then, drop php5 and apply you have suggested?

Copy link
Member

Choose a reason for hiding this comment

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

If we can optimize for PHP 5.6 today, we can make it even better for the next major release later. I'm fine with this for now.

@@ -107,7 +107,7 @@ public function isSeekable()
*/
public function seek($offset, $whence = SEEK_SET)
{
if ($whence == SEEK_SET) {
if ($whence === SEEK_SET) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you use the FQN for this constant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what are you meant

Copy link
Member

Choose a reason for hiding this comment

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

\SEEK_SET

@@ -60,7 +60,7 @@ public static function fromGlobals(
$files = static::normalizeFiles($files ?: $_FILES);
$headers = static::marshalHeaders($server);

if (null === $cookies && array_key_exists('cookie', $headers)) {
if (null === $cookies && isset($headers['cookie'])) {
Copy link
Member

Choose a reason for hiding this comment

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

You need to introduce tests around null values to avoid regressions 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.

I think it's imposible to write this test becase $headers comes from private function which alware return strings pair array.

Copy link
Member

Choose a reason for hiding this comment

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

No need then: nullability was just an assumption on my end.

@@ -206,7 +206,7 @@ public static function marshalHeaders(array $server)

// We will not overwrite existing variables with the
// prefixed versions, though
if (array_key_exists($key, $server)) {
if (isset($server[$key])) {
continue;
Copy link
Member

Choose a reason for hiding this comment

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

You need to introduce tests around null values to avoid regressions here

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, particularly as the values may resolve to a false (e.g., 0, ''). The reason array_key_exists() was used was to allow any value, regardless of whether or not it evaluated as truthy.

@@ -285,11 +285,11 @@ public function getMetadata($key = null)
}

$metadata = stream_get_meta_data($this->resource);
if (! array_key_exists($key, $metadata)) {
return null;
if (isset($metadata[$key])) {
Copy link
Member

Choose a reason for hiding this comment

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

Use ?? here too

Copy link
Member

Choose a reason for hiding this comment

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

If you target PHP 7.1+, you'll need to update the composer.json, rebase against develop, and change the branch this is against.

@@ -641,7 +641,7 @@ private function filterQuery($query)
private function splitQueryValue($value)
{
$data = explode('=', $value, 2);
if (1 === count($data)) {
if (! isset($data[1])) {
Copy link
Member

Choose a reason for hiding this comment

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

This check is not equivalent to the previous: you need to verify that the key 0 exists too

Copy link
Contributor

Choose a reason for hiding this comment

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

@Ocramius explode() should always has key 0 ? https://3v4l.org/viNZT

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Ocramius agree with @samsonasik

Copy link
Member

Choose a reason for hiding this comment

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

Yup, misread

@snapshotpl
Copy link
Contributor Author

@Ocramius all remarks applied in code or I miss something?

@michalbundyra
Copy link
Member

@snapshotpl tests are failing. Why?

{
if ($whence == SEEK_SET) {
if ($whence === \SEEK_SET) {
Copy link
Member

Choose a reason for hiding this comment

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

I would go with import the constant as we do in other places for internal functions:

use const SEEK_SET;

Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

I'm fine with the majority of changes, but I do question whether some of these micro-optimizations make sense, as they make the code less readable.

@@ -206,7 +206,7 @@ public static function marshalHeaders(array $server)

// We will not overwrite existing variables with the
// prefixed versions, though
if (array_key_exists($key, $server)) {
if (isset($server[$key])) {
continue;
Copy link
Member

Choose a reason for hiding this comment

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

Agreed, particularly as the values may resolve to a false (e.g., 0, ''). The reason array_key_exists() was used was to allow any value, regardless of whether or not it evaluated as truthy.

|| strpos($mode, 'w') !== false
|| strpos($mode, 'c') !== false
|| strpos($mode, 'a') !== false
|| strpos($mode, '+') !== false
Copy link
Member

Choose a reason for hiding this comment

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

How is this change "better" or an "optimization", exactly? I find it less readable.

@@ -234,7 +236,7 @@ public function isReadable()
$meta = stream_get_meta_data($this->resource);
$mode = $meta['mode'];

return (strstr($mode, 'r') || strstr($mode, '+'));
return (strpos($mode, 'r') !== false || strpos($mode, '+') !== false);
Copy link
Member

Choose a reason for hiding this comment

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

Same comment as above regarding strstr vs strpos. I recognize that strpos is marginally faster, but most folks agree it's a micro-optimization at best. If using it sacrifices readability, I'm not sure the change is useful.

@@ -285,11 +285,11 @@ public function getMetadata($key = null)
}

$metadata = stream_get_meta_data($this->resource);
if (! array_key_exists($key, $metadata)) {
return null;
if (isset($metadata[$key])) {
Copy link
Member

Choose a reason for hiding this comment

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

If you target PHP 7.1+, you'll need to update the composer.json, rebase against develop, and change the branch this is against.

@weierophinney
Copy link
Member

Closing; many changes were requested, but none provided by the author.

If you or anyone else reading want to see these changes, please submit a new PR incorporating them, along with the requested feedback.

Thanks!

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

Successfully merging this pull request may close these issues.

None yet

5 participants