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 |
||
36 | class Hooks { |
||
37 | |||
38 | public static function connectHooks() { |
||
52 | |||
53 | /** |
||
54 | * listen to write event. |
||
55 | */ |
||
56 | public static function write_hook( $params ) { |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Erase versions of deleted file |
||
66 | * @param array $params |
||
67 | * |
||
68 | * This function is connected to the delete signal of OC_Filesystem |
||
69 | * cleanup the versions directory if the actual file gets deleted |
||
70 | */ |
||
71 | public static function remove_hook($params) { |
||
77 | |||
78 | /** |
||
79 | * mark file as "deleted" so that we can clean up the versions if the file is gone |
||
80 | * @param array $params |
||
81 | */ |
||
82 | public static function pre_remove_hook($params) { |
||
88 | |||
89 | /** |
||
90 | * rename/move versions of renamed/moved files |
||
91 | * @param array $params array with oldpath and newpath |
||
92 | * |
||
93 | * This function is connected to the rename signal of OC_Filesystem and adjust the name and location |
||
94 | * of the stored versions along the actual file |
||
95 | */ |
||
96 | View Code Duplication | public static function rename_hook($params) { |
|
103 | |||
104 | /** |
||
105 | * copy versions of copied files |
||
106 | * @param array $params array with oldpath and newpath |
||
107 | * |
||
108 | * This function is connected to the copy signal of OC_Filesystem and copies the |
||
109 | * the stored versions to the new location |
||
110 | */ |
||
111 | View Code Duplication | public static function copy_hook($params) { |
|
118 | |||
119 | /** |
||
120 | * Remember owner and the owner path of the source file. |
||
121 | * If the file already exists, then it was a upload of a existing file |
||
122 | * over the web interface and we call Storage::store() directly |
||
123 | * |
||
124 | * @param array $params array with oldpath and newpath |
||
125 | * |
||
126 | */ |
||
127 | public static function pre_renameOrCopy_hook($params) { |
||
145 | |||
146 | /** |
||
147 | * Load additional scripts when the files app is visible |
||
148 | */ |
||
149 | public static function onLoadFilesAppScripts() { |
||
152 | } |
||
153 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.