Skip to content

Commit

Permalink
chore: fix some tests and codestyles for php83
Browse files Browse the repository at this point in the history
  • Loading branch information
tlayh committed Mar 19, 2024
1 parent 75a7fad commit 18e7960
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 73 deletions.
62 changes: 33 additions & 29 deletions .code-quality/phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,62 +1,66 @@
parameters:
ignoreErrors:
-
message: "#^(.*)has no return typehint specified\\.$#"
path: ../
-
message: "#^Access to an undefined property Luracast\\\\Restler\\\\Restler\\:\\:\\$_authClasses\\.$#"
count: 1
path: ../Classes/System/RestApi/RestApiRequest.php

-
message: "#^Access to an undefined property Luracast\\\\Restler\\\\Restler\\:\\:\\$_authClasses\\.$#"
count: 2
path: ../Classes/System/RestApi/RestApiRequestScope.php

-
message: "#^Method Aoe\\\\Restler\\\\System\\\\RestApi\\\\RestApiRequestScope\\:\\:getOriginalRestApiRequest\\(\\) should return Luracast\\\\Restler\\\\Restler but returns Luracast\\\\Restler\\\\Scope\\.$#"
count: 1
path: ../Classes/System/RestApi/RestApiRequestScope.php

-
message: "#^Method Aoe\\\\Restler\\\\System\\\\Restler\\\\Builder\\:\\:getCache\\(\\) should return TYPO3\\\\CMS\\\\Core\\\\Cache\\\\Backend\\\\SimpleFileBackend but returns TYPO3\\\\CMS\\\\Core\\\\Cache\\\\Backend\\\\BackendInterface\\.$#"
count: 1
path: ../Classes/System/Restler/Builder.php

-
message: "#^Cognitive complexity for \"Aoe\\\\Restler\\\\System\\\\Restler\\\\Format\\\\HalJsonFormat\\:\\:decode\\(\\)\" is 9, keep it under 8$#"
message: "#^Strict comparison using \\=\\=\\= between bool and null will always evaluate to false\\.$#"
count: 1
path: ../Classes/System/Restler/Format/HalJsonFormat.php

-
message: "#^Cognitive complexity for \"Aoe\\\\Restler\\\\System\\\\Restler\\\\Format\\\\HalJsonFormat\\:\\:encode\\(\\)\" is 17, keep it under 8$#"
message: "#^Method Aoe\\\\Restler\\\\System\\\\TYPO3\\\\Cache\\:\\:getCacheEntry\\(\\) has no return type specified\\.$#"
count: 1
path: ../Classes/System/Restler/Format/HalJsonFormat.php

path: ../Classes/System/TYPO3/Cache.php
-
message: "#^Cognitive complexity for \"Aoe\\\\Restler\\\\System\\\\Restler\\\\Format\\\\HalJsonFormat\\:\\:formatJson\\(\\)\" is 14, keep it under 8$#"
message: "#^Method Aoe\\\\Restler\\\\System\\\\Restler\\\\RestlerExtended\\:\\:handle\\(\\) has no return type specified\\.$#"
count: 1
path: ../Classes/System/Restler/Format/HalJsonFormat.php

path: ../Classes/System/Restler/RestlerExtended.php
-
message: "#^Method Aoe\\\\Restler\\\\System\\\\Restler\\\\RestlerExtended\\:\\:postCall\\(\\) has no return type specified\\.$#"
count: 1
path: ../Classes/System/Restler/RestlerExtended.php
-
message: "#^Dead catch \\- Error is never thrown in the try block\\.$#"
count: 1
path: ../Classes/System/Restler/Builder.php
-
message: "#^Anonymous variable in a `\\$request\\-\\>getAttribute\\('site'\\)\\-\\>\\.\\.\\.\\(\\)` method call can lead to false dead methods\\. Make sure the variable type is known$#"
count: 1
path: ../Classes/System/Dispatcher.php
-
message: "#^Cognitive complexity for \"Aoe\\\\Restler\\\\System\\\\Restler\\\\RestlerExtended\\:\\:getPath\\(\\)\" is 9, keep it under 8$#"
message: "#^Anonymous variable in a `\\$request\\-\\>getAttribute\\('site'\\)\\-\\>getBase\\(\\)\\-\\>\\.\\.\\.\\(\\)` method call can lead to false dead methods\\. Make sure the variable type is known$#"
count: 1
path: ../Classes/System/Dispatcher.php
-
message: "#^Anonymous variable in a `\\$this\\-\\>request\\-\\>getAttribute\\('site'\\)\\-\\>\\.\\.\\.\\(\\)` method call can lead to false dead methods\\. Make sure the variable type is known$#"
count: 1
path: ../Classes/System/Restler/RestlerExtended.php

