Conditions | 5 |
Paths | 6 |
Total Lines | 52 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 1 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
52 | public function execute() |
||
53 | { |
||
54 | if (0 === count($this->translations)) { |
||
55 | throw new InvalidArgumentException('There are no translations to upload.'); |
||
56 | } |
||
57 | |||
58 | if (null === $this->locale) { |
||
59 | throw new InvalidArgumentException('Locale is not set.'); |
||
60 | } |
||
61 | |||
62 | $path = sprintf( |
||
63 | "project/%s/upload-translation?key=%s", |
||
64 | $this->client->getProjectIdentifier(), |
||
65 | $this->client->getProjectApiKey() |
||
66 | ); |
||
67 | |||
68 | $data[] = [ |
||
|
|||
69 | 'name' => 'import_duplicates', |
||
70 | 'contents' => (int)$this->areDuplicatesImported |
||
71 | ]; |
||
72 | $data[] = [ |
||
73 | 'name' => 'import_eq_suggestions', |
||
74 | 'contents' => (int)$this->areEqualSuggestionsImported |
||
75 | ]; |
||
76 | $data[] = [ |
||
77 | 'name' => 'auto_approve_imported', |
||
78 | 'contents' => (int)$this->areImportsAutoApproved |
||
79 | ]; |
||
80 | $data[] = [ |
||
81 | 'name' => 'language', |
||
82 | 'contents' => $this->locale |
||
83 | ]; |
||
84 | |||
85 | if (null !== $this->branch) { |
||
86 | $data[] = [ |
||
87 | 'name' => 'branch', |
||
88 | 'contents' => $this->branch |
||
89 | ]; |
||
90 | } |
||
91 | |||
92 | foreach ($this->translations as $translation) { |
||
93 | $data[] = [ |
||
94 | 'name' => 'files['.$translation->getCrowdinPath().']', |
||
95 | 'contents' => $this->fileReader->readTranslation($translation) |
||
96 | ]; |
||
97 | } |
||
98 | |||
99 | $data = ['multipart' => $data]; |
||
100 | $response = $this->client->getHttpClient()->post($path, $data); |
||
101 | |||
102 | return $response->getBody(); |
||
103 | } |
||
104 | |||
261 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.