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
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"squizlabs/php_codesniffer": "^3.6.1",
"doctrine/coding-standard": "^9.0",
"roave/security-advisories": "dev-latest",
"league/container": "^3.3.3"
"league/container": "^4.1.2"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions examples/2-intermediate-create-middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions examples/4-conditional-handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -43,9 +43,11 @@ 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));

return null;
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down
15 changes: 3 additions & 12 deletions src/Handler/CommandHandlerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,24 @@
use League\Tactician\Middleware;
use Psr\Container\ContainerInterface;

use function get_class;

/**
* Our basic middleware for executing commands.
*
* Feel free to use this as a starting point for your own middleware! :)
*/
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));
$methodToCall = $this->mapping->findHandlerForCommand($command::class);

// 2. Retrieve an instance of the Handler from our PSR-11 container
// This assumes the container id is the same as the class name but
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace League\Tactician\Handler\Mapping\MapByNamingConvention\MethodName;

use function strpos;
use function str_contains;
use function strrpos;
use function strtolower;
use function substr;
Expand All @@ -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);
}

Expand Down
6 changes: 1 addition & 5 deletions src/Handler/Mapping/MapByStaticList/MapByStaticList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions src/Handler/Mapping/MethodDoesNotExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@
*/
interface Middleware
{
/**
* @return mixed
*/
public function execute(object $command, callable $next);
public function execute(object $command, callable $next): mixed;
}