diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..b921988 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,8 @@ +parameters: + level: 8 + ignoreErrors: + #- message: '#Parameter \#1 \$data of static method kbATeam\\DataProtection\\SecureSearch::encrypt\(\) expects string, int given.#' + # path: tests/SecureSearchTest.php + paths: + - src/ + - tests/ diff --git a/src/Log/Engine/GraylogLog.php b/src/Log/Engine/GraylogLog.php index 2f930e6..0bbf535 100644 --- a/src/Log/Engine/GraylogLog.php +++ b/src/Log/Engine/GraylogLog.php @@ -143,7 +143,7 @@ public function __construct($config = []) * * @param mixed $level * @param string $message - * @param array $context + * @param array $context * * @return void * @@ -259,6 +259,7 @@ protected function createMessage(string $level, string $message): GelfMessage /** * Create a debug backtrace. */ + $trace = null; if ($add_file_and_line || $append_backtrace) { $trace = new ClassicBacktrace( $this->getConfig('trace_level_offset'), @@ -270,7 +271,7 @@ protected function createMessage(string $level, string $message): GelfMessage * In case the log didn't happen in memory (like with reflections), add * the filename and line to the message. */ - if ($add_file_and_line && $trace->lastStep('file') !== null) { + if ($add_file_and_line && is_object($trace) && $trace->lastStep('file') !== null) { $gelfMessage->setAdditional('file', $trace->lastStep('file')); $gelfMessage->setAdditional('line', $trace->lastStep('line')); } @@ -291,7 +292,7 @@ protected function createMessage(string $level, string $message): GelfMessage /** * Append backtrace in case it's not already in the message. */ - if ($append_backtrace) { + if ($append_backtrace && is_object($trace)) { /** * Append backtrace to message. */ @@ -302,9 +303,13 @@ protected function createMessage(string $level, string $message): GelfMessage /** * Set function output as additional field. */ - foreach ($this->getConfig('additional', []) as $key => $function) { - if (is_callable($function)) { - $gelfMessage->setAdditional($key, (string)$function()); + + $configStack = $this->getConfig('additional', []); + if (is_array($configStack)) { + foreach ($configStack as $key => $function) { + if (is_callable($function) && is_string($key)) { + $gelfMessage->setAdditional($key, (string)$function()); + } } } @@ -321,7 +326,7 @@ protected function createMessage(string $level, string $message): GelfMessage return $gelfMessage->setShortMessage($shortMessage); } return $gelfMessage - ->setShortMessage($shortMessage) + ->setShortMessage((string)$shortMessage) ->setFullMessage($message); } } diff --git a/tests/GraylogLogTest.php b/tests/GraylogLogTest.php index 05822c3..9f2055c 100644 --- a/tests/GraylogLogTest.php +++ b/tests/GraylogLogTest.php @@ -12,6 +12,7 @@ use InvalidArgumentException; use kbATeam\CakePhpGraylog\Log\Engine\GraylogLog; use LogicException; +use PHPUnit\Framework\AssertionFailedError; use PHPUnit\Framework\Exception; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; @@ -27,39 +28,41 @@ class GraylogLogTest extends TestCase /** * Test inheritance chain to ensure this test deals with the correct class. * @throws Exception + * @return void */ public function testInheritance() { $log = new PublicGraylogLog(); - static::assertInstanceOf(LoggerInterface::class, $log); - static::assertInstanceOf(BaseLog::class, $log); - static::assertInstanceOf(GraylogLog::class, $log); + $this->assertInstanceOf(LoggerInterface::class, $log); + $this->assertInstanceOf(BaseLog::class, $log); + $this->assertInstanceOf(GraylogLog::class, $log); } /** * Test default config settings to ensure that later settings are different. - * @throws PHPUnit_Framework_AssertionFailedError + * @throws AssertionFailedError * @throws InvalidArgumentException + * @return void */ public function testDefaultConfig() { $log = new PublicGraylogLog(); - static::assertSame('udp', $log->getMyConfig('scheme')); - static::assertSame('127.0.0.1', $log->getMyConfig('host')); - static::assertSame(12201, $log->getMyConfig('port')); - static::assertSame(UdpTransport::CHUNK_SIZE_LAN, $log->getMyConfig('chunk_size')); - static::assertNull($log->getMyConfig('ssl_options')); - static::assertSame('CakePHP', $log->getMyConfig('facility')); - static::assertFalse($log->getMyConfig('append_backtrace')); - static::assertFalse($log->getMyConfig('append_session')); - static::assertFalse($log->getMyConfig('append_post')); - static::assertSame([ + $this->assertSame('udp', $log->getMyConfig('scheme')); + $this->assertSame('127.0.0.1', $log->getMyConfig('host')); + $this->assertSame(12201, $log->getMyConfig('port')); + $this->assertSame(UdpTransport::CHUNK_SIZE_LAN, $log->getMyConfig('chunk_size')); + $this->assertNull($log->getMyConfig('ssl_options')); + $this->assertSame('CakePHP', $log->getMyConfig('facility')); + $this->assertFalse($log->getMyConfig('append_backtrace')); + $this->assertFalse($log->getMyConfig('append_session')); + $this->assertFalse($log->getMyConfig('append_post')); + $this->assertSame([ 'password', 'new_password', 'old_password', 'current_password' ], $log->getMyConfig('password_keys')); - static::assertSame([ + $this->assertSame([ LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL, @@ -73,18 +76,19 @@ public function testDefaultConfig() /** * Test that valid ssl options are being added to the configuration. - * @throws PHPUnit_Framework_Exception + * @throws Exception * @throws InvalidArgumentException + * @return void */ public function testValidSslOptions() { $log = new PublicGraylogLog(['ssl_options' => new SslOptions()]); - static::assertInstanceOf(SslOptions::class, $log->getMyConfig('ssl_options')); + $this->assertInstanceOf(SslOptions::class, $log->getMyConfig('ssl_options')); } /** * Provide invalid values for ssl options. - * @return array + * @return array> */ public static function provideInvalidSslOptions(): array { @@ -105,16 +109,17 @@ public static function provideInvalidSslOptions(): array * @param mixed $option * @dataProvider provideInvalidSslOptions * @throws InvalidArgumentException + * @return void */ public function testInvalidSslOptions($option) { $log = new PublicGraylogLog(['ssl_options' => $option]); - static::assertNull($log->getMyConfig('ssl_options')); + $this->assertNull($log->getMyConfig('ssl_options')); } /** * Data provider for connection URLs and their parsed values. - * @return array + * @return array> */ public static function provideConnectionUrl(): array { @@ -134,30 +139,30 @@ public static function provideConnectionUrl(): array * @param int $port * @dataProvider provideConnectionUrl * @throws InvalidArgumentException - * @throws InvalidArgumentException - * @throws InvalidArgumentException + * @return void */ public function testConnectionUrl(string $url, string $scheme, string $host, int $port) { $log = new PublicGraylogLog(['url' => $url]); - static::assertSame($scheme, $log->getMyConfig('scheme')); - static::assertSame($host, $log->getMyConfig('host')); - static::assertSame($port, $log->getMyConfig('port')); + $this->assertSame($scheme, $log->getMyConfig('scheme')); + $this->assertSame($host, $log->getMyConfig('host')); + $this->assertSame($port, $log->getMyConfig('port')); } /** * Test setting only certain log levels. * @throws InvalidArgumentException + * @return void */ public function testSettingLogLevels() { $log = new PublicGraylogLog(['levels' => [LogLevel::ERROR, LogLevel::WARNING, 'i5A64FtlPt']]); - static::assertSame([LogLevel::ERROR, LogLevel::WARNING], $log->getMyConfig('levels')); + $this->assertSame([LogLevel::ERROR, LogLevel::WARNING], $log->getMyConfig('levels')); } /** * Data provider of invalid log levels. - * @return array + * @return array */ public static function provideInvalidLogLevels(): array { @@ -177,13 +182,14 @@ public static function provideInvalidLogLevels(): array * Test setting only invalid log levels resulting in enabling all log levels. * @param mixed $levels * @dataProvider provideInvalidLogLevels + * @return void */ public function testInvalidLogLevels($levels) { $this->expectException(\TypeError::class); $log = new PublicGraylogLog(['levels' => $levels]); - static::assertSame([ + $this->assertSame([ LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL, @@ -198,10 +204,8 @@ public function testInvalidLogLevels($levels) /** * Test creating a GELF message with default settings. * @throws RuntimeException - * @throws PHPUnit_Framework_Exception - * @throws PHPUnit_Framework_Exception - * @throws PHPUnit_Framework_Exception - * @throws PHPUnit_Framework_Exception + * @throws Exception + * @return void */ public function testCreatingLongMessage() { @@ -221,58 +225,61 @@ public function testCreatingLongMessage() 'append_post' => true ]); $message = $log->createMessage(LogLevel::DEBUG, 'mnfiXQoolR'); - static::assertInstanceOf(GelfMessage::class, $message); - static::assertSame('CakePHP', $message->getAdditional('facility')); - static::assertSame(LogLevel::DEBUG, $message->getLevel()); - static::assertSame('mnfiXQoolR', $message->getShortMessage()); - static::assertCount(3, $message->getAllAdditionals()); - static::assertStringContainsString('POST:', $message->getFullMessage()); - static::assertStringContainsString('Session:', $message->getFullMessage()); - static::assertStringContainsString('Trace:', $message->getFullMessage()); + $this->assertInstanceOf(GelfMessage::class, $message); + $this->assertSame('CakePHP', $message->getAdditional('facility')); + $this->assertSame(LogLevel::DEBUG, $message->getLevel()); + $this->assertSame('mnfiXQoolR', $message->getShortMessage()); + $this->assertCount(3, $message->getAllAdditionals()); + $this->assertStringContainsString('POST:', (string)$message->getFullMessage()); + $this->assertStringContainsString('Session:', (string)$message->getFullMessage()); + $this->assertStringContainsString('Trace:', (string)$message->getFullMessage()); unset($_POST); } /** * Test creating a GELF message without any appended debug information. * @throws RuntimeException + * @return void */ public function testShortMessage() { $log = new PublicGraylogLog(); $message = $log->createMessage(LogLevel::ALERT, 'oIEUMcF1Ce'); - static::assertSame(LogLevel::ALERT, $message->getLevel()); - static::assertSame('oIEUMcF1Ce', $message->getShortMessage()); - static::assertNull($message->getFullMessage()); + $this->assertSame(LogLevel::ALERT, $message->getLevel()); + $this->assertSame('oIEUMcF1Ce', $message->getShortMessage()); + $this->assertNull($message->getFullMessage()); } /** * Test getting a UDP transport class from default configuration. * @throws InvalidArgumentException * @throws LogicException - * @throws PHPUnit_Framework_Exception + * @throws Exception + * @return void */ public function testUdpTransport() { $log = new PublicGraylogLog(['ignore_transport_errors' => false]); $transport = $log->getTransport(); - static::assertInstanceOf(UdpTransport::class, $transport); + $this->assertInstanceOf(UdpTransport::class, $transport); /** * Assert that we actually get the same instance. */ - static::assertSame($transport, $log->getTransport()); + $this->assertSame($transport, $log->getTransport()); } /** * Test getting a TCP transport class from default configuration. * @throws InvalidArgumentException * @throws LogicException - * @throws PHPUnit_Framework_Exception + * @throws Exception + * @return void */ public function testTcpTransport() { $log = new PublicGraylogLog(['scheme' => 'tcp', 'ignore_transport_errors' => false]); $transport = $log->getTransport(); - static::assertInstanceOf(TcpTransport::class, $transport); + $this->assertInstanceOf(TcpTransport::class, $transport); } /** @@ -280,20 +287,22 @@ public function testTcpTransport() * @return void * @throws InvalidArgumentException * @throws LogicException - * @throws PHPUnit_Framework_Exception + * @throws Exception + * @return void */ public function testTransportWrapper() { $log = new PublicGraylogLog(); $transport = $log->getTransport(); - static::assertInstanceOf(IgnoreErrorTransportWrapper::class, $transport); + $this->assertInstanceOf(IgnoreErrorTransportWrapper::class, $transport); } /** * Test getting an exception from an invalid scheme. * @throws InvalidArgumentException * @throws LogicException - * @throws PHPUnit_Framework_Exception + * @throws Exception + * @return void */ public function testInvalidScheme() { @@ -307,23 +316,25 @@ public function testInvalidScheme() * Test getting a publisher class from default configuration. * @throws InvalidArgumentException * @throws LogicException - * @throws PHPUnit_Framework_Exception + * @throws Exception + * @return void */ public function testPublisher() { $log = new PublicGraylogLog(); $publisher = $log->getPublisher(); - static::assertInstanceOf(Publisher::class, $publisher); + $this->assertInstanceOf(Publisher::class, $publisher); /** * Assert that we actually get the same instance. */ - static::assertSame($publisher, $log->getPublisher()); + $this->assertSame($publisher, $log->getPublisher()); } /** * Test adding additional field. * @throws RuntimeException * @throws RuntimeException + * @return void */ public function testAddingAdditionalFields() { @@ -335,16 +346,17 @@ public function testAddingAdditionalFields() ] ]); $message = $log->createMessage(LogLevel::INFO, 'Cy6BWVa5E0'); - static::assertSame(LogLevel::INFO, $message->getLevel()); - static::assertSame('Cy6BWVa5E0', $message->getShortMessage()); - static::assertNull($message->getFullMessage()); - static::assertSame('MSg9BrM4DG', $message->getAdditional('bmC5B27F3R')); + $this->assertSame(LogLevel::INFO, $message->getLevel()); + $this->assertSame('Cy6BWVa5E0', $message->getShortMessage()); + $this->assertNull($message->getFullMessage()); + $this->assertSame('MSg9BrM4DG', $message->getAdditional('bmC5B27F3R')); } /** * Test creating a GELF message with all flags enabled. * @throws RuntimeException - * @throws PHPUnit_Framework_Exception + * @throws Exception + * @return void */ public function testNoEmptyPostInLongMessage() { @@ -356,11 +368,11 @@ public function testNoEmptyPostInLongMessage() 'append_post' => true ]); $message = $log->createMessage(LogLevel::CRITICAL, 'oP6MkuApf9'); - static::assertInstanceOf(GelfMessage::class, $message); - static::assertSame('CakePHP', $message->getAdditional('facility')); - static::assertSame(LogLevel::CRITICAL, $message->getLevel()); - static::assertSame('oP6MkuApf9', $message->getShortMessage()); - static::assertCount(3, $message->getAllAdditionals()); - static::assertNull($message->getFullMessage()); + $this->assertInstanceOf(GelfMessage::class, $message); + $this->assertSame('CakePHP', $message->getAdditional('facility')); + $this->assertSame(LogLevel::CRITICAL, $message->getLevel()); + $this->assertSame('oP6MkuApf9', $message->getShortMessage()); + $this->assertCount(3, $message->getAllAdditionals()); + $this->assertNull($message->getFullMessage()); } } diff --git a/tests/GraylogWriteTest.php b/tests/GraylogWriteTest.php index c788c72..efc5a3d 100644 --- a/tests/GraylogWriteTest.php +++ b/tests/GraylogWriteTest.php @@ -16,7 +16,7 @@ class Publisher { /** - * @var TransportInterface + * @var TransportInterface|null */ public $transport; @@ -37,6 +37,7 @@ public function __construct(TransportInterface $transport = null) /** * @param MessageInterface $message * @noinspection PhpUnused + * @return void */ public function publish(MessageInterface $message) { @@ -51,6 +52,7 @@ public function publish(MessageInterface $message) use Gelf\Transport\UdpTransport; use InvalidArgumentException; use LogicException; + use PHPUnit\Framework\Exception; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_Exception; use PHPUnit_Framework_TestCase; @@ -63,10 +65,11 @@ class GraylogWriteTest extends TestCase { /** * Test writing a message using a fake publisher class. - * @throws PHPUnit_Framework_Exception + * @throws Exception * @throws InvalidArgumentException * @throws LogicException * @throws RuntimeException + * @return void */ public function testWriteUsingFakePublisher() {