Completed
Push — master ( 8f5bc0...179bc9 )
by WEBEWEB
03:06
created

ObjectHelper::getHooks()   C

Complexity

Conditions 10
Paths 9

Size

Total Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 6.8969
c 0
b 0
f 0
cc 10
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 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\Argument;
13
14
use Exception;
15
use ReflectionClass;
16
use ReflectionException;
17
use WBW\Library\Core\Exception\Argument\ObjectArgumentException;
18
use WBW\Library\Core\Exception\Reflection\ClassNotFoundException;
19
use WBW\Library\Core\Exception\Reflection\MethodNotFoundException;
20
use WBW\Library\Core\Exception\Reflection\SyntaxErrorException;
21
use WBW\Library\Core\IO\FileHelper;
22
23
/**
24
 * Object helper.
25
 *
26
 * @author webeweb <https://github.com/webeweb/>
27
 * @package WBW\Library\Core\Argument
28
 */
29
class ObjectHelper {
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 = FileHelper::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
     * @throws ReflectionException Throws a Reflection exception if an error occurs.
114
     */
115
    public static function getName($object) {
116
        return (new ReflectionClass($object))->name;
117
    }
118
119
    /**
120
     * Get the short class name.
121
     *
122
     * @param mixed $object The class name or object.
123
     * @return string Returns the short class name.
124
     * @throws ReflectionException Throws a Reflection exception if an error occurs.
125
     */
126
    public static function getShortName($object) {
127
        return (new ReflectionClass($object))->getShortName();
128
    }
129
130
    /**
131
     * Determines if a value is an object.
132
     *
133
     * @param mixed $value The value.
134
     * @throws ObjectArgumentException Throws an Object argument exception if the value is not of expected type.
135
     */
136
    public static function isObject($value) {
137
        if (false === is_object($value)) {
138
            throw new ObjectArgumentException($value);
139
        }
140
    }
141
142
}
143