Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support php 8.0 as minimum version, update dependencies #157

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
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
Next Next commit
Support php 8.0 as minimum version, update dependencies
snapshotpl committed Nov 4, 2021
commit 3ccc2a4badae5da746298e484f07074c23b13b2b
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ jobs:
strategy:
matrix:
operating-system: ['ubuntu-latest']
php-versions: ['7.4', '8.0', '8.1']
php-versions: ['8.0', '8.1']
runs-on: ${{ matrix.operating-system }}
steps:
- name: Checkout
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -10,18 +10,18 @@
}
],
"require": {
"php": ">=7.4",
"psr/container": "^1.0"
"php": "~8.0 || ~8.1",
"psr/container": "^1.0 || ^2.0.1"
},
"require-dev": {
"phpunit/phpunit": "^9.4.2",
"phpstan/phpstan": "^0.12.52",
"phpstan/phpstan-phpunit": "^0.12.16",
"phpstan/phpstan": "^1.0.2",
"phpstan/phpstan-phpunit": "^1.0.0",
"phpstan/extension-installer": "^1.0.5",
"squizlabs/php_codesniffer": "^3.5.8",
"doctrine/coding-standard": "^8.2",
"roave/security-advisories": "dev-latest",
"league/container": "^3.3.3"
"league/container": "^4.1.2"
},
"autoload": {
"psr-4": {
6 changes: 3 additions & 3 deletions examples/2-intermediate-create-middleware.php
Original file line number Diff line number Diff line change
@@ -29,14 +29,14 @@ public function log($info) { echo "LOG: $info\n"; }
// use it later.
class LoggingMiddleware implements Middleware
{
protected $logger;
protected Logger $logger;

public function __construct(Logger $logger)
{
$this->logger = $logger;
}

public function execute($command, callable $next)
public function execute(object $command, callable $next): mixed
{
$commandClass = \get_class($command);

@@ -62,7 +62,7 @@ public function execute($command, callable $next)
// in the previous example, otherwise our commands won't be executed!
$commandBus = new CommandBus(
new LoggingMiddleware(new Logger()),
$handlerMiddleware
$handlerMiddleware,
);

// Controller Code
4 changes: 2 additions & 2 deletions examples/4-conditional-handlers.php
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ public function __construct(CommandBus $commandBus)
$this->commandBus = $commandBus;
}

public function execute($command, callable $next)
public function execute(object $command, callable $next): mixed
{
if ($command instanceof ExternalCommand) {
return $this->commandBus->handle($command);
@@ -43,7 +43,7 @@ public function execute($command, callable $next)
// and we'll create a custom command handler/middleware
final class ExternalCommandHandler implements Middleware
{
public function execute($command, callable $next)
public function execute(object $command, callable $next): mixed
{
echo \sprintf("Dispatched %s!\n", \get_class($command));
}
6 changes: 2 additions & 4 deletions src/CommandBus.php
Original file line number Diff line number Diff line change
@@ -23,10 +23,8 @@ public function __construct(Middleware ...$middleware)

/**
* Executes the given command and optionally returns a value
*
* @return mixed
*/
public function handle(object $command)
public function handle(object $command): mixed
{
return ($this->middlewareChain)($command);
}
@@ -39,7 +37,7 @@ private function createExecutionChain(array $middlewareList): Closure
$lastCallable = static fn () => null;

while ($middleware = array_pop($middlewareList)) {
$lastCallable = static fn (object $command) => $middleware->execute($command, $lastCallable);
$lastCallable = static fn (object $command): mixed => $middleware->execute($command, $lastCallable);
}

return $lastCallable;
11 changes: 2 additions & 9 deletions src/Handler/CommandHandlerMiddleware.php
Original file line number Diff line number Diff line change
@@ -17,21 +17,14 @@
*/
final class CommandHandlerMiddleware implements Middleware
{
private ContainerInterface $container;
private CommandToHandlerMapping $mapping;

public function __construct(ContainerInterface $container, CommandToHandlerMapping $mapping)
public function __construct(private ContainerInterface $container, private CommandToHandlerMapping $mapping)
{
$this->container = $container;
$this->mapping = $mapping;
}

/**
* Executes a command and optionally returns a value
*
* @return mixed
*/
public function execute(object $command, callable $next)
public function execute(object $command, callable $next): mixed
{
// 1. Based on the command we received, get the Handler method to call.
$methodToCall = $this->mapping->findHandlerForCommand(get_class($command));
Original file line number Diff line number Diff line change
@@ -6,11 +6,8 @@

final class Suffix implements ClassNameInflector
{
private string $suffix;

public function __construct(string $suffix)
public function __construct(private string $suffix)
{
$this->suffix = $suffix;
}

public function getClassName(string $commandClassName): string
Original file line number Diff line number Diff line change
@@ -15,14 +15,10 @@
*/
final class MapByNamingConvention implements CommandToHandlerMapping
{
private ClassNameInflector $classNameInflector;

private MethodNameInflector $methodNameInflector;

public function __construct(ClassNameInflector $classNameInflector, MethodNameInflector $methodNameInflector)
{
$this->classNameInflector = $classNameInflector;
$this->methodNameInflector = $methodNameInflector;
public function __construct(
private ClassNameInflector $classNameInflector,
private MethodNameInflector $methodNameInflector,
) {
}

public function findHandlerForCommand(string $commandFQCN): MethodToCall
Original file line number Diff line number Diff line change
@@ -21,17 +21,14 @@
*/
final class HandleClassNameWithoutSuffix implements MethodNameInflector
{
private string $suffix;

private int $suffixLength;
private HandleLastPartOfClassName $handleLastPartOfClassName;

/**
* @param string $suffix The string to remove from end of each class name
*/
public function __construct(string $suffix = 'Command')
public function __construct(private string $suffix = 'Command')
{
$this->suffix = $suffix;
$this->suffixLength = strlen($suffix);
$this->handleLastPartOfClassName = new HandleLastPartOfClassName();
}
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ final class LastPartOfClassName implements MethodNameInflector
public function getMethodName(string $commandClassNameName): string
{
// If class name has a namespace separator, only take last portion
if (strpos($commandClassNameName, '\\') !== false) {
if (str_contains($commandClassNameName, '\\')) {
$commandClassNameName = substr($commandClassNameName, strrpos($commandClassNameName, '\\') + 1);
}

6 changes: 1 addition & 5 deletions src/Handler/Mapping/MapByStaticList/MapByStaticList.php
Original file line number Diff line number Diff line change
@@ -24,13 +24,9 @@
*/
final class MapByStaticList implements CommandToHandlerMapping
{
/** @var array<string, array<string>> */
private array $mapping;

/** @param array<string, array<string>> $mapping */
public function __construct(array $mapping)
public function __construct(private array $mapping)
{
$this->mapping = $mapping;
}

public function findHandlerForCommand(string $commandFQCN): MethodToCall
8 changes: 1 addition & 7 deletions src/Handler/Mapping/MethodDoesNotExist.php
Original file line number Diff line number Diff line change
@@ -15,10 +15,6 @@
*/
final class MethodDoesNotExist extends BadMethodCallException implements Exception
{
private string $className;

private string $methodName;

public static function on(string $className, string $methodName): self
{
return new self(
@@ -39,10 +35,8 @@ public function getMethodName(): string
return $this->methodName;
}

private function __construct(string $message, string $className, string $methodName)
private function __construct(string $message, private string $className, private string $methodName)
{
parent::__construct($message);
$this->className = $className;
$this->methodName = $methodName;
}
}
9 changes: 1 addition & 8 deletions src/Handler/Mapping/MethodToCall.php
Original file line number Diff line number Diff line change
@@ -8,21 +8,14 @@

final class MethodToCall
{
private string $className;

private string $methodName;

public function __construct(string $className, string $methodName)
public function __construct(private string $className, private string $methodName)
{
// If the method does not actually exist, we'll also check if __call exists (mainly for
// legacy purposes). That said, we won't rewrite the method name to __call because our
// static analysis checker might still be able to infer data from the original method name.
if (! method_exists($className, $methodName) && ! method_exists($className, '__call')) {
throw MethodDoesNotExist::on($className, $methodName);
}

$this->className = $className;
$this->methodName = $methodName;
}

public function getClassName(): string
5 changes: 1 addition & 4 deletions src/Middleware.php
Original file line number Diff line number Diff line change
@@ -16,8 +16,5 @@
*/
interface Middleware
{
/**
* @return mixed
*/
public function execute(object $command, callable $next);
public function execute(object $command, callable $next): mixed;
}