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 |
||
25 | class FeatureContext extends BehatContext |
||
26 | { |
||
27 | /** |
||
28 | * Initializes context. |
||
29 | * Every scenario gets its own context object. |
||
30 | * |
||
31 | * @param array $parameters context parameters (set them up through behat.yml) |
||
32 | */ |
||
33 | public function __construct(array $parameters) |
||
37 | |||
38 | static private $vhosts = ["/test-behat", "/test-behat-named"]; |
||
39 | |||
40 | use ETNA\FeatureContext\RabbitMQ; |
||
41 | |||
42 | /** |
||
43 | * @Given /^une application Silex$/ |
||
44 | */ |
||
45 | public function uneApplicationSilex() |
||
50 | |||
51 | /** |
||
52 | * @Given /^la configuration suivante :$/ |
||
53 | */ |
||
54 | public function laConfigurationSuivante(PyStringNode $config) |
||
64 | |||
65 | /** |
||
66 | * @Given /^\$app\["amqp\.chan"\] == \$app\["amqp\.chans"\]\["default"\]$/ |
||
67 | */ |
||
68 | public function chanEstUnAliasVersDefault() |
||
74 | |||
75 | /** |
||
76 | * @Given /^\$app\["amqp\.(\w+)"\]\["(\w+)"\] est du type ([\w\\]+)$/ |
||
77 | */ |
||
78 | public function checkClass($type, $name, $class) |
||
84 | |||
85 | /** |
||
86 | * @Given /^\$app\["amqp\.(\w+)"\]\["(\w+)"\]->(\w+)\(\) == "([^"]*)"$/ |
||
87 | */ |
||
88 | View Code Duplication | public function checkGetterString($type, $name, $method, $value) |
|
95 | |||
96 | /** |
||
97 | * @Given /^\$app\["amqp\.(\w+)"\]\["(\w+)"\]->(\w+)\(\) == (true|false)$/ |
||
98 | */ |
||
99 | View Code Duplication | public function checkGetterBoolean($type, $name, $method, $value) |
|
106 | |||
107 | /** |
||
108 | * @Given /^que je bind une file sur l\'exchange "([^"]*)"$/ |
||
109 | */ |
||
110 | public function queJeBindUneFileSurLExchange($exchange) |
||
116 | |||
117 | /** |
||
118 | * @Given /^que "([^"]*)" est (un exchange|une queue) réservé$/ |
||
119 | */ |
||
120 | public function queEstUnTrucReserve($name, $type) |
||
129 | |||
130 | /** |
||
131 | * @Given /^je devrais avoir une exception "([^"]*)"$/ |
||
132 | */ |
||
133 | public function jeDevraisAvoirUneException($exception) |
||
139 | |||
140 | /** |
||
141 | * @Given /^j\'envoie un message "(\w+)" dans l\'exchange "(\w+)"$/ |
||
142 | */ |
||
143 | public function jEnvoieUnMessage($message, $exchange) |
||
147 | |||
148 | /** |
||
149 | * @Given /^j\'envoie un message "(\w+)" dans la file "([^"]*)"$/ |
||
150 | */ |
||
151 | public function jEnvoieUnMessageDansLaFile($message, $queue) |
||
157 | |||
158 | /** |
||
159 | * @Given /^il doit y avoir un message "([^"]*)" dans la file( "(\w+)")?$/ |
||
160 | */ |
||
161 | public function ilDoitYAvoirUnMessageDansLaFile($message, $queue = null) |
||
162 | { |
||
163 | $this->channel->basic_consume($this->tmp_queue, "behat", false, false, false, false, function ($msg) use ($message) { |
||
164 | $msg->delivery_info['channel']->basic_cancel($msg->delivery_info['consumer_tag']); |
||
165 | |||
166 | if (json_decode($msg->body) != $message) { |
||
167 | throw new Exception("{$msg->body} != {$message}"); |
||
168 | } |
||
169 | }); |
||
170 | $this->channel->wait(); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @Given /^je fais un listen ma callback doit être appelé (\d+) fois$/ |
||
175 | */ |
||
176 | public function jeFaisUnListen($nb) |
||
194 | } |
||
195 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.