Passed
Push — master ( fbb10f...c0768e )
by Vlad
03:01
created

formatValue()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 39
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 12

Importance

Changes 0
Metric Value
cc 12
eloc 19
c 0
b 0
f 0
nc 10
nop 2
dl 0
loc 39
ccs 19
cts 19
cp 1
crap 12
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Schema Validator
4
 *
5
 * @author Vlad Shashkov <[email protected]>
6
 * @copyright Copyright (c) 2021, The Myaza Software
7
 */
8
9
declare(strict_types=1);
10
11
namespace SchemaValidator;
12
13
/**
14
 * Whether to format {@link \DateTime} objects, either with the {@link \IntlDateFormatter}
15
 * (if it is available) or as RFC-3339 dates ("Y-m-d H:i:s").
16
 */
17
const PRETTY_DATE = 1;
18
19
/**
20
 * Whether to cast objects with a "__toString()" method to strings.
21
 */
22
const OBJECT_TO_STRING = 2;
23
24
function findUuidVersion(string $class): ?int
25
{
26 28
    $class   = basename(str_replace('\\', '/', $class));
27 28
    $matches = [];
28
29 28
    preg_match('/(?<version>\d+)/', $class, $matches);
30
31 28
    if ([] === $matches) {
32 5
        return null;
33
    }
34
35 23
    ['version' => $version] = $matches;
36
37 23
    return (int) $version;
38
}
39
40
function formatValue(mixed $value, int $format = 0): string
41
{
42 36
    if (($format & PRETTY_DATE) && $value instanceof \DateTimeInterface) {
43 1
        return $value->format('Y-m-d H:i:s');
44
    }
45
46 35
    if (\is_object($value)) {
47 2
        if (($format & OBJECT_TO_STRING) && $value instanceof \Stringable) {
48 1
            return $value->__toString();
49
        }
50
51 1
        return 'object';
52
    }
53
54 33
    if (\is_array($value)) {
55 2
        return 'array';
56
    }
57
58 31
    if (\is_string($value)) {
59 22
        return '"' . $value . '"';
60
    }
61
62 9
    if (\is_resource($value)) {
63 1
        return 'resource';
64
    }
65
66 8
    if (null === $value) {
67 2
        return 'null';
68
    }
69
70 7
    if (false === $value) {
71 1
        return 'false';
72
    }
73
74 6
    if (true === $value) {
75 1
        return 'true';
76
    }
77
78 5
    return (string) $value;
79
}
80