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

Dumper.php (11 issues)

1
<?php
2
namespace Dallgoot\Yaml;
3
4
use Dallgoot\Yaml\{Types as T, YamlObject, Tag, Compact};
5
use \SplDoublyLinkedList as DLL;
6
7
/**
8
 *
9
 */
10
class Dumper //extends AnotherClass
11
{
12
    private const indent = 2;
13
    private const width = 160;
14
    private const options = 00000;
15
16
    private static $result;
17
    //options
18
    public const EXPAND_SHORT = 00001;
19
    public const SERIALIZE_CUSTOM_OBJECTS = 00010;
20
21
    public function __construct(int $options = null)
22
    {
23
        if (!is_null($options)) {
24
            $this->options = $options;
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
        }
26
    }
27
28
    public static function toString($dataType, int $options):string
29
    {
30
        if (is_null($dataType)) {
31
            throw new \Exception(self::class.": No content to convert to Yaml", 1);
32
        }
33
        $options = is_null($options) ? self::options : $options;
0 ignored issues
show
The condition is_null($options) is always false.
Loading history...
The assignment to $options is dead and can be removed.
Loading history...
34
        self::$result = new DLL;
35
        self::$result->setIteratorMode(DLL::IT_MODE_FIFO | DLL::IT_MODE_DELETE);
36
        if ($dataType instanceof YamlObject) {
37
            return self::dumpYamlObject($dataType);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::dumpYamlObject($dataType) returns the type null which is incompatible with the type-hinted return string.
Loading history...
Are you sure the usage of self::dumpYamlObject($dataType) targeting Dallgoot\Yaml\Dumper::dumpYamlObject() 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...
38
        } elseif (is_array($dataType) && $dataType[0] instanceof YamlObject) {
39
            array_map([self::class, 'dumpYamlObject'], $dataType);
40
        } else {
41
            self::dump($dataType, 0);
42
        }
43
        return implode("\n", self::$result);
0 ignored issues
show
self::result of type SplDoublyLinkedList is incompatible with the type array expected by parameter $pieces of implode(). ( Ignorable by Annotation )

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

43
        return implode("\n", /** @scrutinizer ignore-type */ self::$result);
Loading history...
44
    }
45
46
    public static function toFile(string $file, $dataType, int $options):bool
47
    {
48
        return !is_bool(file_put_contents($file, self::toString($dataType, $options)));
49
    }
50
51
    private static function dump($dataType, int $indent)
52
    {
53
        if (is_object($dataType)) {
54
            if ($dataType instanceof Tag) {
55
                foreach (self::split("!".$dataType->tagName.' '.$dataType->value, $indent) as $chunks) {
0 ignored issues
show
Are you sure $dataType->value of type null|string|Dallgoot\Yaml\Node can be used in concatenation? ( Ignorable by Annotation )

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

55
                foreach (self::split("!".$dataType->tagName.' './** @scrutinizer ignore-type */ $dataType->value, $indent) as $chunks) {
Loading history...
56
                    self::$result->push($chunks);
57
                }
58
            }
59
            if ($dataType instanceof Compact) {
60
                self::dumpCompact($dataType, $indent);
61
            }
62
            if ($dataType instanceof \DateTime) {
63
                # code...
64
            }
65
        } elseif (is_array($dataType)) {
66
            self::dumpSequence($dataType, $indent);
67
        }
68
    }
69
70
    private static function dumpYamlObject(YamlObject $dataType)
71
    {
72
        self::dump($dataType, 0);
73
        self::insertComments($dataType->getComment());
0 ignored issues
show
The method getComment() does not exist on Dallgoot\Yaml\YamlObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

73
        self::insertComments($dataType->/** @scrutinizer ignore-call */ getComment());
Loading history...
74
        //TODO: $references = $dataType->getAllReferences();
75
    }
76
77
    private static function split($string, $indent):array
78
    {
79
        return str_split(str_pad($string, $indent, " ", STR_PAD_LEFT), self::width);
80
    }
81
82
    private static function dumpSequence(array $array, int $indent):void
83
    {
84
        $refKeys = range(0, count($array));
85
        foreach ($array as $key => $item) {
86
            $lineStart = current($refKeys) === $key ? "- " : "- $key: ";
87
            $value     = self::dump($item, $indent + self::indent);
0 ignored issues
show
Are you sure the assignment to $value is correct as self::dump($item, $indent + self::indent) 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 assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
88
            if (is_null($value)) {
89
                self::$result->push(self::split($lineStart, $indent + self::indent)[0]);
90
            } else {
91
                foreach (self::split($lineStart.$value, $indent + self::indent) as $chunks) {
92
                    self::$result->push($chunks);
93
                }
94
            }
95
            next($refKeys);
96
        }
97
    }
98
99
    private static function insertComments($commentsArray):void
100
    {
101
        foreach ($commentsArray as $lineNb => $comment) {
102
            self::$result->add($lineNb, $comment);
103
        }
104
    }
105
106
    public static function dumpCompact($dataType, int $indent)
0 ignored issues
show
The parameter $dataType 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

106
    public static function dumpCompact(/** @scrutinizer ignore-unused */ $dataType, 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...
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

106
    public static function dumpCompact($dataType, /** @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...
107
    {
108
        # code...
109
    }
110
}
111