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 |
||
18 | class CookieFieldStrategyTest extends WebTestCase |
||
19 | { |
||
20 | protected $strategy; |
||
21 | protected $client; |
||
22 | protected $propertyKey; |
||
23 | |||
24 | /** |
||
25 | * UnitTest Starts this on reach test |
||
26 | * @return void |
||
27 | */ |
||
28 | public function setUp() |
||
29 | { |
||
30 | parent::setUp(); |
||
31 | |||
32 | /** @var \Symfony\Bundle\FrameworkBundle\Client client */ |
||
33 | $this->client = static::createClient(); |
||
34 | $this->propertyKey = |
||
35 | $this->client->getKernel()->getContainer()->getParameter('graviton.security.authentication.strategy_key'); |
||
36 | $this->strategy = new CookieFieldStrategy( |
||
37 | $this->propertyKey |
||
38 | ); |
||
39 | |||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::apply |
||
44 | * @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo |
||
45 | * @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField |
||
46 | * |
||
47 | * @dataProvider stringProvider |
||
48 | * |
||
49 | * @param string $fieldValue value to check |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | View Code Duplication | public function testApply($fieldValue) |
|
|
|||
54 | { |
||
55 | $cookie = new Cookie( |
||
56 | $this->propertyKey, |
||
57 | $fieldValue, |
||
58 | time() + 3600 * 24 * 7, |
||
59 | '/', |
||
60 | null, |
||
61 | false, |
||
62 | false |
||
63 | ); |
||
64 | $this->client->getCookieJar()->set($cookie); |
||
65 | $this->client->request( |
||
66 | 'GET', //method |
||
67 | '/', //uri |
||
68 | array(), //parameters |
||
69 | array(), //files |
||
70 | array() //server |
||
71 | ); |
||
72 | |||
73 | $this->assertSame($fieldValue, $this->strategy->apply($this->client->getRequest())); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return array<string> |
||
78 | */ |
||
79 | View Code Duplication | public function stringProvider() |
|
88 | |||
89 | /** |
||
90 | * Todo, find a way to have also to client id set in request stack. |
||
91 | * |
||
92 | * @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::apply |
||
93 | * @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::extractAdUsername |
||
94 | * @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::extractCoreId |
||
95 | * @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo |
||
96 | * @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField |
||
97 | * |
||
98 | * @dataProvider stringExtractProvider |
||
99 | * |
||
100 | * @param string $fieldValue value to check |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | View Code Duplication | public function testApplyExtract($fieldValue) |
|
126 | |||
127 | /** |
||
128 | * @return array<string> |
||
129 | */ |
||
130 | public function stringExtractProvider() |
||
137 | } |
||
138 |
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.