Completed
Pull Request — master (#359)
by Jefersson
06:36
created

CanProxyAssertion::isNotInterface()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
declare(strict_types=1);
20
21
namespace ProxyManager\ProxyGenerator\Assertion;
22
23
use BadMethodCallException;
24
use ProxyManager\Exception\InvalidProxiedClassException;
25
use ReflectionClass;
26
use ReflectionMethod;
27
28
/**
29
 * Assertion that verifies that a class can be proxied
30
 *
31
 * @author Marco Pivetta <[email protected]>
32
 * @license MIT
33
 */
34
final class CanProxyAssertion
35
{
36
    /**
37
     * Disabled constructor: not meant to be instantiated
38
     *
39
     * @throws BadMethodCallException
40
     */
41 1
    public function __construct()
42
    {
43 1
        throw new BadMethodCallException('Unsupported constructor.');
44
    }
45
46
    /**
47
     * @param ReflectionClass $originalClass
48
     * @param bool            $allowInterfaces
49
     *
50
     * @throws InvalidProxiedClassException
51
     */
52
    public static function assertClassCanBeProxied(ReflectionClass $originalClass, bool $allowInterfaces = true) : void
53
    {
54 22
        self::isNotFinal($originalClass);
55
        self::hasNoAbstractProtectedMethods($originalClass);
56 22
57 21
        if (! $allowInterfaces) {
58
            self::isNotInterface($originalClass);
59 20
        }
60 1
    }
61
62 20
    /**
63
     * @param ReflectionClass $originalClass
64
     *
65
     * @throws InvalidProxiedClassException
66
     */
67
    private static function isNotFinal(ReflectionClass $originalClass) : void
68
    {
69
        if ($originalClass->isFinal()) {
70
            throw InvalidProxiedClassException::finalClassNotSupported($originalClass);
71 22
        }
72
    }
73 22
74 1
    /**
75
     * @param ReflectionClass $originalClass
76 21
     *
77
     * @throws InvalidProxiedClassException
78
     */
79
    private static function hasNoAbstractProtectedMethods(ReflectionClass $originalClass) : void
80
    {
81
        $protectedAbstract = array_filter(
82
            $originalClass->getMethods(),
83
            function (ReflectionMethod $method) : bool {
84
                return $method->isAbstract() && $method->isProtected();
85 21
            }
86
        );
87 21
88 21
        if ($protectedAbstract) {
89 21
            throw InvalidProxiedClassException::abstractProtectedMethodsNotSupported($originalClass);
90 15
        }
91 21
    }
92
93
    /**
94 21
     * @param ReflectionClass $originalClass
95 1
     *
96
     * @throws InvalidProxiedClassException
97 20
     */
98
    private static function isNotInterface(ReflectionClass $originalClass) : void
99
    {
100
        if ($originalClass->isInterface()) {
101
            throw InvalidProxiedClassException::interfaceNotSupported($originalClass);
102
        }
103
    }
104
}
105