ValueToStringTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 20
c 1
b 0
f 0
dl 0
loc 49
ccs 20
cts 20
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B valueToString() 0 40 9
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP SDK
6
 *
7
 * PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8
 *
9
 * @link      https://www.reporting.cloud to learn more about ReportingCloud
10
 * @link      https://git.io/Jejj2 for the canonical source repository
11
 * @license   https://git.io/Jejjr
12
 * @copyright © 2022 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Assert;
16
17
/**
18
 * Trait ValueToStringTrait
19
 *
20
 * @package TxTextControl\ReportingCloud
21
 * @author  Jonathan Maron (@JonathanMaron)
22
 */
23
trait ValueToStringTrait
24
{
25
    /**
26
     * Convert value to string
27
     *
28
     * @param mixed $value
29
     *
30
     * @return string
31
     */
32 272
    protected static function valueToString($value): string
33
    {
34 272
        if (null === $value) {
35 2
            return 'null';
36
        }
37
38 270
        if (true === $value) {
39 2
            return 'true';
40
        }
41
42 268
        if (false === $value) {
43 2
            return 'false';
44
        }
45
46 266
        if (is_array($value)) {
47 4
            return 'array';
48
        }
49
50 262
        if (is_object($value)) {
51 4
            if (method_exists($value, '__toString')) {
52 2
                $format = '%1$s: %2$s';
53
54 2
                return sprintf($format, get_class($value), self::valueToString($value->__toString()));
55
            }
56
57 2
            return get_class($value);
58
        }
59
60 260
        if (is_resource($value)) {
61 2
            return 'resource';
62
        }
63
64 258
        if (is_string($value)) {
65 230
            $format = '"%1$s"';
66
67 230
            return sprintf($format, $value);
68
        }
69
70
        // @phpstan-ignore-next-line
71 76
        return (string) $value;
72
    }
73
}
74