Merge pull request #47291 from nextcloud/refactor/log-php-8-1

refactor(Log): Use new in initializer instead of constructor body
pull/47319/head
Côme Chilliet 8 months ago committed by GitHub
commit ba9638eae5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      lib/private/Log.php
  2. 2
      lib/private/Server.php
  3. 4
      psalm.xml
  4. 12
      tests/lib/LoggerTest.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 {

@ -735,7 +735,7 @@ class Server extends ServerContainer implements IServerContainer {
$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 */

@ -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"

@ -17,14 +17,11 @@ use OCP\Support\CrashReport\IRegistry;
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 @@ class LoggerTest extends TestCase implements IWriter {
$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 @@ class LoggerTest extends TestCase implements IWriter {
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']);

Loading…
Cancel
Save