mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 12:10:10 +02:00
Applied fixes from StyleCI
This commit is contained in:
committed by
StyleCI Bot
parent
3663603b80
commit
fe1f144ec3
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
/**
|
||||
* class AbstractFactory
|
||||
* class AbstractFactory.
|
||||
*
|
||||
* Sometimes also known as "Kit" in a GUI libraries.
|
||||
*
|
||||
@@ -20,7 +20,7 @@ namespace DesignPatterns\Creational\AbstractFactory;
|
||||
abstract class AbstractFactory
|
||||
{
|
||||
/**
|
||||
* Creates a text component
|
||||
* Creates a text component.
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
@@ -29,7 +29,7 @@ abstract class AbstractFactory
|
||||
abstract public function createText($content);
|
||||
|
||||
/**
|
||||
* Creates a picture component
|
||||
* Creates a picture component.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $name
|
||||
|
@@ -5,14 +5,14 @@ namespace DesignPatterns\Creational\AbstractFactory\Html;
|
||||
use DesignPatterns\Creational\AbstractFactory\Picture as BasePicture;
|
||||
|
||||
/**
|
||||
* Class Picture
|
||||
* Class Picture.
|
||||
*
|
||||
* Picture is a concrete image for HTML rendering
|
||||
*/
|
||||
class Picture extends BasePicture
|
||||
{
|
||||
/**
|
||||
* some crude rendering from HTML output
|
||||
* some crude rendering from HTML output.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
@@ -5,19 +5,19 @@ namespace DesignPatterns\Creational\AbstractFactory\Html;
|
||||
use DesignPatterns\Creational\AbstractFactory\Text as BaseText;
|
||||
|
||||
/**
|
||||
* Class Text
|
||||
* Class Text.
|
||||
*
|
||||
* Text is a concrete text for HTML rendering
|
||||
*/
|
||||
class Text extends BaseText
|
||||
{
|
||||
/**
|
||||
* some crude rendering from HTML output
|
||||
* some crude rendering from HTML output.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return '<div>' . htmlspecialchars($this->text) . '</div>';
|
||||
return '<div>'.htmlspecialchars($this->text).'</div>';
|
||||
}
|
||||
}
|
||||
|
@@ -3,14 +3,14 @@
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
/**
|
||||
* Class HtmlFactory
|
||||
* Class HtmlFactory.
|
||||
*
|
||||
* HtmlFactory is a concrete factory for HTML component
|
||||
*/
|
||||
class HtmlFactory extends AbstractFactory
|
||||
{
|
||||
/**
|
||||
* Creates a picture component
|
||||
* Creates a picture component.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $name
|
||||
@@ -23,7 +23,7 @@ class HtmlFactory extends AbstractFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a text component
|
||||
* Creates a text component.
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
|
@@ -5,14 +5,14 @@ namespace DesignPatterns\Creational\AbstractFactory\Json;
|
||||
use DesignPatterns\Creational\AbstractFactory\Picture as BasePicture;
|
||||
|
||||
/**
|
||||
* Class Picture
|
||||
* Class Picture.
|
||||
*
|
||||
* Picture is a concrete image for JSON rendering
|
||||
*/
|
||||
class Picture extends BasePicture
|
||||
{
|
||||
/**
|
||||
* some crude rendering from JSON output
|
||||
* some crude rendering from JSON output.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
@@ -5,14 +5,14 @@ namespace DesignPatterns\Creational\AbstractFactory\Json;
|
||||
use DesignPatterns\Creational\AbstractFactory\Text as BaseText;
|
||||
|
||||
/**
|
||||
* Class Text
|
||||
* Class Text.
|
||||
*
|
||||
* Text is a text component with a JSON rendering
|
||||
*/
|
||||
class Text extends BaseText
|
||||
{
|
||||
/**
|
||||
* some crude rendering from JSON output
|
||||
* some crude rendering from JSON output.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
@@ -3,16 +3,15 @@
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
/**
|
||||
* Class JsonFactory
|
||||
* Class JsonFactory.
|
||||
*
|
||||
* JsonFactory is a factory for creating a family of JSON component
|
||||
* (example for ajax)
|
||||
*/
|
||||
class JsonFactory extends AbstractFactory
|
||||
{
|
||||
|
||||
/**
|
||||
* Creates a picture component
|
||||
* Creates a picture component.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $name
|
||||
@@ -25,7 +24,7 @@ class JsonFactory extends AbstractFactory
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a text component
|
||||
* Creates a text component.
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
|
@@ -3,16 +3,15 @@
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
/**
|
||||
* Interface MediaInterface
|
||||
* Interface MediaInterface.
|
||||
*
|
||||
* This contract is not part of the pattern, in general case, each component
|
||||
* are not related
|
||||
*/
|
||||
interface MediaInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* some crude rendering from JSON or html output (depended on concrete class)
|
||||
* some crude rendering from JSON or html output (depended on concrete class).
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
@@ -3,11 +3,10 @@
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
/**
|
||||
* Class Picture
|
||||
* Class Picture.
|
||||
*/
|
||||
abstract class Picture implements MediaInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
@@ -7,7 +7,7 @@ use DesignPatterns\Creational\AbstractFactory\HtmlFactory;
|
||||
use DesignPatterns\Creational\AbstractFactory\JsonFactory;
|
||||
|
||||
/**
|
||||
* AbstractFactoryTest tests concrete factories
|
||||
* AbstractFactoryTest tests concrete factories.
|
||||
*/
|
||||
class AbstractFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
@@ -15,7 +15,7 @@ class AbstractFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
return array(
|
||||
array(new JsonFactory()),
|
||||
array(new HtmlFactory())
|
||||
array(new HtmlFactory()),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ class AbstractFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
$article = array(
|
||||
$factory->createText('Lorem Ipsum'),
|
||||
$factory->createPicture('/image.jpg', 'caption'),
|
||||
$factory->createText('footnotes')
|
||||
$factory->createText('footnotes'),
|
||||
);
|
||||
|
||||
$this->assertContainsOnly('DesignPatterns\Creational\AbstractFactory\MediaInterface', $article);
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
/**
|
||||
* Class Text
|
||||
* Class Text.
|
||||
*/
|
||||
abstract class Text implements MediaInterface
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\Builder;
|
||||
|
||||
/**
|
||||
* BikeBuilder builds bike
|
||||
* BikeBuilder builds bike.
|
||||
*/
|
||||
class BikeBuilder implements BuilderInterface
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\Builder;
|
||||
|
||||
/**
|
||||
* CarBuilder builds car
|
||||
* CarBuilder builds car.
|
||||
*/
|
||||
class CarBuilder implements BuilderInterface
|
||||
{
|
||||
|
@@ -10,9 +10,8 @@ namespace DesignPatterns\Creational\Builder;
|
||||
*/
|
||||
class Director
|
||||
{
|
||||
|
||||
/**
|
||||
* The director don't know about concrete part
|
||||
* The director don't know about concrete part.
|
||||
*
|
||||
* @param BuilderInterface $builder
|
||||
*
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Bike is a bike
|
||||
* Bike is a bike.
|
||||
*/
|
||||
class Bike extends Vehicle
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Car is a car
|
||||
* Car is a car.
|
||||
*/
|
||||
class Car extends Vehicle
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Class Door
|
||||
* Class Door.
|
||||
*/
|
||||
class Door
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Class Engine
|
||||
* Class Engine.
|
||||
*/
|
||||
class Engine
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\Builder\Parts;
|
||||
|
||||
/**
|
||||
* VehicleInterface is a contract for a vehicle
|
||||
* VehicleInterface is a contract for a vehicle.
|
||||
*/
|
||||
abstract class Vehicle
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\Builder\Parts;
|
||||
|
||||
/**
|
||||
* Class Wheel
|
||||
* Class Wheel.
|
||||
*/
|
||||
class Wheel
|
||||
{
|
||||
|
@@ -2,17 +2,16 @@
|
||||
|
||||
namespace DesignPatterns\Creational\Builder\Tests;
|
||||
|
||||
use DesignPatterns\Creational\Builder\Director;
|
||||
use DesignPatterns\Creational\Builder\CarBuilder;
|
||||
use DesignPatterns\Creational\Builder\BikeBuilder;
|
||||
use DesignPatterns\Creational\Builder\BuilderInterface;
|
||||
use DesignPatterns\Creational\Builder\CarBuilder;
|
||||
use DesignPatterns\Creational\Builder\Director;
|
||||
|
||||
/**
|
||||
* DirectorTest tests the builder pattern
|
||||
* DirectorTest tests the builder pattern.
|
||||
*/
|
||||
class DirectorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $director;
|
||||
|
||||
protected function setUp()
|
||||
@@ -24,7 +23,7 @@ class DirectorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
return array(
|
||||
array(new CarBuilder()),
|
||||
array(new BikeBuilder())
|
||||
array(new BikeBuilder()),
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\FactoryMethod;
|
||||
|
||||
/**
|
||||
* Bicycle is a bicycle
|
||||
* Bicycle is a bicycle.
|
||||
*/
|
||||
class Bicycle implements VehicleInterface
|
||||
{
|
||||
@@ -13,7 +13,7 @@ class Bicycle implements VehicleInterface
|
||||
protected $color;
|
||||
|
||||
/**
|
||||
* sets the color of the bicycle
|
||||
* sets the color of the bicycle.
|
||||
*
|
||||
* @param string $rgb
|
||||
*/
|
||||
|
@@ -3,16 +3,15 @@
|
||||
namespace DesignPatterns\Creational\FactoryMethod;
|
||||
|
||||
/**
|
||||
* class FactoryMethod
|
||||
* class FactoryMethod.
|
||||
*/
|
||||
abstract class FactoryMethod
|
||||
{
|
||||
|
||||
const CHEAP = 1;
|
||||
const FAST = 2;
|
||||
|
||||
/**
|
||||
* The children of the class must implement this method
|
||||
* The children of the class must implement this method.
|
||||
*
|
||||
* Sometimes this method can be public to get "raw" object
|
||||
*
|
||||
@@ -23,7 +22,7 @@ abstract class FactoryMethod
|
||||
abstract protected function createVehicle($type);
|
||||
|
||||
/**
|
||||
* Creates a new vehicle
|
||||
* Creates a new vehicle.
|
||||
*
|
||||
* @param int $type
|
||||
*
|
||||
@@ -32,7 +31,7 @@ abstract class FactoryMethod
|
||||
public function create($type)
|
||||
{
|
||||
$obj = $this->createVehicle($type);
|
||||
$obj->setColor("#f00");
|
||||
$obj->setColor('#f00');
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\FactoryMethod;
|
||||
|
||||
/**
|
||||
* Ferrari is a italian car
|
||||
* Ferrari is a italian car.
|
||||
*/
|
||||
class Ferrari implements VehicleInterface
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\FactoryMethod;
|
||||
|
||||
/**
|
||||
* GermanFactory is a vehicle factory in Germany
|
||||
* GermanFactory is a vehicle factory in Germany.
|
||||
*/
|
||||
class GermanFactory extends FactoryMethod
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\FactoryMethod;
|
||||
|
||||
/**
|
||||
* ItalianFactory is vehicle factory in Italy
|
||||
* ItalianFactory is vehicle factory in Italy.
|
||||
*/
|
||||
class ItalianFactory extends FactoryMethod
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\FactoryMethod;
|
||||
|
||||
/**
|
||||
* Porsche is a german car
|
||||
* Porsche is a german car.
|
||||
*/
|
||||
class Porsche implements VehicleInterface
|
||||
{
|
||||
|
@@ -7,21 +7,20 @@ use DesignPatterns\Creational\FactoryMethod\GermanFactory;
|
||||
use DesignPatterns\Creational\FactoryMethod\ItalianFactory;
|
||||
|
||||
/**
|
||||
* FactoryMethodTest tests the factory method pattern
|
||||
* FactoryMethodTest tests the factory method pattern.
|
||||
*/
|
||||
class FactoryMethodTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $type = array(
|
||||
FactoryMethod::CHEAP,
|
||||
FactoryMethod::FAST
|
||||
FactoryMethod::FAST,
|
||||
);
|
||||
|
||||
public function getShop()
|
||||
{
|
||||
return array(
|
||||
array(new GermanFactory()),
|
||||
array(new ItalianFactory())
|
||||
array(new ItalianFactory()),
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -3,12 +3,12 @@
|
||||
namespace DesignPatterns\Creational\FactoryMethod;
|
||||
|
||||
/**
|
||||
* VehicleInterface is a contract for a vehicle
|
||||
* VehicleInterface is a contract for a vehicle.
|
||||
*/
|
||||
interface VehicleInterface
|
||||
{
|
||||
/**
|
||||
* sets the color of the vehicle
|
||||
* sets the color of the vehicle.
|
||||
*
|
||||
* @param string $rgb
|
||||
*/
|
||||
|
@@ -3,24 +3,22 @@
|
||||
namespace DesignPatterns\Creational\Multiton;
|
||||
|
||||
/**
|
||||
* class Multiton
|
||||
* class Multiton.
|
||||
*/
|
||||
class Multiton
|
||||
{
|
||||
/**
|
||||
*
|
||||
* the first instance
|
||||
* the first instance.
|
||||
*/
|
||||
const INSTANCE_1 = '1';
|
||||
|
||||
/**
|
||||
*
|
||||
* the second instance
|
||||
* the second instance.
|
||||
*/
|
||||
const INSTANCE_2 = '2';
|
||||
|
||||
/**
|
||||
* holds the named instances
|
||||
* holds the named instances.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
@@ -28,7 +26,6 @@ class Multiton
|
||||
|
||||
/**
|
||||
* should not be called from outside: private!
|
||||
*
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
@@ -36,7 +33,7 @@ class Multiton
|
||||
|
||||
/**
|
||||
* gets the instance with the given name, e.g. Multiton::INSTANCE_1
|
||||
* uses lazy initialization
|
||||
* uses lazy initialization.
|
||||
*
|
||||
* @param string $instanceName
|
||||
*
|
||||
@@ -52,7 +49,7 @@ class Multiton
|
||||
}
|
||||
|
||||
/**
|
||||
* prevent instance from being cloned
|
||||
* prevent instance from being cloned.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
@@ -61,7 +58,7 @@ class Multiton
|
||||
}
|
||||
|
||||
/**
|
||||
* prevent instance from being unserialized
|
||||
* prevent instance from being unserialized.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
|
@@ -4,7 +4,6 @@ namespace DesignPatterns\Creational\Pool;
|
||||
|
||||
class Pool
|
||||
{
|
||||
|
||||
private $instances = array();
|
||||
private $class;
|
||||
|
||||
|
@@ -4,11 +4,10 @@ namespace DesignPatterns\Creational\Pool;
|
||||
|
||||
class Processor
|
||||
{
|
||||
|
||||
private $pool;
|
||||
private $processing = 0;
|
||||
private $maxProcesses = 3;
|
||||
private $waitingQueue = [];
|
||||
private $waitingQueue = array();
|
||||
|
||||
public function __construct(Pool $pool)
|
||||
{
|
||||
|
@@ -4,7 +4,6 @@ namespace DesignPatterns\Creational\Pool;
|
||||
|
||||
class Worker
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// let's say that constuctor does really expensive work...
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\Prototype;
|
||||
|
||||
/**
|
||||
* Class BarBookPrototype
|
||||
* Class BarBookPrototype.
|
||||
*/
|
||||
class BarBookPrototype extends BookPrototype
|
||||
{
|
||||
@@ -13,7 +13,7 @@ class BarBookPrototype extends BookPrototype
|
||||
protected $category = 'Bar';
|
||||
|
||||
/**
|
||||
* empty clone
|
||||
* empty clone.
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\Prototype;
|
||||
|
||||
/**
|
||||
* class BookPrototype
|
||||
* class BookPrototype.
|
||||
*/
|
||||
abstract class BookPrototype
|
||||
{
|
||||
@@ -19,6 +19,7 @@ abstract class BookPrototype
|
||||
|
||||
/**
|
||||
* @abstract
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function __clone();
|
||||
|
@@ -3,14 +3,14 @@
|
||||
namespace DesignPatterns\Creational\Prototype;
|
||||
|
||||
/**
|
||||
* Class FooBookPrototype
|
||||
* Class FooBookPrototype.
|
||||
*/
|
||||
class FooBookPrototype extends BookPrototype
|
||||
{
|
||||
protected $category = 'Foo';
|
||||
|
||||
/**
|
||||
* empty clone
|
||||
* empty clone.
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
|
@@ -8,10 +8,10 @@ $barPrototype = new BarBookPrototype();
|
||||
// now lets say we need 10,000 books of foo and 5,000 of bar ...
|
||||
for ($i = 0; $i < 10000; $i++) {
|
||||
$book = clone $fooPrototype;
|
||||
$book->setTitle('Foo Book No ' . $i);
|
||||
$book->setTitle('Foo Book No '.$i);
|
||||
}
|
||||
|
||||
for ($i = 0; $i < 5000; $i++) {
|
||||
$book = clone $barPrototype;
|
||||
$book->setTitle('Bar Book No ' . $i);
|
||||
$book->setTitle('Bar Book No '.$i);
|
||||
}
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\SimpleFactory;
|
||||
|
||||
/**
|
||||
* Bicycle is a bicycle
|
||||
* Bicycle is a bicycle.
|
||||
*/
|
||||
class Bicycle implements VehicleInterface
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\SimpleFactory;
|
||||
|
||||
/**
|
||||
* class ConcreteFactory
|
||||
* class ConcreteFactory.
|
||||
*/
|
||||
class ConcreteFactory
|
||||
{
|
||||
@@ -19,18 +19,19 @@ class ConcreteFactory
|
||||
public function __construct()
|
||||
{
|
||||
$this->typeList = array(
|
||||
'bicycle' => __NAMESPACE__ . '\Bicycle',
|
||||
'other' => __NAMESPACE__ . '\Scooter'
|
||||
'bicycle' => __NAMESPACE__.'\Bicycle',
|
||||
'other' => __NAMESPACE__.'\Scooter',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a vehicle
|
||||
* Creates a vehicle.
|
||||
*
|
||||
* @param string $type a known type key
|
||||
*
|
||||
* @return VehicleInterface a new instance of VehicleInterface
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return VehicleInterface a new instance of VehicleInterface
|
||||
*/
|
||||
public function createVehicle($type)
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\SimpleFactory;
|
||||
|
||||
/**
|
||||
* Scooter is a Scooter
|
||||
* Scooter is a Scooter.
|
||||
*/
|
||||
class Scooter implements VehicleInterface
|
||||
{
|
||||
|
@@ -5,11 +5,10 @@ namespace DesignPatterns\Creational\SimpleFactory\Tests;
|
||||
use DesignPatterns\Creational\SimpleFactory\ConcreteFactory;
|
||||
|
||||
/**
|
||||
* SimpleFactoryTest tests the Simple Factory pattern
|
||||
* SimpleFactoryTest tests the Simple Factory pattern.
|
||||
*/
|
||||
class SimpleFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $factory;
|
||||
|
||||
protected function setUp()
|
||||
@@ -21,7 +20,7 @@ class SimpleFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
return array(
|
||||
array('bicycle'),
|
||||
array('other')
|
||||
array('other'),
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\SimpleFactory;
|
||||
|
||||
/**
|
||||
* VehicleInterface is a contract for a vehicle
|
||||
* VehicleInterface is a contract for a vehicle.
|
||||
*/
|
||||
interface VehicleInterface
|
||||
{
|
||||
|
@@ -2,10 +2,8 @@
|
||||
|
||||
namespace DesignPatterns\Creational\Singleton;
|
||||
|
||||
use DesignPatterns\Creational\Singleton\SingletonPatternViolationException;
|
||||
|
||||
/**
|
||||
* class Singleton
|
||||
* class Singleton.
|
||||
*/
|
||||
class Singleton
|
||||
{
|
||||
@@ -13,16 +11,16 @@ class Singleton
|
||||
* @var Singleton reference to singleton instance
|
||||
*/
|
||||
private static $instance;
|
||||
|
||||
|
||||
/**
|
||||
* gets the instance via lazy initialization (created on first usage)
|
||||
* gets the instance via lazy initialization (created on first usage).
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (null === static::$instance) {
|
||||
static::$instance = new static;
|
||||
static::$instance = new static();
|
||||
}
|
||||
|
||||
return static::$instance;
|
||||
@@ -30,28 +28,31 @@ class Singleton
|
||||
|
||||
/**
|
||||
* is not allowed to call from outside: private!
|
||||
*
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* prevent the instance from being cloned
|
||||
* prevent the instance from being cloned.
|
||||
*
|
||||
* @throws SingletonPatternViolationException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public final function __clone()
|
||||
final public function __clone()
|
||||
{
|
||||
throw new SingletonPatternViolationException('This is a Singleton. Clone is forbidden');
|
||||
}
|
||||
|
||||
/**
|
||||
* prevent from being unserialized
|
||||
* prevent from being unserialized.
|
||||
*
|
||||
* @throws SingletonPatternViolationException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public final function __wakeup()
|
||||
final public function __wakeup()
|
||||
{
|
||||
throw new SingletonPatternViolationException('This is a Singleton. __wakeup usage is forbidden');
|
||||
}
|
||||
|
@@ -4,5 +4,4 @@ namespace DesignPatterns\Creational\Singleton;
|
||||
|
||||
class SingletonPatternViolationException extends \Exception
|
||||
{
|
||||
|
||||
}
|
||||
|
@@ -5,11 +5,10 @@ namespace DesignPatterns\Creational\Singleton\Tests;
|
||||
use DesignPatterns\Creational\Singleton\Singleton;
|
||||
|
||||
/**
|
||||
* SingletonTest tests the singleton pattern
|
||||
* SingletonTest tests the singleton pattern.
|
||||
*/
|
||||
class SingletonTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testUniqueness()
|
||||
{
|
||||
$firstCall = Singleton::getInstance();
|
||||
@@ -29,6 +28,7 @@ class SingletonTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
/**
|
||||
* @expectedException \DesignPatterns\Creational\Singleton\SingletonPatternViolationException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNoCloneAllowed()
|
||||
@@ -39,6 +39,7 @@ class SingletonTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
/**
|
||||
* @expectedException \DesignPatterns\Creational\Singleton\SingletonPatternViolationException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testNoSerializationAllowed()
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\StaticFactory;
|
||||
|
||||
/**
|
||||
* Class FormatNumber
|
||||
* Class FormatNumber.
|
||||
*/
|
||||
class FormatNumber implements FormatterInterface
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\StaticFactory;
|
||||
|
||||
/**
|
||||
* Class FormatString
|
||||
* Class FormatString.
|
||||
*/
|
||||
class FormatString implements FormatterInterface
|
||||
{
|
||||
|
@@ -3,7 +3,7 @@
|
||||
namespace DesignPatterns\Creational\StaticFactory;
|
||||
|
||||
/**
|
||||
* Class FormatterInterface
|
||||
* Class FormatterInterface.
|
||||
*/
|
||||
interface FormatterInterface
|
||||
{
|
||||
|
@@ -4,23 +4,24 @@ namespace DesignPatterns\Creational\StaticFactory;
|
||||
|
||||
/**
|
||||
* Note1: Remember, static => global => evil
|
||||
* Note2: Cannot be subclassed or mock-upped or have multiple different instances
|
||||
* Note2: Cannot be subclassed or mock-upped or have multiple different instances.
|
||||
*/
|
||||
class StaticFactory
|
||||
{
|
||||
/**
|
||||
* the parametrized function to get create an instance
|
||||
* the parametrized function to get create an instance.
|
||||
*
|
||||
* @param string $type
|
||||
*
|
||||
* @static
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
* @return FormatterInterface
|
||||
*/
|
||||
public static function factory($type)
|
||||
{
|
||||
$className = __NAMESPACE__ . '\Format' . ucfirst($type);
|
||||
$className = __NAMESPACE__.'\Format'.ucfirst($type);
|
||||
|
||||
if (!class_exists($className)) {
|
||||
throw new \InvalidArgumentException('Missing format class.');
|
||||
|
@@ -5,17 +5,15 @@ namespace DesignPatterns\Creational\StaticFactory\Tests;
|
||||
use DesignPatterns\Creational\StaticFactory\StaticFactory;
|
||||
|
||||
/**
|
||||
* Tests for Static Factory pattern
|
||||
*
|
||||
* Tests for Static Factory pattern.
|
||||
*/
|
||||
class StaticFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function getTypeList()
|
||||
{
|
||||
return array(
|
||||
array('string'),
|
||||
array('number')
|
||||
array('number'),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -33,6 +31,6 @@ class StaticFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testException()
|
||||
{
|
||||
StaticFactory::factory("");
|
||||
StaticFactory::factory('');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user