Skip to content
Merged
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
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/tests export-ignore
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
4 changes: 2 additions & 2 deletions .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.2', '8.3', '8.4', '8.5']
runs-on: ${{ matrix.operating-system }}
steps:
- name: Checkout
Expand All @@ -22,7 +22,7 @@ jobs:
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: ${{ steps.composercache.outputs.dir }}
# Use composer.json for key, if composer.lock is not committed.
Expand Down
18 changes: 0 additions & 18 deletions CHANGELOG.md

This file was deleted.

21 changes: 13 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
}
],
"require": {
"php" : ">=7.4",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"php" : "^8.2",
"psr/log": "^2.0 || ^3.0",
"league/tactician": "dev-master"
},
"require-dev": {
"phpunit/phpunit" : "^8.5",
"squizlabs/php_codesniffer": "^3.5",
"phpstan/phpstan": "^0.12.3",
"phpstan/phpstan-phpunit": "^0.12.3",
"phpstan/extension-installer": "^1.0",
"doctrine/coding-standard": "^8.2"
"phpunit/phpunit" : "^11.5",
"phpstan/phpstan": "^2.1",
"phpstan/phpstan-phpunit": "^2.0",
"phpstan/extension-installer": "^1.4",
"doctrine/coding-standard": "^14.0"
},
"autoload": {
"psr-4": {
Expand All @@ -35,5 +34,11 @@
"psr-4": {
"League\\Tactician\\Logger\\Tests\\": "tests"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"phpstan/extension-installer": true
}
}
}
2 changes: 2 additions & 0 deletions phpcs.xml → phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>

<config name="php_version" value="80200" />

<!-- Ignore warnings, show progress of the run and show sniff names -->
<arg value="nps"/>

Expand Down
10 changes: 10 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
level: max
paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests

ignoreErrors:
-
identifier: property.onlyWritten
path: tests/Fixtures/RegisterUserCommand.php
33 changes: 11 additions & 22 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="League Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" />
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
30 changes: 8 additions & 22 deletions src/Formatter/ClassNameFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,34 @@
use Psr\Log\LogLevel;
use Throwable;

use function get_class;

/**
* Returns log messages only dump the Command & Exception's class names.
*/
class ClassNameFormatter implements Formatter
{
private string $commandReceivedLevel;

private string $commandSucceededLevel;

private string $commandFailedLevel;

public function __construct(
string $commandReceivedLevel = LogLevel::DEBUG,
string $commandSucceededLevel = LogLevel::DEBUG,
string $commandFailedLevel = LogLevel::ERROR
private string $commandReceivedLevel = LogLevel::DEBUG,
private string $commandSucceededLevel = LogLevel::DEBUG,
private string $commandFailedLevel = LogLevel::ERROR,
) {
$this->commandReceivedLevel = $commandReceivedLevel;
$this->commandSucceededLevel = $commandSucceededLevel;
$this->commandFailedLevel = $commandFailedLevel;
}

public function logCommandReceived(LoggerInterface $logger, object $command): void
{
$logger->log($this->commandReceivedLevel, 'Command received: ' . get_class($command), []);
$logger->log($this->commandReceivedLevel, 'Command received: ' . $command::class, []);
}

/**
* {@inheritDoc}
*/
public function logCommandSucceeded(LoggerInterface $logger, object $command, $returnValue): void
public function logCommandSucceeded(LoggerInterface $logger, object $command, mixed $returnValue): void
{
$logger->log($this->commandSucceededLevel, 'Command succeeded: ' . get_class($command), []);
$logger->log($this->commandSucceededLevel, 'Command succeeded: ' . $command::class, []);
}

public function logCommandFailed(LoggerInterface $logger, object $command, Throwable $e): void
{
$logger->log(
$this->commandFailedLevel,
'Command failed: ' . get_class($command),
['exception' => $e]
'Command failed: ' . $command::class,
['exception' => $e],
);
}
}
38 changes: 12 additions & 26 deletions src/Formatter/ClassPropertiesFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,48 @@
use Psr\Log\LogLevel;
use Throwable;

use function get_class;

/**
* Formatter that includes the Command's name and properties for more detail
*/
class ClassPropertiesFormatter implements Formatter
{
private PropertyNormalizer $normalizer;

private string $commandReceivedLevel;

private string $commandSucceededLevel;

private string $commandFailedLevel;

public function __construct(
?PropertyNormalizer $normalizer = null,
string $commandReceivedLevel = LogLevel::DEBUG,
string $commandSucceededLevel = LogLevel::DEBUG,
string $commandFailedLevel = LogLevel::ERROR
PropertyNormalizer|null $normalizer = null,
private string $commandReceivedLevel = LogLevel::DEBUG,
private string $commandSucceededLevel = LogLevel::DEBUG,
private string $commandFailedLevel = LogLevel::ERROR,
) {
$this->normalizer = $normalizer ?: new SimplePropertyNormalizer();
$this->commandReceivedLevel = $commandReceivedLevel;
$this->commandSucceededLevel = $commandSucceededLevel;
$this->commandFailedLevel = $commandFailedLevel;
$this->normalizer = $normalizer ?: new SimplePropertyNormalizer();
}

public function logCommandReceived(LoggerInterface $logger, object $command): void
{
$logger->log(
$this->commandReceivedLevel,
'Command received: ' . get_class($command),
['command' => $this->normalizer->normalize($command)]
'Command received: ' . $command::class,
['command' => $this->normalizer->normalize($command)],
);
}

/**
* {@inheritDoc}
*/
public function logCommandSucceeded(LoggerInterface $logger, object $command, $returnValue): void
public function logCommandSucceeded(LoggerInterface $logger, object $command, mixed $returnValue): void
{
$logger->log(
$this->commandSucceededLevel,
'Command succeeded: ' . get_class($command),
'Command succeeded: ' . $command::class,
[
'command' => $this->normalizer->normalize($command),
]
],
);
}

