Reflection   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 11
dl 0
loc 38
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __callStatic() 0 15 3
A createReflectionObject() 0 7 2
1
<?php
2
3
/**
4
 * Copyright 2017 NanoSector
5
 *
6
 * You should have received a copy of the MIT license with the project.
7
 * See the LICENSE file for more information.
8
 */
9
10
namespace ValidationClosures;
11
12
use Closure;
13
use InvalidArgumentException;
14
use ReflectionClass;
15
use ReflectionException;
16
17
/**
18
 * Class Reflection
19
 * @package ValidationClosures
20
 *
21
 * Taken from \ReflectionClass; all methods which could be used properly as a validation closure.
22
 * Nothing stops you from using the other methods though, but they'll be less useful as a validation mechanism.
23
 * @method static bool hasConstant(string $name)
24
 * @method static bool hasMethod(string $name)
25
 * @method static bool hasProperty(string $name)
26
 * @method static bool implementsInterface(string $interface)
27
 * @method static bool inNamespace()
28
 * @method static bool isAbstract()
29
 * @method static bool isAnonymous()
30
 * @method static bool isCloneable()
31
 * @method static bool isFinal()
32
 * @method static bool isInstance(object $object)
33
 * @method static bool isInstantiable()
34
 * @method static bool isInterface()
35
 * @method static bool isInternal()
36
 * @method static bool isIterateable()
37
 * @method static bool isSubclassOf(string $class)
38
 * @method static bool isTrait()
39
 * @method static bool isUserDefined()
40
 *
41
 */
42
class Reflection
43
{
44
    /**
45
     * @param string $class
46
     *
47
     * @return ReflectionClass
48
     * @throws ReflectionException
49
     */
50 4
	public static function createReflectionObject(string $class): ReflectionClass
51
	{
52 4
		if (!class_exists($class)) {
53 2
            throw new InvalidArgumentException('The given class does not exist');
54
        }
55
56 4
		return new ReflectionClass($class);
57
	}
58
59
	/**
60
	 * @param string $method
61
	 * @param array $arguments
62
	 *
63
	 * @return Closure
64
	 */
65 2
	public static function __callStatic(string $method, array $arguments): Closure
66
	{
67 2
		if (!method_exists(ReflectionClass::class, $method)) {
68 2
            throw new InvalidArgumentException('Cannot create closure from method ReflectionClass::' . $method . ', it does not exist');
69
        }
70
71 2
		return static function ($value) use ($method, $arguments)
72
		{
73 2
			if (!Types::string()($value)) {
74 2
                return false;
75
            }
76
77 2
			$reflection = static::createReflectionObject($value);
78
79 2
			return call_user_func_array([$reflection, $method], $arguments);
80 2
		};
81
	}
82
}