diff --git a/Creational/Builder/BikeBuilder.php b/Creational/Builder/BikeBuilder.php index f83c5db..66162d4 100644 --- a/Creational/Builder/BikeBuilder.php +++ b/Creational/Builder/BikeBuilder.php @@ -2,52 +2,36 @@ namespace DesignPatterns\Creational\Builder; -/** - * BikeBuilder builds bike. - */ +use DesignPatterns\Creational\Builder\Parts\Vehicle; + class BikeBuilder implements BuilderInterface { /** * @var Parts\Bike */ - protected $bike; + private $bike; - /** - * {@inheritdoc} - */ public function addDoors() { } - /** - * {@inheritdoc} - */ public function addEngine() { $this->bike->setPart('engine', new Parts\Engine()); } - /** - * {@inheritdoc} - */ public function addWheel() { $this->bike->setPart('forwardWheel', new Parts\Wheel()); $this->bike->setPart('rearWheel', new Parts\Wheel()); } - /** - * {@inheritdoc} - */ public function createVehicle() { $this->bike = new Parts\Bike(); } - /** - * {@inheritdoc} - */ - public function getVehicle() + public function getVehicle(): Vehicle { return $this->bike; } diff --git a/Creational/Builder/BuilderInterface.php b/Creational/Builder/BuilderInterface.php index 563162f..3bcf961 100644 --- a/Creational/Builder/BuilderInterface.php +++ b/Creational/Builder/BuilderInterface.php @@ -2,30 +2,17 @@ namespace DesignPatterns\Creational\Builder; +use DesignPatterns\Creational\Builder\Parts\Vehicle; + interface BuilderInterface { - /** - * @return mixed - */ public function createVehicle(); - /** - * @return mixed - */ public function addWheel(); - /** - * @return mixed - */ public function addEngine(); - /** - * @return mixed - */ public function addDoors(); - /** - * @return mixed - */ - public function getVehicle(); + public function getVehicle(): Vehicle; } diff --git a/Creational/Builder/CarBuilder.php b/Creational/Builder/CarBuilder.php index a0693d0..c3ffcd6 100644 --- a/Creational/Builder/CarBuilder.php +++ b/Creational/Builder/CarBuilder.php @@ -2,36 +2,26 @@ namespace DesignPatterns\Creational\Builder; -/** - * CarBuilder builds car. - */ +use DesignPatterns\Creational\Builder\Parts\Vehicle; + class CarBuilder implements BuilderInterface { /** * @var Parts\Car */ - protected $car; + private $car; - /** - * @return void - */ public function addDoors() { - $this->car->setPart('rightdoor', new Parts\Door()); + $this->car->setPart('rightDoor', new Parts\Door()); $this->car->setPart('leftDoor', new Parts\Door()); } - /** - * @return void - */ public function addEngine() { $this->car->setPart('engine', new Parts\Engine()); } - /** - * @return void - */ public function addWheel() { $this->car->setPart('wheelLF', new Parts\Wheel()); @@ -40,18 +30,12 @@ class CarBuilder implements BuilderInterface $this->car->setPart('wheelRR', new Parts\Wheel()); } - /** - * @return void - */ public function createVehicle() { $this->car = new Parts\Car(); } - /** - * @return Parts\Car - */ - public function getVehicle() + public function getVehicle(): Vehicle { return $this->car; } diff --git a/Creational/Builder/Director.php b/Creational/Builder/Director.php index 642cd1b..9925d5a 100644 --- a/Creational/Builder/Director.php +++ b/Creational/Builder/Director.php @@ -2,22 +2,17 @@ namespace DesignPatterns\Creational\Builder; +use DesignPatterns\Creational\Builder\Parts\Vehicle; + /** * Director is part of the builder pattern. It knows the interface of the builder - * and builds a complex object with the help of the builder. + * and builds a complex object with the help of the builder * * You can also inject many builders instead of one to build more complex objects */ class Director { - /** - * The director don't know about concrete part. - * - * @param BuilderInterface $builder - * - * @return Parts\Vehicle - */ - public function build(BuilderInterface $builder) + public function build(BuilderInterface $builder): Vehicle { $builder->createVehicle(); $builder->addDoors(); diff --git a/Creational/Builder/Parts/Bike.php b/Creational/Builder/Parts/Bike.php index e5adbba..3f8ffed 100644 --- a/Creational/Builder/Parts/Bike.php +++ b/Creational/Builder/Parts/Bike.php @@ -2,9 +2,6 @@ namespace DesignPatterns\Creational\Builder\Parts; -/** - * Bike is a bike. - */ class Bike extends Vehicle { } diff --git a/Creational/Builder/Parts/Car.php b/Creational/Builder/Parts/Car.php index e345ea9..53eb0d4 100644 --- a/Creational/Builder/Parts/Car.php +++ b/Creational/Builder/Parts/Car.php @@ -2,9 +2,6 @@ namespace DesignPatterns\Creational\Builder\Parts; -/** - * Car is a car. - */ class Car extends Vehicle { } diff --git a/Creational/Builder/Parts/Vehicle.php b/Creational/Builder/Parts/Vehicle.php index 18c47ba..c7e3c15 100644 --- a/Creational/Builder/Parts/Vehicle.php +++ b/Creational/Builder/Parts/Vehicle.php @@ -2,19 +2,16 @@ namespace DesignPatterns\Creational\Builder\Parts; -/** - * Vehicle class is an abstraction for a vehicle. - */ abstract class Vehicle { /** - * @var array + * @var object[] */ - protected $data; + private $data = []; /** * @param string $key - * @param mixed $value + * @param object $value */ public function setPart($key, $value) { diff --git a/Creational/Builder/Tests/DirectorTest.php b/Creational/Builder/Tests/DirectorTest.php index 9f07b83..41023b4 100644 --- a/Creational/Builder/Tests/DirectorTest.php +++ b/Creational/Builder/Tests/DirectorTest.php @@ -3,39 +3,24 @@ namespace DesignPatterns\Creational\Builder\Tests; 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. - */ class DirectorTest extends \PHPUnit_Framework_TestCase { - protected $director; - - protected function setUp() + public function testCanBuildBike() { - $this->director = new Director(); + $bikeBuilder = new BikeBuilder(); + $newVehicle = (new Director())->build($bikeBuilder); + + $this->assertInstanceOf('DesignPatterns\Creational\Builder\Parts\Bike', $newVehicle); } - public function getBuilder() + public function testCanBuildCar() { - return array( - array(new CarBuilder()), - array(new BikeBuilder()), - ); - } + $carBuilder = new CarBuilder(); + $newVehicle = (new Director())->build($carBuilder); - /** - * Here we test the build process. Notice that the client don't know - * anything about the concrete builder. - * - * @dataProvider getBuilder - */ - public function testBuild(BuilderInterface $builder) - { - $newVehicle = $this->director->build($builder); - $this->assertInstanceOf('DesignPatterns\Creational\Builder\Parts\Vehicle', $newVehicle); + $this->assertInstanceOf('DesignPatterns\Creational\Builder\Parts\Car', $newVehicle); } }