mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-09 00:16:32 +02:00
update deps & install rector
This commit is contained in:
@@ -8,14 +8,8 @@ namespace DesignPatterns\Structural\Adapter;
|
||||
*/
|
||||
class EBookAdapter implements Book
|
||||
{
|
||||
/**
|
||||
* @var EBook
|
||||
*/
|
||||
protected $eBook;
|
||||
protected EBook $eBook;
|
||||
|
||||
/**
|
||||
* @param EBook $eBook
|
||||
*/
|
||||
public function __construct(EBook $eBook)
|
||||
{
|
||||
$this->eBook = $eBook;
|
||||
@@ -37,8 +31,6 @@ class EBookAdapter implements Book
|
||||
/**
|
||||
* notice the adapted behavior here: EBook::getPage() will return two integers, but Book
|
||||
* supports only a current page getter, so we adapt the behavior here
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getPage(): int
|
||||
{
|
||||
|
@@ -8,15 +8,8 @@ namespace DesignPatterns\Structural\Adapter;
|
||||
*/
|
||||
class Kindle implements EBook
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $page = 1;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $totalPages = 100;
|
||||
private int $page = 1;
|
||||
private int $totalPages = 100;
|
||||
|
||||
public function pressNext()
|
||||
{
|
||||
|
@@ -4,10 +4,7 @@ namespace DesignPatterns\Structural\Adapter;
|
||||
|
||||
class PaperBook implements Book
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $page;
|
||||
private int $page;
|
||||
|
||||
public function open()
|
||||
{
|
||||
|
@@ -4,22 +4,13 @@ namespace DesignPatterns\Structural\Bridge;
|
||||
|
||||
abstract class Service
|
||||
{
|
||||
/**
|
||||
* @var Formatter
|
||||
*/
|
||||
protected $implementation;
|
||||
protected Formatter $implementation;
|
||||
|
||||
/**
|
||||
* @param Formatter $printer
|
||||
*/
|
||||
public function __construct(Formatter $printer)
|
||||
{
|
||||
$this->implementation = $printer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Formatter $printer
|
||||
*/
|
||||
public function setImplementation(Formatter $printer)
|
||||
{
|
||||
$this->implementation = $printer;
|
||||
|
@@ -11,15 +11,13 @@ class Form implements Renderable
|
||||
/**
|
||||
* @var Renderable[]
|
||||
*/
|
||||
private $elements;
|
||||
private array $elements;
|
||||
|
||||
/**
|
||||
* runs through all elements and calls render() on them, then returns the complete representation
|
||||
* of the form.
|
||||
*
|
||||
* from the outside, one will not see this and the form will act like a single object instance
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render(): string
|
||||
{
|
||||
@@ -34,9 +32,6 @@ class Form implements Renderable
|
||||
return $formCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Renderable $element
|
||||
*/
|
||||
public function addElement(Renderable $element)
|
||||
{
|
||||
$this->elements[] = $element;
|
||||
|
@@ -2,19 +2,21 @@
|
||||
|
||||
namespace DesignPatterns\Structural\Composite\Tests;
|
||||
|
||||
use DesignPatterns\Structural\Composite;
|
||||
use DesignPatterns\Structural\Composite\Form;
|
||||
use DesignPatterns\Structural\Composite\TextElement;
|
||||
use DesignPatterns\Structural\Composite\InputElement;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class CompositeTest extends TestCase
|
||||
{
|
||||
public function testRender()
|
||||
{
|
||||
$form = new Composite\Form();
|
||||
$form->addElement(new Composite\TextElement('Email:'));
|
||||
$form->addElement(new Composite\InputElement());
|
||||
$embed = new Composite\Form();
|
||||
$embed->addElement(new Composite\TextElement('Password:'));
|
||||
$embed->addElement(new Composite\InputElement());
|
||||
$form = new Form();
|
||||
$form->addElement(new TextElement('Email:'));
|
||||
$form->addElement(new InputElement());
|
||||
$embed = new Form();
|
||||
$embed->addElement(new TextElement('Password:'));
|
||||
$embed->addElement(new InputElement());
|
||||
$form->addElement($embed);
|
||||
|
||||
// This is just an example, in a real world scenario it is important to remember that web browsers do not
|
||||
|
@@ -4,10 +4,7 @@ namespace DesignPatterns\Structural\Composite;
|
||||
|
||||
class TextElement implements Renderable
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $text;
|
||||
private string $text;
|
||||
|
||||
public function __construct(string $text)
|
||||
{
|
||||
|
@@ -4,10 +4,7 @@ namespace DesignPatterns\Structural\DataMapper;
|
||||
|
||||
class StorageAdapter
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $data = [];
|
||||
private array $data = [];
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace DesignPatterns\Structural\DataMapper\Tests;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use DesignPatterns\Structural\DataMapper\StorageAdapter;
|
||||
use DesignPatterns\Structural\DataMapper\User;
|
||||
use DesignPatterns\Structural\DataMapper\UserMapper;
|
||||
@@ -21,7 +22,7 @@ class DataMapperTest extends TestCase
|
||||
|
||||
public function testWillNotMapInvalidData()
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
$storage = new StorageAdapter([]);
|
||||
$mapper = new UserMapper($storage);
|
||||
|
@@ -4,15 +4,8 @@ namespace DesignPatterns\Structural\DataMapper;
|
||||
|
||||
class User
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $username;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $email;
|
||||
private string $username;
|
||||
private string $email;
|
||||
|
||||
public static function fromState(array $state): User
|
||||
{
|
||||
@@ -32,18 +25,12 @@ class User
|
||||
$this->email = $email;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUsername()
|
||||
public function getUsername(): string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEmail()
|
||||
public function getEmail(): string
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
@@ -2,16 +2,12 @@
|
||||
|
||||
namespace DesignPatterns\Structural\DataMapper;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class UserMapper
|
||||
{
|
||||
/**
|
||||
* @var StorageAdapter
|
||||
*/
|
||||
private $adapter;
|
||||
private StorageAdapter $adapter;
|
||||
|
||||
/**
|
||||
* @param StorageAdapter $storage
|
||||
*/
|
||||
public function __construct(StorageAdapter $storage)
|
||||
{
|
||||
$this->adapter = $storage;
|
||||
@@ -22,17 +18,13 @@ class UserMapper
|
||||
* in memory. Normally this kind of logic will be implemented using the Repository pattern.
|
||||
* However the important part is in mapRowToUser() below, that will create a business object from the
|
||||
* data fetched from storage
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function findById(int $id): User
|
||||
{
|
||||
$result = $this->adapter->find($id);
|
||||
|
||||
if ($result === null) {
|
||||
throw new \InvalidArgumentException("User #$id not found");
|
||||
throw new InvalidArgumentException("User #$id not found");
|
||||
}
|
||||
|
||||
return $this->mapRowToUser($result);
|
||||
|
@@ -4,10 +4,7 @@ namespace DesignPatterns\Structural\Decorator;
|
||||
|
||||
abstract class BookingDecorator implements Booking
|
||||
{
|
||||
/**
|
||||
* @var Booking
|
||||
*/
|
||||
protected $booking;
|
||||
protected Booking $booking;
|
||||
|
||||
public function __construct(Booking $booking)
|
||||
{
|
||||
|
@@ -4,25 +4,10 @@ namespace DesignPatterns\Structural\DependencyInjection;
|
||||
|
||||
class DatabaseConfiguration
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $host;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $port;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $username;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $password;
|
||||
private string $host;
|
||||
private int $port;
|
||||
private string $username;
|
||||
private string $password;
|
||||
|
||||
public function __construct(string $host, int $port, string $username, string $password)
|
||||
{
|
||||
|
@@ -4,14 +4,8 @@ namespace DesignPatterns\Structural\DependencyInjection;
|
||||
|
||||
class DatabaseConnection
|
||||
{
|
||||
/**
|
||||
* @var DatabaseConfiguration
|
||||
*/
|
||||
private $configuration;
|
||||
private DatabaseConfiguration $configuration;
|
||||
|
||||
/**
|
||||
* @param DatabaseConfiguration $config
|
||||
*/
|
||||
public function __construct(DatabaseConfiguration $config)
|
||||
{
|
||||
$this->configuration = $config;
|
||||
|
@@ -4,20 +4,9 @@ namespace DesignPatterns\Structural\Facade;
|
||||
|
||||
class Facade
|
||||
{
|
||||
/**
|
||||
* @var OperatingSystem
|
||||
*/
|
||||
private $os;
|
||||
private OperatingSystem $os;
|
||||
private Bios $bios;
|
||||
|
||||
/**
|
||||
* @var Bios
|
||||
*/
|
||||
private $bios;
|
||||
|
||||
/**
|
||||
* @param Bios $bios
|
||||
* @param OperatingSystem $os
|
||||
*/
|
||||
public function __construct(Bios $bios, OperatingSystem $os)
|
||||
{
|
||||
$this->bios = $bios;
|
||||
|
@@ -4,20 +4,9 @@ namespace DesignPatterns\Structural\FluentInterface;
|
||||
|
||||
class Sql
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $fields = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $from = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $where = [];
|
||||
private array $fields = [];
|
||||
private array $from = [];
|
||||
private array $where = [];
|
||||
|
||||
public function select(array $fields): Sql
|
||||
{
|
||||
|
@@ -11,10 +11,8 @@ class Character implements Text
|
||||
/**
|
||||
* Any state stored by the concrete flyweight must be independent of its context.
|
||||
* For flyweights representing characters, this is usually the corresponding character code.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
private string $name;
|
||||
|
||||
public function __construct(string $name)
|
||||
{
|
||||
|
@@ -7,9 +7,10 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FlyweightTest extends TestCase
|
||||
{
|
||||
private $characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
|
||||
private array $characters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
|
||||
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
|
||||
private $fonts = ['Arial', 'Times New Roman', 'Verdana', 'Helvetica'];
|
||||
|
||||
private array $fonts = ['Arial', 'Times New Roman', 'Verdana', 'Helvetica'];
|
||||
|
||||
public function testFlyweight()
|
||||
{
|
||||
|
@@ -2,16 +2,18 @@
|
||||
|
||||
namespace DesignPatterns\Structural\Flyweight;
|
||||
|
||||
use Countable;
|
||||
|
||||
/**
|
||||
* A factory manages shared flyweights. Clients should not instantiate them directly,
|
||||
* but let the factory take care of returning existing objects or creating new ones.
|
||||
*/
|
||||
class TextFactory implements \Countable
|
||||
class TextFactory implements Countable
|
||||
{
|
||||
/**
|
||||
* @var Text[]
|
||||
*/
|
||||
private $charPool = [];
|
||||
private array $charPool = [];
|
||||
|
||||
public function get(string $name): Text
|
||||
{
|
||||
|
@@ -4,10 +4,7 @@ namespace DesignPatterns\Structural\Flyweight;
|
||||
|
||||
class Word implements Text
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
private string $name;
|
||||
|
||||
public function __construct(string $name)
|
||||
{
|
||||
|
@@ -4,10 +4,7 @@ namespace DesignPatterns\Structural\Proxy;
|
||||
|
||||
class BankAccountProxy extends HeavyBankAccount implements BankAccount
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $balance;
|
||||
private ?int $balance = null;
|
||||
|
||||
public function getBalance(): int
|
||||
{
|
||||
|
@@ -7,7 +7,7 @@ class HeavyBankAccount implements BankAccount
|
||||
/**
|
||||
* @var int[]
|
||||
*/
|
||||
private $transactions = [];
|
||||
private array $transactions = [];
|
||||
|
||||
public function deposit(int $amount)
|
||||
{
|
||||
|
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace DesignPatterns\Structural\Registry;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
abstract class Registry
|
||||
{
|
||||
const LOGGER = 'logger';
|
||||
@@ -12,19 +14,16 @@ abstract class Registry
|
||||
*
|
||||
* @var Service[]
|
||||
*/
|
||||
private static $services = [];
|
||||
private static array $services = [];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private static $allowedKeys = [
|
||||
private static array $allowedKeys = [
|
||||
self::LOGGER,
|
||||
];
|
||||
|
||||
public static function set(string $key, Service $value)
|
||||
{
|
||||
if (!in_array($key, self::$allowedKeys)) {
|
||||
throw new \InvalidArgumentException('Invalid key given');
|
||||
throw new InvalidArgumentException('Invalid key given');
|
||||
}
|
||||
|
||||
self::$services[$key] = $value;
|
||||
@@ -33,7 +32,7 @@ abstract class Registry
|
||||
public static function get(string $key): Service
|
||||
{
|
||||
if (!in_array($key, self::$allowedKeys) || !isset(self::$services[$key])) {
|
||||
throw new \InvalidArgumentException('Invalid key given');
|
||||
throw new InvalidArgumentException('Invalid key given');
|
||||
}
|
||||
|
||||
return self::$services[$key];
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace DesignPatterns\Structural\Registry\Tests;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use DesignPatterns\Structural\Registry\Registry;
|
||||
use DesignPatterns\Structural\Registry\Service;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
@@ -9,7 +10,10 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class RegistryTest extends TestCase
|
||||
{
|
||||
private $service;
|
||||
/**
|
||||
* @var Service
|
||||
*/
|
||||
private MockObject $service;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
@@ -18,7 +22,6 @@ class RegistryTest extends TestCase
|
||||
|
||||
public function testSetAndGetLogger()
|
||||
{
|
||||
/** @noinspection PhpParamsInspection */
|
||||
Registry::set(Registry::LOGGER, $this->service);
|
||||
|
||||
$this->assertSame($this->service, Registry::get(Registry::LOGGER));
|
||||
@@ -26,7 +29,7 @@ class RegistryTest extends TestCase
|
||||
|
||||
public function testThrowsExceptionWhenTryingToSetInvalidKey()
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
Registry::set('foobar', $this->service);
|
||||
}
|
||||
@@ -40,7 +43,7 @@ class RegistryTest extends TestCase
|
||||
*/
|
||||
public function testThrowsExceptionWhenTryingToGetNotSetKey()
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
Registry::get(Registry::LOGGER);
|
||||
}
|
||||
|
Reference in New Issue
Block a user