ValueToStringTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
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 45
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://tinyurl.com/vmbbh6kd for the canonical source repository
11
 * @license   https://tinyurl.com/3pc9am89
12
 * @copyright © 2023 Text Control GmbH
13
 */
14
15
namespace TextControl\ReportingCloud\Assert;
16
17
/**
18
 * Trait ValueToStringTrait
19
 */
20
trait ValueToStringTrait
21
{
22
    /**
23
     * Convert value to string
24
     */
25 272
    protected static function valueToString(mixed $value): string
26
    {
27 272
        if (null === $value) {
28 2
            return 'null';
29
        }
30
31 270
        if (true === $value) {
32 2
            return 'true';
33
        }
34
35 268
        if (false === $value) {
36 2
            return 'false';
37
        }
38
39 266
        if (is_array($value)) {
40 4
            return 'array';
41
        }
42
43 262
        if (is_object($value)) {
44 4
            if (method_exists($value, '__toString')) {
45 2
                $format = '%1$s: %2$s';
46
47 2
                return sprintf($format, $value::class, self::valueToString($value->__toString()));
48
            }
49
50 2
            return $value::class;
51
        }
52
53 260
        if (is_resource($value)) {
54 2
            return 'resource';
55
        }
56
57 258
        if (is_string($value)) {
58 230
            $format = '"%1$s"';
59
60 230
            return sprintf($format, $value);
61
        }
62
63
        // @phpstan-ignore-next-line
64 76
        return (string) $value;
65
    }
66
}
67