Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
991a10f
Update ignore to ignore vagrant files
eddiejaoude Aug 15, 2015
5df676e
composer phar added to gitignore
eddiejaoude Aug 15, 2015
8f70b11
vagrant & ansible scripts added inc docs in README
eddiejaoude Aug 15, 2015
60a93e2
Link to vagrant docs added to README
eddiejaoude Aug 15, 2015
f158b2c
project path in VM added to README
eddiejaoude Aug 15, 2015
f34535f
Upgraded to PHP7 with ansible
eddiejaoude Aug 15, 2015
ed7d158
Decorator TypeHint test not needed
eddiejaoude Aug 15, 2015
9fc16e8
FactoryMethod updated to PHP7 strict mode
eddiejaoude Aug 15, 2015
75703be
CI config for PHP7 only as not BC
eddiejaoude Aug 15, 2015
3b9097f
StaticFactory updated to PHP7 strict mode
eddiejaoude Aug 16, 2015
10a6ede
Strict type declaration for FactoryMethod Design Pattern
eddiejaoude Aug 16, 2015
5919e86
Strict type declaration for StaticFactory Design Pattern
eddiejaoude Aug 16, 2015
dff806b
Singleton updated to PHP7 strict mode
eddiejaoude Aug 16, 2015
8752405
Updated docblocs for FactoryMethod Design Pattern
eddiejaoude Aug 16, 2015
69ecb73
Factory Method Tests docbloc updated
eddiejaoude Aug 16, 2015
dc38323
Static Factory Design Pattern Docbloc updated
eddiejaoude Aug 16, 2015
579caab
Singleton Docblocs updated
eddiejaoude Aug 16, 2015
4441ceb
Static Factory Test in strict mode
eddiejaoude Aug 16, 2015
a0991d1
Singleton Test in strict mode
eddiejaoude Aug 16, 2015
90ddc4d
Factory Method type confusion fixed in test
eddiejaoude Aug 16, 2015
8bf65ff
Prototype Design Pattern upgraded to PHP7
eddiejaoude Aug 16, 2015
18a3d07
SimpleFactory upgraded to PHP7 strict mode
eddiejaoude Aug 16, 2015
2437040
Removed phpunit output from README
eddiejaoude Aug 18, 2015
313876f
Removed php5-fpm ansible handler
eddiejaoude Aug 18, 2015
2afbb34
Removed commented out code for php5-fpm
eddiejaoude Aug 18, 2015
5397832
Removed Pear & Pecl from Ansible scripts
eddiejaoude Aug 18, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
/vendor/
_build/
*.mo
.vagrant/
phpunit.xml
composer.phar
11 changes: 0 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@ language: php
sudo: false

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

matrix:
allow_failures:
- php: hhvm
- php: 7.0
fast_finish: true

cache:
directories:
Expand Down
6 changes: 4 additions & 2 deletions Creational/FactoryMethod/Bicycle.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
declare(strict_types = 1);

namespace DesignPatterns\Creational\FactoryMethod;

