Completed
Push — master ( 674066...0b0354 )
by WEBEWEB
03:44
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\Helper\Argument;
13
14
use ReflectionClass;
15
use ReflectionException;
16
use WBW\Library\Core\Exception\Argument\ObjectArgumentException;
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\Helper\IO\FileHelper;
21
22
/**
23
 * Object helper.
24
 *
25
 * @author webeweb <https://github.com/webeweb/>
26
 * @package WBW\Library\Core\Helper\Argument
27
 */
28
class ObjectHelper {
29
30
    /**
31
     * Get the hooks.
32
     *
33
     * @param string $classpath The class path.
34
     * @param string $namespace The namespace.
35
     * @param string $classname The class name.
36
     * @param string $extends The class extend.
37
     * @param string $method The class method.
38
     * @return array Returns the hooks array.
39
     * @throws ClassNotFoundException Throws a hook class not found if the classname is not found.
40
     */
41
    public static function getHooks($classpath, $namespace, $classname = null, $extends = null, $method = null) {
42
43
        //
44
        $hooks = [];
45
46
        // Get the filenames.
47
        $filenames = FileHelper::getFileNames($classpath, ".php");
48
49
        // Handle each filenames.
50
        foreach ($filenames as $filename) {
51
52
            // Check the class name.
53
            if (null !== $classname && 0 === preg_match($classname, $filename)) {
54
                continue;
55
            }
56
57
            // Import the class.
58
            try {
59
                require_once $classpath . "/" . $filename;
60
            } catch (Exception $ex) {
0 ignored issues
show
Bug introduced by
The class WBW\Library\Core\Helper\Argument\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
61
                throw new SyntaxErrorException($classpath . "/" . $filename);
62
            }
63
64
            // Init. the complete class name.
65
            $completeClassname = $namespace . basename($filename, ".php");
66
67
            try {
68
                $rc = new ReflectionClass($completeClassname);
69
            } catch (ReflectionException $ex) {
70
                throw new ClassNotFoundException($completeClassname, $ex);
71
            }
72
73
            // Check the extends.
74
            if (false === (null === $extends || true === $rc->isSubclassOf($extends))) {
75
                continue;
76
            }
77
78
            // Initialize the hook.
79
            $hook = [];
80
81
            $hook["classpath"] = $classpath;
82
            $hook["namespace"] = $namespace;
83
            $hook["filename"]  = $filename;
84
            $hook["class"]     = $rc;
85
            $hook["method"]    = null;
86
87
            $hooks[] = $hook;
88
89
            // Check the method.
90
            if (null === $method) {
91
                continue;
92
            }
93
94
            try {
95
                $rm = $rc->getMethod($method);
96
97
                $hooks[count($hooks) - 1]["method"] = $rm;
98
            } catch (ReflectionException $ex) {
99
                throw new MethodNotFoundException($method, $ex);
100
            }
101
        }
102
103
        // Returns the hooks.
104
        return $hooks;
105
    }
106
107
    /**
108
     * Get the class name.
109
     *
110
     * @param mixed $object The class name or object.
111
     * @return string Returns the class name.
112
     * @throws ReflectionException Throws a Reflection exception if an error occurs.
113
     */
114
    public static function getName($object) {
115
        return (new ReflectionClass($object))->name;
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
     * @throws ReflectionException Throws a Reflection exception if an error occurs.
124
     */
125
    public static function getShortName($object) {
126
        return (new ReflectionClass($object))->getShortName();
127
    }
128
129
    /**
130
     * Determines if a value is an object.
131
     *
132
     * @param mixed $value The value.
133
     * @throws ObjectArgumentException Throws an Object argument exception if the value is not of expected type.
134
     */
135
    public static function isObject($value) {
136
        if (false === is_object($value)) {
137
            throw new ObjectArgumentException($value);
138
        }
139
    }
140
141
}
142