ConvertObjectToStringMethod   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 10
c 2
b 0
f 0
dl 0
loc 22
ccs 7
cts 10
cp 0.7
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B convertObjectToString() 0 16 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EngineWorks\DBAL\Internal;
6
7
use BackedEnum;
1 ignored issue
show
Bug introduced by
The type BackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use InvalidArgumentException;
9
use Stringable;
10
use UnitEnum;
1 ignored issue
show
Bug introduced by
The type UnitEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
trait ConvertObjectToStringMethod
13
{
14
    /**
15
     * @param object $variable
16
     * @return string
17
     */
18 8
    private static function convertObjectToString(object $variable): string
19
    {
20 8
        if (PHP_VERSION_ID > 80100) { // PHP 8.1
21 8
            if ($variable instanceof UnitEnum) { // BackedEnum implements UnitEnum
22 8
                return ($variable instanceof BackedEnum) ? strval($variable->value) : $variable->name;
23
            }
24
        }
25 8
        if (class_exists(Stringable::class) && $variable instanceof Stringable) {
26
            return strval($variable);
27
        }
28 8
        if (is_callable([$variable, '__toString'])) {
29 8
            return $variable->__toString();
30
        }
31
32
        throw new InvalidArgumentException(
33
            sprintf('Value of type %s that cannot be parsed as string', get_class($variable))
34
        );
35
    }
36
}
37