Conditions | 13 |
Paths | 6 |
Total Lines | 42 |
Code Lines | 18 |
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 |
||
35 | protected function init() |
||
36 | { |
||
37 | // TODO refactor |
||
38 | |||
39 | $callback = function (DispatchEvent $event) { |
||
40 | // TODO |
||
41 | (isset($this->x) && ($this instanceof OnDispatchInterface || method_exists($this, 'onDispatchState'))) |
||
42 | and call_user_func($this->x, $event); |
||
43 | }; |
||
44 | |||
45 | // TODO |
||
46 | ($this instanceof OnDispatchInterface || method_exists($this, 'onDispatchState')) |
||
47 | and $this->listen(AppEvent::DISPATCH, $callback, AppEvent::FINISH); |
||
48 | |||
49 | ($this instanceof OnRenderComponentInterface) |
||
50 | and $this->listen(static::class, function (RenderEvent $event) { |
||
51 | |||
52 | // TODO redesign |
||
53 | |||
54 | if (($this instanceof OnDispatchInterface) |
||
55 | // TODO |
||
56 | || method_exists($this, 'onDispatchState') |
||
57 | ) { |
||
58 | $this->x = function (DispatchEvent $dispatchEvent) use ($event) { |
||
59 | |||
60 | // TODO |
||
61 | ($this instanceof OnDispatchInterface) and $this->onDispatch($dispatchEvent); |
||
62 | |||
63 | // TODO |
||
64 | method_exists($this, 'onDispatchState') and $this->onDispatchState($dispatchEvent); |
||
65 | |||
66 | // TODO |
||
67 | method_exists($this, 'setRenderEvent') and $this->setRenderEvent($event); |
||
68 | $this->onRender($event); |
||
69 | }; |
||
70 | } else { |
||
71 | // TODO concept |
||
72 | method_exists($this, 'setRenderEvent') and $this->setRenderEvent($event); |
||
73 | $this->onRender($event); |
||
74 | } |
||
75 | }); |
||
76 | } |
||
77 | |||
120 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: