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 |
||
| 19 | final class FormatterManager |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var EventDispatcherInterface |
||
| 23 | */ |
||
| 24 | private $eventDispatcher; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Configuration |
||
| 28 | */ |
||
| 29 | private $siteConfiguration; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var DataProviderManager |
||
| 33 | */ |
||
| 34 | private $dataProviderManager; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $formatters = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $defaultFormatter; |
||
| 45 | |||
| 46 | public function __construct( |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param mixed $context |
||
| 58 | */ |
||
| 59 | private function buildBaseFormatContext($context) : Configuration |
||
| 86 | |||
| 87 | public function buildFormatContext(string $templateId, string $template, array $context) : FormatContext |
||
| 99 | |||
| 100 | public function registerFormatter(string $name, FormatterInterface $formatter) |
||
| 108 | |||
| 109 | public function formatter(string $name) : FormatterInterface |
||
| 113 | |||
| 114 | View Code Duplication | public function formatPage(string $templateId, string $template, array $context) : string |
|
| 124 | |||
| 125 | public function formatSourcePage(SourceInterface $source) : string |
||
| 133 | |||
| 134 | View Code Duplication | public function formatBlocks(string $templateId, string $template = null, array $context) : array |
|
| 144 | |||
| 145 | public function formatSourceBlocks(SourceInterface $source) : array |
||
| 153 | |||
| 154 | public function defaultFormatter() : string |
||
| 158 | |||
| 159 | /** |
||
| 160 | * NOTE: This is a hack because Symfony DiC cannot handle passing Data Provider |
||
| 161 | * Manager via constructor injection as some data providers might also rely |
||
| 162 | * on formatter. Hurray for circular dependencies. :(. |
||
| 163 | */ |
||
| 164 | public function setDataProviderManager(DataProviderManager $dataProviderManager) |
||
| 168 | } |
||
| 169 |
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
dieintroduces 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 withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.