diff --git a/psalm.xml b/psalm.xml index 2d01da2..cb9d2c4 100644 --- a/psalm.xml +++ b/psalm.xml @@ -6,7 +6,7 @@ xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" findUnusedBaselineEntry="true" - findUnusedCode="true" + findUnusedCode="false" > diff --git a/src/Converter.php b/src/Converter.php index f4815e5..be0c1ad 100644 --- a/src/Converter.php +++ b/src/Converter.php @@ -22,21 +22,19 @@ /** * Class to convert an xml string to array */ -final class Converter +class Converter { private array $options; /** * Static constructor. - * - * @psalm-suppress PossiblyUnusedMethod Public api */ - public static function create(array $options = []): self + public static function create(array $options = []): static { - return new self($options); + return new static($options); } - public function __construct(array $options = []) + final public function __construct(array $options = []) { $resolver = new OptionsResolver(); $this->configureOptions($resolver); @@ -51,8 +49,6 @@ public function __construct(array $options = []) * @return array * * @throws ConverterException If errors while parsing XML. - * - * @psalm-suppress PossiblyUnusedMethod Public api */ public function convert(string $xmlToParse): array { @@ -70,30 +66,6 @@ public function convert(string $xmlToParse): array return $array; } - /** - * Create a PHP array from an XML file. - * - * @param string $filename The XML file to parse. - * - * @return array - * - * @throws \RuntimeException If the file does not exist or it's not readable - * - * @psalm-suppress PossiblyUnusedMethod Public api - */ - public function convertFile(string $filename): array - { - if (!file_exists($filename)) { - throw new \RuntimeException("The file `$filename` does not exist."); - } - - if (!is_readable($filename)) { - throw new \RuntimeException("The file `$filename` is not readable: do you have the correct permissions?"); - } - - return $this->convert(file_get_contents($filename)); - } - private function configureOptions(OptionsResolver $resolver): void { $resolver->setDefaults([ diff --git a/src/FileConverter.php b/src/FileConverter.php new file mode 100644 index 0000000..17a6e39 --- /dev/null +++ b/src/FileConverter.php @@ -0,0 +1,44 @@ +convertFile($xmlFile); + $actual = Converter::create()->convert(file_get_contents($xmlFile)); expect($actual)->toBe($expected); })->with(['joomla', 'propel']); diff --git a/tests/Unit/ConverterTest.php b/tests/Unit/ConverterTest.php index 0aa82ca..3478ac9 100644 --- a/tests/Unit/ConverterTest.php +++ b/tests/Unit/ConverterTest.php @@ -121,19 +121,3 @@ $actual = Converter::create(['preserveFirstTag' => true])->convert($xml); expect($actual)->toBe($expected); }); - -it('converts an XML file to an array', function (string $xml, array $expected) { - $file = vfsStream::newFile("test_file.xml")->at($this->getRoot())->setContent($xml); - $actual = $this->converter->convertFile($file->url()); - - expect($expected)->toBe($actual); -})->with('Xml'); - -it('try to convert a not existent file', function () { - $this->converter->convertFile('vfs://root/notexistent.xml'); -})->throws(\RuntimeException::class, 'The file `vfs://root/notexistent.xml` does not exist.'); - -it('try to convert a not readable file', function () { - $file = vfsStream::newFile('notreadable.xml', 200)->at($this->getRoot())->setContent(""); - $this->converter->convertFile($file->url()); -})->throws(\RuntimeException::class, 'The file `vfs://root/notreadable.xml` is not readable: do you have the correct permissions?'); diff --git a/tests/Unit/FileConverterTest.php b/tests/Unit/FileConverterTest.php new file mode 100644 index 0000000..cb9db97 --- /dev/null +++ b/tests/Unit/FileConverterTest.php @@ -0,0 +1,38 @@ +toBeInstanceOf(FileConverter::class); +}); + +it('converts an XML file to an array', function (string $xml, array $expected) { + $file = vfsStream::newFile("test_file.xml")->at($this->getRoot())->setContent($xml); + $actual = FileConverter::create()->convert($file->url()); + + expect($expected)->toBe($actual); +})->with('Xml'); + +it('try to convert a not existent file', function () { + $converter = new FileConverter(); + $converter->convert('vfs://root/notexistent.xml'); +})->throws(\RuntimeException::class, 'The file `vfs://root/notexistent.xml` does not exist.'); + +it('try to convert a not readable file', function () { + $file = vfsStream::newFile('notreadable.xml', 200)->at($this->getRoot())->setContent(""); + FileConverter::create()->convert($file->url()); +})->throws(\RuntimeException::class, 'The file `vfs://root/notreadable.xml` is not readable: do you have the correct permissions?');