Skip to content

Commit ba9638e

Browse files
authored
Merge pull request #47291 from nextcloud/refactor/log-php-8-1
refactor(Log): Use new in initializer instead of constructor body
2 parents 40c91aa + 2ec68b1 commit ba9638e

File tree

4 files changed

+9
-15
lines changed

4 files changed

+9
-15
lines changed

lib/private/Log.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,9 @@ class Log implements ILogger, IDataLogger {
4141
public function __construct(
4242
private IWriter $logger,
4343
private SystemConfig $config,
44-
private ?Normalizer $normalizer = null,
44+
private Normalizer $normalizer = new Normalizer(),
4545
private ?IRegistry $crashReporters = null
4646
) {
47-
// FIXME: php8.1 allows "private Normalizer $normalizer = new Normalizer()," in initializer
48-
if ($normalizer === null) {
49-
$this->normalizer = new Normalizer();
50-
}
5147
}
5248

5349
public function setEventDispatcher(IEventDispatcher $eventDispatcher): void {

lib/private/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ public function __construct($webRoot, \OC\Config $config) {
735735
$logger = $factory->get($logType);
736736
$registry = $c->get(\OCP\Support\CrashReport\IRegistry::class);
737737

738-
return new Log($logger, $this->get(SystemConfig::class), null, $registry);
738+
return new Log($logger, $this->get(SystemConfig::class), crashReporters: $registry);
739739
});
740740
$this->registerAlias(ILogger::class, \OC\Log::class);
741741
/** @deprecated 19.0.0 */

psalm.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
resolveFromConfigFile="true"
99
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1010
xmlns="https://getpsalm.org/schema/config"
11-
xsi:schemaLocation="https://getpsalm.org/schema/config"
12-
errorBaseline="build/psalm-baseline.xml"
11+
xsi:schemaLocation="https://getpsalm.org/schema/config https://getpsalm.org/schema/config"
12+
errorBaseline="build/psalm-baseline.xml"
1313
findUnusedBaselineEntry="false"
1414
findUnusedCode="false"
1515
phpVersion="8.1"

tests/lib/LoggerTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@
1717
use PHPUnit\Framework\MockObject\MockObject;
1818

1919
class LoggerTest extends TestCase implements IWriter {
20-
/** @var SystemConfig|MockObject */
21-
private $config;
20+
private SystemConfig&MockObject $config;
2221

23-
/** @var IRegistry|MockObject */
24-
private $registry;
22+
private IRegistry&MockObject $registry;
2523

26-
/** @var ILogger */
27-
private $logger;
24+
private Log $logger;
2825

2926
/** @var array */
3027
private array $logs = [];
@@ -35,7 +32,7 @@ protected function setUp(): void {
3532
$this->logs = [];
3633
$this->config = $this->createMock(SystemConfig::class);
3734
$this->registry = $this->createMock(IRegistry::class);
38-
$this->logger = new Log($this, $this->config, null, $this->registry);
35+
$this->logger = new Log($this, $this->config, crashReporters: $this->registry);
3936
}
4037

4138
private function mockDefaultLogLevel(): void {
@@ -165,6 +162,7 @@ public function testMatchesCondition(string $userId, array $conditions, array $e
165162

166163
public function testLoggingWithDataArray(): void {
167164
$this->mockDefaultLogLevel();
165+
/** @var IWriter&MockObject */
168166
$writerMock = $this->createMock(IWriter::class);
169167
$logFile = new Log($writerMock, $this->config);
170168
$writerMock->expects($this->once())->method('write')->with('no app in context', ['something' => 'extra', 'message' => 'Testing logging with john']);

0 commit comments

Comments
 (0)