Skip to content

Commit

Permalink
Remove dead code.
Browse files Browse the repository at this point in the history
By defaulting the host when building a URI instance we can remove
a redundant type check. In practice outside of CLI environments, the
host will never be empty, but this fallback makes CLI environments and
testing simpler.
  • Loading branch information
markstory committed May 11, 2017
1 parent 2de06f0 commit 870ec58
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 37 deletions.
4 changes: 4 additions & 0 deletions src/Http/ServerRequestFactory.php
Expand Up @@ -100,6 +100,10 @@ public static function marshalUriFromServer(array $server, array $headers)
$uri = static::updatePath($base, $uri);
}

if (!$uri->getHost()) {
$uri = $uri->withHost('localhost');
}

// Splat on some extra attributes to save
// some method calls.
$uri->base = $base;
Expand Down
33 changes: 9 additions & 24 deletions src/Routing/Router.php
Expand Up @@ -419,34 +419,19 @@ public static function pushRequest(ServerRequest $request)
/**
* Store the request context for a given request.
*
* @param \Cake\Http\ServerRequest|\Psr\Http\Message\ServerRequestInterface $request The request instance.
* @param \Psr\Http\Message\ServerRequestInterface $request The request instance.
* @return void
* @throws InvalidArgumentException When parameter is an incorrect type.
*/
public static function setRequestContext($request)
public static function setRequestContext(ServerRequestInterface $request)
{
if ($request instanceof ServerRequest) {
static::$_requestContext = [
'_base' => $request->base,
'_port' => $request->port(),
'_scheme' => $request->scheme(),
'_host' => $request->host()
];

return;
}
if ($request instanceof ServerRequestInterface) {
$uri = $request->getUri();
static::$_requestContext = [
'_base' => $request->getAttribute('base'),
'_port' => $uri->getPort(),
'_scheme' => $uri->getScheme(),
'_host' => $uri->getHost(),
];

return;
}
throw new InvalidArgumentException('Unknown request type received.');
$uri = $request->getUri();
static::$_requestContext = [
'_base' => $request->getAttribute('base'),
'_port' => $uri->getPort(),
'_scheme' => $uri->getScheme(),
'_host' => $uri->getHost(),
];
}

/**
Expand Down
13 changes: 0 additions & 13 deletions tests/TestCase/Routing/RouterTest.php
Expand Up @@ -2035,7 +2035,6 @@ public function testGenerationWithSslOption()
Router::connect('/:controller/:action/*');

$request = new ServerRequest();
$request->env('HTTP_HOST', 'localhost');
Router::pushRequest(
$request->addParams([
'plugin' => null, 'controller' => 'images', 'action' => 'index'
Expand Down Expand Up @@ -2067,7 +2066,6 @@ public function testGenerateWithSslInSsl()
Router::connect('/:controller/:action/*');

$request = new ServerRequest();
$request->env('HTTP_HOST', 'localhost');
$request->env('HTTPS', 'on');
Router::pushRequest(
$request->addParams([
Expand Down Expand Up @@ -3341,17 +3339,6 @@ public function testSetRequestContextPsr()
$this->assertEquals('/subdir/pages/home', $result);
}

/**
* Test setting the request context.
*
* @expectedException \InvalidArgumentException
* @return void
*/
public function testSetRequestContextInvalid()
{
Router::setRequestContext(new \stdClass);
}

/**
* Test getting path specific middleware.
*
Expand Down

0 comments on commit 870ec58

Please sign in to comment.