Permalink
Browse files

Misc. code cleanup of `\net\socket\Stream`.

  • Loading branch information...
1 parent 70f3cb9 commit 1938ec3f2f0bb13a62ddb5d351cf5a6dd06bbd07 @nateabele nateabele committed Mar 9, 2011
Showing with 12 additions and 15 deletions.
  1. +12 −15 libraries/lithium/net/socket/Stream.php
@@ -30,11 +30,11 @@ class Stream extends \lithium\net\Socket {
public function open() {
$config = $this->_config;
- if (empty($config['scheme']) || empty($config['host'])) {
+ if (!$config['scheme'] || !$config['host']) {
return false;
}
$scheme = ($config['scheme'] !== 'udp') ? 'tcp' : 'udp';
- $port = ($config['port']) ?: 80;
+ $port = $config['port'] ?: 80;
$host = "{$scheme}://{$config['host']}:{$port}";
$flags = STREAM_CLIENT_CONNECT;
@@ -45,15 +45,14 @@ public function open() {
$host, $errorCode, $errorMessage, $config['timeout'], $flags
);
- if (!empty($errorCode) || !empty($errorMessage)) {
+ if ($errorCode || $errorMessage) {
throw new NetworkException($errorMessage);
}
$this->timeout($config['timeout']);
if (!empty($config['encoding'])) {
$this->encoding($config['encoding']);
}
-
return $this->_resource;
}
@@ -67,6 +66,7 @@ public function close() {
return true;
}
fclose($this->_resource);
+
if (is_resource($this->_resource)) {
$this->close();
}
@@ -76,13 +76,10 @@ public function close() {
/**
* Determines if the socket resource is at EOF.
*
- * @return boolean True if resource pointer is at EOF, false otherwise.
+ * @return boolean Returns `true` if resource pointer is at its EOF, `false` otherwise.
*/
public function eof() {
- if (!is_resource($this->_resource)) {
- return true;
- }
- return feof($this->_resource);
+ return is_resource($this->_resource) ? feof($this->_resource) : true;
}
/**
@@ -97,9 +94,10 @@ public function read($length = null, $offset = null) {
if (!is_resource($this->_resource)) {
return false;
}
- return is_null($length) ? stream_get_contents($this->_resource) : stream_get_contents(
- $this->_resource, $length, $offset
- );
+ if (!$length) {
+ return stream_get_contents($this->_resource);
+ }
+ return stream_get_contents($this->_resource, $length, $offset);
}
/**
@@ -144,14 +142,13 @@ public function encoding($charset) {
if (!function_exists('stream_encoding')) {
return false;
}
- return is_resource($this->_resource)
- ? stream_encoding($this->_resource, $charset) : false;
+ return is_resource($this->_resource) ? stream_encoding($this->_resource, $charset) : false;
}
/**
* Aggregates read and write methods into a coherent request response
*
- * @param mixed $message array or object like `\lithium\net\http\Request`
+ * @param mixed $message array or object like `lithium\net\http\Request`
* @param array $options
* - path: path for the current request
* - classes: array of classes to use

0 comments on commit 1938ec3

Please sign in to comment.