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 |
||
| 8 | class FeatureContext implements Context |
||
|
|
|||
| 9 | { |
||
| 10 | private static $PARAMETERS = [ |
||
| 11 | '%base_host%' => 'localhost:8123', |
||
| 12 | ]; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $workingDir; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $phpBin; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var Process |
||
| 26 | */ |
||
| 27 | private $process; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var Process |
||
| 31 | */ |
||
| 32 | private $server; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Process |
||
| 36 | */ |
||
| 37 | private $phantomjs; |
||
| 38 | |||
| 39 | public function __construct() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Cleans test folders in the temporary directory. |
||
| 51 | * |
||
| 52 | * @BeforeSuite |
||
| 53 | * @AfterSuite |
||
| 54 | */ |
||
| 55 | public static function cleanTestFolders() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Prepares test folders in the temporary directory. |
||
| 67 | * |
||
| 68 | * @BeforeScenario |
||
| 69 | */ |
||
| 70 | public function prepareTestFolders() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @AfterScenario |
||
| 82 | */ |
||
| 83 | public function stopServer() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @BeforeScenario @phantomjs |
||
| 92 | */ |
||
| 93 | public function startPhantom() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @AfterScenario |
||
| 109 | */ |
||
| 110 | public function stopPhantom() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Creates a file with specified name and context in current workdir. |
||
| 120 | * |
||
| 121 | * @Given /^(?:there is )?a file named "([^"]*)" with:$/ |
||
| 122 | * |
||
| 123 | * @param string $filename name of the file (relative path) |
||
| 124 | * @param PyStringNode $content PyString string instance |
||
| 125 | */ |
||
| 126 | public function aFileNamedWith($filename, PyStringNode $content) |
||
| 131 | |||
| 132 | private function createFile($filename, $content) |
||
| 139 | |||
| 140 | private function createDirectory($path) |
||
| 146 | |||
| 147 | private static function clearDirectory($path) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @Given the homepage of my application is: |
||
| 167 | */ |
||
| 168 | public function theHomepageOfMyApplicationIs(PyStringNode $string) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @Given the file :file is: |
||
| 175 | */ |
||
| 176 | public function theFileWith($file, PyStringNode $string) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @Given my application is running |
||
| 183 | */ |
||
| 184 | public function myApplicationIsRunning() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @Given I have the following behat configuration: |
||
| 192 | */ |
||
| 193 | public function iHaveTheFollowingBehatConfiguration(PyStringNode $string) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Runs behat command with provided parameters |
||
| 202 | * |
||
| 203 | * @When /^I run "behat(?: ((?:\"|[^"])*))?"$/ |
||
| 204 | * |
||
| 205 | * @param string $argumentsString |
||
| 206 | */ |
||
| 207 | public function iRunBehat($argumentsString = '') |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Checks whether previously ran command passes|fails with provided output. |
||
| 231 | * |
||
| 232 | * @Then /^it should (fail|pass) with:$/ |
||
| 233 | * |
||
| 234 | * @param string $success "fail" or "pass" |
||
| 235 | * @param PyStringNode $text PyString text instance |
||
| 236 | */ |
||
| 237 | public function itShouldPassWith($success, PyStringNode $text) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Checks whether last command output contains provided string. |
||
| 245 | * |
||
| 246 | * @Then the output should contain: |
||
| 247 | * |
||
| 248 | * @param PyStringNode $text PyString text instance |
||
| 249 | */ |
||
| 250 | public function theOutputShouldContain(PyStringNode $text) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Checks whether previously ran command failed|passed. |
||
| 257 | * |
||
| 258 | * @Then /^it should (fail|pass)$/ |
||
| 259 | * |
||
| 260 | * @param string $success "fail" or "pass" |
||
| 261 | */ |
||
| 262 | public function itShouldFail($success) |
||
| 278 | |||
| 279 | private function getExpectedOutput(PyStringNode $expectedText) |
||
| 309 | |||
| 310 | private function getExitCode() |
||
| 314 | |||
| 315 | private function getOutput() |
||
| 329 | } |
||
| 330 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.