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 |
||
14 | class AddFile extends AbstractApi |
||
15 | { |
||
16 | /** @var Translation[] */ |
||
17 | protected $translations; |
||
18 | |||
19 | /** @var string */ |
||
20 | protected $type; |
||
21 | |||
22 | /** @var string */ |
||
23 | protected $branch; |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | */ |
||
28 | public function execute() |
||
29 | { |
||
30 | if (0 === count($this->translations)) { |
||
31 | throw new InvalidArgumentException('There is no files to add.'); |
||
32 | } |
||
33 | |||
34 | $path = sprintf( |
||
35 | "project/%s/add-file?key=%s", |
||
36 | $this->client->getProjectIdentifier(), |
||
37 | $this->client->getProjectApiKey() |
||
38 | ); |
||
39 | |||
40 | $data = $this->parameters; |
||
41 | if (null !== $this->type) { |
||
42 | $data['type'] = $this->type; |
||
43 | } |
||
44 | if (null !== $this->branch) { |
||
45 | $data['branch'] = $this->branch; |
||
46 | } |
||
47 | |||
48 | View Code Duplication | foreach ($this->translations as $translation) { |
|
|
|||
49 | $data['files['.$translation->getCrowdinPath().']'] = '@'.$translation->getLocalPath(); |
||
50 | if ($translation->getTitle()) { |
||
51 | $data['titles['.$translation->getCrowdinPath().']'] = $translation->getTitle(); |
||
52 | } |
||
53 | if ($translation->getExportPattern()) { |
||
54 | $data['export_patterns['.$translation->getCrowdinPath().']'] = $translation->getExportPattern(); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | $request = $this->client->getHttpClient()->post($path, [], $data); |
||
59 | $response = $request->send(); |
||
60 | |||
61 | return $response->getBody(true); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param string $localPath |
||
66 | * @param string $crowdinPath |
||
67 | * @param string $exportPattern |
||
68 | * @param string $title |
||
69 | * |
||
70 | * @return $this |
||
71 | */ |
||
72 | View Code Duplication | public function addTranslation($localPath, $crowdinPath, $exportPattern = null, $title = null) |
|
73 | { |
||
74 | $translation = new Translation($localPath, $crowdinPath); |
||
75 | $translation->setExportPattern($exportPattern); |
||
76 | $translation->setTitle($title); |
||
77 | |||
78 | $this->translations[] = $translation; |
||
79 | |||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @return Translation[] |
||
85 | */ |
||
86 | public function getTranslations() |
||
87 | { |
||
88 | return $this->translations; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getType() |
||
95 | { |
||
96 | return $this->type; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param string $type |
||
101 | * |
||
102 | * @return AddFile |
||
103 | */ |
||
104 | public function setType($type) |
||
105 | { |
||
106 | $this->type = $type; |
||
107 | |||
108 | return $this; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @return string|null |
||
113 | */ |
||
114 | public function getBranch() |
||
118 | |||
119 | /** |
||
120 | * @param string $branch |
||
121 | * |
||
122 | * @return AddFile |
||
123 | */ |
||
124 | public function setBranch($branch) |
||
130 | } |
||
131 |
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.