CanProxyAssertion::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\ProxyGenerator\Assertion;
6
7
use BadMethodCallException;
8
use ProxyManager\Exception\InvalidProxiedClassException;
9
use ReflectionClass;
10
use ReflectionMethod;
11
use function array_filter;
12
13
/**
14
 * Assertion that verifies that a class can be proxied
15
 */
16
final class CanProxyAssertion
17
{
18
    /**
19
     * Disabled constructor: not meant to be instantiated
20
     *
21
     * @throws BadMethodCallException
22
     */
23
    public function __construct()
24 1
    {
25
        throw new BadMethodCallException('Unsupported constructor.');
26 1
    }
27
28
    /**
29
     * @throws InvalidProxiedClassException
30
     */
31
    public static function assertClassCanBeProxied(ReflectionClass $originalClass, bool $allowInterfaces = true) : void
32
    {
33 23
        self::isNotFinal($originalClass);
34
        self::hasNoAbstractProtectedMethods($originalClass);
35 23
36 22
        if ($allowInterfaces) {
37
            return;
38 21
        }
39 20
40
        self::isNotInterface($originalClass);
41
    }
42 1
43 1
    /**
44
     * @throws InvalidProxiedClassException
45
     */
46
    private static function isNotFinal(ReflectionClass $originalClass) : void
47
    {
48
        if ($originalClass->isFinal()) {
49 23
            throw InvalidProxiedClassException::finalClassNotSupported($originalClass);
50
        }
51 23
    }
52 1
53
    /**
54 22
     * @throws InvalidProxiedClassException
55
     */
56
    private static function hasNoAbstractProtectedMethods(ReflectionClass $originalClass) : void
57
    {
58
        $protectedAbstract = array_filter(
59
            $originalClass->getMethods(),
60 22
            static function (ReflectionMethod $method) : bool {
61
                return $method->isAbstract() && $method->isProtected();
62 22
            }
63 22
        );
64
65 16
        if ($protectedAbstract) {
66 22
            throw InvalidProxiedClassException::abstractProtectedMethodsNotSupported($originalClass);
67
        }
68
    }
69 22
70 1
    /**
71
     * @throws InvalidProxiedClassException
72 21
     */
73
    private static function isNotInterface(ReflectionClass $originalClass) : void
74
    {
75
        if ($originalClass->isInterface()) {
76
            throw InvalidProxiedClassException::interfaceNotSupported($originalClass);
77
        }
78 1
    }
79
}
80