diff --git a/Behavioral/NullObject/LoggerInterface.php b/Behavioral/NullObject/LoggerInterface.php index 99a28c7..7c1ff7d 100644 --- a/Behavioral/NullObject/LoggerInterface.php +++ b/Behavioral/NullObject/LoggerInterface.php @@ -3,16 +3,9 @@ namespace DesignPatterns\Behavioral\NullObject; /** - * LoggerInterface is a contract for logging something. - * - * Key feature: NullLogger MUST inherit from this interface like any other Loggers + * Key feature: NullLogger must inherit from this interface like any other loggers */ interface LoggerInterface { - /** - * @param string $str - * - * @return mixed - */ - public function log($str); + public function log(string $str); } diff --git a/Behavioral/NullObject/NullLogger.php b/Behavioral/NullObject/NullLogger.php index a4bf469..afddbd6 100644 --- a/Behavioral/NullObject/NullLogger.php +++ b/Behavioral/NullObject/NullLogger.php @@ -2,19 +2,9 @@ namespace DesignPatterns\Behavioral\NullObject; -/** - * Performance concerns : ok there is a call for nothing but we spare an "if is_null" - * I didn't run a benchmark but I think it's equivalent. - * - * Key feature : of course this logger MUST implement the same interface (or abstract) - * like the other loggers. - */ class NullLogger implements LoggerInterface { - /** - * {@inheritdoc} - */ - public function log($str) + public function log(string $str) { // do nothing } diff --git a/Behavioral/NullObject/PrintLogger.php b/Behavioral/NullObject/PrintLogger.php index 371c1ab..6c1d4ba 100644 --- a/Behavioral/NullObject/PrintLogger.php +++ b/Behavioral/NullObject/PrintLogger.php @@ -2,15 +2,9 @@ namespace DesignPatterns\Behavioral\NullObject; -/** - * PrintLogger is a logger that prints the log entry to standard output. - */ class PrintLogger implements LoggerInterface { - /** - * @param string $str - */ - public function log($str) + public function log(string $str) { echo $str; } diff --git a/Behavioral/NullObject/Service.php b/Behavioral/NullObject/Service.php index 56a3847..81baf8f 100644 --- a/Behavioral/NullObject/Service.php +++ b/Behavioral/NullObject/Service.php @@ -2,24 +2,19 @@ namespace DesignPatterns\Behavioral\NullObject; -/** - * Service is dummy service that uses a logger. - */ class Service { /** * @var LoggerInterface */ - protected $logger; + private $logger; /** - * we inject the logger in ctor and it is mandatory. - * - * @param LoggerInterface $log + * @param LoggerInterface $logger */ - public function __construct(LoggerInterface $log) + public function __construct(LoggerInterface $logger) { - $this->logger = $log; + $this->logger = $logger; } /** @@ -27,8 +22,7 @@ class Service */ public function doSomething() { - // no more check "if (!is_null($this->logger))..." with the NullObject pattern + // notice here that you don't have to check if the logger is set with eg. is_null(), instead just use it $this->logger->log('We are in '.__METHOD__); - // something to do... } } diff --git a/Behavioral/NullObject/Tests/LoggerTest.php b/Behavioral/NullObject/Tests/LoggerTest.php index 034b577..0e35aa5 100644 --- a/Behavioral/NullObject/Tests/LoggerTest.php +++ b/Behavioral/NullObject/Tests/LoggerTest.php @@ -6,17 +6,12 @@ use DesignPatterns\Behavioral\NullObject\NullLogger; use DesignPatterns\Behavioral\NullObject\PrintLogger; use DesignPatterns\Behavioral\NullObject\Service; -/** - * LoggerTest tests for different loggers. - */ class LoggerTest extends \PHPUnit_Framework_TestCase { public function testNullObject() { - // one can use a singleton for NullObjet : I don't think it's a good idea - // because the purpose behind null object is to "avoid special case". $service = new Service(new NullLogger()); - $this->expectOutputString(null); // no output + $this->expectOutputString(null); $service->doSomething(); }