From d5f2dc36e9f8398943d33ad1443031f89126764e Mon Sep 17 00:00:00 2001 From: erseco Date: Sun, 5 Apr 2026 07:54:42 +0100 Subject: [PATCH] Fix lint issuse --- src/ElpParser.php | 205 ++++++++++++++++++----------------- tests/Pest.php | 3 +- tests/TestCase.php | 2 +- tests/Unit/ElpParserTest.php | 81 +++++++------- 4 files changed, 148 insertions(+), 143 deletions(-) diff --git a/src/ElpParser.php b/src/ElpParser.php index bc69a95..4280279 100644 --- a/src/ElpParser.php +++ b/src/ElpParser.php @@ -1,4 +1,5 @@ filePath)) { throw new Exception('File does not exist.'); } @@ -181,7 +182,7 @@ protected function parse(): void * Parse the XML content and extract relevant information * * @param string $xmlContent XML content as a string - * + * * @throws Exception If XML parsing fails * @return void */ @@ -210,7 +211,7 @@ protected function parseXML(string $xmlContent): void * Extract strings from the XML document * * @param SimpleXMLElement $xml XML document - * + * * @return void */ protected function extractStrings(SimpleXMLElement $xml): void @@ -223,7 +224,7 @@ protected function extractStrings(SimpleXMLElement $xml): void * Recursively extract all text strings from XML * * @param SimpleXMLElement $element XML element to extract from - * + * * @return array Extracted strings */ protected function recursiveStringExtraction(SimpleXMLElement $element): array @@ -279,7 +280,7 @@ public function getStrings(): array * Extract metadata from version 3 XML format * * @param SimpleXMLElement $xml XML document - * + * * @return void */ protected function extractVersion3Metadata(SimpleXMLElement $xml): void @@ -290,24 +291,24 @@ protected function extractVersion3Metadata(SimpleXMLElement $xml): void $value = (string)$property->value; switch ($key) { - case 'pp_title': - $this->title = $value; - break; - case 'pp_description': - $this->description = $value; - break; - case 'pp_author': - $this->author = $value; - break; - case 'license': - $this->license = $value; - break; - case 'lom_general_language': - $this->language = $value; - break; - case 'pp_learningResourceType': - $this->learningResourceType = $value; - break; + case 'pp_title': + $this->title = $value; + break; + case 'pp_description': + $this->description = $value; + break; + case 'pp_author': + $this->author = $value; + break; + case 'license': + $this->license = $value; + break; + case 'lom_general_language': + $this->language = $value; + break; + case 'pp_learningResourceType': + $this->learningResourceType = $value; + break; } } } @@ -377,7 +378,7 @@ public function getLearningResourceType(): string * Extract metadata from version 2 XML format * * @param SimpleXMLElement $xml XML document - * + * * @return void */ protected function extractVersion2Metadata(SimpleXMLElement $xml): void @@ -402,35 +403,35 @@ protected function extractVersion2Metadata(SimpleXMLElement $xml): void } elseif ($currentKey !== null) { // Extract the value based on the type of element switch ($elementName) { - case 'unicode': - $metadata[$currentKey] = (string)$element['value']; - break; - case 'bool': - $metadata[$currentKey] = ((string)$element['value']) === '1'; - break; - case 'int': - $metadata[$currentKey] = (int)$element['value']; - break; - case 'list': - // Handle lists if necessary - $listValues = []; - foreach ($element->children() as $listItem) { - if ($listItem->getName() === 'unicode') { - $listValues[] = (string)$listItem['value']; + case 'unicode': + $metadata[$currentKey] = (string)$element['value']; + break; + case 'bool': + $metadata[$currentKey] = ((string)$element['value']) === '1'; + break; + case 'int': + $metadata[$currentKey] = (int)$element['value']; + break; + case 'list': + // Handle lists if necessary + $listValues = []; + foreach ($element->children() as $listItem) { + if ($listItem->getName() === 'unicode') { + $listValues[] = (string)$listItem['value']; + } + // Add handling for other types of elements within the list if necessary } - // Add handling for other types of elements within the list if necessary - } - $metadata[$currentKey] = $listValues; - break; - case 'dictionary': - // Handle nested dictionaries if necessary - // This may require a recursive function - // For simplicity, it can be omitted or implemented as needed - break; + $metadata[$currentKey] = $listValues; + break; + case 'dictionary': + // Handle nested dictionaries if necessary + // This may require a recursive function + // For simplicity, it can be omitted or implemented as needed + break; // Add other cases as needed - default: - // Handle unknown types or ignore them - break; + default: + // Handle unknown types or ignore them + break; } // Reset the current key after assigning the value @@ -445,7 +446,6 @@ protected function extractVersion2Metadata(SimpleXMLElement $xml): void $this->license = $metadata['license'] ?? ''; $this->language = $metadata['_lang'] ?? ''; $this->learningResourceType = $metadata['_learningResourceType'] ?? ''; - } @@ -542,7 +542,7 @@ public function getMetadata(): array $meta = [ [ 'schema' => 'Package', - 'content' => [ + 'content' => [ 'title' => $data['_title'] ?? '', 'lang' => $data['_lang'] ?? '', 'description' => [ @@ -618,40 +618,40 @@ protected function parseElement(SimpleXMLElement $element): mixed $name = $element->getName(); switch ($name) { - case 'unicode': - case 'string': - return (string) $element['value']; - case 'int': - return (int) $element['value']; - case 'bool': - return ((string) $element['value']) === '1'; - case 'list': - $list = []; - foreach ($element->children() as $child) { - $list[] = $this->parseElement($child); - } - return $list; - case 'dictionary': - $dict = []; - $key = null; - foreach ($element->children() as $child) { - $cname = $child->getName(); - if (($cname === 'string' || $cname === 'unicode') && (string) $child['role'] === 'key') { - $key = (string) $child['value']; - } elseif ($key !== null) { - $dict[$key] = $this->parseElement($child); - $key = null; + case 'unicode': + case 'string': + return (string) $element['value']; + case 'int': + return (int) $element['value']; + case 'bool': + return ((string) $element['value']) === '1'; + case 'list': + $list = []; + foreach ($element->children() as $child) { + $list[] = $this->parseElement($child); } - } - return $dict; - case 'instance': - return $this->parseElement($element->dictionary); - case 'none': - return null; - case 'reference': - return ['ref' => (string) $element['key']]; - default: - return null; + return $list; + case 'dictionary': + $dict = []; + $key = null; + foreach ($element->children() as $child) { + $cname = $child->getName(); + if (($cname === 'string' || $cname === 'unicode') && (string) $child['role'] === 'key') { + $key = (string) $child['value']; + } elseif ($key !== null) { + $dict[$key] = $this->parseElement($child); + $key = null; + } + } + return $dict; + case 'instance': + return $this->parseElement($element->dictionary); + case 'none': + return null; + case 'reference': + return ['ref' => (string) $element['key']]; + default: + return null; } } @@ -726,14 +726,14 @@ protected function slug(string $text): string * Extract contents of an ELP file to a specified directory * * @param string $destinationPath Directory to extract contents to - * + * * @throws Exception If extraction fails * @return void */ public function extract(string $destinationPath): void { $zip = new ZipArchive(); - + if ($zip->open($this->filePath) !== true) { throw new Exception("Unable to open ELP file for extraction"); } @@ -826,8 +826,9 @@ function removeAccents(string $text, string $locale = ''): string 'Ố' => 'O', 'ố' => 'o', 'Ớ' => 'O', 'ớ' => 'o', 'Ứ' => 'U', 'ứ' => 'u', ]; - if ('de_DE' === $locale || 'de_DE_formal' === $locale - || 'de_CH' === $locale || 'de_CH_informal' === $locale + if ( + 'de_DE' === $locale || 'de_DE_formal' === $locale + || 'de_CH' === $locale || 'de_CH_informal' === $locale || 'de_AT' === $locale ) { $chars['Ä'] = 'Ae'; diff --git a/tests/Pest.php b/tests/Pest.php index 55b4f92..b7b7080 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -35,7 +35,8 @@ */ expect()->extend( - 'toBeOne', function () { + 'toBeOne', + function () { return $this->toBe(1); } ); diff --git a/tests/TestCase.php b/tests/TestCase.php index 2cf2b7b..ca8e76a 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,4 +1,5 @@ toBeTrue('Test ELP file for version 2 not found'); - + $parser = ELPParser::fromFile($elpFile); - + // Check version detection expect($parser->getVersion())->toBe(3); @@ -40,21 +42,22 @@ $strings = $parser->getStrings(); expect($strings)->toBeArray(); expect(count($strings))->toBeGreaterThan(0); - + // Optionally, check for some expected content // expect($strings)->toContain('Some expected text from version 2 file'); } ); it( - 'can parse another version 3 ELP file', function () { + 'can parse another version 3 ELP file', + function () { $elpFile = __DIR__ . '/../Fixtures/exe3-parada-2-riesgos-de-la-ruta-itinerario-para-la-empleabilidad-i.elp'; - + // Ensure the test file exists expect(file_exists($elpFile))->toBeTrue('Test ELP file for version 2 not found'); - + $parser = ELPParser::fromFile($elpFile); - + // Check version detection expect($parser->getVersion())->toBe(3); @@ -70,21 +73,22 @@ $strings = $parser->getStrings(); expect($strings)->toBeArray(); expect(count($strings))->toBeGreaterThan(0); - + // Optionally, check for some expected content // expect($strings)->toContain('Some expected text from version 2 file'); } ); it( - 'can parse a version 2 ELP file', function () { + 'can parse a version 2 ELP file', + function () { $elpFile = __DIR__ . '/../Fixtures/exe2-ipe1_parada2.elp'; - + // Ensure the test file exists expect(file_exists($elpFile))->toBeTrue('Test ELP file for version 3 not found'); - + $parser = ELPParser::fromFile($elpFile); - + // Check version detection expect($parser->getVersion())->toBe(2); @@ -101,7 +105,7 @@ $strings = $parser->getStrings(); expect($strings)->toBeArray(); // expect(count($strings))->toBeGreaterThan(0); - + // Optionally, check for some expected content // expect($strings)->toContain('Some expected text from version 3 file'); } @@ -157,23 +161,24 @@ function () { ); it( - 'can extract an ELP file using a temporary directory', function () { + 'can extract an ELP file using a temporary directory', + function () { $elpFile = __DIR__ . '/../Fixtures/exe2-ipe1_parada3.elp'; - + // Create a unique temporary directory $tempDir = sys_get_temp_dir() . '/elp_extracted_' . uniqid(); mkdir($tempDir, 0700, true); - + try { // Create an instance of the parser $parser = ELPParser::fromFile($elpFile); - + // Attempt to extract to the temporary directory $parser->extract($tempDir); - + // Verify that the extraction directory was created expect(is_dir($tempDir))->toBeTrue('The extraction directory was not created'); - + // Verify that the contentv3.xml file exists within the extracted files expect(file_exists($tempDir . '/contentv3.xml'))->toBeTrue('contentv3.xml not found in the extracted files'); } finally { @@ -187,14 +192,15 @@ function () { ); it( - 'can parse a version v26 simple ELP file', function () { + 'can parse a version v26 simple ELP file', + function () { $elpFile = __DIR__ . '/../Fixtures/exe26-editado-con-2.6-simplificado.elp'; - + // Ensure the test file exists expect(file_exists($elpFile))->toBeTrue('Test ELP file for version 3 not found'); - + $parser = ELPParser::fromFile($elpFile); - + // Check version detection expect($parser->getVersion())->toBe(2); @@ -211,21 +217,22 @@ function () { $strings = $parser->getStrings(); expect($strings)->toBeArray(); // expect(count($strings))->toBeGreaterThan(0); - + // Optionally, check for some expected content // expect($strings)->toContain('Some expected text from version 3 file'); } ); it( - 'can parse a version v26 more simple ELP file', function () { + 'can parse a version v26 more simple ELP file', + function () { $elpFile = __DIR__ . '/../Fixtures/exe26-editado-con-2.6-sencillo.elp'; - + // Ensure the test file exists expect(file_exists($elpFile))->toBeTrue('Test ELP file for version 3 not found'); - + $parser = ELPParser::fromFile($elpFile); - + // Check version detection expect($parser->getVersion())->toBe(2); @@ -242,17 +249,18 @@ function () { $strings = $parser->getStrings(); expect($strings)->toBeArray(); // expect(count($strings))->toBeGreaterThan(0); - + // Optionally, check for some expected content // expect($strings)->toContain('Some expected text from version 3 file'); } ); it( - 'throws an exception for invalid ELP file', function () { + 'throws an exception for invalid ELP file', + function () { // Test with inexistent file - $invalidFile0 = __DIR__ . '/../Fixtures/nonexisting.zip'; + $invalidFile0 = __DIR__ . '/../Fixtures/nonexisting.zip'; expect(fn() => ELPParser::fromFile($invalidFile0)) ->toThrow(Exception::class, 'File does not exist.'); @@ -262,13 +270,8 @@ function () { ->toThrow(Exception::class, 'The file is not a valid ZIP file.'); // Test with ZIP but no XML - $invalidFile2 = __DIR__ . '/../Fixtures/invalid.zip'; + $invalidFile2 = __DIR__ . '/../Fixtures/invalid.zip'; expect(fn() => ELPParser::fromFile($invalidFile2)) ->toThrow(Exception::class, 'Invalid ELP file: No content XML found.'); - - - } ); - -