Skip to content

Commit

Permalink
Add doctrine/coding standards to src/
Browse files Browse the repository at this point in the history
  • Loading branch information
rosstuck committed Sep 16, 2020
1 parent 7d36b3f commit b29e852
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ before_script:
script:
- vendor/bin/phpstan --level=max analyse src tests
- vendor/bin/phpunit --coverage-clover=build/logs/clover.xml
- vendor/bin/phpcs --standard=PSR2 src
- vendor/bin/phpcs --standard=Doctrine src

after_script:
- php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"phpstan/phpstan-phpunit": "^0.12",
"phpstan/extension-installer": "^1.0",
"league/container": "^3.3",
"squizlabs/php_codesniffer": "^3.4"
"squizlabs/php_codesniffer": "^3.4",
"doctrine/coding-standard": "^8.1"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions src/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public function handle(object $command)
/**
* @param Middleware[] $middlewareList
*/
private function createExecutionChain(array $middlewareList) : callable
private function createExecutionChain(array $middlewareList): callable
{
$lastCallable = static function () : void {
$lastCallable = static function (): void {
// the final callable is a no-op
};

while ($middleware = \array_pop($middlewareList)) {
while ($middleware = array_pop($middlewareList)) {
$lastCallable = static function ($command) use ($middleware, $lastCallable) {
return $middleware->execute($command, $lastCallable);
};
Expand Down
9 changes: 4 additions & 5 deletions src/Handler/CommandHandlerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use League\Tactician\Handler\Mapping\CommandToHandlerMapping;
use League\Tactician\Middleware;
use Psr\Container\ContainerInterface;

use function get_class;

/**
Expand All @@ -16,15 +17,13 @@
*/
final class CommandHandlerMiddleware implements Middleware
{
/** @var ContainerInterface */
private $container;
/** @var CommandToHandlerMapping */
private $mapping;
private ContainerInterface $container;
private CommandToHandlerMapping $mapping;

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

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Handler/Mapping/ClassName/Suffix.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

final class Suffix implements ClassNameInflector
{
/** @var string */
private $suffix;
private string $suffix;

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

public function getClassName(string $commandClassName) : string
public function getClassName(string $commandClassName): string
{
return $commandClassName . $this->suffix;
}
Expand Down
1 change: 1 addition & 0 deletions src/Handler/Mapping/CommandToHandlerMapping.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace League\Tactician\Handler\Mapping;
Expand Down
1 change: 1 addition & 0 deletions src/Handler/Mapping/FailedToMapCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace League\Tactician\Handler\Mapping;
Expand Down
9 changes: 4 additions & 5 deletions src/Handler/Mapping/MapByNamingConvention.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace League\Tactician\Handler\Mapping;
Expand All @@ -12,15 +13,13 @@
*/
final class MapByNamingConvention implements CommandToHandlerMapping
{
/** @var ClassNameInflector */
private $classNameInflector;
private ClassNameInflector $classNameInflector;

/** @var MethodNameInflector */
private $methodNameInflector;
private MethodNameInflector $methodNameInflector;

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

Expand Down
5 changes: 3 additions & 2 deletions src/Handler/Mapping/MapByStaticList.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace League\Tactician\Handler\Mapping;
Expand All @@ -20,7 +21,7 @@
final class MapByStaticList implements CommandToHandlerMapping
{
/** @var array<string, array<string>> */
private $mapping;
private array $mapping;

/** @param array<string, array<string>> $mapping */
public function __construct(array $mapping)
Expand All @@ -30,7 +31,7 @@ public function __construct(array $mapping)

public function mapCommandToHandler(string $commandFQCN): MethodToCall
{
if (! \array_key_exists($commandFQCN, $this->mapping)) {
if (! array_key_exists($commandFQCN, $this->mapping)) {
throw FailedToMapCommand::className($commandFQCN);
}

Expand Down
8 changes: 3 additions & 5 deletions src/Handler/Mapping/MethodDoesNotExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
*/
final class MethodDoesNotExist extends BadMethodCallException implements Exception
{
/** @var object */
private $command;

public static function on(string $className, string $methodName): self
{
return new self(
"The handler method {$className}::{$methodName} does not exist. Please check your Tactician mapping " .
"configuration or check verify that {$methodName} is actually declared in {$className}"
'The handler method ' . $className . ' :: ' . $methodName . ' does not exist. Please check your ' .
'Tactician mapping configuration or check verify that ' . $methodName . ' is actually declared in . ' .
$className
);
}
}
5 changes: 1 addition & 4 deletions src/Handler/Mapping/MethodName/Handle.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
*/
final class Handle implements MethodNameInflector
{
/**
* {@inheritdoc}
*/
public function getMethodName(string $commandClassName) : string
public function getMethodName(string $commandClassName): string
{
return 'handle';
}
Expand Down
19 changes: 8 additions & 11 deletions src/Handler/Mapping/MethodName/HandleClassNameWithoutSuffix.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,29 @@
*/
final class HandleClassNameWithoutSuffix implements MethodNameInflector
{
/** @var string */
private $suffix;
private string $suffix;

/** @var int */
private $suffixLength;
/** @var HandleLastPartOfClassName */
private $handleLastPartOfClassName;
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')
{
$this->suffix = $suffix;
$this->suffixLength = \strlen($suffix);
$this->suffix = $suffix;
$this->suffixLength = strlen($suffix);
$this->handleLastPartOfClassName = new HandleLastPartOfClassName();
}

public function getMethodName(string $commandClassName) : string
public function getMethodName(string $commandClassName): string
{
$methodName = $this->handleLastPartOfClassName->getMethodName($commandClassName);

if (\substr($methodName, $this->suffixLength * -1) !== $this->suffix) {
if (substr($methodName, $this->suffixLength * -1) !== $this->suffix) {
return $methodName;
}

return \substr($methodName, 0, \strlen($methodName) - $this->suffixLength);
return substr($methodName, 0, strlen($methodName) - $this->suffixLength);
}
}
10 changes: 3 additions & 7 deletions src/Handler/Mapping/MethodName/HandleLastPartOfClassName.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@
*/
final class HandleLastPartOfClassName implements MethodNameInflector
{
/** @var LastPartOfClassName */
private $lastPartOfClassName;
private LastPartOfClassName $lastPartOfClassName;

public function __construct()
{
$this->lastPartOfClassName = new LastPartOfClassName();
}

/**
* {@inheritdoc}
*/
public function getMethodName(string $commandClassName) : string
public function getMethodName(string $commandClassName): string
{
$commandName = $this->lastPartOfClassName->getMethodName($commandClassName);

return 'handle' . \ucfirst($commandName);
return 'handle' . ucfirst($commandName);
}
}
5 changes: 1 addition & 4 deletions src/Handler/Mapping/MethodName/Invoke.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
*/
final class Invoke implements MethodNameInflector
{
/**
* {@inheritdoc}
*/
public function getMethodName(string $commandClassName) : string
public function getMethodName(string $commandClassName): string
{
return '__invoke';
}
Expand Down
11 changes: 4 additions & 7 deletions src/Handler/Mapping/MethodName/LastPartOfClassName.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
*/
final class LastPartOfClassName implements MethodNameInflector
{
/**
* {@inheritdoc}
*/
public function getMethodName(string $commandClassNameName) : string
public function getMethodName(string $commandClassNameName): string
{
// If class name has a namespace separator, only take last portion
if (\strpos($commandClassNameName, '\\') !== false) {
$commandClassNameName = \substr($commandClassNameName, \strrpos($commandClassNameName, '\\') + 1);
if (strpos($commandClassNameName, '\\') !== false) {
$commandClassNameName = substr($commandClassNameName, strrpos($commandClassNameName, '\\') + 1);
}

return \strtolower($commandClassNameName[0]) . \substr($commandClassNameName, 1);
return strtolower($commandClassNameName[0]) . substr($commandClassNameName, 1);
}
}
2 changes: 1 addition & 1 deletion src/Handler/Mapping/MethodName/MethodNameInflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ interface MethodNameInflector
/**
* Return the method name to call on the command handler.
*/
public function getMethodName(string $commandClassName) : string;
public function getMethodName(string $commandClassName): string;
}
11 changes: 5 additions & 6 deletions src/Handler/Mapping/MethodToCall.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace League\Tactician\Handler\Mapping;
Expand All @@ -7,22 +8,20 @@

final class MethodToCall
{
/** @var string */
private $className;
private string $className;

/** @var string */
private $methodName;
private string $methodName;

public function __construct(string $className, 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')) {
if (! method_exists($className, $methodName) && ! method_exists($className, '__call')) {
throw MethodDoesNotExist::on($className, $methodName);
}

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

Expand Down

0 comments on commit b29e852

Please sign in to comment.