From 6338b58570f634c1293684acd1fdc614bff2e3b6 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Wed, 12 Feb 2025 10:48:01 +0100 Subject: [PATCH] improve code style --- src/LoggerFactory.php | 2 +- src/ProcessorsTrait.php | 24 ++++++++++++++++-------- src/SensitiveFilterProcessor.php | 21 ++++++++++++++++++--- src/SpecialHandlersTrait.php | 13 ++++++++----- tests/ProcessorsTestTrait.php | 2 +- tests/SensitiveFilterProcessorTest.php | 6 +++--- tests/SpecialHandlersTestTrait.php | 2 +- 7 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/LoggerFactory.php b/src/LoggerFactory.php index 7a9686d..6ad1564 100644 --- a/src/LoggerFactory.php +++ b/src/LoggerFactory.php @@ -159,4 +159,4 @@ class LoggerFactory return OX_BASE_PATH . '/log' . DIRECTORY_SEPARATOR . $fileName; } -} \ No newline at end of file +} diff --git a/src/ProcessorsTrait.php b/src/ProcessorsTrait.php index 09e6420..9158c3b 100644 --- a/src/ProcessorsTrait.php +++ b/src/ProcessorsTrait.php @@ -23,7 +23,13 @@ use RuntimeException; trait ProcessorsTrait { - public function applyProcessors($logger, array $processorFlags): Logger + /** + * @param Logger $logger + * @param array> $processorFlags + * + * @return Logger + */ + public function applyProcessors(Logger $logger, array $processorFlags): Logger { $this->applyUidProcessor($processorFlags, $logger); $this->applyFilterSensitiveProcessor($processorFlags, $logger); @@ -32,11 +38,12 @@ trait ProcessorsTrait } /** - * @param array $processorFlags - * @param $logger + * @param array> $processorFlags + * @param Logger $logger + * * @return void */ - protected function applyUidProcessor(array $processorFlags, $logger): void + protected function applyUidProcessor(array $processorFlags, Logger $logger): void { if (in_array(self::PROCESSOR_UNIQUE_ID, $processorFlags, true) || in_array(self::PROCESSOR_UNIQUE_ID, array_keys($processorFlags), true) @@ -46,11 +53,12 @@ trait ProcessorsTrait } /** - * @param array $processorFlags - * @param $logger + * @param array> $processorFlags + * @param Logger $logger + * * @return void */ - protected function applyFilterSensitiveProcessor(array $processorFlags, $logger): void + protected function applyFilterSensitiveProcessor(array $processorFlags, Logger $logger): void { if (in_array(self::PROCESSOR_FILTERSENSITIVE, array_keys($processorFlags), true)) { $options = $processorFlags[self::PROCESSOR_FILTERSENSITIVE] ?? []; @@ -63,4 +71,4 @@ trait ProcessorsTrait $logger->pushProcessor(new SensitiveFilterProcessor($searchList)); } } -} \ No newline at end of file +} diff --git a/src/SensitiveFilterProcessor.php b/src/SensitiveFilterProcessor.php index 8c144dd..12c62e0 100644 --- a/src/SensitiveFilterProcessor.php +++ b/src/SensitiveFilterProcessor.php @@ -21,14 +21,26 @@ use Monolog\Processor\ProcessorInterface; class SensitiveFilterProcessor implements ProcessorInterface { - public function __construct(protected array $secrets, protected ?string $replacement = null) + protected string $replacement; + + /** + * @param string[] $secrets + * @param string|null $replacement + */ + public function __construct(protected array $secrets, ?string $replacement = null) { - $this->replacement ??= '*****'; + $this->replacement = $replacement ?? '*****'; $this->convertStringsToRegex($this->secrets); } + /** + * @param string[] $search + * + * @return void + */ protected function convertStringsToRegex(array $search = []): void { + $searchStrings = []; array_map( function ($search) use (&$searchStrings) { if (!$this->stringIsRegexp($search)) { @@ -61,9 +73,12 @@ class SensitiveFilterProcessor implements ProcessorInterface $item ); } elseif (is_array($item)) { + /** @phpstan-ignore argument.type */ $records[$key] = $this($item); } } + + /** @phpstan-ignore return.type */ return $records; } -} \ No newline at end of file +} diff --git a/src/SpecialHandlersTrait.php b/src/SpecialHandlersTrait.php index 1266c18..e7fe394 100644 --- a/src/SpecialHandlersTrait.php +++ b/src/SpecialHandlersTrait.php @@ -72,8 +72,9 @@ trait SpecialHandlersTrait } /** - * @param array $specialHandlerFlags + * @param array> $specialHandlerFlags * @param HandlerInterface $handler + * * @return HandlerInterface */ protected function applyBufferHandler(array $specialHandlerFlags, HandlerInterface $handler): HandlerInterface @@ -94,8 +95,9 @@ trait SpecialHandlersTrait } /** - * @param array $specialHandlerFlags + * @param array> $specialHandlerFlags * @param HandlerInterface $handler + * * @return HandlerInterface */ protected function applyLogOnErrorOnlyHandler(array $specialHandlerFlags, HandlerInterface $handler): HandlerInterface @@ -114,8 +116,9 @@ trait SpecialHandlersTrait } /** - * @param array $specialHandlerFlags + * @param array> $specialHandlerFlags * @param HandlerInterface $handler + * * @return HandlerInterface */ protected function applyMakeUniqueHandler(array $specialHandlerFlags, HandlerInterface $handler): HandlerInterface @@ -126,7 +129,7 @@ trait SpecialHandlersTrait } elseif (in_array(self::SPECIAL_HANDLERS_MAKE_UNIQUE, array_keys($specialHandlerFlags), true)) { $options = $specialHandlerFlags[self::SPECIAL_HANDLERS_MAKE_UNIQUE]; $handler = $this->makeUnique( - /** @phpstan-ignore argument.type */ + /** @phpstan-ignore argument.type */ $handler, /** @phpstan-ignore argument.type */ $options[self::MAKEUNIQUE_OPTION_LEVEL] ?? Logger::ERROR, @@ -136,4 +139,4 @@ trait SpecialHandlersTrait } return $handler; } -} \ No newline at end of file +} diff --git a/tests/ProcessorsTestTrait.php b/tests/ProcessorsTestTrait.php index f7d6127..16eef1e 100644 --- a/tests/ProcessorsTestTrait.php +++ b/tests/ProcessorsTestTrait.php @@ -132,4 +132,4 @@ trait ProcessorsTestTrait yield 'misconfiguration' => [[LoggerFactory::PROCESSOR_FILTERSENSITIVE => [LoggerFactory::FILTERSENSITIVE_SECRETS => 'foo']], 0, true]; yield 'no configuration' => [[], 0, false]; } -} \ No newline at end of file +} diff --git a/tests/SensitiveFilterProcessorTest.php b/tests/SensitiveFilterProcessorTest.php index a1fc184..669e4ce 100644 --- a/tests/SensitiveFilterProcessorTest.php +++ b/tests/SensitiveFilterProcessorTest.php @@ -122,7 +122,7 @@ class SensitiveFilterProcessorTest extends ApiTestCase 'subkey3' => [ 'subsubkey3' => 'value+password+value+secret1', ], - ] + ], ]; $expected = [ @@ -135,7 +135,7 @@ class SensitiveFilterProcessorTest extends ApiTestCase 'subkey3' => [ 'subsubkey3' => 'value+###+value+###', ], - ] + ], ]; $this->assertSame( @@ -147,4 +147,4 @@ class SensitiveFilterProcessorTest extends ApiTestCase ) ); } -} \ No newline at end of file +} diff --git a/tests/SpecialHandlersTestTrait.php b/tests/SpecialHandlersTestTrait.php index 4fc34d6..2ccad2d 100644 --- a/tests/SpecialHandlersTestTrait.php +++ b/tests/SpecialHandlersTestTrait.php @@ -243,4 +243,4 @@ trait SpecialHandlersTestTrait DeduplicationHandler::class, ]; } -} \ No newline at end of file +}