dallgoot /
yaml
| 1 | <?php |
||||||
| 2 | namespace Dallgoot\Yaml; |
||||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||||
| 3 | |||||||
| 4 | use Dallgoot\Yaml\{Types as T, YamlObject, Tag, Compact}; |
||||||
|
0 ignored issues
–
show
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
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\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\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...
|
|||||||
| 5 | use \SplDoublyLinkedList as DLL; |
||||||
| 6 | |||||||
| 7 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 8 | * |
||||||
| 9 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 10 | class Dumper //extends AnotherClass |
||||||
| 11 | { |
||||||
| 12 | private const indent = 2; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 13 | private const width = 120; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 14 | private const options = 00000; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 15 | |||||||
| 16 | private static $result; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 17 | private static $options; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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
|
|||||||
| 23 | { |
||||||
| 24 | if (is_int($options)) self::$options = $options; |
||||||
|
1 ignored issue
–
show
|
|||||||
| 25 | } |
||||||
| 26 | |||||||
| 27 | public static function toString($dataType, int $options):string |
||||||
|
0 ignored issues
–
show
|
|||||||
| 28 | { |
||||||
| 29 | if (is_null($dataType)) throw new \Exception(self::class.": No content to convert to Yaml", 1); |
||||||
|
1 ignored issue
–
show
|
|||||||
| 30 | self::$options = is_int($options) ? $options : self::$options; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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
|
|||||||
| 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
|
|||||||
| 54 | { |
||||||
| 55 | if (is_scalar($dataType)) { |
||||||
| 56 | switch (gettype($dataType)) { |
||||||
| 57 | case 'boolean': return $dataType ? 'true' : 'false';break; |
||||||
|
1 ignored issue
–
show
break is not strictly necessary here and could be removed.
The 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 Loading history...
|
|||||||
| 58 | case 'float': if (is_infinite($dataType)) return $dataType > 0 ? '.inf' : '-.inf'; |
||||||
|
2 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||||||
| 59 | case 'double': if (is_nan($dataType)) return '.nan'; |
||||||
|
2 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||||||
| 60 | default: |
||||||
|
1 ignored issue
–
show
|
|||||||
| 61 | return $dataType; |
||||||
| 62 | } |
||||||
| 63 | } elseif (is_object($dataType)) { |
||||||
| 64 | self::dumpObject($dataType, $indent); |
||||||
|
0 ignored issues
–
show
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
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
|
|||||||
| 71 | { |
||||||
| 72 | self::$result->push("---"); |
||||||
| 73 | return self::dump($dataType, 0); |
||||||
|
0 ignored issues
–
show
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 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
|
|||||||
| 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
|
|||||||
| 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
|
|||||||
| 93 | } else { |
||||||
| 94 | self::add($lineStart, $indent ); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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
|
|||||||
| 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
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
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...
|
|||||||
| 109 | { |
||||||
| 110 | if ($dataType instanceof Tag) { |
||||||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||||
| 111 | if (is_scalar($dataType->value)) { |
||||||
| 112 | return "!".$dataType->tagName.' '.$dataType->value; |
||||||
| 113 | } else{ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 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
|
|||||||
| 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
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
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
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...
|
|||||||
| 136 | { |
||||||
| 137 | // if (empty($object)) return "{}"; |
||||||
| 138 | // if (empty($object)) return "[]"; |
||||||
| 139 | } |
||||||
| 140 | } |
||||||
| 141 |