/**
* Bicycle is a bicycle
* Class Bicycle
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type of comments is non sens. Why do you have to write Class Bicycle when is clearly that you define this class 3 lines below ?

* @package DesignPatterns\Creational\FactoryMethod
*/
class Bicycle implements VehicleInterface
{
Expand All @@ -17,7 +19,7 @@ class Bicycle implements VehicleInterface
*
* @param string $rgb
*/
public function setColor($rgb)
public function setColor(string $rgb)
{
$this->color = $rgb;
}
Expand Down
36 changes: 23 additions & 13 deletions Creational/FactoryMethod/FactoryMethod.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
<?php
declare(strict_types = 1);

namespace DesignPatterns\Creational\FactoryMethod;

/**
* class FactoryMethod
* Class FactoryMethod
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too and for every class the same

* @package DesignPatterns\Creational\FactoryMethod
*/
abstract class FactoryMethod
{

/** @var int */
const CHEAP = 1;

/** @var int */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

definitely FAST and CHEAP are constants not variables. I would use @const or no docblock at all

const FAST = 2;

/**
* The children of the class must implement this method
*
* Sometimes this method can be public to get "raw" object
*
* @param string $type a generic type
*
* @return VehicleInterface a new vehicle
*/
abstract protected function createVehicle($type);
/** @var array */
public static $typeTexts = [
1 => 'Cheap',
2 => 'Fast',
];

/**
* Creates a new vehicle
Expand All @@ -29,11 +28,22 @@ abstract protected function createVehicle($type);
*
* @return VehicleInterface a new vehicle
*/
public function create($type)
public function create(int $type) : VehicleInterface
{
$obj = $this->createVehicle($type);
$obj->setColor("#f00");

return $obj;
}

/**
* The children of the class must implement this method
*
* Sometimes this method can be public to get "raw" object
*
* @param int $type a generic type
*
* @return VehicleInterface a new vehicle
*/
abstract protected function createVehicle(int $type) : VehicleInterface;
}
6 changes: 4 additions & 2 deletions Creational/FactoryMethod/Ferrari.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
declare(strict_types = 1);

namespace DesignPatterns\Creational\FactoryMethod;

/**
* Ferrari is a italian car
* Class Ferrari
* @package DesignPatterns\Creational\FactoryMethod
*/
class Ferrari implements VehicleInterface
{
Expand All @@ -15,7 +17,7 @@ class Ferrari implements VehicleInterface
/**
* @param string $rgb
*/
public function setColor($rgb)
public function setColor(string $rgb)
{
$this->color = $rgb;
}
Expand Down
11 changes: 8 additions & 3 deletions Creational/FactoryMethod/GermanFactory.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php
declare(strict_types = 1);

namespace DesignPatterns\Creational\FactoryMethod;

/**
* GermanFactory is a vehicle factory in Germany
* Class GermanFactory
* @package DesignPatterns\Creational\FactoryMethod
*/
class GermanFactory extends FactoryMethod
{
/**
* {@inheritdoc}
*/
protected function createVehicle($type)
protected function createVehicle(int $type) : VehicleInterface
{
switch ($type) {
case parent::CHEAP:
Expand All @@ -25,7 +27,10 @@ protected function createVehicle($type)
return $obj;
break;
default:
throw new \InvalidArgumentException("$type is not a valid vehicle");
throw new \InvalidArgumentException(
'Not a valid vehicle, vehicles allowed: ' .
implode(', ', FactoryMethod::$typeTexts)
);
}
}
}
11 changes: 8 additions & 3 deletions Creational/FactoryMethod/ItalianFactory.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php
declare(strict_types = 1);

namespace DesignPatterns\Creational\FactoryMethod;

/**
* ItalianFactory is vehicle factory in Italy
* Class ItalianFactory
* @package DesignPatterns\Creational\FactoryMethod
*/
class ItalianFactory extends FactoryMethod
{
/**
* {@inheritdoc}
*/
protected function createVehicle($type)
protected function createVehicle(int $type) : VehicleInterface
{
switch ($type) {
case parent::CHEAP:
Expand All @@ -20,7 +22,10 @@ protected function createVehicle($type)
return new Ferrari();
break;
default:
throw new \InvalidArgumentException("$type is not a valid vehicle");
throw new \InvalidArgumentException(
'Not a valid vehicle, vehicles allowed: ' .
implode(', ', FactoryMethod::$typeTexts)
);
}
}
}
6 changes: 4 additions & 2 deletions Creational/FactoryMethod/Porsche.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
declare(strict_types = 1);

namespace DesignPatterns\Creational\FactoryMethod;

/**
* Porsche is a german car
* Class Porsche
* @package DesignPatterns\Creational\FactoryMethod
*/
class Porsche implements VehicleInterface
{
Expand All @@ -15,7 +17,7 @@ class Porsche implements VehicleInterface
/**
* @param string $rgb
*/
public function setColor($rgb)
public function setColor(string $rgb)
{
$this->color = $rgb;
}
Expand Down
40 changes: 25 additions & 15 deletions Creational/FactoryMethod/Tests/FactoryMethodTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types = 1);

namespace DesignPatterns\Creational\FactoryMethod\Tests;

Expand All @@ -7,22 +8,29 @@
use DesignPatterns\Creational\FactoryMethod\ItalianFactory;

/**
* FactoryMethodTest tests the factory method pattern
* Class FactoryMethodTest
* @package DesignPatterns\Creational\FactoryMethod\Tests
*/
class FactoryMethodTest extends \PHPUnit_Framework_TestCase
{

protected $type = array(
/** @var array */
protected $type = [
FactoryMethod::CHEAP,
FactoryMethod::FAST
);
FactoryMethod::FAST,
];

/** @var int */
const UNKNOWN = 123;

/**
* @return array
*/
public function getShop()
{
return array(
array(new GermanFactory()),
array(new ItalianFactory())
);
return [
[new GermanFactory()],
[new ItalianFactory()],
];
}

/**
Expand All @@ -32,19 +40,21 @@ public function testCreation(FactoryMethod $shop)
{
// this test method acts as a client for the factory. We don't care
// about the factory, all we know is it can produce vehicle
foreach ($this->type as $oneType) {
$vehicle = $shop->create($oneType);
$this->assertInstanceOf('DesignPatterns\Creational\FactoryMethod\VehicleInterface', $vehicle);
foreach ($this->type as $type) {
$this->assertInstanceOf(
'DesignPatterns\Creational\FactoryMethod\VehicleInterface',
$shop->create($type)
);
}
}

/**
* @dataProvider getShop
* @dataProvider getShop
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage spaceship is not a valid vehicle
* @expectedExceptionMessage Not a valid vehicle, vehicles allowed: Cheap, Fast
*/
public function testUnknownType(FactoryMethod $shop)
{
$shop->create('spaceship');
$shop->create(self::UNKNOWN);
}
}
6 changes: 4 additions & 2 deletions Creational/FactoryMethod/VehicleInterface.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
declare(strict_types = 1);

namespace DesignPatterns\Creational\FactoryMethod;

/**
* VehicleInterface is a contract for a vehicle
* Interface VehicleInterface
* @package DesignPatterns\Creational\FactoryMethod
*/
interface VehicleInterface
{
Expand All @@ -12,5 +14,5 @@ interface VehicleInterface
*
* @param string $rgb
*/
public function setColor($rgb);
public function setColor(string $rgb);
}
4 changes: 3 additions & 1 deletion Creational/Prototype/BarBookPrototype.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
declare(strict_types = 1);

namespace DesignPatterns\Creational\Prototype;

/**
* Class BarBookPrototype
* @package DesignPatterns\Creational\Prototype
*/
class BarBookPrototype extends BookPrototype
{
Expand All @@ -13,7 +15,7 @@ class BarBookPrototype extends BookPrototype
protected $category = 'Bar';

/**
* empty clone
* Do not allow clone
*/
public function __clone()
{
Expand Down
7 changes: 4 additions & 3 deletions Creational/Prototype/BookPrototype.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
declare(strict_types = 1);

namespace DesignPatterns\Creational\Prototype;

/**
* class BookPrototype
* Class BookPrototype
* @package DesignPatterns\Creational\Prototype
*/
abstract class BookPrototype
{
Expand All @@ -19,14 +21,13 @@ abstract class BookPrototype

/**
* @abstract
* @return void
*/
abstract public function __clone();

/**
* @return string
*/
public function getTitle()
public function getTitle() : string
{
return $this->title;
}
Expand Down
6 changes: 5 additions & 1 deletion Creational/Prototype/FooBookPrototype.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<?php
declare(strict_types = 1);

namespace DesignPatterns\Creational\Prototype;

/**
* Class FooBookPrototype
* @package DesignPatterns\Creational\Prototype
*/
class FooBookPrototype extends BookPrototype
{

/** @var string */
protected $category = 'Foo';

/**
* empty clone
* Do not allow clone
*/
public function __clone()
{
Expand Down
Loading