| Conditions | 1 | 
| Paths | 1 | 
| Total Lines | 62 | 
| Code Lines | 55 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| 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 | ||
| 49 | private static function addMailgunEvent(EntityManager $manager, $mailgunApiKey, $eventType, $messageId, $count) | ||
| 50 |     { | ||
| 51 | $mailgunEvent = new MailgunEvent(); | ||
| 52 | $mailgunEvent->setEvent($eventType); | ||
| 53 |         $mailgunEvent->setDomain('acme'); | ||
| 54 | $d = new \DateTime(rand(1, 200).' days '.rand(1, 86400).' seconds ago'); | ||
| 55 | $mailgunEvent->setTimestamp($d->getTimestamp()); | ||
| 56 | $mailgunEvent->setToken(md5(time().$count)); | ||
| 57 |         $mailgunEvent->setRecipient('recipient-'.$count.'@email.com'); | ||
| 58 |         $mailgunEvent->setSender('some-sender-'.$count.'@email.com'); | ||
| 59 |         $mailgunEvent->setMessageHeaders(json_encode(array('some_json' => 'data', 'Subject' => "this mail was sent because it's important."))); | ||
| 60 | $mailgunEvent->setMessageId($messageId); | ||
| 61 |         $mailgunEvent->setSignature(hash_hmac('SHA256', $mailgunEvent->getTimestamp().$mailgunEvent->getToken(), $mailgunApiKey)); | ||
| 62 |         $mailgunEvent->setDescription('some description'); | ||
| 63 |         $mailgunEvent->setReason('don\'t know the reason'); | ||
| 64 |         $mailgunEvent->setIp('42.42.42.42'); | ||
| 65 |         $mailgunEvent->setErrorCode('123'); | ||
| 66 |         $mailgunEvent->setCountry('CH'); | ||
| 67 |         $mailgunEvent->setCity('Zurich'); | ||
| 68 |         $mailgunEvent->setRegion('8000'); | ||
| 69 |         $mailgunEvent->setCampaignId('2014-01-01'); | ||
| 70 |         $mailgunEvent->setCampaignName('newsletter'); | ||
| 71 |         $mailgunEvent->setClientName('some client'); | ||
| 72 |         $mailgunEvent->setClientOs('some os'); | ||
| 73 |         $mailgunEvent->setClientType('some type'); | ||
| 74 |         $mailgunEvent->setDeviceType('some device'); | ||
| 75 |         $mailgunEvent->setMailingList('no list'); | ||
| 76 |         $mailgunEvent->setTag('hmmm no tag'); | ||
| 77 |         $mailgunEvent->setUserAgent('Firefox 42'); | ||
| 78 |         $mailgunEvent->setUrl(''); | ||
| 79 | $manager->persist($mailgunEvent); | ||
| 80 | |||
| 81 | $file = new UploadedFile(realpath(__DIR__.'/testAttachment.small.png'), 'some.real.file.name1.png'); | ||
| 82 | $attachment = new MailgunAttachment($mailgunEvent); | ||
| 83 | $attachment->setContent(file_get_contents($file->getRealPath())); | ||
| 84 | $attachment->setName(md5(time() + rand(0, 100)).'.'.$file->getClientOriginalExtension()); | ||
| 85 | $attachment->setSize($file->getSize()); | ||
| 86 | $attachment->setType($file->getType()); | ||
| 87 | $attachment->setCounter(1); | ||
| 88 | $manager->persist($attachment); | ||
| 89 | |||
| 90 | $attachment = new MailgunAttachment($mailgunEvent); | ||
| 91 | $attachment->setContent(file_get_contents($file->getRealPath())); | ||
| 92 | $attachment->setName(md5(time() + rand(0, 100)).'.'.$file->getClientOriginalExtension()); | ||
| 93 | $attachment->setSize($file->getSize()); | ||
| 94 | $attachment->setType($file->getType()); | ||
| 95 | $attachment->setCounter(2); | ||
| 96 | $manager->persist($attachment); | ||
| 97 | |||
| 98 | $variable = new MailgunCustomVariable($mailgunEvent); | ||
| 99 | $variable->setEventId($mailgunEvent->getId()); | ||
| 100 |         $variable->setContent(array('some data1')); | ||
| 101 |         $variable->setVariableName('some custom variable for event'.$mailgunEvent->getId()); | ||
| 102 | $manager->persist($variable); | ||
| 103 | |||
| 104 | $variable = new MailgunCustomVariable($mailgunEvent); | ||
| 105 | $variable->setEventId($mailgunEvent->getId()); | ||
| 106 |         $variable->setContent(array('some data2')); | ||
| 107 |         $variable->setVariableName('some custom variable for event'.$mailgunEvent->getId()); | ||
| 108 | $manager->persist($variable); | ||
| 109 | |||
| 110 | return $mailgunEvent; | ||
| 111 | } | ||
| 226 |