Passed
Push — master ( 315e77...e369d0 )
by Jonathan
16:56
created

AbstractAssert::valueToString()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 9

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 36
ccs 20
cts 20
cp 1
rs 8.0555
c 0
b 0
f 0
cc 9
nc 9
nop 1
crap 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://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Assert;
16
17
/**
18
 * Abstract AbstractAssert
19
 *
20
 * This component is based on Webmozart\Assert.
21
 * See: https://github.com/webmozart/assert
22
 *
23
 * At the time of implementation (March 2019), the above component did not support strict types.
24
 * Since the ReportingCloud PHP SDK does support strict types, the necessary functions where cherry-picked from
25
 * Webmozart\Assert, strict typed, and insert into this component.
26
 *
27
 * @package TxTextControl\ReportingCloud
28
 * @author  Jonathan Maron (@JonathanMaron)
29
 */
30
abstract class AbstractAssert
31
{
32
    /**
33
     * Convert value to string
34
     *
35
     * @param mixed $value
36
     *
37
     * @return string
38
     */
39 421
    protected static function valueToString($value): string
40
    {
41 421
        if (null === $value) {
42 3
            return 'null';
43
        }
44
45 418
        if (true === $value) {
46 6
            return 'true';
47
        }
48
49 412
        if (false === $value) {
50 9
            return 'false';
51
        }
52
53 403
        if (is_array($value)) {
54 12
            return 'array';
55
        }
56
57 391
        if (is_object($value)) {
58 6
            if (method_exists($value, '__toString')) {
59 3
                $format = '%1$s: %2$s';
60 3
                return sprintf($format, get_class($value), self::valueToString($value->__toString()));
61
            }
62 3
            return get_class($value);
63
        }
64
65 388
        if (is_resource($value)) {
66 3
            return 'resource';
67
        }
68
69 385
        if (is_string($value)) {
70 337
            $format = '"%1$s"';
71 337
            return sprintf($format, $value);
72
        }
73
74 120
        return (string) $value;
75
    }
76
}
77