Skip to content

Commit 43678b8

Browse files
authored
Improving the file handling (#81)
* Rebuilding file handling * Raise cyclomatic complexity level due to isXMLFileValid method and use better naming in constants
1 parent 4dc6dd1 commit 43678b8

File tree

10 files changed

+231
-92
lines changed

10 files changed

+231
-92
lines changed

cyclomatic.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<rule ref="rulesets/codesize.xml/CyclomaticComplexity">
1515
<priority>1</priority>
1616
<properties>
17-
<property name="reportLevel" value="4" />
17+
<property name="reportLevel" value="7" />
1818
</properties>
1919
</rule>
2020
</ruleset>

src/Constants/ErrorMessages.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
final class ErrorMessages
1111
{
1212
public const XML_EMPTY_TRIMMED = 'The provided XML content is, after trimming, in fact an empty string';
13-
public const XML_NO_NAME = 'xmlParseEntityRef: no name';
14-
public const NO_FILE_CONTENTS = 'Could not get file contents';
13+
public const EMPTY_FILE = 'File is empty';
14+
public const FILE_DOES_NOT_EXIST = 'File does not exist';
15+
public const FILE_COULD_NOT_BE_OPENED = 'File could not be opened';
1516
}

src/Exceptions/EmptyFile.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Hyperized\Xml\Exceptions;
4+
5+
use Exception;
6+
7+
/**
8+
* Class EmptyFile
9+
*
10+
* @package Hyperized\Xml\Exceptions
11+
*/
12+
class EmptyFile extends Exception
13+
{
14+
}

src/Exceptions/FileDoesNotExist.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Hyperized\Xml\Exceptions;
4+
5+
use Exception;
6+
7+
/**
8+
* Class FileDoesNotExist
9+
*
10+
* @package Hyperized\Xml\Exceptions
11+
*/
12+
class FileDoesNotExist extends Exception
13+
{
14+
}

src/Types/File.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Hyperized\Xml\Types;
4+
5+
use Hyperized\Xml\Constants\ErrorMessages;
6+
use Hyperized\Xml\Exceptions\EmptyFile;
7+
use Hyperized\Xml\Exceptions\FileCouldNotBeOpenedException;
8+
use Hyperized\Xml\Exceptions\FileDoesNotExist;
9+
10+
/**
11+
* Class File
12+
*
13+
* @package Hyperized\Xml\Types\Xml
14+
*/
15+
class File
16+
{
17+
/**
18+
* @var string
19+
*/
20+
private $path;
21+
22+
/**
23+
* File constructor.
24+
*
25+
* @param string $path
26+
* @throws FileDoesNotExist
27+
*/
28+
public function __construct(string $path)
29+
{
30+
$this->path = $path;
31+
$this->pathExists();
32+
}
33+
34+
/**
35+
* @return bool
36+
* @throws FileDoesNotExist
37+
*/
38+
private function pathExists(): bool
39+
{
40+
if (!file_exists($this->path)) {
41+
throw new FileDoesNotExist(ErrorMessages::FILE_DOES_NOT_EXIST);
42+
}
43+
return true;
44+
}
45+
46+
/**
47+
* @return string
48+
* @throws EmptyFile
49+
* @throws FileCouldNotBeOpenedException
50+
*/
51+
public function getContents(): string
52+
{
53+
$contents = file_get_contents($this->path);
54+
55+
if ($contents === false) {
56+
throw new FileCouldNotBeOpenedException(ErrorMessages::FILE_COULD_NOT_BE_OPENED);
57+
}
58+
59+
if ($contents === '') {
60+
throw new EmptyFile(ErrorMessages::EMPTY_FILE);
61+
}
62+
63+
return $contents;
64+
}
65+
66+
/**
67+
* @return string
68+
*/
69+
public function getPath(): string
70+
{
71+
return $this->path;
72+
}
73+
}

src/Types/Files/Xml.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Hyperized\Xml\Types\Files;
4+
5+
use Hyperized\Xml\Types\File;
6+
7+
/**
8+
* Class Xml
9+
*
10+
* @package Hyperized\Xml\Types\Files
11+
*/
12+
class Xml extends File
13+
{
14+
}

src/Types/Files/Xsd.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Hyperized\Xml\Types\Files;
4+
5+
use Hyperized\Xml\Types\File;
6+
7+
/**
8+
* Class Xsd
9+
*
10+
* @package Hyperized\Xml\Types\Files
11+
*/
12+
class Xsd extends File
13+
{
14+
}

src/Validator.php

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace Hyperized\Xml;
44

55
use DOMDocument;
6-
use Exception;
76
use Hyperized\Xml\Constants\ErrorMessages;
87
use Hyperized\Xml\Constants\Strings;
98
use Hyperized\Xml\Exceptions\FileCouldNotBeOpenedException;
109
use Hyperized\Xml\Exceptions\InvalidXml;
10+
use Hyperized\Xml\Exceptions\EmptyFile;
11+
use Hyperized\Xml\Exceptions\FileDoesNotExist;
12+
use Hyperized\Xml\Types\Files\Xml;
13+
use Hyperized\Xml\Types\Files\Xsd;
1114
use function is_string;
1215
use LibXMLError;
1316

@@ -28,30 +31,46 @@ final class Validator implements ValidatorInterface
2831
*/
2932
private $encoding = Strings::UTF_8;
3033

31-
/**
32-
* @param string $xmlPath
33-
* @param string|null $xsdPath
34-
* @return bool
35-
* @throws FileCouldNotBeOpenedException
36-
* @throws InvalidXml
37-
*/
3834
public function isXMLFileValid(string $xmlPath, string $xsdPath = null): bool
3935
{
40-
return $this->isXMLStringValid(self::getFileContent($xmlPath), $xsdPath);
36+
try {
37+
$string = (new Xml($xmlPath))
38+
->getContents();
39+
} catch (EmptyFile $e) {
40+
return false;
41+
} catch (FileCouldNotBeOpenedException $e) {
42+
return false;
43+
} catch (FileDoesNotExist $e) {
44+
return false;
45+
}
46+
47+
if ($xsdPath !== null) {
48+
try {
49+
$xsdPath = (new Xsd($xsdPath))
50+
->getPath();
51+
} catch (FileDoesNotExist $e) {
52+
return false;
53+
}
54+
}
55+
56+
return $this->isXMLStringValid($string, $xsdPath);
4157
}
4258

4359
/**
4460
* @param string $xml
4561
* @param string|null $xsdPath
4662
* @return bool
47-
* @throws InvalidXml
4863
*/
4964
public function isXMLStringValid(string $xml, string $xsdPath = null): bool
5065
{
51-
if (is_string($xsdPath)) {
52-
return $this->isXMLValid($xml, $xsdPath);
66+
try {
67+
if (is_string($xsdPath)) {
68+
return $this->isXMLValid($xml, $xsdPath);
69+
}
70+
return $this->isXMLValid($xml);
71+
} catch (InvalidXml $e) {
72+
return false;
5373
}
54-
return $this->isXMLValid($xml);
5574
}
5675

5776
/**
@@ -112,22 +131,6 @@ static function (
112131
}
113132
}
114133

115-
/**
116-
* @param string $fileName
117-
* @return string
118-
* @throws FileCouldNotBeOpenedException
119-
*/
120-
private static function getFileContent(string $fileName): string
121-
{
122-
try {
123-
$contents = file_get_contents($fileName);
124-
} catch (Exception $exception) {
125-
throw new FileCouldNotBeOpenedException(ErrorMessages::NO_FILE_CONTENTS);
126-
}
127-
128-
return !$contents ? $contents : '';
129-
}
130-
131134
/**
132135
* @return string
133136
*/

tests/InvalidStreamWrapper.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Hyperized\Xml\Tests;
4+
5+
/**
6+
* Class InvalidStreamWrapper
7+
*
8+
* @package Hyperized\Xml\Types
9+
*/
10+
class InvalidStreamWrapper
11+
{
12+
/**
13+
* @return bool
14+
*/
15+
public function stream_open(): bool
16+
{
17+
return false;
18+
}
19+
20+
/**
21+
* @return array
22+
*/
23+
public function url_stat(): array
24+
{
25+
return [];
26+
}
27+
}

0 commit comments

Comments
 (0)