From 2ec68b1eb90aca54e69defd42bf0a610a09f4f49 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sat, 17 Aug 2024 00:04:55 +0200 Subject: [PATCH] refactor(Log): Use new in initializer instead of constructor body PHP 8.1 allows us to now move the `new` into the initializer, this makes the code a bit nicer (and 3 lines shorter). Signed-off-by: Ferdinand Thiessen --- lib/private/Log.php | 6 +----- lib/private/Server.php | 2 +- psalm.xml | 4 ++-- tests/lib/LoggerTest.php | 12 +++++------- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/private/Log.php b/lib/private/Log.php index 4fce04367079f..ea99564283220 100644 --- a/lib/private/Log.php +++ b/lib/private/Log.php @@ -41,13 +41,9 @@ class Log implements ILogger, IDataLogger { public function __construct( private IWriter $logger, private SystemConfig $config, - private ?Normalizer $normalizer = null, + private Normalizer $normalizer = new Normalizer(), private ?IRegistry $crashReporters = null ) { - // FIXME: php8.1 allows "private Normalizer $normalizer = new Normalizer()," in initializer - if ($normalizer === null) { - $this->normalizer = new Normalizer(); - } } public function setEventDispatcher(IEventDispatcher $eventDispatcher): void { diff --git a/lib/private/Server.php b/lib/private/Server.php index a0072b43ee20a..aaf4c925f9885 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -735,7 +735,7 @@ public function __construct($webRoot, \OC\Config $config) { $logger = $factory->get($logType); $registry = $c->get(\OCP\Support\CrashReport\IRegistry::class); - return new Log($logger, $this->get(SystemConfig::class), null, $registry); + return new Log($logger, $this->get(SystemConfig::class), crashReporters: $registry); }); $this->registerAlias(ILogger::class, \OC\Log::class); /** @deprecated 19.0.0 */ diff --git a/psalm.xml b/psalm.xml index dece0c3eb8886..1d62b7327bc30 100644 --- a/psalm.xml +++ b/psalm.xml @@ -8,8 +8,8 @@ resolveFromConfigFile="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" - xsi:schemaLocation="https://getpsalm.org/schema/config" - errorBaseline="build/psalm-baseline.xml" + xsi:schemaLocation="https://getpsalm.org/schema/config https://getpsalm.org/schema/config" + errorBaseline="build/psalm-baseline.xml" findUnusedBaselineEntry="false" findUnusedCode="false" phpVersion="8.1" diff --git a/tests/lib/LoggerTest.php b/tests/lib/LoggerTest.php index 6343ce6206501..888190bcd970c 100644 --- a/tests/lib/LoggerTest.php +++ b/tests/lib/LoggerTest.php @@ -17,14 +17,11 @@ use PHPUnit\Framework\MockObject\MockObject; class LoggerTest extends TestCase implements IWriter { - /** @var SystemConfig|MockObject */ - private $config; + private SystemConfig&MockObject $config; - /** @var IRegistry|MockObject */ - private $registry; + private IRegistry&MockObject $registry; - /** @var ILogger */ - private $logger; + private Log $logger; /** @var array */ private array $logs = []; @@ -35,7 +32,7 @@ protected function setUp(): void { $this->logs = []; $this->config = $this->createMock(SystemConfig::class); $this->registry = $this->createMock(IRegistry::class); - $this->logger = new Log($this, $this->config, null, $this->registry); + $this->logger = new Log($this, $this->config, crashReporters: $this->registry); } private function mockDefaultLogLevel(): void { @@ -165,6 +162,7 @@ public function testMatchesCondition(string $userId, array $conditions, array $e public function testLoggingWithDataArray(): void { $this->mockDefaultLogLevel(); + /** @var IWriter&MockObject */ $writerMock = $this->createMock(IWriter::class); $logFile = new Log($writerMock, $this->config); $writerMock->expects($this->once())->method('write')->with('no app in context', ['something' => 'extra', 'message' => 'Testing logging with john']);