public function logCommandFailed(LoggerInterface $logger, object $command, Throwable $e): void
{
$logger->log(
$this->commandFailedLevel,
'Command failed: ' . get_class($command),
['exception' => $e]
'Command failed: ' . $command::class,
['exception' => $e],
);
}
}
5 changes: 1 addition & 4 deletions src/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ interface Formatter
{
public function logCommandReceived(LoggerInterface $logger, object $command): void;

/**
* @param mixed $returnValue
*/
public function logCommandSucceeded(LoggerInterface $logger, object $command, $returnValue): void;
public function logCommandSucceeded(LoggerInterface $logger, object $command, mixed $returnValue): void;

public function logCommandFailed(LoggerInterface $logger, object $command, Throwable $e): void;
}
17 changes: 5 additions & 12 deletions src/LoggerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,13 @@
*/
class LoggerMiddleware implements Middleware
{
private LoggerInterface $logger;

private Formatter $formatter;

public function __construct(Formatter $formatter, LoggerInterface $logger)
{
$this->formatter = $formatter;
$this->logger = $logger;
public function __construct(
private Formatter $formatter,
private LoggerInterface $logger,
) {
}

/**
* {@inheritdoc}
*/
public function execute(object $command, callable $next)
public function execute(object $command, callable $next): mixed
{
$this->formatter->logCommandReceived($this->logger, $command);

Expand Down
11 changes: 4 additions & 7 deletions src/PropertyNormalizer/SimplePropertyNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use ReflectionClass;

use function get_class;
use function get_resource_type;
use function gettype;

Expand All @@ -20,10 +19,10 @@
*/
class SimplePropertyNormalizer implements PropertyNormalizer
{
/** {@inheritDoc} */
/** @return array<string, mixed> */
public function normalize(object $command): array
{
$reflectionClass = new ReflectionClass(get_class($command));
$reflectionClass = new ReflectionClass($command::class);

$properties = [];
foreach ($reflectionClass->getProperties() as $property) {
Expand All @@ -38,14 +37,12 @@ public function normalize(object $command): array
* Return the given (property) value as a descriptive string
*
* @param mixed $value Can be literally anything
*
* @return mixed
*/
protected function formatValue($value)
protected function formatValue(mixed $value): mixed
{
switch (gettype($value)) {
case 'object':
return 'object(' . get_class($value) . ')';
return 'object(' . $value::class . ')';

case 'array':
return '*array*';
Expand Down
10 changes: 5 additions & 5 deletions tests/Formatter/ClassNameFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testBasicSuccessMessageIsLogged(): void
$this->logger->expects(self::once())->method('log')->with(
LogLevel::DEBUG,
'Command succeeded: ' . RegisterUserCommand::class,
[]
[],
);

$this->formatter->logCommandSucceeded($this->logger, new RegisterUserCommand(), null);
Expand All @@ -41,7 +41,7 @@ public function testCommandReceivedCreatesExpectedMessage(): void
$this->logger->expects(self::once())->method('log')->with(
LogLevel::DEBUG,
'Command received: ' . RegisterUserCommand::class,
[]
[],
);

$this->formatter->logCommandReceived($this->logger, new RegisterUserCommand());
Expand All @@ -54,7 +54,7 @@ public function testCommandFailedCreatesExpectedMessage(): void
$this->logger->expects(self::once())->method('log')->with(
LogLevel::ERROR,
'Command failed: ' . RegisterUserCommand::class,
['exception' => $exception]
['exception' => $exception],
);

$this->formatter->logCommandFailed($this->logger, new RegisterUserCommand(), $exception);
Expand All @@ -69,7 +69,7 @@ public function testCustomReceivedLogLevel(): void
->method('log')
->with(
LogLevel::WARNING,
'Command received: League\Tactician\Logger\Tests\Fixtures\RegisterUserCommand'
'Command received: League\Tactician\Logger\Tests\Fixtures\RegisterUserCommand',
);

$formatter->logCommandReceived($this->logger, new RegisterUserCommand());
Expand All @@ -84,7 +84,7 @@ public function testCustomSuccessLogLevel(): void
->method('log')
->with(
LogLevel::NOTICE,
'Command succeeded: League\Tactician\Logger\Tests\Fixtures\RegisterUserCommand'
'Command succeeded: League\Tactician\Logger\Tests\Fixtures\RegisterUserCommand',
);
$formatter->logCommandSucceeded($this->logger, new RegisterUserCommand(), null);
}
Expand Down
Loading
Loading