2
2
3
3
namespace Differ \Parser ;
4
4
5
+ use Exception ;
5
6
use Symfony \Component \Yaml \Yaml ;
6
7
7
8
use function Differ \Formatters \selectFormatter ;
@@ -11,24 +12,30 @@ function parse(array $resultDiff, string $format): string
11
12
return selectFormatter ($ resultDiff , $ format );
12
13
}
13
14
15
+ /**
16
+ * @throws Exception
17
+ */
14
18
function parseToJson (string $ path ): array
15
19
{
16
20
$ content = getFileContent ($ path );
17
21
$ offset = strpos ($ path , '. ' );
18
22
19
23
if ($ offset === false ) {
20
- throw new \ Exception ("Incorrect path " );
24
+ throw new Exception ("Incorrect path: { $ path } " );
21
25
}
22
26
23
27
$ extension = substr ($ path , $ offset + 1 );
24
28
25
29
return match ($ extension ) {
26
30
'json ' => json_decode ($ content , true ),
27
31
'yaml ' => Yaml::parse ($ content ),
28
- default => throw new \ Exception ("There is no such extension : {$ extension }" )
32
+ default => throw new Exception ("The file format is not supported by the current version of the program : {$ extension }" )
29
33
};
30
34
}
31
35
36
+ /**
37
+ * @throws Exception
38
+ */
32
39
function getFileContent (string $ path ): string
33
40
{
34
41
$ dirPath = __DIR__ ;
@@ -40,7 +47,7 @@ function getFileContent(string $path): string
40
47
}
41
48
42
49
if (!file_exists ($ fullPath )) {
43
- throw new \ Exception ("Incorrect path " );
50
+ throw new Exception ("File does not exist: { $ path} " );
44
51
}
45
52
46
53
return file_get_contents ($ fullPath );
0 commit comments