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 |
||
15 | abstract class AbstractControllerTest extends WebTestCase |
||
16 | { |
||
17 | protected static $ENVIRONMENT; |
||
18 | protected static $TEMP_PATH; |
||
19 | |||
20 | /** |
||
21 | * @var Filesystem |
||
22 | */ |
||
23 | protected static $fileSystem; |
||
24 | /** |
||
25 | * @var Client |
||
26 | */ |
||
27 | protected static $client; |
||
28 | /** |
||
29 | * @var Router |
||
30 | */ |
||
31 | protected static $router; |
||
32 | |||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | * |
||
36 | * @throws \Exception |
||
37 | */ |
||
38 | public static function setUpBeforeClass() |
||
39 | { |
||
40 | static::$fileSystem = new Filesystem(); |
||
41 | static::$fileSystem->remove(__DIR__.static::$TEMP_PATH); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | * |
||
47 | * @throws \Symfony\Component\Filesystem\Exception\IOException |
||
48 | */ |
||
49 | public static function tearDownAfterClass() |
||
50 | { |
||
51 | if (in_array(getenv('DELETE_TEMP_FILES'), ['true', '1', 1, true], true)) { |
||
52 | static::$fileSystem->remove(__DIR__.static::$TEMP_PATH); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | * |
||
59 | * @throws \Exception |
||
60 | */ |
||
61 | protected function setUp() |
||
62 | { |
||
63 | static::$client = static::createClient(['environment' => static::$ENVIRONMENT]); |
||
64 | static::$router = static::$kernel->getContainer()->get('router'); |
||
65 | } |
||
66 | |||
67 | // |
||
68 | // PhpUnit |
||
69 | // |
||
70 | |||
71 | /** |
||
72 | * @return array |
||
73 | */ |
||
74 | abstract public function formatProvider(); |
||
75 | |||
76 | // |
||
77 | // Helper |
||
78 | // |
||
79 | |||
80 | /** |
||
81 | * @param string $uri |
||
82 | * @param string $format |
||
83 | * |
||
84 | * @throws \InvalidArgumentException |
||
85 | * @throws \Symfony\Component\Filesystem\Exception\IOException |
||
86 | * |
||
87 | * @return Spreadsheet |
||
88 | */ |
||
89 | protected function getDocument(string $uri, string $format = 'xlsx'): Spreadsheet |
||
90 | { |
||
91 | // generate source |
||
92 | static::$client->request('GET', $uri); |
||
93 | $source = static::$client->getResponse()->getContent(); |
||
94 | |||
95 | // create paths |
||
96 | $tempDirPath = __DIR__.static::$TEMP_PATH; |
||
97 | $tempFilePath = $tempDirPath.'simple'.'.'.$format; |
||
98 | |||
99 | // save source |
||
100 | static::$fileSystem->dumpFile($tempFilePath, $source); |
||
101 | |||
102 | // load source |
||
103 | switch ($format) { |
||
104 | case 'ods': |
||
105 | $readerType = 'Ods'; |
||
106 | break; |
||
107 | case 'xls': |
||
108 | $readerType = 'Xls'; |
||
109 | break; |
||
110 | case 'xlsx': |
||
111 | $readerType = 'Xlsx'; |
||
112 | break; |
||
113 | default: |
||
114 | throw new \InvalidArgumentException(); |
||
115 | } |
||
116 | |||
117 | return IOFactory::createReader($readerType)->load($tempFilePath); |
||
118 | } |
||
119 | } |
||
120 |