diff --git a/Build/phpstan/phpstan-baseline.neon b/Build/phpstan/phpstan-baseline.neon index d09ba1ee9c2658c34f96eb25bed60c89f8a15aa8..8167cbc2c1964a79b4fea03c6e07259ca556e93f 100644 --- a/Build/phpstan/phpstan-baseline.neon +++ b/Build/phpstan/phpstan-baseline.neon @@ -5385,11 +5385,6 @@ parameters: count: 1 path: ../../typo3/sysext/workspaces/Classes/Controller/PreviewController.php - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: ../../typo3/sysext/workspaces/Classes/Domain/Record/WorkspaceRecord.php - - message: "#^Offset 0 does not exist on non\\-empty\\-array\\<string, mixed\\>\\.$#" count: 2 diff --git a/typo3/sysext/workspaces/Classes/Domain/Record/WorkspaceRecord.php b/typo3/sysext/workspaces/Classes/Domain/Record/WorkspaceRecord.php index cf5be07e3d17cf212fc9871194d6489186f260ed..b31306ca83ab5e4c55101960f556794d5aef9abc 100644 --- a/typo3/sysext/workspaces/Classes/Domain/Record/WorkspaceRecord.php +++ b/typo3/sysext/workspaces/Classes/Domain/Record/WorkspaceRecord.php @@ -78,9 +78,8 @@ class WorkspaceRecord extends AbstractRecord } elseif (empty($record)) { $record = static::fetch('sys_workspace', $uid); } - // [phpstan] Unsafe usage of new static() - // todo: Either mark this class or its constructor final or use new self instead. - return new static($record); + + return GeneralUtility::makeInstance(self::class, $record); } /** diff --git a/typo3/sysext/workspaces/Tests/Unit/Domain/Model/WorkspaceRecordTest.php b/typo3/sysext/workspaces/Tests/Unit/Domain/Model/WorkspaceRecordTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f52d8f045ac5b61c8b9b0eae508702fab87a5cbd --- /dev/null +++ b/typo3/sysext/workspaces/Tests/Unit/Domain/Model/WorkspaceRecordTest.php @@ -0,0 +1,99 @@ +<?php + +declare(strict_types=1); + +/* + * This file is part of the TYPO3 CMS project. + * + * It is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License, either version 2 + * of the License, or any later version. + * + * For the full copyright and license information, please read the + * LICENSE.txt file that was distributed with this source code. + * + * The TYPO3 project - inspiring people to share! + */ + +namespace TYPO3\CMS\Workspaces\Tests\Unit\Domain\Model; + +use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\GeneralUtilityFixture; +use TYPO3\CMS\Workspaces\Domain\Record\AbstractRecord; +use TYPO3\CMS\Workspaces\Domain\Record\WorkspaceRecord; +use TYPO3\TestingFramework\Core\Unit\UnitTestCase; + +class WorkspaceRecordTest extends UnitTestCase +{ + protected function setUp(): void + { + parent::setUp(); + GeneralUtilityFixture::resetFinalClassNameCache(); + } + + protected function tearDown(): void + { + GeneralUtilityFixture::resetFinalClassNameCache(); + parent::tearDown(); + } + + /** + * @test + */ + public function isAbstractRecord(): void + { + $subject = new WorkspaceRecord([]); + + self::assertInstanceOf(AbstractRecord::class, $subject); + } + + /** + * @test + */ + public function getReturnsWorkspaceRecordInstance(): void + { + $instance = WorkspaceRecord::get(1, ['title' => '']); + + self::assertInstanceOf(WorkspaceRecord::class, $instance); + } + + /** + * @test + */ + public function getWithNonZeroUidAndNonEmptyDataReturnsInstanceWithTheProvidedData(): void + { + $title = 'some record title'; + + $instance = WorkspaceRecord::get(1, ['title' => $title]); + + self::assertSame($title, $instance->getTitle()); + } + + /** + * @test + */ + public function getCalledTwoTimesWithTheSameUidAndDataDataReturnsDifferentInstancesForEachCall(): void + { + $uid = 1; + $data = ['title' => '']; + + $instance1 = WorkspaceRecord::get($uid, $data); + $instance2 = WorkspaceRecord::get($uid, $data); + + self::assertNotSame($instance1, $instance2); + } + + /** + * @test + */ + public function getForConfiguredXclassReturnsInstanceOfXclass(): void + { + $xclassInstance = new class([]) extends WorkspaceRecord { + }; + $xclassName = get_class($xclassInstance); + $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][WorkspaceRecord::class] = ['className' => $xclassName]; + + $instance = WorkspaceRecord::get(1, ['title' => '']); + + self::assertInstanceOf($xclassName, $instance); + } +}