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

Port must be an integer and other small improvements in Uri #321

Merged
merged 2 commits into from
Jul 24, 2018

Conversation

snapshotpl
Copy link
Contributor

No description provided.

@snapshotpl snapshotpl changed the title Port must be an integer and other small improvements Port must be an integer and other small improvements in Uri Jul 19, 2018
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.

Similar to #319, this poses a BC break, as it disallows previously allowed values that might be an artifact of manually parsing a string URL in order to obtain the port.

We likely should NOT allow float values, as that would be lossy when we cast to int; however, string int values SHOULD be allowed currently.

src/Uri.php Outdated
@@ -333,17 +335,13 @@ public function withHost($host)
*/
public function withPort($port)
{
if (! is_numeric($port) && $port !== null) {
if (! is_int($port) && $port !== null) {
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 problematic, as the port value may be derived from a string. This is why the check was originally for is_numeric, and we cast to int. While PHP's parse_url() does the right thing, and casts the discovered port to an integer, the value might come from another location:

  • from $_SERVER, in which case it's a string
  • by splitting the authority portion of the URI, in which case it's a string
  • by manually parsing the URI in order to provide a more robust solution than parse_url(), in which case casting would be up to the parser

Requiring an int at this point is a BC break.

test/UriTest.php Outdated
$this->assertSame($uri, $new);
$this->assertEquals('3001', $new->getPort());
$this->assertEquals(3001, $new->getPort());
Copy link
Member

Choose a reason for hiding this comment

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

Internally, since we cast to an integer already, the above line should become:

$this->assertSame(3001, $new->getPort());

That change should be made regardless of accepting the other portions of this patch.

test/UriTest.php Outdated
@@ -159,22 +158,23 @@ public function testWithPortReturnsNewInstanceWithProvidedPort($port)
public function testWithPortReturnsSameInstanceWithProvidedPortIsSameAsBefore()
{
$uri = new Uri('https://user:pass@local.example.com:3001/foo?bar=baz#quz');
$new = $uri->withPort('3001');
$new = $uri->withPort(3001);
Copy link
Member

Choose a reason for hiding this comment

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

The fact that this line must change is an indication of the BC break.

test/UriTest.php Outdated
'string' => [ 'string' ],
'string-int' => [ '3000' ],
'array' => [ [ 3000 ] ],
'object' => [ (object) [ 3000 ] ],
Copy link
Member

Choose a reason for hiding this comment

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

This one is invalid, and needs to be changed regardless of other changes. It should read:

'object' => [ (object) ['port' => 3000]],

@snapshotpl
Copy link
Contributor Author

@weierophinney can we keep this changes to v2.0?

@weierophinney
Copy link
Member

can we keep this changes to v2.0?

I'd rather not.

Since the spec itself doesn't have explicit scalar type hints defined, what the constructors and various with*() methods accept is of less importance than what the accessors return. If we can massage the incoming data to the required type without data loss, we should.

If/when we do an update to the PSR-7 specification to add scalar type hints, we can revisit.

@snapshotpl
Copy link
Contributor Author

👍

@weierophinney weierophinney merged commit c798ed9 into zendframework:master Jul 24, 2018
weierophinney added a commit that referenced this pull request Jul 24, 2018
Port must be an integer and other small improvements in Uri
weierophinney added a commit that referenced this pull request Jul 24, 2018
weierophinney added a commit that referenced this pull request Jul 24, 2018
weierophinney added a commit that referenced this pull request Jul 24, 2018
@weierophinney
Copy link
Member

Thanks, @snapshotpl!

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.

2 participants