Passed
Branch development (ddad06)
by Jonathan
15:35
created

AbstractAssert   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 61.9%

Importance

Changes 0
Metric Value
wmc 11
eloc 20
dl 0
loc 58
ccs 13
cts 21
cp 0.619
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A typeToString() 0 5 2
B valueToString() 0 35 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 397
    protected static function valueToString($value): string
40
    {
41 397
        if (null === $value) {
42
            return 'null';
43
        }
44
45 397
        if (true === $value) {
46 3
            return 'true';
47
        }
48
49 394
        if (false === $value) {
50 6
            return 'false';
51
        }
52
53 388
        if (is_array($value)) {
54 9
            return 'array';
55
        }
56
57 379
        if (is_object($value)) {
58
            if (method_exists($value, '__toString')) {
59
                return get_class($value) . ': ' . self::valueToString($value->__toString());
60
            }
61
62
            return get_class($value);
63
        }
64
65 379
        if (is_resource($value)) {
66
            return 'resource';
67
        }
68
69 379
        if (is_string($value)) {
70 331
            return sprintf('"%1$s"', $value);
71
        }
72
73 120
        return (string) $value;
74
    }
75
76
    /**
77
     * Convert value to class name or type
78
     *
79
     * @param mixed $value
80
     *
81
     * @return string
82
     */
83
    protected static function typeToString($value): string
84
    {
85
        $ret = is_object($value) ? get_class($value) : gettype($value);
86
87
        return (string) $ret;
88
    }
89
}
90