-
message: "#^Cognitive complexity for \"Aoe\\\\Restler\\\\System\\\\Restler\\\\RestlerExtended\\:\\:handleRequestByTypo3Cache\\(\\)\" is 13, keep it under 8$#"
message: "#^Anonymous variable in a `\\$this\\-\\>request\\-\\>getAttribute\\('site'\\)\\-\\>getBase\\(\\)\\-\\>\\.\\.\\.\\(\\)` method call can lead to false dead methods\\. Make sure the variable type is known$#"
count: 1
path: ../Classes/System/Restler/RestlerExtended.php

-
message: "#^Cognitive complexity for \"Aoe\\\\Restler\\\\System\\\\Restler\\\\Routes\\:\\:containsUrl\\(\\)\" is 16, keep it under 8$#"
message: "#^Method Aoe\\\\Restler\\\\System\\\\RestApi\\\\RestApiRequest\\:\\:composeHeaders\\(\\) has no return type specified\\.$#"
count: 1
path: ../Classes/System/Restler/Routes.php
path: ../Classes/System/RestApi/RestApiRequest.php
-
message: "#^Strict comparison using \\=\\=\\= between bool and null will always evaluate to false\\.$#"
message: "#^Method Aoe\\\\Restler\\\\System\\\\RestApi\\\\RestApiRequest\\:\\:postCall\\(\\) has no return type specified\\.$#"
count: 1
path: ../Classes/System/Restler/Format/HalJsonFormat.php
path: ../Classes/System/RestApi/RestApiRequest.php
-
message: "#^Cognitive complexity for \"Aoe\\\\Restler\\\\System\\\\Restler\\\\Builder\\:\\:addApiClassesByGlobalArray\\(\\)\" is 9, keep it under 8$#"
message: "#^Property Aoe\\\\Restler\\\\System\\\\RestApi\\\\RestApiRequestScope\\:\\:\\$originalRestApiRequest \\(Luracast\\\\Restler\\\\Scope\\) on left side of \\?\\? is not nullable\\.$#"
count: 1
path: ../Classes/System/Restler/Builder.php
path: ../Classes/System/RestApi/RestApiRequestScope.php
-
message: "#^Comparison operation \"\\>\" between int\\<1, max\\> and 0 is always true\\.$#"
count: 1
path: ../Classes/System/Restler/Format/HalJsonFormat.php
17 changes: 2 additions & 15 deletions .code-quality/phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
includes:
- phpstan-baseline.neon
- ../.Build/vendor/symplify/phpstan-rules/packages/cognitive-complexity/config/cognitive-complexity-rules.neon

rules:
- Symplify\PHPStanRules\Rules\NoDefaultExceptionRule
- ../.Build/vendor/symplify/phpstan-rules/config/code-complexity-rules.neon
- ../.Build/vendor/symplify/phpstan-rules/config/services/services.neon

parameters:
level: 6
Expand All @@ -17,17 +15,6 @@ parameters:
checkAlwaysTrueStrictComparison: false

services:
- class: Symplify\Astral\Naming\SimpleNameResolver
- class: Symplify\PHPStanRules\CognitiveComplexity\AstCognitiveComplexityAnalyzer
- class: Symplify\PHPStanRules\CognitiveComplexity\NodeTraverser\ComplexityNodeTraverserFactory
- class: Symplify\PHPStanRules\CognitiveComplexity\DataCollector\CognitiveComplexityDataCollector
- class: Symplify\PHPStanRules\CognitiveComplexity\NodeVisitor\NestingNodeVisitor
- class: Symplify\PHPStanRules\CognitiveComplexity\NodeVisitor\ComplexityNodeVisitor
- class: Symplify\PHPStanRules\CognitiveComplexity\NodeAnalyzer\ComplexityAffectingNodeFinder
- class: Symplify\PackageBuilder\Php\TypeChecker
- class: Symplify\PackageBuilder\Matcher\ArrayStringAndFnMatcher
- class: Symplify\PHPStanRules\TypeAnalyzer\ObjectTypeAnalyzer
- class: Symplify\PHPStanRules\TypeAnalyzer\TypeUnwrapper
-
class: Symplify\PHPStanRules\Rules\ForbiddenFuncCallRule
tags: [phpstan.rules.rule]
Expand Down
4 changes: 4 additions & 0 deletions .code-quality/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector;
use Rector\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\AssertEqualsToSameRector;
use Rector\Set\ValueObject\SetList;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
Expand Down Expand Up @@ -40,6 +41,9 @@
AddMethodCallBasedStrictParamTypeRector::class,
FlipTypeControlToUseExclusiveTypeRector::class,
YieldDataProviderRector::class,
AssertEqualsToSameRector::class => [
__DIR__ . '/../Tests/Unit/System/Restler/ExceptionHandlerTest.php',
],
])
->withAutoloadPaths([__DIR__ . '/../Classes'])
->registerService(RemoveUnusedPrivatePropertyRector::class);
2 changes: 1 addition & 1 deletion Classes/System/RestApi/RestApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function composeHeaders(RestException $e = null)
public function getRequestData($includeQueryParameters = true): array
{
$requestData = [];
if ($this->restApiRequestMethod == 'PUT' || $this->restApiRequestMethod == 'PATCH' || $this->restApiRequestMethod == 'POST') {
if ($this->restApiRequestMethod === 'PUT' || $this->restApiRequestMethod === 'PATCH' || $this->restApiRequestMethod === 'POST') {
$requestData = array_merge($this->restApiPostData, [Defaults::$fullRequestDataName => $this->restApiPostData]);
}

Expand Down
4 changes: 1 addition & 3 deletions Classes/System/Restler/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ private function configureRestler(RestlerExtended $restler): void
{
$restlerConfigurationClasses = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['restler']['restlerConfigurationClasses'];

if (isset($GLOBALS['TYPO3_Restler']['restlerConfigurationClasses']) && is_array(
$GLOBALS['TYPO3_Restler']['restlerConfigurationClasses']
)) {
if (!is_array($restlerConfigurationClasses) || $restlerConfigurationClasses === []) {
$message = 'No restler-configuration-class found (at least one restler-configuration-class is required)! ';
$message .= 'The configuration-class must be registered in ext_localconf.php of your TYPO3-extension like this: ';
$message .= '$GLOBALS[\'TYPO3_CONF_VARS\'][\'SC_OPTIONS\'][\'restler\'][\'restlerConfigurationClasses\'][] =
Expand Down
2 changes: 1 addition & 1 deletion Classes/System/Restler/ConfigurationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ interface ConfigurationInterface extends SingletonInterface
* This is the right place, where you can write your restler-code!
* Use this method instead of the index.php-file, which you normally would use, to configuring/using restler!
*/
public function configureRestler(Restler $restler);
public function configureRestler(Restler $restler): void;
}
4 changes: 2 additions & 2 deletions Classes/System/Restler/Format/HalJsonFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function decode($data): ?array
/**
* Throws an exception if an error occurred during the last JSON encoding/decoding
*/
protected function handleJsonError()
protected function handleJsonError(): void
{
if (function_exists('json_last_error_msg') && json_last_error() !== JSON_ERROR_NONE) {
// PHP >= 5.5.0
Expand Down Expand Up @@ -261,7 +261,7 @@ private function formatJson(string $json): string
case '"':
if ($c == 0) {
$inString = true;
} elseif ($c > 0 && $json[$c - 1] != '\\') {
} elseif ($c > 0 && $json[$c - 1] !== '\\') {
$inString = !$inString;
}
// fall-through
Expand Down
1 change: 1 addition & 0 deletions Classes/System/TYPO3/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use LogicException;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Context\TypoScriptAspect;
Expand Down
3 changes: 0 additions & 3 deletions Classes/System/TYPO3/RestlerEnhancer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
*/
class RestlerEnhancer extends RestlerBuilderAware implements DecoratingEnhancerInterface
{
/**
* @var string|mixed
*/
private string $default;

public function __construct(array $configuration)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @package Restler
* @subpackage Tests
*/
abstract class BaseTest extends UnitTestCase
class BaseTest extends UnitTestCase
{
/**
* call unaccessible method of an object (to test it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,14 @@ protected function setUp(): void
{
parent::setUp();

$this->apiMethodInfoMock = $this->getMockBuilder(ApiMethodInfo::class)
->disableOriginalConstructor()
->getMock();
$this->apiMethodInfoMock = new ApiMethodInfo();

/** @var \Luracast\Restler\Restler $restlerMock */
$restlerMock = $this->getMockBuilder(Restler::class)->disableOriginalConstructor()->getMock();
$restlerMock->apiMethodInfo = $this->apiMethodInfoMock;

$this->controller = new ExplorerAuthenticationController();
$this->inject($this->controller, 'restler', $restlerMock);
$this->controller->restler = $restlerMock;
}

public function testCheckThatAuthenticationWillFail(): void
Expand Down
16 changes: 4 additions & 12 deletions Tests/Unit/Controller/FeUserAuthenticationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ protected function setUp(): void
$this->controller = new FeUserAuthenticationController($this->typo3LoaderMock);
}

/**
* Cleans up the environment after running a test.
*/
protected function tearDown(): void
{
parent::tearDown();
}

public function testCheckThatAuthenticationWillFailWhenControllerIsNotResponsibleForAuthenticationCheck(): void
{
$this->typo3LoaderMock->expects(self::never())->method('initializeFrontendUser');
Expand Down Expand Up @@ -109,7 +101,7 @@ public function testShouldSetPageIdZeroIfArgumentDoesNotExist(): void
/** @var Restler $restlerMock */
$restlerMock = $this->getMockBuilder(Restler::class)->disableOriginalConstructor()->getMock();
$restlerMock->apiMethodInfo = $apiMethodInfoMock;
$this->inject($this->controller, 'restler', $restlerMock);
$this->controller->restler = $restlerMock;

$this->controller->checkAuthentication = true;
$this->controller->argumentNameOfPageId = 'pid';
Expand All @@ -118,7 +110,7 @@ public function testShouldSetPageIdZeroIfArgumentDoesNotExist(): void
$method = $reflection->getMethod('determinePageIdFromArguments');
$method->setAccessible(true);

$this->assertSame(0, $method->invoke($this->controller));
$this->assertSame('0', $method->invoke($this->controller));
}

public function testShouldSetPageIdIfArgumentDoesExist(): void
Expand All @@ -137,7 +129,7 @@ public function testShouldSetPageIdIfArgumentDoesExist(): void
/** @var Restler $restlerMock */
$restlerMock = $this->getMockBuilder(Restler::class)->disableOriginalConstructor()->getMock();
$restlerMock->apiMethodInfo = $apiMethodInfoMock;
$this->inject($this->controller, 'restler', $restlerMock);
$this->controller->restler = $restlerMock;

$this->controller->checkAuthentication = true;
$this->controller->argumentNameOfPageId = 'pid';
Expand All @@ -146,7 +138,7 @@ public function testShouldSetPageIdIfArgumentDoesExist(): void
$method = $reflection->getMethod('determinePageIdFromArguments');
$method->setAccessible(true);

$this->assertSame(4711, $method->invoke($this->controller));
$this->assertSame('4711', $method->invoke($this->controller));
}

public function testCheckForCorrectAuthenticationString(): void
Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/System/Restler/ExceptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testCanHandleAllExceptions(int $statusCode, string $statusName):
$exceptionHandler->expects(self::any())->method('getRestlerException')->willReturn($restlerException);

$functionName = 'handle' . $statusCode;
$this->assertEquals($message, $exceptionHandler->$functionName());
$this->assertEquals($message, $exceptionHandler->{$functionName}());
}

public static function statusCodes(): array
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@
],
"test:unit": [
"[ -e .Build/bin/phpunit ] || composer update",
"TYPO3_PATH_ROOT=.Build/Web .Build/bin/phpunit -c .Build/vendor/typo3/testing-framework/Resources/Core/Build/UnitTests.xml Tests/Unit"
"TYPO3_PATH_ROOT=.Build/Web .Build/bin/phpunit -c .Build/vendor/typo3/testing-framework/Resources/Core/Build/UnitTests-v10.xml Tests/Unit"
],
"test:unit-filter": [
"[ -e .Build/bin/phpunit ] || composer update",
"TYPO3_PATH_ROOT=.Build/Web .Build/bin/phpunit -c .Build/vendor/typo3/testing-framework/Resources/Core/Build/UnitTests-v10.xml Tests/Unit --filter=ExceptionHandlerTest"
],
"code-style": [
"[ -e ./.Build/bin/rector ] || composer install",
Expand Down

0 comments on commit 18e7960

Please sign in to comment.