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 |
||
31 | class Parser |
||
32 | { |
||
33 | /** |
||
34 | * The .env to parse |
||
35 | * |
||
36 | * @var string $file |
||
37 | */ |
||
38 | public $file; |
||
39 | |||
40 | /** |
||
41 | * The Env key parser |
||
42 | * |
||
43 | * @var \M1\Env\Parser\KeyParser $key_parser |
||
44 | */ |
||
45 | private $key_parser; |
||
46 | |||
47 | /** |
||
48 | * The line num of the current value |
||
49 | * |
||
50 | * @var int $line_num |
||
51 | */ |
||
52 | public $line_num; |
||
53 | |||
54 | /** |
||
55 | * The current parsed values/lines |
||
56 | * |
||
57 | * @var array $lines |
||
58 | */ |
||
59 | public $lines; |
||
60 | |||
61 | /** |
||
62 | * If to throw ParseException in the .env |
||
63 | * |
||
64 | * @var bool $origin_exception |
||
65 | */ |
||
66 | public $origin_exception; |
||
67 | |||
68 | /** |
||
69 | * The String helper class |
||
70 | * |
||
71 | * @var \M1\Env\Helper\StringHelper $string_helper |
||
72 | */ |
||
73 | public $string_helper; |
||
74 | |||
75 | |||
76 | /** |
||
77 | * The Env value parser |
||
78 | * |
||
79 | * @var \M1\Env\Parser\ValueParser $value_parser |
||
80 | */ |
||
81 | public $value_parser; |
||
82 | |||
83 | /** |
||
84 | * The parser constructor |
||
85 | * |
||
86 | * @param string $file The .env to parse |
||
87 | * @param bool $origin_exception Whether or not to throw ParseException in the .env |
||
88 | */ |
||
89 | 63 | public function __construct($file, $origin_exception = false) |
|
97 | |||
98 | /** |
||
99 | * Opens the .env, parses it then returns the contents |
||
100 | * |
||
101 | * @return array The .env contents |
||
102 | */ |
||
103 | 63 | public function parse() |
|
113 | |||
114 | /** |
||
115 | * Parses the .env line by line |
||
116 | * |
||
117 | * @param array $raw_content The raw content of the file |
||
118 | * |
||
119 | * @throws \M1\Env\Exception\ParseException If the file does not have a key=value structure |
||
120 | * |
||
121 | * @return array The .env contents |
||
122 | */ |
||
123 | 60 | public function parseContent(array $raw_content) |
|
140 | |||
141 | /** |
||
142 | * Parses a line of the .env |
||
143 | * |
||
144 | * @param string $raw_line The raw content of the line |
||
145 | * |
||
146 | * @return array The parsed lines |
||
147 | */ |
||
148 | 57 | private function parseLine($raw_line) |
|
162 | |||
163 | 57 | private function parseExport($raw_line) |
|
185 | |||
186 | /** |
||
187 | * Gets the key = value items from the line |
||
188 | * |
||
189 | * @param string $raw_line The raw content of the line |
||
190 | * |
||
191 | * @throws \M1\Env\Exception\ParseException If the line does not have a key=value structure |
||
192 | * |
||
193 | * @return array The parsed lines |
||
194 | */ |
||
195 | 54 | private function parseKeyValue($raw_line) |
|
211 | } |
||
212 |
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.