Commits

Thomas Weinert committed 844f197

Cleanup source and comments

Comments (0)

Files changed (27)

 /.buildpath
 /.project
 /.settings
+/.idea
 /serproxy
 /build
 

src/Carica/Io/ByteArray.php

     /**
      * Read an hexadecimal encoded binary string
      *
-     * @param string $string
+     * @param array $bytes
      * @param boolean $resize
      * @throws \OutOfBoundsException
      */
     /**
      * Get the byte array as an hexdec string.
      *
+     * @param string $separator
+     *
      * @return string
      */
-    public function asHex() {
+    public function asHex($separator = '') {
       $result = '';
       foreach ($this->_bytes as $byte) {
-        $result .= str_pad(dechex($byte), 2, '0', STR_PAD_LEFT);
+        $result .= str_pad(dechex($byte), 2, '0', STR_PAD_LEFT).$separator;
       }
-      return $result;
+      return empty($separator)
+        ? $result
+        : substr($result, 0, -strlen($separator));
     }
 
     /**
     /**
      * Validate the given value is a storeable in a single byte
      *
-     * @param integer $offset
-     * @throws \OutOfBoundsException
+     * @param integer $value
+     * @throws \OutOfRangeException
      */
     private function validateValue($value) {
       if (($value < 0 || $value > 255)) {
     /**
      * Create a new ByteArray from an array of bytes
      *
-     * @param array $bytes
+     * @param array $array
      * @return \Carica\Io\ByteArray
      */
     public static function createFromArray(array $array) {

src/Carica/Io/Callbacks.php

     /**
      * Execute the callbacks
      *
-     * @param mixed $argument,...
+     * @param mixed [$argument,...]
      */
     public function fire() {
       if (!$this->_disabled) {
     /**
      * If the object is used as an functor, call fire()
      *
-     * @param mixed $argument,...
+     * @param mixed [$argument,...]
+     * @return mixed
      */
     public function __invoke() {
       return call_user_func_array(array($this, 'fire'), func_get_args());
      * Block changes to the object properties
      *
      * @param string $name
+     * @param mixed $value
+     *
+     * @throws \LogicException
      */
     public function __set($name, $value) {
       throw new \LogicException('Unknown/Readonly property: '.$name);

src/Carica/Io/Deferred.php

         $master = new Deferred();
         $resolveArguments = array();
         foreach ($arguments as $index => $argument) {
+          /**
+           * @var Deferred\Promise $argument
+           */
           if ($argument instanceOf Deferred || $argument instanceOf Deferred\Promise) {
             $argument
               ->done(

src/Carica/Io/Event/Emitter.php

      * Add a listener object. If a callable is added, it is wrapped into a listener
      *
      * @param string $event
-     * @param Carica\Io\Event\Emitter\Listener $listener
+     * @param Emitter\Listener $listener
      */
     public function on($event, $listener) {
       $listener = $listener instanceOf Emitter\Listener
      * into a listener
      *
      * @param string $event
-     * @param Carica\Io\Event\Emitter\Listener $listener
+     * @param Emitter\Listener $listener
      */
     public function once($event, $listener) {
       $listener = $listener instanceOf Emitter\Listener\Once
      * Remode the specified listenr from the event
      *
      * @param string $event
-     * @param Carica\Io\Event\Emitter\Listener $listener
+     * @param Emitter\Listener $listener
      */
     public function removeListener($event, $listener) {
       if (isset($this->_events[$event])) {
      * Return an list of a listeners attached to the event
      *
      * @param string $event
-     * @return array(Carica\Io\Event\Emitter\Listener)
+     * @return array(Emitter\Listener)
      */
     public function listeners($event) {
       return isset($this->_events[$event]) ? $this->_events[$event] : array();
      * Emit an event to all attached listeners
      *
      * @param string $event
-     * @param mixed $argument,...
+     * @param mixed [$argument,...]
      */
     public function emit($event) {
       $arguments = func_get_args();

src/Carica/Io/Event/Emitter/Aggregation.php

   trait Aggregation {
 
     /**
-     * @var Carica\Io\Event\Emitter
+     * @var Event\Emitter
      */
     private $_eventEmitter = NULL;
 
     /**
      * Getter/Setter for the event emitter including implicit create.
      *
-     * @param Carica\Io\Event\Emitter $emitter
-     * @return Carica\Io\Event\Emitter
+     * @param Event\Emitter $emitter
+     * @return Event\Emitter
      */
     public function events(Event\Emitter $emitter = NULL) {
       if (NULL !== $emitter) {

src/Carica/Io/Event/Loop.php

 
 namespace Carica\Io\Event {
 
+  use Carica\Io;
+
   interface Loop {
 
     function setTimeout(Callable $callback, $milliseconds);
 
     function remove($listener);
 
-    function run(\Carica\Io\Deferred\Promise $for = NULL);
+    function run(Io\Deferred\Promise $for = NULL);
 
     function stop();
 

src/Carica/Io/Event/Loop/Aggregation.php

   trait Aggregation {
 
     /**
-     * @var Carica\Io\Event\Loop
+     * @var Event\Loop
      */
     private $_eventLoop = NULL;
 
      * Getter/Setter for the event loop including implicit create. The create uses
      * the factory fetching a global instance of the loop by default.
      *
-     * @param Carica\Io\Event\Loop $loop
-     * @return Carica\Io\Event\Loop
+     * @param Event\Loop $loop
+     * @return Event\Loop
      */
     public function loop(Event\Loop $loop = NULL) {
       if (NULL !== $loop) {

src/Carica/Io/Event/Loop/Factory.php

 
 namespace Carica\Io\Event\Loop {
 
+  use Carica\Io;
   use Carica\Io\Event;
 
   class Factory {
 
     /**
-     * @var Carica\Io\Event\Loop
+     * @var Event\Loop
      */
     private static $_globalLoop = NULL;
 
     /**
      * Create a event loop
      *
-     * @return Carica\Io\Event\Loop
+     * @return Event\Loop
      */
     public static function create() {
       if (self::useLibevent()) {
     /**
      * Return a global event loop instance, create it if it does not exists yet.
      *
-     * @return Carica\Io\Event\Loop
+     * @return Event\Loop
      */
     public static function get() {
       if (is_null(self::$_globalLoop)) {
     /**
      * Destroy the global event loop
      *
-     * @return Carica\Io\Event\Loop
+     * @return Event\Loop
      */
     public static function reset() {
       self::$_useLibevent = NULL;
     /**
      * Run the global event loop
      */
-    public static function run(\Carica\Io\Deferred\Promise $for = NULL) {
+    public static function run(Io\Deferred\Promise $for = NULL) {
       self::get()->run($for);
     }
   }

src/Carica/Io/Event/Loop/Libevent.php

 
 namespace Carica\Io\Event\Loop {
 
+  use Carica\Io;
   use Carica\Io\Event;
 
   class Libevent implements Event\Loop {
 
+    /**
+     * @var resource
+     */
     private $_base = NULL;
+    /**
+     * @var array(string=>Libevent\Listener)
+     */
     private $_timers = array();
+    /**
+     * @var array(resource=>Libevent\Listener\Stream)
+     */
     private $_streams = array();
 
+    /**
+     * @param resource $base
+     */
     public function __construct($base) {
       $this->_base = $base;
     }
 
+    /**
+     * @param callable $callback
+     * @param integer $milliseconds
+     *
+     * @return Libevent\Listener\Timeout
+     */
     public function setTimeout(Callable $callback, $milliseconds) {
       $event = new Libevent\Listener\Timeout($this, $callback, $milliseconds);
       $this->_timers[spl_object_hash($event)] = $event();
       return $event;
     }
 
+    /**
+     * @param callable $callback
+     * @param integer $milliseconds
+     *
+     * @return Libevent\Listener\Interval
+     */
     public function setInterval(Callable $callback, $milliseconds) {
       $event = new Libevent\Listener\Interval($this, $callback, $milliseconds);
       $this->_timers[spl_object_hash($event)] = $event();
       return $event;
     }
 
+    /**
+     * @param callable $callback
+     * @param resource $stream
+     *
+     * @return mixed
+     * @throws \LogicException
+     */
     public function setStreamReader(Callable $callback, $stream) {
       if (!is_resource($stream)) {
-        throw new LogicException('%s needs a valid stream resource.', __METHOD__);
+        throw new \LogicException('%s needs a valid stream resource.', __METHOD__);
       }
       if (!isset($this->_streams[$stream])) {
-        $this->_streams[$stream] = new Libevent\Listener\Stream($this, $stream);
+        $this->_streams[$stream] = $listener = new Libevent\Listener\Stream($this, $stream);
+      } else {
+        $listener = $this->_streams[$stream];
       }
-      $result = $this->_streams[$stream]->onRead($callback);
+      $result = $listener->onRead($callback);
       return $result;
     }
 
     public function remove($event) {
+      /**
+       * @var Libevent\Listener $listener
+       */
       if (isset($event)) {
         $key = spl_object_hash($event);
         if (array_key_exists($key, $this->_timers)) {
       }
     }
 
-    public function run(\Carica\Io\Deferred\Promise $for = NULL) {
+    public function run(Io\Deferred\Promise $for = NULL) {
       if (isset($for) &&
-          $for->state() === \Carica\Io\Deferred::STATE_PENDING) {
+          $for->state() === Io\Deferred::STATE_PENDING) {
         $loop = $this;
         $for->always(
           function () use ($loop) {
         );
       }
       if (isset($for) &&
-          $for->state() !== \Carica\Io\Deferred::STATE_PENDING) {
+          $for->state() !== Io\Deferred::STATE_PENDING) {
         event_base_loop($this->_base, EVLOOP_ONCE);
       } else {
         event_base_loop($this->_base);

src/Carica/Io/Event/Loop/StreamSelect.php

 
 namespace Carica\Io\Event\Loop {
 
+  use Carica\Io;
   use Carica\Io\Event;
   use Carica\Io\Event\Loop\StreamSelect\Listener;
 
   class StreamSelect implements Event\Loop {
 
+    private $_running;
     private $_wait = 5;
 
     private $_timers = array();
      * it is still pending and add a callback to stop the loop if is is
      * finished.
      *
-     * @param \Carica\Io\Deferred\Promise $for
+     * @param Io\Deferred\Promise $for
      */
-    public function run(\Carica\Io\Deferred\Promise $for = NULL) {
+    public function run(Io\Deferred\Promise $for = NULL) {
       $this->_running = TRUE;
       if (isset($for) &&
-          $for->state() === \Carica\Io\Deferred::STATE_PENDING) {
+          $for->state() === Io\Deferred::STATE_PENDING) {
         $loop = $this;
         $for->always(
           function () use ($loop) {
 
     private function tick() {
       if ($this->_running) {
+        /**
+         * @var StreamSelect\Listener $listener
+         */
         if ($this->_hasResources) {
           $read = $this->_resources['read'];
           $write = $this->_resources['write'];

src/Carica/Io/FileSystem/Aggregation.php

 
 namespace Carica\Io\FileSystem {
 
+  use Carica\Io;
+
   trait Aggregation {
 
     private $_fileSystem = NULL;
     /**
      * Create an return a FileSystem instance
      *
-     * @param \Carica\Io\FileSystem $fileSystem
+     * @param Io\FileSystem $fileSystem
+     * @return Io\FileSystem
      */
-    public function fileSystem(\Carica\Io\FileSystem $fileSystem = NULL) {
+    public function fileSystem(Io\FileSystem $fileSystem = NULL) {
       if (isset($fileSystem)) {
         $this->_fileSystem = $fileSystem;
       } elseif (NULL === $this->_fileSystem) {
-        $this->_fileSystem = new \Carica\Io\FileSystem();
+        $this->_fileSystem = new Io\FileSystem();
       }
       return $this->_fileSystem;
     }

src/Carica/Io/Loader.php

      * The actual autloader function that will be registered.
      *
      * @param string $class
+     * @return boolean
      */
     public static function load($class) {
       if ($file = self::getFilename($class)) {

src/Carica/Io/Network/Http/Header.php

 
     /**
      * @param string $name
-     * @param string|array|Traversable $data
+     * @param string|array|\Traversable $data
      */
     public function __construct($name, $data = array()) {
       $this->setName($name);
 
     /**
      * Set the http header name
+     *
      * @param string $name
+     * @throws \UnexpectedValueException
      */
     public function setName($name) {
       if (trim($name) == '') {
 
     /**
      * @param string $name
-     * @throws InvalidArgumentException
-     * @return string|mixed|ArrayObject
+     * @throws \LogicException
+     * @return string|mixed|\ArrayObject
      */
     public function __get($name) {
       switch ($name) {
 
     /**
      * @param string $name
-     * @param string|array|Traversable $value
+     * @param string|array|\Traversable $value
      * @throws \LogicException
      */
     public function __set($name, $value) {
 
     /**
      * @param integer $offset
+     * @return string
      */
     public function offsetGet($offset) {
       return $this->_values->offsetGet($offset);

src/Carica/Io/Network/Http/Headers.php

       unset($this->_headers[$this->prepareKey($name)]);
     }
 
-    private function prepareKey($name, $silent = FALSE) {
+    private function prepareKey($name) {
       $name = trim($name);
       if (empty($name)) {
         throw new \InvalidArgumentException('The header name can not be empty.');

src/Carica/Io/Network/Http/Response.php

       switch ($name) {
       case 'version' :
         $this->setVersion($value);
-        return;
+        return NULL;
       case 'status' :
         $this->setStatus($value);
-        return;
+        return NULL;
       case 'connection' :
       case 'content' :
       case 'headers' :

src/Carica/Io/Network/Http/Route.php

      * Attach a callback as a route target starting with the given path. This
      * is not unlike match but ignores if here is path has additional parts.
      *
-     * @param unknown $path
+     * @param string $path
      * @param callable $callback
-     * @return \Carica\Io\Network\Http\Route\Target\StartsWith
+     * @return Route\Target\StartsWith
      */
     public function startsWith($path, Callable $callback) {
       $this->_targets[] = $target = new Route\Target\StartsWith($callback, $path);
 
     /**
      * Allow to trigger the route
-     * @param unknown $request
+     *
+     * @param Request $request
+     * @return NULL|Response
      */
     public function __invoke($request) {
       return $this->fire($request);
      * Allow to iterate the attached route targets
      *
      * @see IteratorAggregate::getIterator()
-     * @return Iterator
+     * @return \Iterator
      */
     public function getIterator() {
       return new \ArrayIterator($this->_targets);

src/Carica/Io/Network/Http/Route/File.php

           );
           return $response;
         } else {
-          return new Carica\Io\Network\Http\Response\Error(
+          return new Http\Response\Error(
             $request, 403
           );
         }

src/Carica/Io/Network/Server.php

           stream_set_blocking($stream, 0);
           $this->resource($stream);
           $this->events()->emit('listen', $this->_address.':'.$port);
+          return TRUE;
         }
+        return FALSE;
       }
+      return TRUE;
     }
 
     public function close() {

src/Carica/Io/Stream/File.php

 
 namespace Carica\Io\Stream {
 
+  use Carica\Io;
   use Carica\Io\Event;
 
-  class File implements \Carica\Io\Stream {
+  class File implements Io\Stream {
 
     use Event\Emitter\Aggregation;
     use Event\Loop\Aggregation;
     /**
      * Read some bytes from the file resource
      *
-     * @param number $bytes
+     * @param integer $bytes
      * @return string|NULL
      */
     public function read($bytes = 1024) {
     /**
      * Write some bytes to the file resource
      *
-     * @param unknown $data
+     * @param string|array(integer) $data
+     * @return bool
      */
     public function write($data) {
       if ($resource = $this->resource()) {
         fwrite(
           $resource,
-          $writtenData = is_array($data) ? \Carica\Io\encodeBinaryFromArray($data) : $data
+          $writtenData = is_array($data) ? Io\encodeBinaryFromArray($data) : $data
         );
         $this->events()->emit('write-data', $writtenData);
         return TRUE;

src/Carica/Io/Stream/Serial.php

 
 namespace Carica\Io\Stream {
 
+  use Carica\Io;
   use Carica\Io\Event;
 
-  class Serial implements \Carica\Io\Stream {
+  class Serial implements Io\Stream {
 
     use Event\Emitter\Aggregation;
     use Event\Loop\Aggregation;
       if ($resource = $this->resource()) {
         fwrite(
           $resource,
-          $writtenData = is_array($data) ? \Carica\Io\encodeBinaryFromArray($data) : $data
+          $writtenData = is_array($data) ? Io\encodeBinaryFromArray($data) : $data
         );
         $this->events()->emit('write-data', $writtenData);
         // according to a php bug report, reading is only possible after writing some stuff

src/Carica/Io/Stream/Serial/Device.php

         $command = sprintf('stty -f %s speed %d', $device, $baud);
       } elseif (substr(PHP_OS, 0, 5) === "Linux") {
         $pattern = '(^/dev/tty\w+\d+$)';
-        $prepare = sprintf('stty -F %s %d', $device, $baud);
+        $command = sprintf('stty -F %s %d', $device, $baud);
       } else {
         throw new \LogicException(sprintf('Unsupport OS: "%s".', PHP_OS));
       }
     }
 
     public function __toString() {
-      return $this->getDevice();
+      return (string)$this->getDevice();
     }
   }
 }

src/Carica/Io/Stream/Tcp.php

 
 namespace Carica\Io\Stream {
 
+  use Carica\Io;
   use Carica\Io\Event;
 
-  class Tcp implements \Carica\Io\Stream {
+  class Tcp implements Io\Stream {
 
     use Event\Emitter\Aggregation;
     use Event\Loop\Aggregation;
       if ($resource = $this->resource()) {
         $bytesSent = @stream_socket_sendto(
           $resource,
-          $writtenData = is_array($data) ? \Carica\Io\encodeBinaryFromArray($data) : $data
+          $writtenData = is_array($data) ? Io\encodeBinaryFromArray($data) : $data
         );
         if ($bytesSent != -1) {
           $this->events()->emit('write-data', $writtenData);

tests/Carica/Io/ByteArrayTest.php

       $this->setExpectedException(
         'OutOfRangeException', 'Zero or negative length is not possible'
       );
-      $bytes = new ByteArray(-3);
+      new ByteArray(-3);
     }
 
     /**

tests/Carica/Io/Deferred/MySQLTest.php

       $mysql = new MySQL($this->getMySQLConnectionFixture(FALSE));
       $mysql->loop($this->getLoopFixture());
       $promise = $mysql('SQL');
-      $this->assertEquals(\Carica\Io\Deferred::STATE_REJECTED, $promise->state());
+      $this->assertEquals(Io\Deferred::STATE_REJECTED, $promise->state());
     }
 
     /**
       $mysql = new MySQL($this->getMySQLConnectionFixture(TRUE));
       $mysql->loop($this->getLoopFixture());
       $promise = $mysql('SQL');
-      $this->assertEquals(\Carica\Io\Deferred::STATE_RESOLVED, $promise->state());
+      $this->assertEquals(Io\Deferred::STATE_RESOLVED, $promise->state());
     }
 
     public function getLoopFixture() {

tests/Carica/Io/Deferred/PromiseTest.php

      * @covers Carica\Io\Deferred\Promise::then
      */
     public function testThenWithNullArguments() {
-      $function = function() {};
       $defer = $this->getMock('Carica\Io\Deferred');
       $defer
         ->expects($this->once())

tests/Carica/Io/DeferredTest.php

      */
     public function testWhenWithOneArgumentThatsNotReferredReturnsResolvedPromise() {
       $result = NULL;
-      $promise = Deferred::when(42)
+      Deferred::when(42)
         ->then(
           function ($argument) use (&$result) {
             $result = $argument;
      */
     public function testWhenWithOneArgumentThatsAnArrayOfCallbacks() {
       $result = array();
-      $promise = Deferred::when(42)
+      Deferred::when(42)
         ->then(
           array(
             function ($argument) use (&$result) {