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 |
||
| 15 | class ApnChannel |
||
| 16 | { |
||
| 17 | const SANDBOX = 0; |
||
| 18 | const PRODUCTION = 1; |
||
| 19 | |||
| 20 | /** @var string */ |
||
| 21 | protected $environment; |
||
| 22 | |||
| 23 | /** @var string */ |
||
| 24 | protected $certificate; |
||
| 25 | |||
| 26 | /** @var string|null */ |
||
| 27 | protected $passPhrase; |
||
| 28 | |||
| 29 | /** @var \ZendService\Apple\Apns\Client\Message */ |
||
| 30 | protected $client; |
||
| 31 | |||
| 32 | /** @var \Illuminate\Events\Dispatcher */ |
||
| 33 | protected $events; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param \ZendService\Apple\Apns\Client\Message $client |
||
| 37 | * @param \Illuminate\Events\Dispatcher $events |
||
| 38 | * @param string $environment |
||
| 39 | * @param string $certificate |
||
| 40 | * @param string|null $passPhrase |
||
| 41 | */ |
||
| 42 | 2 | public function __construct( |
|
| 43 | Client $client, |
||
| 44 | Dispatcher $events, |
||
| 45 | $environment, |
||
| 46 | $certificate, |
||
| 47 | $passPhrase = null |
||
| 48 | ) { |
||
| 49 | 2 | $this->client = $client; |
|
| 50 | 2 | $this->events = $events; |
|
| 51 | 2 | $this->environment = $environment; |
|
| 52 | 2 | $this->certificate = $certificate; |
|
| 53 | 2 | $this->passPhrase = $passPhrase; |
|
| 54 | 2 | } |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Send the notification to Apple Push Notification Service. |
||
| 58 | * |
||
| 59 | * @param mixed $notifiable |
||
| 60 | * @param \Illuminate\Notifications\Notification $notification |
||
| 61 | * |
||
| 62 | * @throws \NotificationChannels\Apn\Exceptions\SendingFailed |
||
| 63 | */ |
||
| 64 | 2 | public function send($notifiable, Notification $notification) |
|
| 65 | { |
||
| 66 | 2 | $tokens = (array) $notifiable->routeNotificationFor('apn'); |
|
| 67 | 2 | if (! $tokens) { |
|
|
|
|||
| 68 | return; |
||
| 69 | } |
||
| 70 | |||
| 71 | 2 | $message = $notification->toApn($notifiable); |
|
| 72 | 2 | if (! $message) { |
|
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | 2 | $this->openConnection(); |
|
| 77 | |||
| 78 | 2 | foreach ($tokens as $token) { |
|
| 79 | try { |
||
| 80 | 2 | $alert = new Alert(); |
|
| 81 | 2 | $alert->setTitle($message->title); |
|
| 82 | 2 | $alert->setBody($message->body); |
|
| 83 | |||
| 84 | 2 | $packet = new Packet(); |
|
| 85 | 2 | $packet->setToken($token); |
|
| 86 | 2 | $packet->setBadge($message->badge); |
|
| 87 | 2 | $packet->setSound($message->sound); |
|
| 88 | 2 | $packet->setCategory($message->category); |
|
| 89 | 2 | $packet->setContentAvailable($message->contentAvailable); |
|
| 90 | 2 | $packet->setAlert($alert); |
|
| 91 | 2 | $packet->setCustom($message->custom); |
|
| 92 | |||
| 93 | 2 | $response = $this->client->send($packet); |
|
| 94 | |||
| 95 | 2 | if ($response->getCode() !== Response::RESULT_OK) { |
|
| 96 | 1 | $this->events->fire( |
|
| 97 | 1 | new NotificationFailed($notifiable, $notification, $this, [ |
|
| 98 | 1 | 'token' => $token, |
|
| 99 | 2 | 'error' => $response->getCode(), |
|
| 100 | ]) |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | } catch (Exception $e) { |
||
| 104 | 2 | throw SendingFailed::create($e); |
|
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | 2 | $this->closeConnection(); |
|
| 109 | 2 | } |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Open the connection. |
||
| 113 | * |
||
| 114 | * @throws \NotificationChannels\Apn\Exceptions\ConnectionFailed |
||
| 115 | */ |
||
| 116 | 2 | View Code Duplication | private function openConnection() |
| 117 | { |
||
| 118 | try { |
||
| 119 | 2 | $this->client->open($this->environment, $this->certificate, $this->passPhrase); |
|
| 120 | } catch (Exception $exception) { |
||
| 121 | throw Exceptions\ConnectionFailed::create($exception); |
||
| 122 | } |
||
| 123 | 2 | } |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Close the connection. |
||
| 127 | */ |
||
| 128 | 2 | private function closeConnection() |
|
| 132 | } |
||
| 133 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.