From a6caba1c81318379537d848a768e430aa25b0e87 Mon Sep 17 00:00:00 2001 From: Felix Jacobi Date: Fri, 15 Mar 2024 23:40:08 +0100 Subject: [PATCH] Fixed PHPStan errors up to level 7 --- phpstan.neon | 2 +- src/Core/Track.php | 2 +- src/Http/Transport/CurlTransport.php | 2 ++ src/Parameters/CreateMeetingParameters.php | 6 +++++- src/Parameters/InsertDocumentParameters.php | 6 +++++- src/Responses/BaseResponseAsJson.php | 2 +- tests/common/TestCase.php | 14 ++++++++++++-- ...php => BigBlueButtonFunctionalTestAbstract.php} | 14 +++++++++++--- .../BigBlueButtonWithCurlTransportTest.php | 2 +- .../BigBlueButtonWithPsrHttpClientTransport.php | 2 +- ...lueButtonWithSymfonyHttpClientTransportTest.php | 2 +- tests/unit/BigBlueButtonTest.php | 2 ++ .../Parameters/CreateMeetingParametersTest.php | 6 +++++- tests/unit/Responses/GetMeetingsResponseTest.php | 4 ++++ tests/unit/Responses/GetRecordingsResponseTest.php | 5 ++++- tests/unit/Responses/HooksDestroyResponseTest.php | 2 ++ .../unit/Responses/InsertDocumentResponseTest.php | 7 ++++++- .../Responses/IsMeetingRunningResponseTest.php | 7 ++++++- tests/unit/Util/ArrayHelperTest.php | 10 ++++------ tests/unit/Util/SimpleXMLElementExtendedTest.php | 6 +++++- 20 files changed, 79 insertions(+), 24 deletions(-) rename tests/functional/{AbstractBigBlueButtonFunctionalTest.php => BigBlueButtonFunctionalTestAbstract.php} (95%) diff --git a/phpstan.neon b/phpstan.neon index 4817ce70..79781d51 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,7 +4,7 @@ parameters: - tests parallel: maximumNumberOfProcesses: 4 - level: 6 + level: 7 inferPrivatePropertyTypeFromConstructor: true bootstrapFiles: - tools/bootstrap.php diff --git a/src/Core/Track.php b/src/Core/Track.php index da7ba15b..06f2f315 100644 --- a/src/Core/Track.php +++ b/src/Core/Track.php @@ -37,7 +37,7 @@ private string $source; - public function __construct(object $track) + public function __construct(\stdClass $track) { $this->href = $track->href; $this->kind = $track->kind; diff --git a/src/Http/Transport/CurlTransport.php b/src/Http/Transport/CurlTransport.php index 8da7fb6e..3016e65d 100644 --- a/src/Http/Transport/CurlTransport.php +++ b/src/Http/Transport/CurlTransport.php @@ -195,6 +195,8 @@ private static function getHeadersAndContentFromCurlHandle(\CurlHandle $curlHand $headers = []; curl_setopt($curlHandle, \CURLOPT_HEADER, true); + + /** @var string|false $responseContent */ $responseContent = curl_exec($curlHandle); // @codeCoverageIgnoreStart diff --git a/src/Parameters/CreateMeetingParameters.php b/src/Parameters/CreateMeetingParameters.php index da640590..e42e0b50 100644 --- a/src/Parameters/CreateMeetingParameters.php +++ b/src/Parameters/CreateMeetingParameters.php @@ -446,6 +446,10 @@ public function getModules(): string // Get xml as string after modules have been added $resultXML = $xml->asXML(); + if (false === $resultXML) { + throw new \LogicException('Could not generate XML.'); + } + // If xml was not modified (no modules added), return an empty string if ($emptyXML === $resultXML) { return ''; @@ -496,7 +500,7 @@ public function getHTTPQuery(): string // Pre-defined groups to automatically assign the students to a given breakout room if (!empty($this->breakoutRoomsGroups)) { $queries = array_merge($queries, [ - 'groups' => json_encode($this->breakoutRoomsGroups), + 'groups' => json_encode($this->breakoutRoomsGroups, \JSON_THROW_ON_ERROR), ]); } diff --git a/src/Parameters/InsertDocumentParameters.php b/src/Parameters/InsertDocumentParameters.php index 8ffad3c8..700fe48e 100644 --- a/src/Parameters/InsertDocumentParameters.php +++ b/src/Parameters/InsertDocumentParameters.php @@ -72,7 +72,7 @@ public function removePresentation(string $url): self return $this; } - public function getPresentationsAsXML(): string|false + public function getPresentationsAsXML(): string { $result = ''; @@ -87,6 +87,10 @@ public function getPresentationsAsXML(): string|false $result = $xml->asXML(); } + if (false === $result) { + throw new \LogicException('Could not generate XML.'); + } + return $result; } } diff --git a/src/Responses/BaseResponseAsJson.php b/src/Responses/BaseResponseAsJson.php index aff7bebd..cba03793 100644 --- a/src/Responses/BaseResponseAsJson.php +++ b/src/Responses/BaseResponseAsJson.php @@ -53,7 +53,7 @@ public function getRawJson(): string|false */ public function getRawArray(): array { - return json_decode(json_encode($this->data), true); + return json_decode(json_encode($this->data, \JSON_THROW_ON_ERROR), true); } public function getMessage(): string diff --git a/tests/common/TestCase.php b/tests/common/TestCase.php index 85376e93..1300cb46 100644 --- a/tests/common/TestCase.php +++ b/tests/common/TestCase.php @@ -294,12 +294,22 @@ protected function getUpdateRecordingsParamsMock(array $params): UpdateRecording protected function loadXmlFile(string $path): \SimpleXMLElement { - return simplexml_load_string(file_get_contents($path)); + $content = file_get_contents($path); + $this->assertIsString($content); + + $xml = simplexml_load_string($content); + + $this->assertNotFalse($xml); + + return $xml; } protected function loadJsonFile(string $path): string { - return file_get_contents($path); + $content = file_get_contents($path); + $this->assertIsString($content); + + return $content; } protected function minifyString(string $string): string diff --git a/tests/functional/AbstractBigBlueButtonFunctionalTest.php b/tests/functional/BigBlueButtonFunctionalTestAbstract.php similarity index 95% rename from tests/functional/AbstractBigBlueButtonFunctionalTest.php rename to tests/functional/BigBlueButtonFunctionalTestAbstract.php index 074963ff..3b260025 100644 --- a/tests/functional/AbstractBigBlueButtonFunctionalTest.php +++ b/tests/functional/BigBlueButtonFunctionalTestAbstract.php @@ -36,7 +36,7 @@ /** * Class BigBlueButtonIntegrationTest. */ -abstract class AbstractBigBlueButtonFunctionalTest extends TestCase +abstract class BigBlueButtonFunctionalTestAbstract extends TestCase { private BigBlueButton $bbb; @@ -153,7 +153,11 @@ public function testCreateMeetingWithDocumentUrlAndFileName(): void public function testCreateMeetingWithDocumentEmbedded(): void { $params = $this->getCreateMock($this->generateCreateParams()); - $params->addPresentation('bbb_logo.png', file_get_contents(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'bbb_logo.png')); + + $content = file_get_contents(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'bbb_logo.png'); + $this->assertIsString($content); + + $params->addPresentation('bbb_logo.png', $content); $result = $this->bbb->createMeeting($params); @@ -172,7 +176,11 @@ public function testCreateMeetingWithMultiDocument(): void { $params = $this->getCreateMock($this->generateCreateParams()); $params->addPresentation('https://picsum.photos/3840/2160/?random', null, 'presentation.png'); - $params->addPresentation('logo.png', file_get_contents(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'bbb_logo.png')); + + $content = file_get_contents(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'bbb_logo.png'); + $this->assertIsString($content); + + $params->addPresentation('logo.png', $content); $result = $this->bbb->createMeeting($params); diff --git a/tests/functional/BigBlueButtonWithCurlTransportTest.php b/tests/functional/BigBlueButtonWithCurlTransportTest.php index 61d40766..c331a7c4 100644 --- a/tests/functional/BigBlueButtonWithCurlTransportTest.php +++ b/tests/functional/BigBlueButtonWithCurlTransportTest.php @@ -24,7 +24,7 @@ use BigBlueButton\Http\Transport\CurlTransport; use BigBlueButton\Http\Transport\TransportInterface; -final class BigBlueButtonWithCurlTransportTest extends AbstractBigBlueButtonFunctionalTest +final class BigBlueButtonWithCurlTransportTest extends BigBlueButtonFunctionalTestAbstract { protected static function createTransport(): TransportInterface { diff --git a/tests/functional/BigBlueButtonWithPsrHttpClientTransport.php b/tests/functional/BigBlueButtonWithPsrHttpClientTransport.php index cd5bd19c..64d21dca 100644 --- a/tests/functional/BigBlueButtonWithPsrHttpClientTransport.php +++ b/tests/functional/BigBlueButtonWithPsrHttpClientTransport.php @@ -27,7 +27,7 @@ use Symfony\Component\HttpClient\CurlHttpClient; use Symfony\Component\HttpClient\Psr18Client; -final class BigBlueButtonWithPsrHttpClientTransport extends AbstractBigBlueButtonFunctionalTest +final class BigBlueButtonWithPsrHttpClientTransport extends BigBlueButtonFunctionalTestAbstract { protected static function createTransport(): TransportInterface { diff --git a/tests/functional/BigBlueButtonWithSymfonyHttpClientTransportTest.php b/tests/functional/BigBlueButtonWithSymfonyHttpClientTransportTest.php index 3f75ce6f..d51e71f9 100644 --- a/tests/functional/BigBlueButtonWithSymfonyHttpClientTransportTest.php +++ b/tests/functional/BigBlueButtonWithSymfonyHttpClientTransportTest.php @@ -24,7 +24,7 @@ use BigBlueButton\Http\Transport\Bridge\SymfonyHttpClient\SymfonyHttpClientTransport; use BigBlueButton\Http\Transport\TransportInterface; -final class BigBlueButtonWithSymfonyHttpClientTransportTest extends AbstractBigBlueButtonFunctionalTest +final class BigBlueButtonWithSymfonyHttpClientTransportTest extends BigBlueButtonFunctionalTestAbstract { protected static function createTransport(): TransportInterface { diff --git a/tests/unit/BigBlueButtonTest.php b/tests/unit/BigBlueButtonTest.php index 4410d474..eab2272a 100644 --- a/tests/unit/BigBlueButtonTest.php +++ b/tests/unit/BigBlueButtonTest.php @@ -292,6 +292,8 @@ public function testCreateWithPresentation(): void $payload = $request->getPayload(); $xml = simplexml_load_string($payload); + self::assertNotFalse($xml); + $presentations = $xml->module[0]; $clientSettingsOverride = $xml->module[1]; diff --git a/tests/unit/Parameters/CreateMeetingParametersTest.php b/tests/unit/Parameters/CreateMeetingParametersTest.php index 46d2e57c..f64933de 100644 --- a/tests/unit/Parameters/CreateMeetingParametersTest.php +++ b/tests/unit/Parameters/CreateMeetingParametersTest.php @@ -217,7 +217,11 @@ public function testGetPresentationsAsXMLWithFile(): void { $params = $this->generateCreateParams(); $createMeetingParams = $this->getCreateMock($params); - $createMeetingParams->addPresentation('bbb_logo.png', file_get_contents(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'bbb_logo.png')); + + $content = file_get_contents(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'bbb_logo.png'); + $this->assertIsString($content); + + $createMeetingParams->addPresentation('bbb_logo.png', $content); $this->assertXmlStringEqualsXmlFile(__DIR__.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'fixtures'.\DIRECTORY_SEPARATOR.'presentation_with_embedded_file.xml', $createMeetingParams->getModules()); } diff --git a/tests/unit/Responses/GetMeetingsResponseTest.php b/tests/unit/Responses/GetMeetingsResponseTest.php index fc3d2c31..e2a46054 100644 --- a/tests/unit/Responses/GetMeetingsResponseTest.php +++ b/tests/unit/Responses/GetMeetingsResponseTest.php @@ -85,12 +85,16 @@ public function testGetMeetingsNoMeetings(): void { // scalelite response no meetings $xml = simplexml_load_string('SUCCESSnoMeetingsNo meetings were found on this server.'); + $this->assertInstanceOf(\SimpleXMLElement::class, $xml); + $this->meetings = new GetMeetingsResponse($xml); $this->assertEquals('SUCCESS', $this->meetings->getReturnCode()); $this->assertCount(0, $this->meetings->getMeetings()); // normal bbb response no meetings $xml = simplexml_load_string('SUCCESSnoMeetingsNo meetings were found on this server.'); + $this->assertInstanceOf(\SimpleXMLElement::class, $xml); + $this->meetings = new GetMeetingsResponse($xml); $this->assertEquals('SUCCESS', $this->meetings->getReturnCode()); $this->assertCount(0, $this->meetings->getMeetings()); diff --git a/tests/unit/Responses/GetRecordingsResponseTest.php b/tests/unit/Responses/GetRecordingsResponseTest.php index d95d8e6b..38bc372a 100644 --- a/tests/unit/Responses/GetRecordingsResponseTest.php +++ b/tests/unit/Responses/GetRecordingsResponseTest.php @@ -128,7 +128,10 @@ public function testHasNoRecordings(): void There are no recordings for the meeting(s). '; - $response = new GetRecordingsResponse(simplexml_load_string($xml)); + $xml = simplexml_load_string($xml); + $this->assertInstanceOf(\SimpleXMLElement::class, $xml); + + $response = new GetRecordingsResponse($xml); $this->assertTrue($response->hasNoRecordings()); } diff --git a/tests/unit/Responses/HooksDestroyResponseTest.php b/tests/unit/Responses/HooksDestroyResponseTest.php index 7704ee1a..4838e7cf 100644 --- a/tests/unit/Responses/HooksDestroyResponseTest.php +++ b/tests/unit/Responses/HooksDestroyResponseTest.php @@ -53,6 +53,7 @@ public function testHooksDestroyResponseTypes(): void public function testHookDestroyMissingHook(): void { $xml = simplexml_load_string('FAILEDdestroyMissingHookThe hook informed was not found.'); + $this->assertInstanceOf(\SimpleXMLElement::class, $xml); $destroyResponse = new HooksDestroyResponse($xml); $this->assertTrue($destroyResponse->failed()); @@ -62,6 +63,7 @@ public function testHookDestroyMissingHook(): void public function testHookDestroyHookError(): void { $xml = simplexml_load_string('FAILEDdestroyHookErrorAn error happened while removing your hook. Check the logs.'); + $this->assertInstanceOf(\SimpleXMLElement::class, $xml); $destroyResponse = new HooksDestroyResponse($xml); $this->assertTrue($destroyResponse->failed()); diff --git a/tests/unit/Responses/InsertDocumentResponseTest.php b/tests/unit/Responses/InsertDocumentResponseTest.php index 01d1012e..62899ad6 100644 --- a/tests/unit/Responses/InsertDocumentResponseTest.php +++ b/tests/unit/Responses/InsertDocumentResponseTest.php @@ -41,7 +41,12 @@ public function testIsMeetingRunningResponseContent(): void { $this->assertEquals('SUCCESS', $this->running->getReturnCode()); - $this->assertEquals('SUCCESS', $this->minifyString($this->running->getRawXml()->asXML())); + $xml = $this->running->getRawXml()->asXML(); + $this->assertIsString($xml); + + $this->assertEquals('SUCCESS', $this->minifyString( + $xml + )); } public function testIsMeetingRunningResponseTypes(): void diff --git a/tests/unit/Responses/IsMeetingRunningResponseTest.php b/tests/unit/Responses/IsMeetingRunningResponseTest.php index 70dce13f..003cf7f2 100644 --- a/tests/unit/Responses/IsMeetingRunningResponseTest.php +++ b/tests/unit/Responses/IsMeetingRunningResponseTest.php @@ -43,7 +43,12 @@ public function testIsMeetingRunningResponseContent(): void $this->assertEquals('SUCCESS', $this->running->getReturnCode()); $this->assertTrue($this->running->isRunning()); - $this->assertEquals('SUCCESStrue', $this->minifyString($this->running->getRawXml()->asXML())); + $xml = $this->running->getRawXml()->asXML(); + $this->assertIsString($xml); + + $this->assertEquals('SUCCESStrue', $this->minifyString( + $xml + )); } public function testIsMeetingRunningResponseTypes(): void diff --git a/tests/unit/Util/ArrayHelperTest.php b/tests/unit/Util/ArrayHelperTest.php index 192e5ea6..917f2767 100644 --- a/tests/unit/Util/ArrayHelperTest.php +++ b/tests/unit/Util/ArrayHelperTest.php @@ -29,9 +29,7 @@ */ final class ArrayHelperTest extends TestCase { - /** - * @return iterable, 1: array, 2: bool, 3: array}> - */ + /** @return iterable> */ public static function provideArrays(): iterable { yield 'simple flat arrays' => [ @@ -63,9 +61,9 @@ public static function provideArrays(): iterable /** * @dataProvider provideArrays * - * @param array $input1 - * @param array $input2 - * @param array $output + * @param array $input1 + * @param array $input2 + * @param array $output */ public function testMergeRecursive(array $input1, array $input2, bool $reorderNested, array $output): void { diff --git a/tests/unit/Util/SimpleXMLElementExtendedTest.php b/tests/unit/Util/SimpleXMLElementExtendedTest.php index c29a09c7..57dc6395 100644 --- a/tests/unit/Util/SimpleXMLElementExtendedTest.php +++ b/tests/unit/Util/SimpleXMLElementExtendedTest.php @@ -38,9 +38,13 @@ public function testAddChildWithCData(): void $module = $xml->addChildWithCData('module', '{"foo": {"foo": "baa"}}'); $module->addAttribute('name', 'clientSettingsOverride'); + $result = $xml->asXML(); + + $this->assertNotFalse($result); + $expected = ' '; - $this->assertXmlStringEqualsXmlString($expected, $xml->asXML()); + $this->assertXmlStringEqualsXmlString($expected, $result); } }