Conditions | 13 |
Paths | 17 |
Total Lines | 73 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
47 | public function __construct($className) |
||
48 | { |
||
49 | parent::__construct($className); |
||
50 | |||
51 | if ( ! empty($this->name)) { |
||
52 | $this->namespace = get_namespace($className); |
||
53 | |||
54 | $nameParts = explode('\\', $this->name); |
||
55 | |||
56 | $segments = []; |
||
57 | foreach ($nameParts as $namePart) { |
||
58 | if ( ! in_array(strtolower($namePart), [ |
||
59 | 'app', |
||
60 | 'apps', |
||
61 | 'modules', |
||
62 | 'components', |
||
63 | 'plugins', |
||
64 | 'controllers', |
||
65 | ])) { |
||
66 | $namePart = dash($namePart); |
||
67 | |||
68 | if ( ! in_array($namePart, $segments)) { |
||
69 | array_push($segments, $namePart); |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | |||
74 | if ($methods = $this->getMethods(\ReflectionMethod::IS_PUBLIC)) { |
||
75 | foreach ($methods as $method) { |
||
76 | if (strpos($method->name, '__') === false and ! in_array($method->name, |
||
77 | [ |
||
78 | 'route', |
||
79 | 'getClassInfo', |
||
80 | 'form', |
||
81 | 'add', |
||
82 | 'add-new', |
||
83 | 'edit', |
||
84 | 'create', |
||
85 | 'read', |
||
86 | 'update', |
||
87 | 'delete', |
||
88 | 'open', |
||
89 | 'download', |
||
90 | 'detail', |
||
91 | 'overview', |
||
92 | 'view', |
||
93 | 'settings', |
||
94 | 'setting', |
||
95 | 'sendError', |
||
96 | 'sendPayload' |
||
97 | ])) { |
||
98 | $methodSegments = $segments; |
||
99 | |||
100 | $method->segment = dash($method->name); |
||
|
|||
101 | |||
102 | if ( ! in_array($method->name, $methodSegments) and $method->name !== 'index') { |
||
103 | array_push($methodSegments, dash($method->name)); |
||
104 | } |
||
105 | |||
106 | $method->segments = implode('/', $methodSegments); |
||
107 | $method->hash = md5($method->segments); |
||
108 | |||
109 | $this->methods[$method->segment] = $method; |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
114 | if(empty($this->methods) and $this->hasMethod('route')) { |
||
115 | $method = $this->getMethod('route'); |
||
116 | $method->segment = dash($method->name); |
||
117 | $method->segments = implode('/', $segments); |
||
118 | $method->hash = md5($method->segments); |
||
119 | $this->methods[$method->segment] = $method; |
||
120 | } |
||
123 | } |