| Conditions | 5 | 
| Paths | 6 | 
| Total Lines | 54 | 
| Code Lines | 33 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 0 | ||
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 |         $this->addUrlParameter('key', $this->client->getProjectApiKey()); | 
            ||
| 63 | |||
| 64 | $path = sprintf(  | 
            ||
| 65 | "project/%s/upload-translation?%s",  | 
            ||
| 66 | $this->client->getProjectIdentifier(),  | 
            ||
| 67 | $this->getUrlQueryString()  | 
            ||
| 68 | );  | 
            ||
| 69 | |||
| 70 | $data[] = [  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 71 | 'name' => 'import_duplicates',  | 
            ||
| 72 | 'contents' => (int)$this->areDuplicatesImported  | 
            ||
| 73 | ];  | 
            ||
| 74 | $data[] = [  | 
            ||
| 75 | 'name' => 'import_eq_suggestions',  | 
            ||
| 76 | 'contents' => (int)$this->areEqualSuggestionsImported  | 
            ||
| 77 | ];  | 
            ||
| 78 | $data[] = [  | 
            ||
| 79 | 'name' => 'auto_approve_imported',  | 
            ||
| 80 | 'contents' => (int)$this->areImportsAutoApproved  | 
            ||
| 81 | ];  | 
            ||
| 82 | $data[] = [  | 
            ||
| 83 | 'name' => 'language',  | 
            ||
| 84 | 'contents' => $this->locale  | 
            ||
| 85 | ];  | 
            ||
| 86 | |||
| 87 |         if (null !== $this->branch) { | 
            ||
| 88 | $data[] = [  | 
            ||
| 89 | 'name' => 'branch',  | 
            ||
| 90 | 'contents' => $this->branch  | 
            ||
| 91 | ];  | 
            ||
| 92 | }  | 
            ||
| 93 | |||
| 94 |         foreach ($this->translations as $translation) { | 
            ||
| 95 | $data[] = [  | 
            ||
| 96 | 'name' => 'files['.$translation->getCrowdinPath().']',  | 
            ||
| 97 | 'contents' => $this->fileReader->readTranslation($translation)  | 
            ||
| 98 | ];  | 
            ||
| 99 | }  | 
            ||
| 100 | |||
| 101 | $data = ['multipart' => $data];  | 
            ||
| 102 | $response = $this->client->getHttpClient()->post($path, $data);  | 
            ||
| 103 | |||
| 104 | return $response->getBody();  | 
            ||
| 105 | }  | 
            ||
| 106 | |||
| 263 | 
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.