Completed
Push — master ( 552809...642f7d )
by WEBEWEB
05:57
created

ClassUtility   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
c 2
b 0
f 0
lcom 0
cbo 4
dl 0
loc 100
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C getHooks() 0 65 10
A getName() 0 3 1
A getShortName() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
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 WBW\Library\Core\Utility\Reflection;
13
14
use Exception;
15
use ReflectionClass;
16
use ReflectionException;
17
use WBW\Library\Core\Exception\Reflection\ClassNotFoundException;
18
use WBW\Library\Core\Exception\Reflection\MethodNotFoundException;
19
use WBW\Library\Core\Exception\Reflection\SyntaxErrorException;
20
use WBW\Library\Core\Utility\IO\FileUtility;
21
22
/**
23
 * Class utility.
24
 *
25
 * @author webeweb <https://github.com/webeweb/>
26
 * @package WBW\Library\Core\Utility\Reflection
27
 * @final
28
 */
29
final class ClassUtility {
30
31
    /**
32
     * Get the hooks.
33
     *
34
     * @param string $classpath The class path.
35
     * @param string $namespace The namespace.
36
     * @param string $classname The class name.
37
     * @param string $extends The class extend.
38
     * @param string $method The class method.
39
     * @return array Returns the hooks array.
40
     * @throws ClassNotFoundException Throws a hook class not found if the classname is not found.
41
     */
42
    public static function getHooks($classpath, $namespace, $classname = null, $extends = null, $method = null) {
43
44
        //
45
        $hooks = [];
46
47
        // Get the filenames.
48
        $filenames = FileUtility::getFileNames($classpath, ".php");
49
50
        // Handle each filenames.
51
        foreach ($filenames as $filename) {
52
53
            // Check the class name.
54
            if (null !== $classname && 0 === preg_match($classname, $filename)) {
55
                continue;
56
            }
57
58
            // Import the class.
59
            try {
60
                require_once $classpath . "/" . $filename;
61
            } catch (Exception $ex) {
62
                throw new SyntaxErrorException($classpath . "/" . $filename);
63
            }
64
65
            // Init. the complete class name.
66
            $completeClassname = $namespace . basename($filename, ".php");
67
68
            try {
69
                $rc = new ReflectionClass($completeClassname);
70
            } catch (ReflectionException $ex) {
71
                throw new ClassNotFoundException($completeClassname, $ex);
72
            }
73
74
            // Check the extends.
75
            if (false === (null === $extends || true === $rc->isSubclassOf($extends))) {
76
                continue;
77
            }
78
79
            // Initialize the hook.
80
            $hook = [];
81
82
            $hook["classpath"] = $classpath;
83
            $hook["namespace"] = $namespace;
84
            $hook["filename"]  = $filename;
85
            $hook["class"]     = $rc;
86
            $hook["method"]    = null;
87
88
            $hooks[] = $hook;
89
90
            // Check the method.
91
            if (null === $method) {
92
                continue;
93
            }
94
95
            try {
96
                $rm = $rc->getMethod($method);
97
98
                $hooks[count($hooks) - 1]["method"] = $rm;
99
            } catch (ReflectionException $ex) {
100
                throw new MethodNotFoundException($method, $ex);
101
            }
102
        }
103
104
        // Returns the hooks.
105
        return $hooks;
106
    }
107
108
    /**
109
     * Get the class name.
110
     *
111
     * @param mixed $object The class name or object.
112
     * @return string Returns the class name.
113
     */
114
    public static function getName($object) {
115
        return (new ReflectionClass($object))->getName();
0 ignored issues
show
Bug introduced by
Consider using (new \ReflectionClass($object))->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
116
    }
117
118
    /**
119
     * Get the short class name.
120
     *
121
     * @param mixed $object The class name or object.
122
     * @return string Returns the short class name.
123
     */
124
    public static function getShortName($object) {
125
        return (new ReflectionClass($object))->getShortName();
126
    }
127
128
}
129