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

@ -6,11 +6,8 @@ use Psr\Http\Message\RequestInterface;
abstract class Handler
{
private ?Handler $successor = null;
public function __construct(Handler $handler = null)
public function __construct(private ?Handler $successor = null)
{
$this->successor = $handler;
}
/**

View File

@ -7,13 +7,9 @@ use Psr\Http\Message\RequestInterface;
class HttpInMemoryCacheHandler extends Handler
{
private array $data;
public function __construct(array $data, ?Handler $successor = null)
public function __construct(private array $data, ?Handler $successor = null)
{
parent::__construct($successor);
$this->data = $data;
}
protected function processing(RequestInterface $request): ?string

View File

@ -8,15 +8,12 @@ namespace DesignPatterns\Behavioral\Command;
*/
class AddMessageDateCommand implements UndoableCommand
{
private Receiver $output;
/**
* Each concrete command is built with different receivers.
* There can be one, many or completely no receivers, but there can be other commands in the parameters.
*/
public function __construct(Receiver $console)
public function __construct(private Receiver $output)
{
$this->output = $console;
}
/**

View File

@ -8,15 +8,12 @@ namespace DesignPatterns\Behavioral\Command;
*/
class HelloCommand implements Command
{
private Receiver $output;
/**
* Each concrete command is built with different receivers.
* There can be one, many or completely no receivers, but there can be other commands in the parameters
*/
public function __construct(Receiver $console)
public function __construct(private Receiver $output)
{
$this->output = $console;
}
/**

View File

@ -4,13 +4,8 @@ namespace DesignPatterns\Behavioral\Iterator;
class Book
{
private string $author;
private string $title;
public function __construct(string $title, string $author)
public function __construct(private string $title, private string $author)
{
$this->author = $author;
$this->title = $title;
}
public function getAuthor(): string

View File

@ -4,14 +4,8 @@ namespace DesignPatterns\Behavioral\Mediator;
class UserRepositoryUiMediator implements Mediator
{
private UserRepository $userRepository;
private Ui $ui;
public function __construct(UserRepository $userRepository, Ui $ui)
public function __construct(private UserRepository $userRepository, private Ui $ui)
{
$this->userRepository = $userRepository;
$this->ui = $ui;
$this->userRepository->setMediator($this);
$this->ui->setMediator($this);
}

View File

@ -4,11 +4,8 @@ namespace DesignPatterns\Behavioral\Memento;
class Memento
{
private State $state;
public function __construct(State $stateToSave)
public function __construct(private State $state)
{
$this->state = $stateToSave;
}
public function getState(): State

View File

@ -4,7 +4,7 @@ namespace DesignPatterns\Behavioral\Memento;
use InvalidArgumentException;
class State
class State implements \Stringable
{
const STATE_CREATED = 'created';
const STATE_OPENED = 'opened';

View File

@ -4,11 +4,8 @@ namespace DesignPatterns\Behavioral\NullObject;
class Service
{
private Logger $logger;
public function __construct(Logger $logger)
public function __construct(private Logger $logger)
{
$this->logger = $logger;
}
/**

View File

@ -12,7 +12,6 @@ use SplObserver;
*/
class User implements SplSubject
{
private string $email;
private SplObjectStorage $observers;
public function __construct()
@ -32,7 +31,6 @@ class User implements SplSubject
public function changeEmail(string $email)
{
$this->email = $email;
$this->notify();
}

View File

@ -4,11 +4,8 @@ namespace DesignPatterns\Behavioral\Specification;
class Item
{
private float $price;
public function __construct(float $price)
public function __construct(private float $price)
{
$this->price = $price;
}
public function getPrice(): float

View File

@ -4,11 +4,8 @@ namespace DesignPatterns\Behavioral\Specification;
class NotSpecification implements Specification
{
private Specification $specification;
public function __construct(Specification $specification)
public function __construct(private Specification $specification)
{
$this->specification = $specification;
}
public function isSatisfiedBy(Item $item): bool

View File

@ -4,13 +4,8 @@ namespace DesignPatterns\Behavioral\Specification;
class PriceSpecification implements Specification
{
private ?float $maxPrice;
private ?float $minPrice;
public function __construct(?float $minPrice, ?float $maxPrice)
public function __construct(private ?float $minPrice, private ?float $maxPrice)
{
$this->minPrice = $minPrice;
$this->maxPrice = $maxPrice;
}
public function isSatisfiedBy(Item $item): bool

View File

@ -7,8 +7,6 @@ interface Comparator
/**
* @param mixed $a
* @param mixed $b
*
* @return int
*/
public function compare($a, $b): int;
}

View File

@ -4,11 +4,8 @@ namespace DesignPatterns\Behavioral\Strategy;
class Context
{
private Comparator $comparator;
public function __construct(Comparator $comparator)
public function __construct(private Comparator $comparator)
{
$this->comparator = $comparator;
}
public function executeStrategy(array $elements): array

View File

@ -4,11 +4,8 @@ namespace DesignPatterns\Behavioral\Visitor;
class Group implements Role
{
private string $name;
public function __construct(string $name)
public function __construct(private string $name)
{
$this->name = $name;
}
public function getName(): string

View File

@ -4,11 +4,8 @@ namespace DesignPatterns\Behavioral\Visitor;
class User implements Role
{
private string $name;
public function __construct(string $name)
public function __construct(private string $name)
{
$this->name = $name;
}
public function getName(): string

View File

@ -21,8 +21,6 @@ class AbstractFactoryTest extends TestCase
/**
* @dataProvider provideFactory
*
* @param WriterFactory $writerFactory
*/
public function testCanCreateCsvWriterOnUnix(WriterFactory $writerFactory)
{

View File

@ -4,13 +4,7 @@ namespace DesignPatterns\Creational\Builder\Parts;
abstract class Vehicle
{
/**
* @var object[]
*/
private array $data = [];
public function setPart(string $key, object $value)
{
$this->data[$key] = $value;
}
}

View File

@ -4,11 +4,8 @@ namespace DesignPatterns\Creational\FactoryMethod;
class FileLogger implements Logger
{
private string $filePath;
public function __construct(string $filePath)
public function __construct(private string $filePath)
{
$this->filePath = $filePath;
}
public function log(string $message)

View File

@ -4,11 +4,8 @@ namespace DesignPatterns\Creational\FactoryMethod;
class FileLoggerFactory implements LoggerFactory
{
private string $filePath;
public function __construct(string $filePath)
public function __construct(private string $filePath)
{
$this->filePath = $filePath;
}
public function createLogger(): Logger

View File

@ -6,11 +6,8 @@ use DateTime;
class StringReverseWorker
{
private DateTime $createdAt;
public function __construct()
{
$this->createdAt = new DateTime();
}
public function run(string $text)

View File

@ -28,13 +28,6 @@ final class Singleton
{
}
/**
* prevent the instance from being cloned (which would create a second instance of it)
*/
private function __clone()
{
}
/**
* prevent from being unserialized (which would create a second instance of it)
*/

View File

@ -1,11 +1,15 @@
FROM composer AS composer
WORKDIR /app
ADD . /app
RUN composer install \
&& ./vendor/bin/phpcs --ignore=_build . \
&& ./vendor/bin/phpunit \
&& ./vendor/bin/psalm --show-info=false \
&& ./check-refs-readmes
RUN composer install
FROM php:8-cli-alpine
WORKDIR /app
COPY --from=composer /app /app
RUN ./vendor/bin/phpcs --ignore=_build . \
&& ./vendor/bin/phpunit \
&& ./vendor/bin/psalm --show-info=false \
&& ./check-refs-readmes
FROM python AS sphinx_build
WORKDIR /app

View File

@ -4,15 +4,13 @@ namespace DesignPatterns\More\EAV;
use SplObjectStorage;
class Attribute
class Attribute implements \Stringable
{
private SplObjectStorage $values;
private string $name;
public function __construct(string $name)
public function __construct(private string $name)
{
$this->values = new SplObjectStorage();
$this->name = $name;
}
public function addValue(Value $value)

View File

@ -4,7 +4,7 @@ namespace DesignPatterns\More\EAV;
use SplObjectStorage;
class Entity
class Entity implements \Stringable
{
/**
* @var SplObjectStorage<Value,Value>
@ -12,19 +12,11 @@ class Entity
private $values;
/**
* @var string
*/
private string $name;
/**
* @param string $name
* @param Value[] $values
*/
public function __construct(string $name, $values)
public function __construct(private string $name, $values)
{
/** @var SplObjectStorage<Value,Value> values */
$this->values = new SplObjectStorage();
$this->name = $name;
foreach ($values as $value) {
$this->values->attach($value);

View File

@ -2,16 +2,10 @@
namespace DesignPatterns\More\EAV;
class Value
class Value implements \Stringable
{
private Attribute $attribute;
private string $name;
public function __construct(Attribute $attribute, string $name)
public function __construct(private Attribute $attribute, private string $name)
{
$this->name = $name;
$this->attribute = $attribute;
$attribute->addValue($this);
}

View File

@ -4,11 +4,6 @@ namespace DesignPatterns\More\Repository\Domain;
class Post
{
private PostId $id;
private PostStatus $status;
private string $title;
private string $text;
public static function draft(PostId $id, string $title, string $text): Post
{
return new self(
@ -29,12 +24,12 @@ class Post
);
}
private function __construct(PostId $id, PostStatus $status, string $title, string $text)
{
$this->id = $id;
$this->status = $status;
$this->text = $text;
$this->title = $title;
private function __construct(
private PostId $id,
private PostStatus $status,
private string $title,
private string $text
) {
}
public function getId(): PostId

View File

@ -13,8 +13,6 @@ use InvalidArgumentException;
*/
class PostId
{
private int $id;
public static function fromInt(int $id): PostId
{
self::ensureIsValid($id);
@ -22,9 +20,8 @@ class PostId
return new self($id);
}
private function __construct(int $id)
private function __construct(private int $id)
{
$this->id = $id;
}
public function toInt(): int

View File

@ -21,9 +21,6 @@ class PostStatus
self::STATE_PUBLISHED_ID => self::STATE_PUBLISHED,
];
private int $id;
private string $name;
public static function fromInt(int $statusId)
{
self::ensureIsValidId($statusId);
@ -43,10 +40,8 @@ class PostStatus
return new self($state, $status);
}
private function __construct(int $id, string $name)
private function __construct(private int $id, private string $name)
{
$this->id = $id;
$this->name = $name;
}
public function toInt(): int

View File

@ -17,11 +17,8 @@ use DesignPatterns\More\Repository\Domain\PostId;
*/
class PostRepository
{
private Persistence $persistence;
public function __construct(Persistence $persistence)
public function __construct(private Persistence $persistence)
{
$this->persistence = $persistence;
}
public function generateId(): PostId

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
{

View File

@ -10,7 +10,7 @@
],
"minimum-stability": "stable",
"require": {
"php": ">=7.4",
"php": ">=8.0",
"psr/http-message": "^1.0",
"ext-json": "*"
},
@ -18,7 +18,7 @@
"phpunit/phpunit": "^9",
"squizlabs/php_codesniffer": "^3",
"flyeralarm/php-code-validator": "^2.2",
"vimeo/psalm": "^3",
"vimeo/psalm": "^4",
"psalm/plugin-phpunit": "*",
"rector/rector": "*"
},

2135
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -37,7 +37,6 @@
<MissingReturnType errorLevel="info"/>
<MissingPropertyType errorLevel="info"/>
<InvalidDocblock errorLevel="info"/>
<MisplacedRequiredParam errorLevel="info"/>
<PropertyNotSetInConstructor errorLevel="info"/>
<MissingConstructor errorLevel="info"/>