Browse Source

Add some return type hints and fix up HandlerWrapper

Jordi Boggiano 10 years ago
parent
commit
3d30ba5ecc

+ 2 - 1
src/Monolog/Handler/AbstractSyslogHandler.php

@@ -12,6 +12,7 @@
 namespace Monolog\Handler;
 
 use Monolog\Logger;
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\LineFormatter;
 
 /**
@@ -94,7 +95,7 @@ abstract class AbstractSyslogHandler extends AbstractProcessingHandler
     /**
      * {@inheritdoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new LineFormatter('%channel%.%level_name%: %message% %context% %extra%');
     }

+ 2 - 1
src/Monolog/Handler/AmqpHandler.php

@@ -12,6 +12,7 @@
 namespace Monolog\Handler;
 
 use Monolog\Logger;
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\JsonFormatter;
 use PhpAmqpLib\Message\AMQPMessage;
 use PhpAmqpLib\Channel\AMQPChannel;
@@ -140,7 +141,7 @@ class AmqpHandler extends AbstractProcessingHandler
     /**
      * {@inheritDoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
     }

+ 2 - 1
src/Monolog/Handler/BrowserConsoleHandler.php

@@ -12,6 +12,7 @@
 namespace Monolog\Handler;
 
 use Monolog\Formatter\LineFormatter;
+use Monolog\Formatter\FormatterInterface;
 
 /**
  * Handler sending logs to browser's javascript console with no browser extension required
@@ -32,7 +33,7 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
      *
      *     You can do [[blue text]]{color: blue} or [[green background]]{background-color: green; color: white}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new LineFormatter('[[%channel%]]{macro: autolabel} [[%level_name%]]{font-weight: bold} %message%');
     }

+ 2 - 1
src/Monolog/Handler/ChromePHPHandler.php

@@ -12,6 +12,7 @@
 namespace Monolog\Handler;
 
 use Monolog\Formatter\ChromePHPFormatter;
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Logger;
 
 /**
@@ -88,7 +89,7 @@ class ChromePHPHandler extends AbstractProcessingHandler
     /**
      * {@inheritDoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new ChromePHPFormatter();
     }

+ 2 - 1
src/Monolog/Handler/CouchDBHandler.php

@@ -11,6 +11,7 @@
 
 namespace Monolog\Handler;
 
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\JsonFormatter;
 use Monolog\Logger;
 
@@ -65,7 +66,7 @@ class CouchDBHandler extends AbstractProcessingHandler
     /**
      * {@inheritDoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
     }

+ 2 - 1
src/Monolog/Handler/DoctrineCouchDBHandler.php

@@ -13,6 +13,7 @@ namespace Monolog\Handler;
 
 use Monolog\Logger;
 use Monolog\Formatter\NormalizerFormatter;
+use Monolog\Formatter\FormatterInterface;
 use Doctrine\CouchDB\CouchDBClient;
 
 /**
@@ -38,7 +39,7 @@ class DoctrineCouchDBHandler extends AbstractProcessingHandler
         $this->client->postDocument($record['formatted']);
     }
 
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new NormalizerFormatter;
     }

+ 2 - 1
src/Monolog/Handler/DynamoDbHandler.php

@@ -13,6 +13,7 @@ namespace Monolog\Handler;
 
 use Aws\Common\Aws;
 use Aws\DynamoDb\DynamoDbClient;
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\ScalarFormatter;
 use Monolog\Logger;
 
@@ -82,7 +83,7 @@ class DynamoDbHandler extends AbstractProcessingHandler
     /**
      * {@inheritdoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new ScalarFormatter(self::DATE_FORMAT);
     }

+ 2 - 2
src/Monolog/Handler/ElasticSearchHandler.php

@@ -76,7 +76,7 @@ class ElasticSearchHandler extends AbstractProcessingHandler
     /**
      * {@inheritdoc}
      */
