Completed
Push — master ( 7942ad...1c8947 )
by WEBEWEB
01:50
created

ClassUtility::getHooks()   C

Complexity

Conditions 10
Paths 9

Size

Total Lines 65
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 65
rs 6.2553
cc 10
eloc 32
nc 9
nop 5

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 NdC/WBW
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 short class name.
110
     *
111
     * @param mixed $classname The class name or object.
112
     * @return string Returns the short class name.
113
     */
114
    public static function getShortName($classname) {
115
        return (new ReflectionClass($classname))->getShortName();
116
    }
117
118
}
119