JsonMethods::details()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * This file is part of Cors
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Cors\Infrastructure\Converter;
13
14
use Slick\ErrorHandler\Exception\ExceptionInspector;
15
use Throwable;
16
17
/**
18
 * JsonMethods
19
 *
20
 * @package Slick\Cors\Infrastructure\Converter
21
 */
22
trait JsonMethods
23
{
24
25
    /**
26
     * Clears the title of a Throwable object.
27
     *
28
     * @param Throwable $throwable The Throwable object to extract the title from.
29
     * @return string The cleared title.
30
     */
31
    private function clearTitle(Throwable $throwable): string
32
    {
33
        $parts = explode('\\', get_class($throwable));
34
        $name = array_pop($parts);
35
        $titleParts = preg_split('/(?=[A-Z])/', $name);
36
        return ucfirst(trim(strtolower(implode(' ', $titleParts ?: []))));
37
    }
38
39
    /**
40
     * Generates details based on the Throwable object and ExceptionInspector.
41
     *
42
     * @param Throwable $throwable The Throwable object used to retrieve error message.
43
     * @param ExceptionInspector $inspector The ExceptionInspector object to provide additional information.
44
     * @return string The generated details based on the Throwable and ExceptionInspector.
45
     */
46
    private function details(Throwable $throwable, ExceptionInspector $inspector): string
47
    {
48
        $errorMessage = $throwable->getMessage() ? $throwable->getMessage(). " " : null;
49
        $help = preg_split('/\r?\n/', ltrim((string)$inspector->help()), 2);
50
        $help = is_array($help) ? $help[0] : '';
0 ignored issues
show
introduced by
The condition is_array($help) is always true.
Loading history...
51
        $details = $errorMessage . $help[0];
52
        return trim($details);
53
    }
54
}
55