Conditions | 21 |
Paths | 864 |
Total Lines | 79 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 2 |
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 |
||
30 | public function send(EmailInterface $email) |
||
31 | { |
||
32 | $content = null; |
||
33 | if ($email->getHtmlBody()) { |
||
34 | $content = new Content("text/html", $email->getHtmlBody()); |
||
35 | } elseif ($email->getTextBody()) { |
||
36 | $content = new Content("text/plain", $email->getTextBody()); |
||
37 | } |
||
38 | |||
39 | $mail = new Mail( |
||
40 | $this->mapEmail($email->getFrom()), |
||
41 | $email->getSubject(), |
||
42 | null, |
||
43 | $content |
||
44 | ); |
||
45 | |||
46 | /** @var Personalization $personalization */ |
||
47 | $personalization = $mail->personalization; |
||
48 | |||
49 | foreach ($email->getTo() as $recipient) { |
||
50 | $personalization->addTo($this->mapEmail($recipient)); |
||
51 | } |
||
52 | |||
53 | if ($email->getReplyTo()) { |
||
54 | foreach ($email->getReplyTo() as $recipient) { |
||
55 | $mail->setReplyTo($this->mapEmail($recipient)); |
||
56 | break; // only one reply to |
||
57 | } |
||
58 | } |
||
59 | |||
60 | if ($email->getCc()) { |
||
61 | foreach ($email->getCc() as $recipient) { |
||
62 | $personalization->addCc($this->mapEmail($recipient)); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | if ($email->getBcc()) { |
||
67 | foreach ($email->getBcc() as $recipient) { |
||
68 | $personalization->addBcc($this->mapEmail($recipient)); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | if ($email->getAttachements()) { |
||
73 | foreach ($email->getAttachements() as $attachement) { |
||
74 | $finalAttachment = new Attachment(); |
||
75 | $finalAttachment->setType($attachement->getMimeType()); |
||
76 | $finalAttachment->setFilename($attachement->getName()); |
||
77 | if (!$attachement->getPath() && $attachement->getContent()) { |
||
78 | $finalAttachment->setContent(base64_encode($attachement->getContent())); |
||
79 | } elseif ($attachement->getPath()) { |
||
80 | $finalAttachment->setContent(base64_encode(file_get_contents($attachement->getPath()))); |
||
81 | } |
||
82 | $mail->addAttachment($finalAttachment); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | $sg = new \SendGrid($this->apiKey); |
||
87 | /** @var Response $response */ |
||
88 | $response = $sg->client->mail()->send()->post($mail); |
||
89 | |||
90 | if ($response->statusCode() >= 200 && $response->statusCode() < 300) { |
||
91 | if ($this->logger) { |
||
92 | $this->logger->info("Email sent: '{$email->getSubject()}'", $email); |
||
93 | } |
||
94 | } else { |
||
95 | switch ($response->statusCode()) { |
||
96 | case 401: |
||
97 | if ($this->logger) { |
||
98 | $this->logger->info("Email error: 'unauthorized'", $email); |
||
99 | } |
||
100 | throw new UnauthorizedException; |
||
101 | default: |
||
102 | if ($this->logger) { |
||
103 | $this->logger->info("Email error: 'invalid request'", $email); |
||
104 | } |
||
105 | throw new InvalidRequestException; |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
119 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: