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 |
||
23 | class Bootstrap |
||
24 | { |
||
25 | /** |
||
26 | * Application configuration cache key |
||
27 | */ |
||
28 | const CACHE_KEY = 'config'; |
||
29 | |||
30 | /** |
||
31 | * @var Application |
||
32 | */ |
||
33 | private $app; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $cacheKey; |
||
39 | |||
40 | /** |
||
41 | * @param object|Application $app |
||
42 | */ |
||
43 | public function __construct(Application $app) |
||
47 | |||
48 | /** |
||
49 | * @return string |
||
50 | */ |
||
51 | private function getCacheKey() |
||
55 | |||
56 | /** |
||
57 | * @param string $key |
||
58 | * @return $this |
||
59 | */ |
||
60 | public function setCacheKey($key) |
||
65 | |||
66 | /** |
||
67 | * Configures the application |
||
68 | * |
||
69 | * Returns cached early if any. |
||
70 | * |
||
71 | * @triggers configure |
||
72 | * @return $this |
||
73 | */ |
||
74 | public function configure() |
||
97 | |||
98 | /** |
||
99 | * Configure application services |
||
100 | * |
||
101 | * @return $this |
||
102 | */ |
||
103 | protected function configureServices() |
||
114 | |||
115 | /** |
||
116 | * Attach core listeners |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function attachCoreListeners() |
||
125 | |||
126 | /** |
||
127 | * Detach core listeners |
||
128 | * |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function detachCoreListeners() |
||
136 | |||
137 | /** |
||
138 | * Attach application listeners |
||
139 | * |
||
140 | * @return $this |
||
141 | */ |
||
142 | public function attachListeners() |
||
147 | |||
148 | /** |
||
149 | * @param $listener |
||
150 | */ |
||
151 | protected function attachListener($listener) |
||
156 | |||
157 | /** |
||
158 | * @param $listener |
||
159 | */ |
||
160 | protected function detachListener($listener) |
||
165 | |||
166 | /** |
||
167 | * @param callable $callback |
||
168 | */ |
||
169 | View Code Duplication | protected function eachCoreListener(callable $callback) |
|
180 | |||
181 | /** |
||
182 | * @param callable $callback |
||
183 | */ |
||
184 | View Code Duplication | protected function eachListener(callable $callback) |
|
195 | } |
||
196 |
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.