Passed
Branch master (f496ba)
by stéphane
02:11
created

Dumper::split()   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\{Types as T, YamlObject, Tag, Compact};
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Dallgoot\Yaml\Compact. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, Dallgoot\Yaml\Tag. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, Dallgoot\Yaml\YamlObject. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use \SplDoublyLinkedList as DLL;
6
7
/**
0 ignored issues
show
Coding Style introduced by
Doc comment is empty
Loading history...
8
 *
9
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
10
class Dumper //extends AnotherClass
11
{
12
    private const indent = 2;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected INDENT).
Loading history...
13
    private const width = 120;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected WIDTH).
Loading history...
14
    private const options = 00000;
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected OPTIONS).
Loading history...
15
16
    private static $result;
0 ignored issues
show
Coding Style introduced by
Private member variable "result" must be prefixed with an underscore
Loading history...
17
    private static $options;
0 ignored issues
show
Coding Style introduced by
Private member variable "options" must be prefixed with an underscore
Loading history...
18
    //options
19
    public const EXPAND_SHORT = 00001;
20
    public const SERIALIZE_CUSTOM_OBJECTS = 00010;
21
22
    public function __construct(int $options = null)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
23
    {
24
        if (is_int($options)) self::$options = $options;
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
25
    }
26
27
    public static function toString($dataType, int $options):string
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
28
    {
29
        if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1);
1 ignored issue
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
30
        self::$options = is_int($options) ? $options : self::$options;
0 ignored issues
show
introduced by
The condition is_int($options) is always true.
Loading history...
31
        self::$result = new DLL;
32
        self::$result->setIteratorMode(DLL::IT_MODE_FIFO | DLL::IT_MODE_DELETE);
33
        if ($dataType instanceof YamlObject) {
34
            self::dumpYamlObject($dataType);
35
        } elseif (is_array($dataType) && $dataType[0] instanceof YamlObject) {
36
            array_map([self::class, 'dumpYamlObject'], $dataType);
37
        } else {
38
            self::dump($dataType, 0);
39
        }
40
        $out = '';
41
        foreach (self::$result as $value) {
42
            $out .= $value."\n";
43
        }
44
        self::$result = null;
45
        return $out;
46
    }
47
48
    public static function toFile(string $filePath, $dataType, int $options):bool
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
49
    {
50
        return !is_bool(file_put_contents($filePath, self::toString($dataType, $options)));
51
    }
52
53
    private static function dump($dataType, int $indent)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "Dumper::dump" must be prefixed with an underscore
Loading history...
54
    {
55
        if (is_scalar($dataType)) {
56
            switch (gettype($dataType)) {
57
                case 'boolean': return $dataType ? 'true' : 'false';break;
1 ignored issue
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
58
                case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf';
2 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
Coding Style introduced by
Inline control structures are discouraged
Loading history...
59
                case 'double': if (is_nan($dataType)) return '.nan';
2 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
Coding Style introduced by
Inline control structures are discouraged
Loading history...
60
                default:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
61
                    return $dataType;
62
            }
63
        } elseif (is_object($dataType)) {
64
            self::dumpObject($dataType, $indent);
0 ignored issues
show
Bug Best Practice introduced by
The method Dallgoot\Yaml\Dumper::dumpObject() is not static, but was called statically. ( Ignorable by Annotation )

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

64
            self::/** @scrutinizer ignore-call */ 
65
                  dumpObject($dataType, $indent);
Loading history...
65
        } elseif (is_array($dataType)) {
66
            self::dumpSequence($dataType, $indent);
67
        }
68
    }
69
70
    private static function dumpYamlObject(YamlObject $dataType)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "Dumper::dumpYamlObject" must be prefixed with an underscore
Loading history...
71
    {
72
        self::$result->push("---");
73
        return self::dump($dataType, 0);
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::dump($dataType, 0) targeting Dallgoot\Yaml\Dumper::dump() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
74
        // self::insertComments($dataType->getComment());
75
        //TODO: $references = $dataType->getAllReferences();
76
    }
77
78
    private static function add($value, $indent)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "Dumper::add" must be prefixed with an underscore
Loading history...
79
    {
80
        $newVal = str_repeat(" ", $indent).$value;
81
        foreach (str_split($newVal, self::width) as $chunks) {
82
            self::$result->push($chunks);
83
        }
84
    }
85
86
    private static function dumpSequence(array $array, int $indent):void
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "Dumper::dumpSequence" must be prefixed with an underscore
Loading history...
87
    {
88
        $refKeys = range(0, count($array));
89
        foreach ($array as $key => $item) {
90
            $lineStart = current($refKeys) === $key ? "- " : "- $key: ";
91
            if (is_scalar($item)) {
92
                self::add($lineStart.$item, $indent );
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
93
            } else {
94
                self::add($lineStart, $indent );
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
95
                self::dump($item, $indent + self::indent);
96
            }
97
            next($refKeys);
98
        }
99
    }
100
101
    private static function insertComments(array $commentsArray):void
0 ignored issues
show
Unused Code introduced by
The method insertComments() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "Dumper::insertComments" must be prefixed with an underscore
Loading history...
102
    {
103
        foreach ($commentsArray as $lineNb => $comment) {
104
            self::$result->add($lineNb, $comment);
105
        }
106
    }
107
108
    private function dumpObject(object $object, int $indent)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed. ( Ignorable by Annotation )

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

108
    private function dumpObject(/** @scrutinizer ignore-unused */ object $object, int $indent)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "Dumper::dumpObject" must be prefixed with an underscore
Loading history...
109
    {
110
        if ($dataType instanceof Tag) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $dataType seems to be never defined.
Loading history...
111
            if (is_scalar($dataType->value)) {
112
                return "!".$dataType->tagName.' '.$dataType->value;
113
            } else{
0 ignored issues
show
Coding Style introduced by
Expected "} else \n"; found " else{\n"
Loading history...
114
                yield "!".$dataType->tagName;
115
                self::dump($dataType->value, $indent + self::indent);
116
            }
117
        }
118
        if ($dataType instanceof Compact) {//TODO
119
            self::dumpCompact($dataType, $indent);
120
        }
121
        if ($dataType instanceof \DateTime) {
122
            # code...
0 ignored issues
show
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
123
        }
124
        $propList = get_object_vars($dataType);
125
        foreach ($propList as $property => $value) {
126
            if (is_scalar($value)) {
127
                self::add("$property: ".$value, $indent);
128
            } else {
129
                self::add("$property: ", $indent);
130
                self::dump($value, $indent + self::indent);
131
            }
132
        }
133
    }
134
135
    public static function dumpCompact(Compact $object, int $indent)
0 ignored issues
show
Unused Code introduced by
The parameter $object is not used and could be removed. ( Ignorable by Annotation )

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

135
    public static function dumpCompact(/** @scrutinizer ignore-unused */ Compact $object, int $indent)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $indent is not used and could be removed. ( Ignorable by Annotation )

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

135
    public static function dumpCompact(Compact $object, /** @scrutinizer ignore-unused */ int $indent)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
136
    {
137
        // if (empty($object)) return "{}";
138
        // if (empty($object)) return "[]";
139
    }
140
}
141