From 644d9cbd49c602e95fea3fef49103aeeb17a0bcf Mon Sep 17 00:00:00 2001 From: Dominik Liebler Date: Fri, 13 Sep 2013 12:00:39 +0200 Subject: [PATCH] cs --- NullObject/LoggerInterface.php | 14 ++++++------- NullObject/NullLogger.php | 11 ++++------ NullObject/PrintLogger.php | 11 ++++------ NullObject/Service.php | 20 +++++++++++------- Observer/User.php | 35 ++++++++++++++++++------------- Observer/UserObserver.php | 2 -- Proxy/Record.php | 10 ++++----- Proxy/RecordProxy.php | 2 +- Registry/Registry.php | 26 +++++++++++++++-------- SimpleFactory/Bicycle.php | 15 +++++++------ SimpleFactory/ConcreteFactory.php | 12 +++++------ Tests/Observer/ObserverTest.php | 6 +++--- 12 files changed, 86 insertions(+), 78 deletions(-) diff --git a/NullObject/LoggerInterface.php b/NullObject/LoggerInterface.php index 2cb19d0..9acf855 100644 --- a/NullObject/LoggerInterface.php +++ b/NullObject/LoggerInterface.php @@ -1,18 +1,18 @@ logger = $log; } + /** + * do something ... + */ public function doSomething() { // no more check "if (!is_null($this->logger))..." with the NullObject pattern $this->logger->log('We are in ' . __METHOD__); // something to do... } - -} \ No newline at end of file +} diff --git a/Observer/User.php b/Observer/User.php index 3b41867..3ee3fba 100644 --- a/Observer/User.php +++ b/Observer/User.php @@ -10,49 +10,57 @@ namespace DesignPatterns\Observer; */ class User implements \SplSubject { - - protected $_data = array(); - /** + * user data + * * @var array */ - protected $_observers = array(); + protected $data = array(); + + /** + * observers + * + * @var array + */ + protected $observers = array(); /** * attach a new observer * * @param \SplObserver $observer + * * @return void */ public function attach(\SplObserver $observer) { - $this->_observers[] = $observer; + $this->observers[] = $observer; } /** * detach an observer * * @param \SplObserver $observer + * * @return void */ public function detach(\SplObserver $observer) { - $index = array_search($observer, $this->_observers); + $index = array_search($observer, $this->observers); if (false !== $index) { - unset($this->_observers[$index]); + unset($this->observers[$index]); } } /** - * + * notify observers * * @return void */ public function notify() { /** @var SplObserver $observer */ - foreach ($this->_observers as $observer) { + foreach ($this->observers as $observer) { $observer->update($this); } } @@ -61,17 +69,16 @@ class User implements \SplSubject * Ideally one would better write setter/getter for all valid attributes and only call notify() * on attributes that matter when changed * - * @param $name - * @param $value + * @param string $name + * @param mixed $value + * * @return void */ public function __set($name, $value) { - $this->_data[$name] = $value; + $this->data[$name] = $value; // notify the observers, that user has been updated $this->notify(); } - } - diff --git a/Observer/UserObserver.php b/Observer/UserObserver.php index 3cb8b9a..549da20 100644 --- a/Observer/UserObserver.php +++ b/Observer/UserObserver.php @@ -17,7 +17,6 @@ namespace DesignPatterns\Observer; */ class UserObserver implements \SplObserver { - /** * This is the only method to implement as an observer. * It is called by the Subject (usually by SplSubject::notify() ) @@ -28,5 +27,4 @@ class UserObserver implements \SplObserver { echo get_class($subject) . ' has been updated'; } - } diff --git a/Proxy/Record.php b/Proxy/Record.php index 194908e..722d946 100644 --- a/Proxy/Record.php +++ b/Proxy/Record.php @@ -18,14 +18,14 @@ class Record /** * @var array|null */ - protected $_data; + protected $data; /** * @param null $data */ public function __construct($data = null) { - $this->_data = (array) $data; + $this->data = (array) $data; } /** @@ -38,7 +38,7 @@ class Record */ public function __set($name, $value) { - $this->_data[(string) $name] = $value; + $this->data[(string) $name] = $value; } /** @@ -50,8 +50,8 @@ class Record */ public function __get($name) { - if (array_key_exists($name, $this->_data)) { - return $this->_data[(string) $name]; + if (array_key_exists($name, $this->data)) { + return $this->data[(string) $name]; } else { return null; } diff --git a/Proxy/RecordProxy.php b/Proxy/RecordProxy.php index 00cc18c..63f7904 100644 --- a/Proxy/RecordProxy.php +++ b/Proxy/RecordProxy.php @@ -47,4 +47,4 @@ class RecordProxy extends Record $this->isDirty = true; parent::__set($name, $value); } -} \ No newline at end of file +} diff --git a/Registry/Registry.php b/Registry/Registry.php index ea96491..65509c0 100644 --- a/Registry/Registry.php +++ b/Registry/Registry.php @@ -14,39 +14,47 @@ namespace DesignPatterns; * - Yii Framework: CWebApplication holds all the application components, such as CWebUser, CUrlManager, etc. * */ - abstract class Registry { const LOGGER = 'logger'; - protected static $_storedValues; + /** + * @var array + */ + protected static $storedValues = array(); /** - * @static + * sets a value + * * @param string $key - * @param mixed $value + * @param mixed $value + * + * @static * @return void */ public static function set($key, $value) { - self::$_storedValues[$key] = $value; + self::$storedValues[$key] = $value; } /** - * @static + * gets a value from the registry + * * @param string $key + * + * @static * @return mixed */ public static function get($key) { - return self::$_storedValues[$key]; + return self::$storedValues[$key]; } // typically there would be methods to check if a key has already been registered and so on ... } -// while bootstraping the application +// while bootstrapping the application Registry::set(Registry::LOGGER, new \StdClass()); // throughout the application -Registry::get(Registry::LOGGER)->log('foo'); \ No newline at end of file +Registry::get(Registry::LOGGER)->log('foo'); diff --git a/SimpleFactory/Bicycle.php b/SimpleFactory/Bicycle.php index 50f1415..fbe605d 100644 --- a/SimpleFactory/Bicycle.php +++ b/SimpleFactory/Bicycle.php @@ -1,9 +1,5 @@ assertAttributeEmpty('_observers', $subject); + $this->assertAttributeEmpty('observers', $subject); $subject->attach($this->observer); - $this->assertAttributeNotEmpty('_observers', $subject); + $this->assertAttributeNotEmpty('observers', $subject); $subject->detach($this->observer); - $this->assertAttributeEmpty('_observers', $subject); + $this->assertAttributeEmpty('observers', $subject); } /**