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 abstract class Handler
{ {
private ?Handler $successor = null; public function __construct(private ?Handler $successor = null)
public function __construct(Handler $handler = null)
{ {
$this->successor = $handler;
} }
/** /**

View File

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

View File

@ -8,15 +8,12 @@ namespace DesignPatterns\Behavioral\Command;
*/ */
class AddMessageDateCommand implements UndoableCommand class AddMessageDateCommand implements UndoableCommand
{ {
private Receiver $output;
/** /**
* Each concrete command is built with different receivers. * 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. * 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 class HelloCommand implements Command
{ {
private Receiver $output;
/** /**
* Each concrete command is built with different receivers. * 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 * 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 class Book
{ {
private string $author; public function __construct(private string $title, private string $author)
private string $title;
public function __construct(string $title, string $author)
{ {
$this->author = $author;
$this->title = $title;
} }
public function getAuthor(): string public function getAuthor(): string

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,13 +4,7 @@ namespace DesignPatterns\Creational\Builder\Parts;
abstract class Vehicle abstract class Vehicle
{ {
/**
* @var object[]
*/
private array $data = [];
public function setPart(string $key, object $value) 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 class FileLogger implements Logger
{ {
private string $filePath; public function __construct(private string $filePath)
public function __construct(string $filePath)
{ {
$this->filePath = $filePath;
} }
public function log(string $message) public function log(string $message)

View File

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

View File

@ -6,11 +6,8 @@ use DateTime;
class StringReverseWorker class StringReverseWorker
{ {
private DateTime $createdAt;
public function __construct() public function __construct()
{ {
$this->createdAt = new DateTime();
} }
public function run(string $text) 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) * prevent from being unserialized (which would create a second instance of it)
*/ */

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -4,9 +4,6 @@ namespace DesignPatterns\Structural\DataMapper;
class User class User
{ {
private string $username;
private string $email;
public static function fromState(array $state): User public static function fromState(array $state): User
{ {
// validate state before accessing keys! // 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 public function getUsername(): string

View File

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

View File

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

View File

@ -4,17 +4,12 @@ namespace DesignPatterns\Structural\DependencyInjection;
class DatabaseConfiguration class DatabaseConfiguration
{ {
private string $host; public function __construct(
private int $port; private string $host,
private string $username; private int $port,
private string $password; 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 getHost(): string public function getHost(): string

View File

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

View File

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

View File

@ -2,7 +2,7 @@
namespace DesignPatterns\Structural\FluentInterface; namespace DesignPatterns\Structural\FluentInterface;
class Sql class Sql implements \Stringable
{ {
private array $fields = []; private array $fields = [];
private array $from = []; 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. * Any state stored by the concrete flyweight must be independent of its context.
* For flyweights representing characters, this is usually the corresponding character code. * For flyweights representing characters, this is usually the corresponding character code.
*/ */
private string $name; public function __construct(private string $name)
public function __construct(string $name)
{ {
$this->name = $name;
} }
public function render(string $font): string public function render(string $font): string

View File

@ -4,11 +4,8 @@ namespace DesignPatterns\Structural\Flyweight;
class Word implements Text class Word implements Text
{ {
private string $name; public function __construct(private string $name)
public function __construct(string $name)
{ {
$this->name = $name;
} }
public function render(string $font): string 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 // years and decades ago must be fetched from a database or web service
// and the balance must be calculated from it // 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 InvalidArgumentException;
use DesignPatterns\Structural\Registry\Registry; use DesignPatterns\Structural\Registry\Registry;
use DesignPatterns\Structural\Registry\Service; use DesignPatterns\Structural\Registry\Service;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class RegistryTest extends TestCase class RegistryTest extends TestCase
{ {
/** private Service $service;
* @var Service
*/
private MockObject $service;
protected function setUp(): void protected function setUp(): void
{ {

View File

@ -10,7 +10,7 @@
], ],
"minimum-stability": "stable", "minimum-stability": "stable",
"require": { "require": {
"php": ">=7.4", "php": ">=8.0",
"psr/http-message": "^1.0", "psr/http-message": "^1.0",
"ext-json": "*" "ext-json": "*"
}, },
@ -18,7 +18,7 @@
"phpunit/phpunit": "^9", "phpunit/phpunit": "^9",
"squizlabs/php_codesniffer": "^3", "squizlabs/php_codesniffer": "^3",
"flyeralarm/php-code-validator": "^2.2", "flyeralarm/php-code-validator": "^2.2",
"vimeo/psalm": "^3", "vimeo/psalm": "^4",
"psalm/plugin-phpunit": "*", "psalm/plugin-phpunit": "*",
"rector/rector": "*" "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"/> <MissingReturnType errorLevel="info"/>
<MissingPropertyType errorLevel="info"/> <MissingPropertyType errorLevel="info"/>
<InvalidDocblock errorLevel="info"/> <InvalidDocblock errorLevel="info"/>
<MisplacedRequiredParam errorLevel="info"/>
<PropertyNotSetInConstructor errorLevel="info"/> <PropertyNotSetInConstructor errorLevel="info"/>
<MissingConstructor errorLevel="info"/> <MissingConstructor errorLevel="info"/>