From ae61728591812d136e3c85b4c7993e808addf2c0 Mon Sep 17 00:00:00 2001 From: Oleg Kasyanov Date: Mon, 2 Dec 2024 16:36:52 +0400 Subject: [PATCH] PhpStan warnings have been resolved --- composer.json | 1 - src/SessionHandler.php | 14 ++++++- src/SessionMiddlewareFactory.php | 3 ++ tests/unit/SessionHandlerTest.php | 43 ++++++++++++--------- tests/unit/SessionMiddlewareFactoryTest.php | 21 ++++++---- tests/unit/SessionMiddlewareTest.php | 18 +++++---- 6 files changed, 64 insertions(+), 36 deletions(-) diff --git a/composer.json b/composer.json index 7d727f4..9ae9437 100644 --- a/composer.json +++ b/composer.json @@ -68,7 +68,6 @@ "check": [ "@cs-check", "@static-analysis", - "@taint-analysis", "@test" ], "cs-check": "phpcs", diff --git a/src/SessionHandler.php b/src/SessionHandler.php index 9917d27..62fbedb 100644 --- a/src/SessionHandler.php +++ b/src/SessionHandler.php @@ -23,8 +23,15 @@ final class SessionHandler implements SessionInterface private int $lifeTime = 10800; private string $sessionId = ''; + + /** + * @var array + */ private array $data = []; + /** + * @param array $options + */ public function __construct(PDO $pdo, CookieManagerInterface $cookieManager, array $options = []) { $this->pdo = $pdo; @@ -36,13 +43,13 @@ public function startSession(ServerRequestInterface $request): void { $id = trim((string) ($request->getCookieParams()[$this->cookieName] ?? '')); - if (! empty($id) && strlen($id) == 32) { + if ($id !== '' && strlen($id) === 32) { $this->sessionId = $id; $stmt = $this->pdo->prepare('SELECT * FROM `system__session` WHERE `session_id` = :id'); $stmt->bindValue(':id', $id); $stmt->execute(); - /** @var array|false $result */ + /** @var array|false $result */ $result = $stmt->fetch(); if ($result !== false && $result['modified'] > time() - $this->lifeTime) { @@ -128,6 +135,9 @@ private function sendCookies(string $id, ResponseInterface $response): ResponseI return $response->withHeader('Pragma', 'no-cache'); } + /** + * @param array $options + */ private function resolveOptions(array $options): void { if (isset($options['cookie_name'])) { diff --git a/src/SessionMiddlewareFactory.php b/src/SessionMiddlewareFactory.php index 34548c0..444e33b 100644 --- a/src/SessionMiddlewareFactory.php +++ b/src/SessionMiddlewareFactory.php @@ -41,6 +41,9 @@ public function __invoke(ContainerInterface $container): SessionMiddleware return new SessionMiddleware($session); } + /** + * @param array $config + */ public function checkGc(array $config): bool { $file = (string) ($config['gc_timestamp_file'] ?? ''); diff --git a/tests/unit/SessionHandlerTest.php b/tests/unit/SessionHandlerTest.php index 629ab19..6fe0357 100644 --- a/tests/unit/SessionHandlerTest.php +++ b/tests/unit/SessionHandlerTest.php @@ -16,6 +16,10 @@ class SessionHandlerTest extends MysqlTestCase { private SessionHandler $session; private ServerRequestInterface $request; + + /** + * @var array + */ private array $options = [ 'cookie_name' => 'TESTSESSION', 'cookie_domain' => 'localhost', @@ -31,7 +35,7 @@ public function setUp(): void $loader->loadFile('install/sql/tables.sql'); if ($loader->hasErrors()) { - $this->fail(implode("\n", $loader->getErrors())); + self::fail(implode("\n", $loader->getErrors())); } $this->session = new SessionHandler( @@ -47,46 +51,47 @@ public function setUp(): void public function testImplementsSessionInterface(): void { - $this->assertInstanceOf(SessionInterface::class, $this->session); + /** @phpstan-ignore staticMethod.alreadyNarrowedType */ + self::assertInstanceOf(SessionInterface::class, $this->session); } public function testHasMethodWithExistingKey(): void { $this->initializeSessionWithData(); - $this->assertTrue($this->session->has('foo')); + self::assertTrue($this->session->has('foo')); } public function testHasMethodWithNonExistentKey(): void { $this->initializeSessionWithData(); - $this->assertFalse($this->session->has('bar')); + self::assertFalse($this->session->has('bar')); } public function testGetMethodWithExistingKey(): void { $this->initializeSessionWithData(); - $this->assertSame('test-session', $this->session->get('foo')); + self::assertSame('test-session', $this->session->get('foo')); } public function testGetMethodWithNonExistentKeyReturnNull(): void { $this->initializeSessionWithData(); - $this->assertNull($this->session->get('bar')); + self::assertNull($this->session->get('bar')); } public function testGetMethodWithNonExistentKeyReturnDefaultValue(): void { $this->initializeSessionWithData(); - $this->assertSame('mydata', $this->session->get('bar', 'mydata')); + self::assertSame('mydata', $this->session->get('bar', 'mydata')); } public function testUnsetMethod(): void { $this->initializeSessionWithData(); - $this->assertTrue($this->session->has('foo')); + self::assertTrue($this->session->has('foo')); $this->session->unset('foo'); $this->session->unset('bar'); - $this->assertFalse($this->session->has('foo')); + self::assertFalse($this->session->has('foo')); } public function testSetMethod(): void @@ -94,8 +99,8 @@ public function testSetMethod(): void $this->initializeSessionWithData(); $this->session->set('foo', 'newdata'); $this->session->set('baz', 'bat'); - $this->assertSame('newdata', $this->session->get('foo')); - $this->assertSame('bat', $this->session->get('baz')); + self::assertSame('newdata', $this->session->get('foo')); + self::assertSame('bat', $this->session->get('baz')); } public function testClearMethod(): void @@ -103,14 +108,14 @@ public function testClearMethod(): void $this->initializeSessionWithData(); $this->session->set('baz', 'bat'); $this->session->clear(); - $this->assertFalse($this->session->has('foo')); - $this->assertFalse($this->session->has('baz')); + self::assertFalse($this->session->has('foo')); + self::assertFalse($this->session->has('baz')); } public function testWithExpiredData(): void { - $this->initializeSessionWithData(30000); - $this->assertFalse($this->session->has('foo')); + self::initializeSessionWithData(30000); + self::assertFalse($this->session->has('foo')); } public function testGarbageCollector(): void @@ -120,7 +125,7 @@ public function testGarbageCollector(): void $query = self::getPdo()->query( "SELECT * FROM `system__session` WHERE `session_id` = 'ssssssssssssssssssssssssssssssss'" ); - $this->assertEquals(0, $query->rowCount()); + self::assertEquals(0, $query->rowCount()); } public function testPersistenceWithExistingSessionId(): void @@ -135,7 +140,7 @@ public function testPersistenceWithExistingSessionId(): void $this->options ); $session2->startSession($this->request); - $this->assertEquals('bat', $session2->get('baz')); + self::assertEquals('bat', $session2->get('baz')); } public function testPeresistenceGenerateNewsessionId(): void @@ -156,7 +161,7 @@ public function testPeresistenceGenerateNewsessionId(): void $this->options ); $newSession->startSession($request); - $this->assertEquals('bat', $newSession->get('baz')); + self::assertEquals('bat', $newSession->get('baz')); } public function testPersistenceIfNoIdAndNoData(): void @@ -164,7 +169,7 @@ public function testPersistenceIfNoIdAndNoData(): void $this->session->startSession($this->createMock(ServerRequestInterface::class)); $this->session->persistSession(new Response()); $query = self::getPdo()->query('SELECT * FROM `system__session`'); - $this->assertEquals(0, $query->rowCount()); + self::assertEquals(0, $query->rowCount()); } private function initializeSessionWithData(int $modified = 0): void diff --git a/tests/unit/SessionMiddlewareFactoryTest.php b/tests/unit/SessionMiddlewareFactoryTest.php index 1fec117..3f9df1c 100644 --- a/tests/unit/SessionMiddlewareFactoryTest.php +++ b/tests/unit/SessionMiddlewareFactoryTest.php @@ -12,6 +12,7 @@ use Mobicms\Testutils\MysqlTestCase; use Mobicms\Testutils\SqlDumpLoader; use PDO; +use PHPUnit\Framework\MockObject\Exception; use Psr\Container\ContainerInterface; use function is_file; @@ -51,7 +52,7 @@ public function testExceptionIfTimestampFileIsNotWritable(): void public function testNeedGarbageCollection(): void { touch($this->file, time() - 10000); - $this->assertTrue( + self::assertTrue( $this->factory->checkGc( [ 'gc_timestamp_file' => $this->file, @@ -64,7 +65,7 @@ public function testNeedGarbageCollection(): void public function testNotNeedGarbageCollection(): void { touch($this->file); - $this->assertFalse( + self::assertFalse( $this->factory->checkGc( [ 'gc_timestamp_file' => $this->file, @@ -81,23 +82,28 @@ public function testCreateTimestampFile(): void } $this->factory->checkGc(['gc_timestamp_file' => $this->file]); - $this->assertTrue(is_file($this->file)); + self::assertTrue(is_file($this->file)); } - public function testFactoryReturnsSessionMiddlewareInstance() + public function testFactoryReturnsSessionMiddlewareInstance(): void { $loader = new SqlDumpLoader(self::getPdo()); $loader->loadFile('install/sql/tables.sql'); if ($loader->hasErrors()) { - $this->fail(implode("\n", $loader->getErrors())); + self::fail(implode("\n", $loader->getErrors())); } touch($this->file, time() - 10000); $result = (new SessionMiddlewareFactory())($this->getContainer(['gc_timestamp_file' => $this->file])); - $this->assertInstanceOf(SessionMiddleware::class, $result); + /** @phpstan-ignore staticMethod.alreadyNarrowedType */ + self::assertInstanceOf(SessionMiddleware::class, $result); } + /** + * @param array $options + * @throws Exception + */ private function getContainer(array $options): ContainerInterface { $config = $this->createMock(ConfigInterface::class); @@ -117,7 +123,8 @@ private function getContainer(array $options): ContainerInterface fn($val) => match ($val) { ConfigInterface::class => $config, PDO::class => self::getPdo(), - CookieManagerInterface::class => $this->createMock(CookieManagerInterface::class) + CookieManagerInterface::class => $this->createMock(CookieManagerInterface::class), + default => null, } ); diff --git a/tests/unit/SessionMiddlewareTest.php b/tests/unit/SessionMiddlewareTest.php index 912260b..5950e5c 100644 --- a/tests/unit/SessionMiddlewareTest.php +++ b/tests/unit/SessionMiddlewareTest.php @@ -10,13 +10,15 @@ use Mobicms\Testutils\MysqlTestCase; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; class SessionMiddlewareTest extends MysqlTestCase { private SessionMiddleware $middleware; + /** + * @throws \PHPUnit\Framework\MockObject\Exception + */ public function setUp(): void { $this->middleware = new SessionMiddleware( @@ -27,11 +29,6 @@ public function setUp(): void ); } - public function testImplementsMiddlewareInterface(): void - { - $this->assertInstanceOf(MiddlewareInterface::class, $this->middleware); - } - public function testProcess(): void { $result = $this->middleware->process( @@ -39,9 +36,13 @@ public function testProcess(): void $this->mockRequestHandler() ); - $this->assertInstanceOf(ResponseInterface::class, $result); + /** @phpstan-ignore staticMethod.alreadyNarrowedType */ + self::assertInstanceOf(ResponseInterface::class, $result); } + /** + * @throws \PHPUnit\Framework\MockObject\Exception + */ private function mockRequest(): ServerRequestInterface { $request = $this->createMock(ServerRequestInterface::class); @@ -52,6 +53,9 @@ private function mockRequest(): ServerRequestInterface return $request; } + /** + * @throws \PHPUnit\Framework\MockObject\Exception + */ private function mockRequestHandler(): RequestHandlerInterface { $handler = $this->createMock(RequestHandlerInterface::class);