This commit is contained in:
Dominik Liebler
2021-04-12 14:04:45 +02:00
parent 5c5d453506
commit 4678b5d86f
50 changed files with 992 additions and 1430 deletions

View File

@@ -8,11 +8,8 @@ namespace DesignPatterns\Structural\Adapter;
*/
class EBookAdapter implements Book
{
protected EBook $eBook;
public function __construct(EBook $eBook)
public function __construct(protected EBook $eBook)
{
$this->eBook = $eBook;
}
/**

View File

@@ -4,11 +4,8 @@ namespace DesignPatterns\Structural\Bridge;
abstract class Service
{
protected Formatter $implementation;
public function __construct(Formatter $printer)
public function __construct(protected Formatter $implementation)
{
$this->implementation = $printer;
}
public function setImplementation(Formatter $printer)

View File

@@ -27,9 +27,7 @@ class Form implements Renderable
$formCode .= $element->render();
}
$formCode .= '</form>';
return $formCode;
return $formCode . '</form>';
}
public function addElement(Renderable $element)

View File

@@ -4,11 +4,8 @@ namespace DesignPatterns\Structural\Composite;
class TextElement implements Renderable
{
private string $text;
public function __construct(string $text)
public function __construct(private string $text)
{
$this->text = $text;
}
public function render(): string

View File

@@ -4,16 +4,11 @@ namespace DesignPatterns\Structural\DataMapper;
class StorageAdapter
{
private array $data = [];
public function __construct(array $data)
public function __construct(private array $data)
{
$this->data = $data;
}
/**
* @param int $id
*
* @return array|null
*/
public function find(int $id)

View File

@@ -4,9 +4,6 @@ namespace DesignPatterns\Structural\DataMapper;
class User
{
private string $username;
private string $email;
public static function fromState(array $state): User
{
// validate state before accessing keys!
@@ -17,12 +14,8 @@ class User
);
}
public function __construct(string $username, string $email)
public function __construct(private string $username, private string $email)
{
// validate parameters before setting them!
$this->username = $username;
$this->email = $email;
}
public function getUsername(): string

View File

@@ -6,11 +6,8 @@ use InvalidArgumentException;
class UserMapper
{
private StorageAdapter $adapter;
public function __construct(StorageAdapter $storage)
public function __construct(private StorageAdapter $adapter)
{
$this->adapter = $storage;
}
/**

View File

@@ -4,10 +4,7 @@ namespace DesignPatterns\Structural\Decorator;
abstract class BookingDecorator implements Booking
{
protected Booking $booking;
public function __construct(Booking $booking)
public function __construct(protected Booking $booking)
{
$this->booking = $booking;
}
}

View File

@@ -4,17 +4,12 @@ namespace DesignPatterns\Structural\DependencyInjection;
class DatabaseConfiguration
{
private string $host;
private int $port;
private string $username;
private string $password;
public function __construct(string $host, int $port, string $username, string $password)
{
$this->host = $host;
$this->port = $port;
$this->username = $username;
$this->password = $password;
public function __construct(
private string $host,
private int $port,
private string $username,
private string $password
) {
}
public function getHost(): string

View File

@@ -4,11 +4,8 @@ namespace DesignPatterns\Structural\DependencyInjection;
class DatabaseConnection
{
private DatabaseConfiguration $configuration;
public function __construct(DatabaseConfiguration $config)
public function __construct(private DatabaseConfiguration $configuration)
{
$this->configuration = $config;
}
public function getDsn(): string

View File

@@ -4,13 +4,8 @@ namespace DesignPatterns\Structural\Facade;
class Facade
{
private OperatingSystem $os;
private Bios $bios;
public function __construct(Bios $bios, OperatingSystem $os)
public function __construct(private Bios $bios, private OperatingSystem $os)
{
$this->bios = $bios;
$this->os = $os;
}
public function turnOn()

View File

@@ -2,7 +2,7 @@
namespace DesignPatterns\Structural\FluentInterface;
class Sql
class Sql implements \Stringable
{
private array $fields = [];
private array $from = [];

View File

@@ -12,11 +12,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.
*/
private string $name;
public function __construct(string $name)
public function __construct(private string $name)
{
$this->name = $name;
}
public function render(string $font): string

View File

@@ -4,11 +4,8 @@ namespace DesignPatterns\Structural\Flyweight;
class Word implements Text
{
private string $name;
public function __construct(string $name)
public function __construct(private string $name)
{
$this->name = $name;
}
public function render(string $font): string

View File

@@ -20,6 +20,6 @@ class HeavyBankAccount implements BankAccount
// years and decades ago must be fetched from a database or web service
// and the balance must be calculated from it
return (int) array_sum($this->transactions);
return array_sum($this->transactions);
}
}

View File

@@ -5,15 +5,11 @@ namespace DesignPatterns\Structural\Registry\Tests;
use InvalidArgumentException;
use DesignPatterns\Structural\Registry\Registry;
use DesignPatterns\Structural\Registry\Service;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class RegistryTest extends TestCase
{
/**
* @var Service
*/
private MockObject $service;
private Service $service;
protected function setUp(): void
{