-    public function setFormatter(FormatterInterface $formatter)
+    public function setFormatter(FormatterInterface $formatter): HandlerInterface
     {
         if ($formatter instanceof ElasticaFormatter) {
             return parent::setFormatter($formatter);
@@ -96,7 +96,7 @@ class ElasticSearchHandler extends AbstractProcessingHandler
     /**
      * {@inheritDoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new ElasticaFormatter($this->options['index'], $this->options['type']);
     }

+ 2 - 1
src/Monolog/Handler/ErrorLogHandler.php

@@ -12,6 +12,7 @@
 namespace Monolog\Handler;
 
 use Monolog\Formatter\LineFormatter;
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Logger;
 
 /**
@@ -60,7 +61,7 @@ class ErrorLogHandler extends AbstractProcessingHandler
     /**
      * {@inheritDoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new LineFormatter('[%datetime%] %channel%.%level_name%: %message% %context% %extra%');
     }

+ 2 - 1
src/Monolog/Handler/FirePHPHandler.php

@@ -12,6 +12,7 @@
 namespace Monolog\Handler;
 
 use Monolog\Formatter\WildfireFormatter;
+use Monolog\Formatter\FormatterInterface;
 
 /**
  * Simple FirePHP Handler (http://www.firephp.org/), which uses the Wildfire protocol.
@@ -87,7 +88,7 @@ class FirePHPHandler extends AbstractProcessingHandler
     /**
      * {@inheritDoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new WildfireFormatter();
     }

+ 2 - 1
src/Monolog/Handler/FleepHookHandler.php

@@ -11,6 +11,7 @@
 
 namespace Monolog\Handler;
 
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\LineFormatter;
 use Monolog\Logger;
 
@@ -63,7 +64,7 @@ class FleepHookHandler extends SocketHandler
      *
      * @return LineFormatter
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new LineFormatter(null, null, true, true);
     }

+ 2 - 2
src/Monolog/Handler/FlowdockHandler.php

@@ -53,7 +53,7 @@ class FlowdockHandler extends SocketHandler
     /**
      * {@inheritdoc}
      */
-    public function setFormatter(FormatterInterface $formatter)
+    public function setFormatter(FormatterInterface $formatter): HandlerInterface
     {
         if (!$formatter instanceof FlowdockFormatter) {
             throw new \InvalidArgumentException('The FlowdockHandler requires an instance of Monolog\Formatter\FlowdockFormatter to function correctly');
@@ -67,7 +67,7 @@ class FlowdockHandler extends SocketHandler
      *
      * @return FormatterInterface
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         throw new \InvalidArgumentException('The FlowdockHandler must be configured (via setFormatter) with an instance of Monolog\Formatter\FlowdockFormatter to function correctly');
     }

+ 3 - 3
src/Monolog/Handler/FormattableHandlerInterface.php

@@ -24,14 +24,14 @@ interface FormattableHandlerInterface
      * Sets the formatter.
      *
      * @param  FormatterInterface $formatter
-     * @return self
+     * @return HandlerInterface self
      */
-    public function setFormatter(FormatterInterface $formatter);
+    public function setFormatter(FormatterInterface $formatter): HandlerInterface;
 
     /**
      * Gets the formatter.
      *
      * @return FormatterInterface
      */
-    public function getFormatter();
+    public function getFormatter(): FormatterInterface;
 }

+ 3 - 3
src/Monolog/Handler/FormattableHandlerTrait.php

@@ -30,7 +30,7 @@ trait FormattableHandlerTrait
     /**
      * {@inheritdoc}
      */
-    public function setFormatter(FormatterInterface $formatter)
+    public function setFormatter(FormatterInterface $formatter): HandlerInterface
     {
         $this->formatter = $formatter;
 
@@ -40,7 +40,7 @@ trait FormattableHandlerTrait
     /**
      * {@inheritdoc}
      */
-    public function getFormatter()
+    public function getFormatter(): FormatterInterface
     {
         if (!$this->formatter) {
             $this->formatter = $this->getDefaultFormatter();
@@ -54,7 +54,7 @@ trait FormattableHandlerTrait
      *
      * @return FormatterInterface
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new LineFormatter();
     }

+ 2 - 1
src/Monolog/Handler/GelfHandler.php

@@ -17,6 +17,7 @@ use Gelf\Publisher;
 use InvalidArgumentException;
 use Monolog\Logger;
 use Monolog\Formatter\GelfMessageFormatter;
+use Monolog\Formatter\FormatterInterface;
 
 /**
  * Handler to send messages to a Graylog2 (http://www.graylog2.org) server
@@ -66,7 +67,7 @@ class GelfHandler extends AbstractProcessingHandler
     /**
      * {@inheritDoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new GelfMessageFormatter();
     }

+ 38 - 14
src/Monolog/Handler/HandlerWrapper.php

@@ -28,7 +28,7 @@ use Monolog\Formatter\FormatterInterface;
  *
  * @author Alexey Karapetov <alexey@karapetov.com>
  */
-class HandlerWrapper implements HandlerInterface
+class HandlerWrapper implements HandlerInterface, ProcessableHandlerInterface, FormattableHandlerInterface
 {
     /**
      * @var HandlerInterface
@@ -47,7 +47,7 @@ class HandlerWrapper implements HandlerInterface
     /**
      * {@inheritdoc}
      */
-    public function isHandling(array $record)
+    public function isHandling(array $record): bool
     {
         return $this->handler->isHandling($record);
     }
@@ -55,7 +55,7 @@ class HandlerWrapper implements HandlerInterface
     /**
      * {@inheritdoc}
      */
-    public function handle(array $record)
+    public function handle(array $record): bool
     {
         return $this->handler->handle($record);
     }
@@ -71,34 +71,58 @@ class HandlerWrapper implements HandlerInterface
     /**
      * {@inheritdoc}
      */
-    public function pushProcessor($callback)
+    public function close()
     {
-        $this->handler->pushProcessor($callback);
-        return $this;
+        return $this->handler->close();
     }
 
     /**
      * {@inheritdoc}
      */
-    public function popProcessor()
+    public function pushProcessor(callable $callback): HandlerInterface
     {
-        return $this->handler->popProcessor();
+        if ($this->handler instanceof ProcessableHandlerInterface) {
+            $this->handler->pushProcessor($callback);
+
+            return $this;
+        }
+
+        throw new \LogicException('The wrapped handler does not implement ' . ProcessableHandlerInterface::class);
     }
 
     /**
      * {@inheritdoc}
      */
-    public function setFormatter(FormatterInterface $formatter)
+    public function popProcessor(): callable
     {
-        $this->handler->setFormatter($formatter);
-        return $this;
+        if ($this->handler instanceof ProcessableHandlerInterface) {
+            return $this->handler->popProcessor();
+        }
+
+        throw new \LogicException('The wrapped handler does not implement ' . ProcessableHandlerInterface::class);
     }
 
     /**
      * {@inheritdoc}
      */
-    public function getFormatter()
+    public function setFormatter(FormatterInterface $formatter): HandlerInterface
     {
-        return $this->handler->getFormatter();
+        if ($this->handler instanceof FormattableHandlerInterface) {
+            $this->handler->setFormatter($formatter);
+        }
+
+        throw new \LogicException('The wrapped handler does not implement ' . FormattableHandlerInterface::class);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getFormatter(): FormatterInterface
+    {
+        if ($this->handler instanceof FormattableHandlerInterface) {
+            return $this->handler->getFormatter($formatter);
+        }
+
+        throw new \LogicException('The wrapped handler does not implement ' . FormattableHandlerInterface::class);
     }
-}
+}

+ 2 - 1
src/Monolog/Handler/LogglyHandler.php

@@ -12,6 +12,7 @@
 namespace Monolog\Handler;
 
 use Monolog\Logger;
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\LogglyFormatter;
 
 /**
@@ -95,7 +96,7 @@ class LogglyHandler extends AbstractProcessingHandler
         Curl\Util::execute($ch);
     }
 
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new LogglyFormatter();
     }

+ 2 - 1
src/Monolog/Handler/MongoDBHandler.php

@@ -15,6 +15,7 @@ use MongoDB\Driver\BulkWrite;
 use MongoDB\Driver\Manager;
 use MongoDB\Client;
 use Monolog\Logger;
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\NormalizerFormatter;
 
 /**
@@ -77,7 +78,7 @@ class MongoDBHandler extends AbstractProcessingHandler
     /**
      * {@inheritDoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new NormalizerFormatter;
     }

+ 2 - 1
src/Monolog/Handler/NewRelicHandler.php

@@ -13,6 +13,7 @@ namespace Monolog\Handler;
 
 use Monolog\Logger;
 use Monolog\Formatter\NormalizerFormatter;
+use Monolog\Formatter\FormatterInterface;
 
 /**
  * Class to record a log on a NewRelic application.
@@ -191,7 +192,7 @@ class NewRelicHandler extends AbstractProcessingHandler
     /**
      * {@inheritDoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new NormalizerFormatter();
     }

+ 2 - 1
src/Monolog/Handler/PHPConsoleHandler.php

@@ -13,6 +13,7 @@ namespace Monolog\Handler;
 
 use Exception;
 use Monolog\Formatter\LineFormatter;
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Logger;
 use PhpConsole\Connector;
 use PhpConsole\Handler as VendorPhpConsoleHandler;
@@ -235,7 +236,7 @@ class PHPConsoleHandler extends AbstractProcessingHandler
     /**
      * {@inheritDoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new LineFormatter('%message%');
     }

+ 4 - 3
src/Monolog/Handler/ProcessableHandlerInterface.php

@@ -22,14 +22,15 @@ interface ProcessableHandlerInterface
      * Adds a processor in the stack.
      *
      * @param  callable $callback
-     * @return self
+     * @return HandlerInterface self
      */
-    public function pushProcessor(callable $callback);
+    public function pushProcessor(callable $callback): HandlerInterface;
 
     /**
      * Removes the processor on top of the stack and returns it.
      *
      * @return callable
+     * @throws LogicException In case the processor stack is empty
      */
-    public function popProcessor();
+    public function popProcessor(): callable;
 }

+ 2 - 2
src/Monolog/Handler/ProcessableHandlerTrait.php

@@ -28,7 +28,7 @@ trait ProcessableHandlerTrait
     /**
      * {@inheritdoc}
      */
-    public function pushProcessor(callable $callback)
+    public function pushProcessor(callable $callback): HandlerInterface
     {
         if (!is_callable($callback)) {
             throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
@@ -41,7 +41,7 @@ trait ProcessableHandlerTrait
     /**
      * {@inheritdoc}
      */
-    public function popProcessor()
+    public function popProcessor(): callable
     {
         if (!$this->processors) {
             throw new \LogicException('You tried to pop from an empty processor stack.');

+ 1 - 1
src/Monolog/Handler/RavenHandler.php

@@ -194,7 +194,7 @@ class RavenHandler extends AbstractProcessingHandler
     /**
      * {@inheritDoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new LineFormatter('[%channel%] %message%');
     }

+ 2 - 1
src/Monolog/Handler/RedisHandler.php

@@ -12,6 +12,7 @@
 namespace Monolog\Handler;
 
 use Monolog\Formatter\LineFormatter;
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Logger;
 
 /**
@@ -90,7 +91,7 @@ class RedisHandler extends AbstractProcessingHandler
     /**
      * {@inheritDoc}
      */
-    protected function getDefaultFormatter()
+    protected function getDefaultFormatter(): FormatterInterface
     {
         return new LineFormatter();
     }

+ 2 - 1
src/Monolog/Handler/ZendMonitorHandler.php

@@ -10,6 +10,7 @@
 
 namespace Monolog\Handler;
 
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\NormalizerFormatter;
 use Monolog\Logger;
 
@@ -78,7 +79,7 @@ class ZendMonitorHandler extends AbstractProcessingHandler
     /**
      * {@inheritdoc}
      */
-    public function getDefaultFormatter()
+    public function getDefaultFormatter(): FormatterInterface
     {
         return new NormalizerFormatter();
     }

+ 0 - 40
tests/Monolog/Handler/HandlerWrapperTest.php

@@ -87,44 +87,4 @@ class HandlerWrapperTest extends TestCase
 
         $this->assertEquals($result, $this->wrapper->handleBatch($records));
     }
-
-    public function testPushProcessor()
-    {
-        $processor = function () {};
-        $this->handler->expects($this->once())
-            ->method('pushProcessor')
-            ->with($processor);
-
-        $this->assertEquals($this->wrapper, $this->wrapper->pushProcessor($processor));
-    }
-
-    public function testPopProcessor()
-    {
-        $processor = function () {};
-        $this->handler->expects($this->once())
-            ->method('popProcessor')
-            ->willReturn($processor);
-
-        $this->assertEquals($processor, $this->wrapper->popProcessor());
-    }
-
-    public function testSetFormatter()
-    {
-        $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
-        $this->handler->expects($this->once())
-            ->method('setFormatter')
-            ->with($formatter);
-
-        $this->assertEquals($this->wrapper, $this->wrapper->setFormatter($formatter));
-    }
-
-    public function testGetFormatter()
-    {
-        $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
-        $this->handler->expects($this->once())
-            ->method('getFormatter')
-            ->willReturn($formatter);
-
-        $this->assertEquals($formatter, $this->wrapper->getFormatter());
-    }
 }