Completed
Push — master ( 296743...12eab9 )
by Kirill
36:17
created

Support::valueToScalar()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 8.8337

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 13
cts 17
cp 0.7647
rs 8.1155
c 0
b 0
f 0
cc 8
nc 8
nop 1
crap 8.8337
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL;
11
12
use Railt\SDL\Contracts\Behavior\AllowsTypeIndication;
13
use Railt\SDL\Contracts\Definitions\Definition;
14
use Railt\SDL\Contracts\Definitions\TypeDefinition;
15
use Railt\SDL\Contracts\Dependent\ArgumentDefinition;
16
use Railt\SDL\Contracts\Dependent\FieldDefinition;
17
18
/**
19
 * Trait Support
20
 */
21
trait Support
22
{
23
    /**
24
     * @var string
25
     */
26
    private static $typeDefinition = '%s<%s>';
27
28
    /**
29
     * @var string
30
     */
31
    private static $fieldDefinition = '{%s: %s}';
32
33
    /**
34
     * @var string
35
     */
36
    private static $argumentDefinition = '(%s: %s)';
37
38
    /**
39
     * @var string
40
     */
41
    private static $syntaxList = '[%s]';
42
43
    /**
44
     * @var string
45
     */
46
    private static $syntaxNonNull = '%s!';
47
48
    /**
49
     * @param AllowsTypeIndication|Definition $type
50
     * @return string
51
     */
52 3285
    protected function typeToString(Definition $type): string
53
    {
54 3285
        if ($type instanceof ArgumentDefinition) {
55 2355
            return \vsprintf(self::$argumentDefinition, [
56 2355
                $type->getName(),
57 2355
                $this->typeIndicatorToString($type),
58
            ]);
59
        }
60
61 1074
        if ($type instanceof FieldDefinition) {
62 215
            return \vsprintf(self::$fieldDefinition, [
63 215
                $type->getName(),
64 215
                $this->typeIndicatorToString($type),
65
            ]);
66
        }
67
68 1042
        if ($type instanceof TypeDefinition) {
69 1042
            return \sprintf(self::$typeDefinition, $type->getTypeName(), $type->getName());
70
        }
71
72
        return $type->getName();
73
    }
74
75
    /**
76
     * @param AllowsTypeIndication $type
77
     * @return string
78
     */
79 2570
    protected function typeIndicatorToString(AllowsTypeIndication $type): string
80
    {
81
        try {
82 2570
            $result = $type->getTypeDefinition()->getName();
83
84 2570
            if ($type->isList()) {
85 1239
                if ($type->isListOfNonNulls()) {
86 1223
                    $result = \sprintf(self::$syntaxNonNull, $result);
87
                }
88
89 1239
                $result = \sprintf(self::$syntaxList, $result);
90
            }
91
92 2570
            if ($type->isNonNull()) {
93 337
                $result = \sprintf(self::$syntaxNonNull, $result);
94
            }
95
96 2570
            return $result;
97
        } catch (\Throwable $e) {
98
            return '?';
99
        }
100
    }
101
102
    /**
103
     * @param $value
104
     * @return string
105
     */
106
    protected function valueWithType($value): string
107
    {
108
        return \mb_strtolower(\gettype($value)) . ' ' . $this->valueToString($value);
109
    }
110
111
    /**
112
     * @param mixed $value
113
     * @return mixed
114
     */
115 1946
    protected function valueToScalar($value)
116
    {
117 1946
        if (\is_scalar($value)) {
118 829
            return $value;
119
        }
120
121 1910
        if (\is_iterable($value)) {
122 1910
            $result = [];
123
124
            /** @var iterable $value */
125 1910
            foreach ($value as $key => $sub) {
126 1201
                $result[$key] = $this->valueToScalar($sub);
127
            }
128
129 1910
            return $result;
130
        }
131
132 1201
        if ($value instanceof Definition) {
133
            return $this->typeToString($value);
134
        }
135
136 1201
        if ($value instanceof \Illuminate\Contracts\Support\Arrayable) {
137
            return $this->valueToScalar($value->toArray());
138
        }
139
140 1201
        if ($value instanceof \Illuminate\Contracts\Support\Jsonable) {
141
            return $this->valueToScalar(\json_decode($value->toJson(), true));
142
        }
143
144 1201
        if ($value instanceof \JsonSerializable) {
0 ignored issues
show
Bug introduced by
The class JsonSerializable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
145
            return $this->valueToScalar(\json_encode($value->jsonSerialize(), true));
146
        }
147
148 1201
        return \ucfirst(\strtolower(\gettype($value)));
149
    }
150
151
    /**
152
     * @param mixed|iterable|null $value
153
     * @return string
154
     */
155 1946
    protected function valueToString($value): string
156
    {
157 1946
        $result = $this->valueToScalar($value);
158
159 1946
        if (\is_array($result)) {
160 1910
            $result = \json_encode($result);
161 1910
            $result = \preg_replace('/"([_A-Za-z][_0-9A-Za-z]*)":/u', '$1: ', $result);
162 1910
            $result = \preg_replace('/:\s+(.*?),/u', ': $1, ', $result);
163
164 1910
            return $result;
165
        }
166
167 36
        return (string)$result;
168
    }
169
}
170