Test Failed
Branch master (b38a7a)
by stéphane
12:04
created

Yaml::dump()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Dallgoot;
4
5
/**
6
 * TODO
7
 *
8
 * @author  Stéphane Rebai <[email protected]>
9
 * @license Apache 2.0
10
 * @link    TODO : url to specific online doc
11
 */
12
final class Yaml
13
{
14
    /**
15
     * Parse the given Yaml string to a PHP type
16
     *
17
     * @param string   $someYaml Some yaml
18
     * @param int|null $options  from Loader class, bitwise combination of
19
     *                           Loader::IGNORE_DIRECTIVES
20
     *                           Loader::IGNORE_COMMENTS
21
     *                           Loader::NO_PARSING_EXCEPTIONS
22
     *                           Loader::NO_OBJECT_FOR_DATE
23
     * @param int|null $debug    define the level of debugging (true = default)
24
     *
25
     * @return YamlObject|array|null a Yaml document as YamlObject OR multiple documents as an array of YamlObject,
0 ignored issues
show
Bug introduced by
The type Dallgoot\YamlObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
     *                               NULL if Error
27
     * @throws \Exception coming from Dallgoot\Yaml\Loader
28
     * @see    Dallgoot\Yaml\Loader
29
     */
30
    public static function parse(string $someYaml, $options = null, $debug = null)
31
    {
32
        try {
33
            return (new Yaml\Loader(null, $options, $debug))->parse($someYaml);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Dallgoot\Yaml...ebug)->parse($someYaml) also could return the type Dallgoot\Yaml\YamlObject which is incompatible with the documented return type Dallgoot\YamlObject|array|null.
Loading history...
34
        } catch (\Exception|\Error|\ParseError $e) {
35
            throw new \Exception(__CLASS__." Error while parsing YAML string", 1, $e);
36
        }
37
    }
38
39
    /**
40
     * Load the given file and parse its content (assumed YAML) to a PHP type
41
     *
42
     * @param string   $fileName Some file path name
43
     * @param int|null $options  from Loader class, bitwise combination of
44
     *                           Loader::IGNORE_DIRECTIVES
45
     *                           Loader::IGNORE_COMMENTS
46
     *                           Loader::NO_PARSING_EXCEPTIONS
47
     *                           Loader::NO_OBJECT_FOR_DATE
48
     * @param int|null $debug    define the level of debugging (true = default)
49
     *
50
     * @return YamlObject|array|null a Yaml document as YamlObject OR multiple documents as an array of YamlObject,
51
     *                               NULL if Error
52
     * @throws \Exception coming from Dallgoot\Yaml\Loader
53
     * @see    Dallgoot\Yaml\Loader
54
     */
55
    public static function parseFile(string $fileName, $options = null, $debug = null)
56
    {
57
        try {
58
            return (new Yaml\Loader($fileName, $options, (int) $debug))->parse();
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Dallgoot\Yaml..., (int)$debug)->parse() also could return the type Dallgoot\Yaml\YamlObject which is incompatible with the documented return type Dallgoot\YamlObject|array|null.
Loading history...
59
        } catch (\Exception|\Error|\ParseError $e) {
60
            throw new \Exception(__CLASS__." Error during parsing '$fileName'", 1, $e);
61
        }
62
63
    }
64
65
    /**
66
     * Returns the YAML representation corresponding to given PHP variable
67
     *
68
     * @param mixed    $somePhpVar Some php variable
69
     * @param int|null $options    enable/disable some options see Dumper
70
     *
71
     * @return string  ( the representation of $somePhpVar as a YAML content (single or multiple document accordingly) )
72
     * @throws \Exception on errors during building YAML string coming from Dumper class
73
     * @see    Dumper
74
     */
75
    public static function dump($somePhpVar, $options = null):string
76
    {
77
        try {
78
            return Yaml\Dumper::toString($somePhpVar, $options);
79
        } catch (\Exception|\Error|\ParseError $e) {
80
            throw new \Exception(__CLASS__." Error dumping", 1, $e);
81
        }
82
    }
83
84
85
    /**
86
     * Builds the YAML representation corresponding to given PHP variable ($somePhpVar)
87
     * AND save it as file with the $fileName provided.
88
     *
89
     * @param string   $fileName   The file name
90
     * @param mixed    $somePhpVar Some php variable
91
     * @param int|null $options    Dumper::constants as options
92
     *
93
     * @return boolean  true if YAML built and saved , false if error during writing file
94
     * @throws \Exception on errors (from Dumper::toString) during building YAML string
95
     * @see    Dumper
96
     */
97
    public static function dumpFile(string $fileName, $somePhpVar, $options = null):bool
98
    {
99
        try {
100
            return Yaml\Dumper::toFile($fileName, $somePhpVar, $options);
101
        } catch (\Exception|\Error|\ParseError $e) {
102
            throw new \Exception(__CLASS__." Error during dumping '$fileName'", 1, $e);
103
        }
104
    }
105
106
    public static function isOneOf($subject, array $comparison):bool
107
    {
108
        foreach ($comparison as $className) {
109
            $fqn = __NAMESPACE__."\\Yaml\\$className";
110
            if ($subject instanceof $fqn) return true;
111
        }
112
        return false;
113
    }
114
}