ProxiedMethodsFilter::getAbstractProxiedMethods()   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
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProxyManager\ProxyGenerator\Util;
6
7
use ReflectionClass;
8
use ReflectionMethod;
9
use function array_filter;
10
use function array_flip;
11
use function array_key_exists;
12
use function array_map;
13
use function array_values;
14
use function strtolower;
15
16
/**
17
 * Utility class used to filter methods that can be proxied
18
 */
19
final class ProxiedMethodsFilter
20
{
21
    /** @var array<int, string> */
22
    private static $defaultExcluded = [
23
        '__get',
24
        '__set',
25
        '__isset',
26
        '__unset',
27
        '__clone',
28
        '__sleep',
29
        '__wakeup',
30
    ];
31
32
    /**
33
     * @param ReflectionClass    $class    reflection class from which methods should be extracted
34
     * @param array<int, string> $excluded methods to be ignored
35
     *
36
     * @return ReflectionMethod[]
37
     */
38 16
    public static function getProxiedMethods(ReflectionClass $class, ?array $excluded = null) : array
39
    {
40 16
        return self::doFilter($class, $excluded ?? self::$defaultExcluded);
41
    }
42
43
    /**
44
     * @param ReflectionClass    $class    reflection class from which methods should be extracted
45
     * @param array<int, string> $excluded methods to be ignored
46
     *
47
     * @return ReflectionMethod[]
48
     */
49 8
    public static function getAbstractProxiedMethods(ReflectionClass $class, ?array $excluded = null) : array
50
    {
51 8
        return self::doFilter($class, $excluded ?? self::$defaultExcluded, true);
52
    }
53
54
    /**
55
     * @param array<int, string> $excluded
56
     *
57
     * @return array<int, ReflectionMethod>
0 ignored issues
show
Documentation introduced by
The doc-type array<int, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
58
     */
59 24
    private static function doFilter(ReflectionClass $class, array $excluded, bool $requireAbstract = false) : array
60
    {
61 24
        $ignored = array_flip(array_map('strtolower', $excluded));
62
63 24
        return array_values(array_filter(
64 24
            $class->getMethods(ReflectionMethod::IS_PUBLIC),
65
            static function (ReflectionMethod $method) use ($ignored, $requireAbstract) : bool {
66 20
                return (! $requireAbstract || $method->isAbstract()) && ! (
67 18
                    array_key_exists(strtolower($method->getName()), $ignored)
68 20
                    || self::methodCannotBeProxied($method)
69
                );
70 24
            }
71
        ));
72
    }
73
74
    /**
75
     * Checks whether the method cannot be proxied
76
     */
77 14
    private static function methodCannotBeProxied(ReflectionMethod $method) : bool
78
    {
79 14
        return $method->isConstructor() || $method->isFinal() || $method->isStatic();
80
    }
81
}
82