Completed
Push — master ( f496ba...7a981c )
by stéphane
02:09
created

Yaml::dumpFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Dallgoot\Yaml;
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
4
use Dallgoot\Yaml as Y;
5
// declaring constants for Dallgoot\Yaml
6
$TYPES = ['DIRECTIVE',
7
            'DOC_START',
8
            'DOC_END',
9
            'COMMENT',
10
            'BLANK',
11
            'ROOT',
12
            'KEY',
13
            'ITEM',
14
            'MAPPING',
15
            'SEQUENCE',
16
            'MAPPING_SHORT',
17
            'SEQUENCE_SHORT',
18
            'PARTIAL',
19
            'LITT', //litteral
20
            'LITT_FOLDED',//litteral
21
            'SCALAR',
22
            'TAG',
23
            'JSON',
24
            'QUOTED',
25
            'RAW',
26
            'REF_DEF', //reference
27
            'REF_CALL', //reference
28
            'SET',
29
            'SET_KEY',
30
            'SET_VALUE'];
31
32
33
foreach ($TYPES as $power => $name) {
34
    define(__NAMESPACE__."\\$name", 2**$power);
35
}
36
37
const LITTERALS = Y\LITT | Y\LITT_FOLDED;
0 ignored issues
show
Bug introduced by
The constant Dallgoot\Yaml\Y\LITT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant Dallgoot\Yaml\Y\LITT_FOLDED was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
38
// print_r(get_defined_constants(true)['user']);
39
40
namespace Dallgoot;
41
42
class Yaml
0 ignored issues
show
Coding Style introduced by
Missing class doc comment
Loading history...
43
{
44
    /* @var null|array */
45
    private static $TYPE_NAMES = null;
0 ignored issues
show
Coding Style introduced by
Private member variable "TYPE_NAMES" must be prefixed with an underscore
Loading history...
46
47
    /**
48
     * Gets the name for a given constant declared in the Dallgoot\Yaml namespace
49
     * @param      integer  $typeInteger  The constant value
3 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
50
     *
51
     * @return     string    The name.
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
52
     */
53
    public static function getName($typeInteger)
54
    {
55
        if(is_null(self::$TYPE_NAMES)) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
56
            $f = function ($v) { return str_replace('Dallgoot\Yaml\\', '', $v);};
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
57
            self::$TYPE_NAMES = array_map($f, array_flip(get_defined_constants(true)['user']));
58
        }
59
        return self::$TYPE_NAMES[$typeInteger];
60
    }
61
62
    /**
63
     * Parse the given Yaml string to a PHP type
64
     *
65
     * @param      string  $someYaml  Some yaml
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
66
     *
67
     * @return     YamlObject|array    ( return a PHP type representation with Yaml document as YamlObject and multiple
1 ignored issue
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...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
68
     * documents as an array of YamlObject )
69
     */
70
    public static function parse(string $someYaml)
71
    {
72
        return (new Yaml\Loader)->parse($someYaml);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Dallgoot\Yaml...der()->parse($someYaml) also could return the type Dallgoot\Yaml\YamlObject which is incompatible with the documented return type array|Dallgoot\YamlObject.
Loading history...
73
    }
74
75
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $fileName should have a doc-comment as per coding-style.
Loading history...
76
     * Load the given file and parse its content (assumed YAML) to a PHP type
77
     *
78
     * @param      string  $someYaml  Some yaml
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Doc comment for parameter $someYaml does not match actual variable name $fileName
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
79
     *
80
     * @return     YamlObject|array    ( return a PHP type representation with Yaml document as YamlObject and multiple
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
81
     * documents as an array of YamlObject )
82
     */
83
    public static function parseFile(string $fileName)
84
    {
85
        return (new Yaml\Loader($fileName))->parse();
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Dallgoot\Yaml...der($fileName)->parse() also could return the type Dallgoot\Yaml\YamlObject which is incompatible with the documented return type array|Dallgoot\YamlObject.
Loading history...
86
    }
87
88
    /**
89
     * Returns the YAML representation corresponding to given PHP variable
90
     *
91
     * @param      mixed  $somePhpVar  Some php variable
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 2 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
92
     *
93
     * @return     string  ( the representation of $somePhpVar as a YAML content (single or multiple document according to argument) )
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
94
     * @throws   Exception on errors during building YAML string
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
95
     * @see Dumper::toString
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 4 spaces but found 1
Loading history...
96
     */
97
    public static function dump($somePhpVar):string
98
    {
99
        return Yaml\Dumper::toString($somePhpVar);
0 ignored issues
show
Bug introduced by
The call to Dallgoot\Yaml\Dumper::toString() has too few arguments starting with options. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        return Yaml\Dumper::/** @scrutinizer ignore-call */ toString($somePhpVar);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
100
    }
101
102
    /**
103
     * Builds the YAML representation corresponding to given PHP variable ($somePhpVar)
104
     * AND save it as file with the $fileName provided.
105
     *
106
     * @param      string   $fileName    The file name
3 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter type; 3 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
107
     * @param      mixed   $somePhpVar  Some php variable
3 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 3 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 6
Loading history...
108
     *
109
     * @return     boolean  true if YAML built and saved , false otherwise
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 5
Loading history...
110
     * @throws   Exception on errors during building YAML string
1 ignored issue
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
111
     */
112
    public static function dumpFile(string $fileName, $somePhpVar):boolean
0 ignored issues
show
Bug introduced by
The type Dallgoot\boolean 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...
113
    {
114
        return Yaml\Dumper::toFile($fileName, $somePhpVar);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Dallgoot\Yaml\Dum...$fileName, $somePhpVar) returns the type boolean which is incompatible with the type-hinted return Dallgoot\boolean.
Loading history...
Bug introduced by
The call to Dallgoot\Yaml\Dumper::toFile() has too few arguments starting with options. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

114
        return Yaml\Dumper::/** @scrutinizer ignore-call */ toFile($fileName, $somePhpVar);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
115
    }
116
}
117