BasicInterfaceResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 26
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolveInterface() 0 21 5
1
<?php
2
3
/*
4
 * This file is part of the olvlvl/symfony-dependency-injection-proxy package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace olvlvl\SymfonyDependencyInjectionProxy\InterfaceResolver;
13
14
use LogicException;
15
use olvlvl\SymfonyDependencyInjectionProxy\InterfaceResolver;
16
17
use function class_exists;
18
use function class_implements;
19
use function count;
20
use function implode;
21
use function interface_exists;
22
use function reset;
23
24
final class BasicInterfaceResolver implements InterfaceResolver
25
{
26
    /**
27
     * @inheritdoc
28
     */
29
    public function resolveInterface(string $class): string
30
    {
31
        if (interface_exists($class)) {
32
            return $class;
33
        }
34
35
        if (class_exists($class)) {
36
            /** @phpstan-var class-string[]|false $interfaces */
37
            $interfaces = class_implements($class);
38
39
            if ($interfaces) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $interfaces of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40
                if (count($interfaces) > 1) {
41
                    $interfaces = implode(', ', $interfaces);
42
                    throw new LogicException("Don't know which interface to choose from for $class: $interfaces.");
43
                }
44
45
                return reset($interfaces);
46
            }
47
        }
48
49
        throw new LogicException("Unable to determine the interface to implement for $class.");
50
    }
51
}
52