Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
17 | class EventsTree implements EventsTreeInterface |
||
18 | { |
||
19 | use \arc\traits\Proxy { |
||
20 | \arc\traits\Proxy::__construct as private ProxyConstruct; |
||
21 | } |
||
22 | |||
23 | private $tree = null; |
||
24 | |||
25 | /** |
||
26 | * @param \arc\tree\NamedNode $tree The tree storage for event listeners. |
||
27 | */ |
||
28 | 10 | public function __construct($tree) |
|
33 | |||
34 | /** |
||
35 | * Adds an event listener for the given event and returns it. |
||
36 | * @param string $eventName The event to listen for |
||
37 | * @param callable $callback The function to call when the event occurs. |
||
38 | * @return Listener |
||
39 | */ |
||
40 | 12 | public function listen($eventName, $callback) |
|
44 | |||
45 | /** |
||
46 | * Adds an event listener for the given event and returns it. The listener |
||
47 | * will trigger in the capture phase - before any listeners in the listen phase. |
||
48 | * @param string $eventName The name of the event to listen for. |
||
49 | * @param callable $callback The function to call when the event occurs. |
||
50 | * @return Listener |
||
51 | */ |
||
52 | 4 | public function capture($eventName, $callback) |
|
56 | |||
57 | /** |
||
58 | * Fires an event. If the event objects preventDefault() method has been called it |
||
59 | * will return false, otherwise the - potentially changed - eventData will be returned. |
||
60 | * @param string $eventName The name of the event to fire. |
||
61 | * @param array $eventData Optional. Data passed to each handler through the event object. |
||
62 | * @return false or $eventData - which may have been modified |
||
63 | */ |
||
64 | 12 | public function fire( $eventName, $eventData = array() ) |
|
79 | |||
80 | /** |
||
81 | * Returns a new EventStack with the given path. |
||
82 | * @param string $path The path to listen or fire an event. |
||
83 | * @return EventStack a new EventStack for the given path. |
||
84 | */ |
||
85 | 8 | public function cd($path) |
|
89 | |||
90 | /** |
||
91 | * Non-fluent api method to add a listener. |
||
92 | * @param string $eventName The name of the event to listen for. |
||
93 | * @param Callable $callback The callback method to call. |
||
94 | * @param bool $capture Optional. If true listen in the capture phase. Default is false - listen phase. |
||
95 | * @return Listener |
||
96 | */ |
||
97 | 12 | private function addListener($eventName, $callback, $capture = false) |
|
113 | |||
114 | /** |
||
115 | * Non-fluent api method to remove a listener. |
||
116 | * @param string $eventName The name of the event the listener is registered for. |
||
117 | * @param int $id The id of the listener. |
||
118 | * @param string $path Optional. The path the listener is registered on. Default is '/'. |
||
119 | * @param bool $capture Optional. If true the listener is triggered in the capture phase. |
||
120 | * Default is false. |
||
121 | */ |
||
122 | 12 | public function removeListener($eventName, $id, $capture = false) |
|
127 | |||
128 | /** |
||
129 | * Calls each listener with the given event untill a listener returns false. |
||
130 | */ |
||
131 | 12 | private function walkListeners($event) |
|
162 | } |
||
163 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.