dallgoot /
yaml
| 1 | <?php |
||||||
| 2 | |||||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||||
| 3 | namespace Dallgoot\Yaml; |
||||||
| 4 | |||||||
| 5 | use Dallgoot\Yaml\{Node as Node, Types as T, YamlObject, Tag, Builder}; |
||||||
|
0 ignored issues
–
show
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
namespace OtherDir;
use SomeDir\Foo; // This now conflicts the class OtherDir\Foo
If both files PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as // Bar.php
namespace OtherDir;
use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
This use statement conflicts with another class in this namespace,
Dallgoot\Yaml\Builder. 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
namespace OtherDir;
use SomeDir\Foo; // This now conflicts the class OtherDir\Foo
If both files PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as // Bar.php
namespace OtherDir;
use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
namespace OtherDir;
use SomeDir\Foo; // This now conflicts the class OtherDir\Foo
If both files PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as // Bar.php
namespace OtherDir;
use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
This use statement conflicts with another class in this namespace,
Dallgoot\Yaml\Node. 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
namespace OtherDir;
use SomeDir\Foo; // This now conflicts the class OtherDir\Foo
If both files PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as // Bar.php
namespace OtherDir;
use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
|
|||||||
| 6 | use \SplDoublyLinkedList as DLL; |
||||||
| 7 | |||||||
| 8 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 9 | * |
||||||
| 10 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 11 | class Builder |
||||||
| 12 | { |
||||||
| 13 | private static $root; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 14 | private static $debug; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 15 | |||||||
| 16 | const ERROR_NO_KEYNAME = self::class.": key has NO IDENTIFIER on line %d"; |
||||||
| 17 | const INVALID_DOCUMENT = self::class.": DOCUMENT %d can NOT be a mapping AND a sequence"; |
||||||
| 18 | |||||||
| 19 | |||||||
| 20 | private static function build(object $node, &$parent = null) |
||||||
|
1 ignored issue
–
show
|
|||||||
| 21 | { |
||||||
| 22 | if ($node instanceof DLL) return self::buildDLL($node, $parent); |
||||||
|
1 ignored issue
–
show
|
|||||||
| 23 | return self::buildNode($node, $parent); |
||||||
| 24 | } |
||||||
| 25 | |||||||
| 26 | private static function buildDLL(DLL $node, &$parent) |
||||||
|
0 ignored issues
–
show
|
|||||||
| 27 | { |
||||||
| 28 | $type = property_exists($node, "type") ? $node->type : null; |
||||||
| 29 | if (in_array($type, [T::RAW, T::LITTERAL, T::LITTERAL_FOLDED])) { |
||||||
| 30 | return self::litteral($node, $type); |
||||||
| 31 | } |
||||||
| 32 | $p = $parent; |
||||||
| 33 | switch ($type) { |
||||||
| 34 | case T::MAPPING: //fall through |
||||||
|
1 ignored issue
–
show
|
|||||||
| 35 | case T::SET: $p = new \StdClass;break; |
||||||
|
1 ignored issue
–
show
|
|||||||
| 36 | case T::SEQUENCE: $p = [];break; |
||||||
|
1 ignored issue
–
show
|
|||||||
| 37 | // case T::KEY: $p = $parent;break; |
||||||
| 38 | } |
||||||
| 39 | $out = null; |
||||||
| 40 | foreach ($node as $child) { |
||||||
| 41 | $result = self::build($child, $p); |
||||||
| 42 | if (!is_null($result)) { |
||||||
| 43 | if (is_string($result)) { |
||||||
| 44 | $out .= $result.' '; |
||||||
| 45 | } else { |
||||||
| 46 | return $result; |
||||||
| 47 | } |
||||||
| 48 | } |
||||||
| 49 | } |
||||||
| 50 | return is_null($out) ? $p : rtrim($out); |
||||||
| 51 | } |
||||||
| 52 | |||||||
| 53 | private static function buildNode(Node $node, &$parent) |
||||||
|
0 ignored issues
–
show
|
|||||||
| 54 | { |
||||||
| 55 | list($line, $type, $value, $identifier) = [$node->line, $node->type, $node->value, $node->identifier]; |
||||||
| 56 | switch ($type) { |
||||||
| 57 | case T::COMMENT: self::$root->addComment($line, $value);return; |
||||||
|
1 ignored issue
–
show
|
|||||||
| 58 | case T::DIRECTIVE: return;//TODO |
||||||
|
1 ignored issue
–
show
|
|||||||
| 59 | case T::ITEM: self::buildItem($value, $parent);return; |
||||||
|
1 ignored issue
–
show
|
|||||||
| 60 | case T::KEY: self::buildKey($node, $parent);return; |
||||||
|
1 ignored issue
–
show
|
|||||||
| 61 | case T::REF_DEF: //fall through |
||||||
|
1 ignored issue
–
show
|
|||||||
| 62 | case T::REF_CALL://TODO: self::build returns what ? |
||||||
|
1 ignored issue
–
show
|
|||||||
| 63 | $tmp = is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
||||||
|
0 ignored issues
–
show
Are you sure the usage of
self::build($value, $parent) targeting Dallgoot\Yaml\Builder::build() 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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||||
| 64 | if ($type === T::REF_DEF) self::$root->addReference($identifier, $tmp); |
||||||
|
1 ignored issue
–
show
|
|||||||
| 65 | return self::$root->getReference($identifier); |
||||||
| 66 | case T::SET_KEY: |
||||||
|
1 ignored issue
–
show
|
|||||||
| 67 | $key = json_encode(self::build($value, $parent), JSON_PARTIAL_OUTPUT_ON_ERROR|JSON_UNESCAPED_SLASHES); |
||||||
|
0 ignored issues
–
show
Are you sure the usage of
self::build($value, $parent) targeting Dallgoot\Yaml\Builder::build() 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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
It seems like
$value can also be of type null and string; however, parameter $node of Dallgoot\Yaml\Builder::build() does only seem to accept object, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 68 | if (empty($key)) |
||||||
|
1 ignored issue
–
show
|
|||||||
| 69 | throw new \Exception("Cant serialize complex key: ".var_export($value, true), 1); |
||||||
| 70 | $parent->{$key} = null; |
||||||
| 71 | return; |
||||||
| 72 | case T::SET_VALUE: |
||||||
|
1 ignored issue
–
show
|
|||||||
| 73 | $prop = array_keys(get_object_vars($parent)); |
||||||
| 74 | $key = end($prop); |
||||||
| 75 | if (property_exists($value, "type") && in_array($value->type, [T::ITEM, T::MAPPING])) { |
||||||
|
1 ignored issue
–
show
|
|||||||
| 76 | $p = $value->type === T::ITEM ? [] : new \StdClass; |
||||||
| 77 | self::build($value, $p); |
||||||
| 78 | } else { |
||||||
|
1 ignored issue
–
show
|
|||||||
| 79 | $p = self::build($value, $parent->{$key}); |
||||||
|
0 ignored issues
–
show
Are you sure the assignment to
$p is correct as self::build($value, $parent->$key) targeting Dallgoot\Yaml\Builder::build() 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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||||
| 80 | } |
||||||
|
1 ignored issue
–
show
|
|||||||
| 81 | $parent->{$key} = $p; |
||||||
| 82 | return; |
||||||
| 83 | case T::TAG: |
||||||
|
1 ignored issue
–
show
|
|||||||
| 84 | if ($parent === self::$root) { |
||||||
|
1 ignored issue
–
show
|
|||||||
| 85 | $parent->addTag($identifier);return; |
||||||
| 86 | } else { |
||||||
|
1 ignored issue
–
show
|
|||||||
| 87 | if (in_array($identifier, ['!binary', '!str'])) { |
||||||
|
1 ignored issue
–
show
|
|||||||
| 88 | if ($value->value instanceof DLL) $value->value->type = T::RAW; |
||||||
|
1 ignored issue
–
show
|
|||||||
| 89 | else $value->type = T::RAW; |
||||||
|
2 ignored issues
–
show
|
|||||||
| 90 | } |
||||||
|
1 ignored issue
–
show
|
|||||||
| 91 | $val = is_null($value) ? null : self::build($value, $node); |
||||||
|
0 ignored issues
–
show
It seems like
$value can also be of type string; however, parameter $node of Dallgoot\Yaml\Builder::build() does only seem to accept object, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 92 | return new Tag($identifier, $val); |
||||||
| 93 | } |
||||||
|
1 ignored issue
–
show
|
|||||||
| 94 | default: |
||||||
|
1 ignored issue
–
show
|
|||||||
| 95 | return is_object($value) ? self::build($value, $parent) : $node->getPhpValue(); |
||||||
|
0 ignored issues
–
show
Are you sure the usage of
self::build($value, $parent) targeting Dallgoot\Yaml\Builder::build() 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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||||
| 96 | } |
||||||
| 97 | } |
||||||
| 98 | |||||||
| 99 | /** |
||||||
| 100 | * Builds a key and set the property + value to the parent given |
||||||
| 101 | * |
||||||
| 102 | * @param Node $node The node |
||||||
|
3 ignored issues
–
show
|
|||||||
| 103 | * @param object|array $parent The parent |
||||||
|
3 ignored issues
–
show
|
|||||||
| 104 | * @throws \ParseError if Key has no name(identifier) |
||||||
|
0 ignored issues
–
show
|
|||||||
| 105 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 106 | private static function buildKey($node, &$parent):void |
||||||
|
0 ignored issues
–
show
|
|||||||
| 107 | { |
||||||
| 108 | list($identifier, $value) = [$node->identifier, $node->value]; |
||||||
| 109 | if (is_null($identifier)) { |
||||||
| 110 | throw new \ParseError(sprintf(self::ERROR_NO_KEYNAME, $node->line)); |
||||||
| 111 | } else { |
||||||
| 112 | if ($value instanceof Node && in_array($value->type, [T::KEY, T::ITEM])) { |
||||||
| 113 | $parent->{$identifier} = $value->type === T::KEY ? new \StdClass : []; |
||||||
| 114 | self::build($value, $parent->{$identifier}); |
||||||
| 115 | } elseif (is_object($value)) { |
||||||
| 116 | $parent->{$identifier} = self::build($value, $parent->{$identifier}); |
||||||
|
0 ignored issues
–
show
Are you sure the assignment to
$parent->$identifier is correct as self::build($value, $parent->$identifier) targeting Dallgoot\Yaml\Builder::build() 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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||||
| 117 | } else { |
||||||
| 118 | $parent->{$identifier} = $node->getPhpValue(); |
||||||
| 119 | } |
||||||
| 120 | } |
||||||
| 121 | } |
||||||
| 122 | |||||||
| 123 | private static function buildItem($value, &$parent):void |
||||||
|
0 ignored issues
–
show
|
|||||||
| 124 | { |
||||||
| 125 | if ($value instanceof Node && $value->type === T::KEY) { |
||||||
| 126 | $parent[$value->identifier] = self::build($value->value, $parent[$value->identifier]); |
||||||
|
0 ignored issues
–
show
It seems like
$value->value can also be of type null and string; however, parameter $node of Dallgoot\Yaml\Builder::build() does only seem to accept object, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
Are you sure the assignment to
$parent[$value->identifier] is correct as self::build($value->valu...nt[$value->identifier]) targeting Dallgoot\Yaml\Builder::build() 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 The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||||
| 127 | } else { |
||||||
| 128 | $index = count($parent); |
||||||
| 129 | $parent[$index] = self::build($value, $parent[$index]); |
||||||
| 130 | } |
||||||
| 131 | } |
||||||
| 132 | |||||||
| 133 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 134 | * Builds a file. check multiple documents & split if more than one documents |
||||||
| 135 | * |
||||||
| 136 | * @param Node $root The root node |
||||||
|
3 ignored issues
–
show
|
|||||||
| 137 | * @return array|YamlObject list of documents or juste one. |
||||||
|
1 ignored issue
–
show
|
|||||||
| 138 | */ |
||||||
| 139 | public static function buildContent(Node $root, int $debug) |
||||||
| 140 | { |
||||||
| 141 | self::$debug = $debug; |
||||||
| 142 | $totalDocStart = 0; |
||||||
| 143 | $documents = []; |
||||||
| 144 | if ($root->value instanceof Node) { |
||||||
| 145 | $q = new DLL; |
||||||
| 146 | $q->push($root->value); |
||||||
| 147 | return [self::buildDocument($q, 0)]; |
||||||
| 148 | } |
||||||
| 149 | $root->value->setIteratorMode(DLL::IT_MODE_DELETE); |
||||||
|
0 ignored issues
–
show
The method
setIteratorMode() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 150 | foreach ($root->value as $child) { |
||||||
| 151 | if ($child->type === T::DOC_START) $totalDocStart++; |
||||||
|
1 ignored issue
–
show
|
|||||||
| 152 | //if 0 or 1 DOC_START = we are still in first document |
||||||
| 153 | $currentDoc = $totalDocStart > 1 ? $totalDocStart - 1 : 0; |
||||||
| 154 | if (!isset($documents[$currentDoc])) $documents[$currentDoc] = new DLL(); |
||||||
|
1 ignored issue
–
show
|
|||||||
| 155 | $documents[$currentDoc]->push($child); |
||||||
| 156 | } |
||||||
| 157 | $debug >= 2 && var_dump($documents); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 158 | $content = array_map([self::class, 'buildDocument'], $documents, array_keys($documents)); |
||||||
| 159 | return count($content) === 1 ? $content[0] : $content; |
||||||
| 160 | } |
||||||
| 161 | |||||||
| 162 | private static function buildDocument(DLL $list, int $key):YamlObject |
||||||
|
0 ignored issues
–
show
|
|||||||
| 163 | { |
||||||
| 164 | self::$root = new YamlObject(); |
||||||
| 165 | $childTypes = self::getChildrenTypes($list); |
||||||
| 166 | $isMapping = count(array_intersect($childTypes, [T::KEY, T::MAPPING])) > 0; |
||||||
| 167 | $isSequence = in_array(T::ITEM, $childTypes); |
||||||
| 168 | $isSet = in_array(T::SET_VALUE, $childTypes); |
||||||
| 169 | if ($isMapping && $isSequence) { |
||||||
| 170 | throw new \ParseError(sprintf(self::INVALID_DOCUMENT, $key)); |
||||||
| 171 | } else { |
||||||
| 172 | switch (true) { |
||||||
| 173 | case $isSequence: $list->type = T::SEQUENCE;break; |
||||||
|
1 ignored issue
–
show
|
|||||||
| 174 | case $isSet: $list->type = T::SET;break; |
||||||
|
1 ignored issue
–
show
|
|||||||
| 175 | case $isMapping://fall through |
||||||
|
1 ignored issue
–
show
|
|||||||
| 176 | default:$list->type = T::MAPPING; |
||||||
|
1 ignored issue
–
show
|
|||||||
| 177 | } |
||||||
| 178 | } |
||||||
| 179 | self::$debug >= 3 && var_dump(self::$root, $list); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 180 | $string = ''; |
||||||
| 181 | foreach ($list as $child) { |
||||||
| 182 | $result = self::build($child, self::$root); |
||||||
| 183 | if (is_string($result)) { |
||||||
| 184 | $string .= $result.' '; |
||||||
| 185 | } |
||||||
| 186 | } |
||||||
| 187 | if (!empty($string)) { |
||||||
| 188 | self::$root->setText(rtrim($string)); |
||||||
| 189 | } |
||||||
| 190 | return self::$root; |
||||||
| 191 | } |
||||||
| 192 | |||||||
| 193 | private static function litteral(DLL $children, $type):string |
||||||
|
0 ignored issues
–
show
|
|||||||
| 194 | { |
||||||
| 195 | $children->rewind(); |
||||||
| 196 | $refIndent = $children->current()->indent; |
||||||
| 197 | $separator = $type === T::RAW ? '' : "\n"; |
||||||
| 198 | $action = function ($c) { return $c->value; }; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 199 | if ($type === T::LITTERAL_FOLDED) { |
||||||
| 200 | $separator = ' '; |
||||||
| 201 | $action = function ($c) use ($refIndent) { |
||||||
| 202 | return $c->indent > $refIndent || $c->type === T::EMPTY ? "\n".$c->value : $c->value; |
||||||
| 203 | }; |
||||||
| 204 | } |
||||||
| 205 | $tmp = []; |
||||||
| 206 | $children->rewind(); |
||||||
| 207 | foreach ($children as $child) { |
||||||
| 208 | $tmp[] = $action($child); |
||||||
| 209 | } |
||||||
| 210 | return implode($separator, $tmp); |
||||||
| 211 | } |
||||||
| 212 | |||||||
| 213 | private static function getChildrenTypes(DLL $children):array |
||||||
|
0 ignored issues
–
show
|
|||||||
| 214 | { |
||||||
| 215 | $types = []; |
||||||
| 216 | foreach ($children as $child) { |
||||||
| 217 | $types[] = $child->type; |
||||||
| 218 | } |
||||||
| 219 | return array_unique($types); |
||||||
| 220 | } |
||||||
| 221 | } |
||||||
| 222 |