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 |
||
29 | class SessionsListener implements EventSubscriberInterface |
||
30 | { |
||
31 | private $mink; |
||
32 | private $defaultSession; |
||
33 | private $javascriptSession; |
||
34 | |||
35 | /** |
||
36 | * @var string[] The available javascript sessions |
||
37 | */ |
||
38 | private $availableJavascriptSessions; |
||
39 | |||
40 | /** |
||
41 | * Initializes initializer. |
||
42 | * |
||
43 | * @param Mink $mink |
||
44 | * @param string $defaultSession |
||
45 | * @param string|null $javascriptSession |
||
46 | * @param string[] $availableJavascriptSessions |
||
47 | */ |
||
48 | public function __construct(Mink $mink, $defaultSession, $javascriptSession, array $availableJavascriptSessions = array()) |
||
49 | { |
||
50 | $this->mink = $mink; |
||
51 | $this->defaultSession = $defaultSession; |
||
52 | $this->javascriptSession = $javascriptSession; |
||
53 | $this->availableJavascriptSessions = $availableJavascriptSessions; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | public static function getSubscribedEvents() |
||
60 | { |
||
61 | return array( |
||
62 | ScenarioTested::BEFORE => array('prepareDefaultMinkSession', 10), |
||
63 | ExampleTested::BEFORE => array('prepareDefaultMinkSession', 10), |
||
64 | ExerciseCompleted::AFTER => array('tearDownMinkSessions', -10) |
||
65 | ); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Configures default Mink session before each scenario. |
||
70 | * Configuration is based on provided scenario tags: |
||
71 | * |
||
72 | * `@javascript` tagged scenarios will get `javascript_session` as default session |
||
73 | * `@mink:CUSTOM_NAME tagged scenarios will get `CUSTOM_NAME` as default session |
||
74 | * Other scenarios get `default_session` as default session |
||
75 | * |
||
76 | * `@insulated` tag will cause Mink to stop current sessions before scenario |
||
77 | * instead of just soft-resetting them |
||
78 | * |
||
79 | * @param ScenarioLikeTested $event |
||
80 | * |
||
81 | * @throws ProcessingException when the @javascript tag is used without a javascript session |
||
82 | */ |
||
83 | public function prepareDefaultMinkSession(ScenarioLikeTested $event) |
||
84 | { |
||
85 | $scenario = $event->getScenario(); |
||
86 | $feature = $event->getFeature(); |
||
87 | $session = null; |
||
88 | |||
89 | foreach (array_merge($feature->getTags(), $scenario->getTags()) as $tag) { |
||
90 | if ('javascript' === $tag) { |
||
91 | $session = $this->getJavascriptSession($event->getSuite()); |
||
92 | } elseif (preg_match('/^mink\:(.+)/', $tag, $matches)) { |
||
93 | $session = $matches[1]; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | if (null === $session) { |
||
98 | $session = $this->getDefaultSession($event->getSuite()); |
||
99 | } |
||
100 | |||
101 | if ($scenario->hasTag('insulated') || $feature->hasTag('insulated')) { |
||
102 | $this->mink->stopSessions(); |
||
103 | } else { |
||
104 | $this->mink->resetSessions(); |
||
105 | } |
||
106 | |||
107 | $this->mink->setDefaultSessionName($session); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Stops all started Mink sessions. |
||
112 | */ |
||
113 | public function tearDownMinkSessions() |
||
117 | |||
118 | private function getDefaultSession(Suite $suite) |
||
119 | { |
||
120 | if (!$suite->hasSetting('mink_session')) { |
||
139 | |||
140 | private function getJavascriptSession(Suite $suite) |
||
177 | } |
||
178 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.