GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 653851...73c53f )
by Cees-Jan
09:24
created

functions.php ➔ normalizeContextWithFormatValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 18
ccs 6
cts 6
cp 1
crap 4
rs 9.6666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace WyriHaximus\PSR3;
4
5
use Psr\Log\InvalidArgumentException;
6
use Psr\Log\LogLevel;
7
8
/**
9
 * Logging levels PSR-3 LogLevel enum.
10
 *
11
 * @var array $levels Logging levels
12
 */
13
const LOG_LEVELS = [
14
    LogLevel::DEBUG     => 'DEBUG',
15
    LogLevel::INFO      => 'INFO',
16
    LogLevel::NOTICE    => 'NOTICE',
17
    LogLevel::WARNING   => 'WARNING',
18
    LogLevel::ERROR     => 'ERROR',
19
    LogLevel::CRITICAL  => 'CRITICAL',
20
    LogLevel::ALERT     => 'ALERT',
21
    LogLevel::EMERGENCY => 'EMERGENCY',
22
];
23
24
/**
25
 * Functions in this file are a continuation of the code from this
26
 * https://github.com/Seldaek/monolog/blob/6e6586257d9fb231bf039563632e626cdef594e5/src/Monolog/Processor/PsrLogMessageProcessor.php file.
27
 */
28
29
/**
30
 * @param  string $message
31
 * @param  array  $context
32
 * @return string
33
 */
34
function processPlaceHolders(string $message, array $context): string
35
{
36 3
    if (false === \strpos($message, '{')) {
37 1
        return $message;
38
    }
39
40 2
    $replacements = [];
41 2
    foreach ($context as $key => $value) {
42 2
        $replacements['{'.$key.'}'] = formatValue($value);
43
    }
44
45 2
    return \strtr($message, $replacements);
46
}
47
48
function formatValue($value): string
49
{
50 9
    if (\is_null($value) || \is_scalar($value) || (\is_object($value) && \method_exists($value, '__toString'))) {
51 7
        return (string)$value;
52
    }
53
54 2
    if (\is_object($value)) {
55 1
        return '[object '.\get_class($value).']';
56
    }
57
58 1
    return '['.\gettype($value).']';
59
}
60
61
function normalizeContext(array $context): array
62
{
63 4
    foreach ($context as $index => $value) {
64 3
        if (\is_array($value)) {
65 2
            $context[$index] = normalizeContext($value);
66 2
            continue;
67
        }
68
69 2
        if (\is_resource($value)) {
70 2
            $context[$index] = \sprintf('[resource] (%s)', \get_resource_type($value));
71 2
            continue;
72
        }
73
    }
74
75 4
    return $context;
76
}
77
78
function normalizeContextWithFormatValue(array $context): array
79
{
80 11
    foreach ($context as $index => $value) {
81 11
        if (\is_array($value)) {
82 11
            $context[$index] = normalizeContext($value);
83 3
            continue;
84 3
        }
85
86
        if (\is_resource($value)) {
87
            $context[$index] = \sprintf('[resource] (%s)', \get_resource_type($value));
88 8
            continue;
89
        }
90
91
        $context[$index] = formatValue($value);
92
    }
93
94
    return $context;
95
}
96
97
function checkCorrectLogLevel(string $level): bool
98
{
99
    $level = \strtolower($level);
100
    $levels = LOG_LEVELS;
101
    if (!isset($levels[$level])) {
102
        throw new \InvalidArgumentException(
103
            'Level "' . $level . '" is not defined, use one of: '.\implode(', ', \array_keys(LOG_LEVELS))
104
        );
105
    }
106
107
    return true;
108
}
109