Skip to content

Commit

Permalink
Merge pull request #47291 from nextcloud/refactor/log-php-8-1
Browse files Browse the repository at this point in the history
refactor(Log): Use new in initializer instead of constructor body
  • Loading branch information
come-nc authored Aug 19, 2024
2 parents 40c91aa + 2ec68b1 commit ba9638e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
6 changes: 1 addition & 5 deletions lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 5 additions & 7 deletions tests/lib/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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 {
Expand Down Expand Up @@ -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']);
Expand Down

0 comments on commit ba9638e

Please sign in to comment.