GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Email   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 176
Duplicated Lines 22.73 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 3
dl 40
loc 176
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
C sendEmail() 0 51 7
A getPasswordResetBody() 10 10 1
A getEmailVerificationBody() 10 10 1
A getRevokeEmailBody() 10 10 1
A getUpdateEmailBody() 10 10 1
A getReportBugBody() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
 /**
4
  * Email Class
5
  *
6
  * Sending emails via SMTP.
7
  * It uses PHPMailer library to send emails.
8
  *
9
  * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
10
  * @author     Omar El Gabry <[email protected]>
11
  */
12
13
 class Email{
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
14
15
     /**
16
      * This is the constructor for Email object.
17
      *
18
      * @access private
19
      */
20
     private function __construct(){}
21
22
     /**
23
      * send an email
24
      *
25
      * @access public
26
      * @static static method
27
      * @param  string  $type Email constant - check config.php
28
      * @param  string  $email
29
      * @param  array   $userData
30
      * @param  array   $data any associated data with the email
31
      * @throws Exception If failed to send the email
32
      */
33
     public static function sendEmail($type, $email, $userData, $data){
34
35
         $mail             = new PHPMailer();
36
         $mail->IsSMTP();
37
38
         // good for debugging, otherwise keep it commented
39
         // $mail->SMTPDebug  = EMAIL_SMTP_DEBUG;
40
         $mail->SMTPAuth   = Config::get('EMAIL_SMTP_AUTH');
0 ignored issues
show
Documentation Bug introduced by
It seems like \Config::get('EMAIL_SMTP_AUTH') can also be of type string. However, the property $SMTPAuth is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
41
         $mail->SMTPSecure = Config::get('EMAIL_SMTP_SECURE');
42
         $mail->Host       = Config::get('EMAIL_SMTP_HOST');
43
         $mail->Port       = Config::get('EMAIL_SMTP_PORT');
0 ignored issues
show
Documentation Bug introduced by
It seems like \Config::get('EMAIL_SMTP_PORT') can also be of type string. However, the property $Port is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
44
         $mail->Username   = Config::get('EMAIL_SMTP_USERNAME');
45
         $mail->Password   = Config::get('EMAIL_SMTP_PASSWORD');
46
47
         $mail->SetFrom(Config::get('EMAIL_FROM'), Config::get('EMAIL_FROM_NAME'));
48
         $mail->AddReplyTo(Config::get('EMAIL_REPLY_TO'));
49
50
         switch($type){
51
             case (Config::get('EMAIL_EMAIL_VERIFICATION')):
52
                 $mail->Body = self::getEmailVerificationBody($userData, $data);
53
                 $mail->Subject    = Config::get('EMAIL_EMAIL_VERIFICATION_SUBJECT');
54
                 $mail->AddAddress($email);
55
                 break;
56
             case (Config::get('EMAIL_REVOKE_EMAIL')):
57
                 $mail->Body = self::getRevokeEmailBody($userData, $data);
58
                 $mail->Subject    = Config::get('EMAIL_REVOKE_EMAIL_SUBJECT');
59
                 $mail->AddAddress($email);
60
                 break;
61
             case (Config::get('EMAIL_UPDATE_EMAIL')):
62
                 $mail->Body = self::getUpdateEmailBody($userData, $data);
63
                 $mail->Subject    = Config::get('EMAIL_UPDATE_EMAIL_SUBJECT');
64
                 $mail->AddAddress($email);
65
                 break;
66
             case (Config::get('EMAIL_PASSWORD_RESET')):
67
                 $mail->Body = self::getPasswordResetBody($userData, $data);
68
                 $mail->Subject    = Config::get('EMAIL_PASSWORD_RESET_SUBJECT');
69
                 $mail->AddAddress($email);
70
                 break;
71
             case (Config::get('EMAIL_REPORT_BUG')):
72
                 $mail->Body = self::getReportBugBody($userData, $data);
73
                 $mail->Subject    = "[".ucfirst($data["label"])."] " . Config::get('EMAIL_REPORT_BUG_SUBJECT') . " | " . $data["subject"];
74
                 $mail->AddAddress($email);
75
                 break;
76
         }
77
78
         // If you don't have an email setup, you can instead save emails in log.txt file using Logger.
79
         // Logger::log("EMAIL", $mail->Body);
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80
         if(!$mail->Send()) {
81
             throw new Exception("Email couldn't be sent to ". $userData["id"] ." for type: ". $type);
82
         }
83
     }
84
85
     /**
86
      * Construct the body of Password Reset email
87
      *
88
      * @access private
89
      * @static static method
90
      * @param  array   $userData
91
      * @param  array   $data
92
      * @return string  The body of the email.
93
      */
94 View Code Duplication
     private static function getPasswordResetBody($userData, $data){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
96
         $body = "";
97
         $body .= "Dear " . $userData["name"] . ", \n\nYou can reset your password from the following link: ";
98
         $body .= Config::get('EMAIL_PASSWORD_RESET_URL') . "?id=" . urlencode(Encryption::encryptId($userData["id"])) . "&token=" . urlencode($data["password_token"]);
99
         $body .= "\n\nIf you didn't request to reset your password, Please contact the admin directly.";
100
         $body .= "\n\nRegards\nmini PHP Team";
101
102
         return $body;
103
     }
104
105
     /**
106
      * Construct the body of Email Verification email
107
      *
108
      * @access private
109
      * @static static method
110
      * @param  array   $userData
111
      * @param  array   $data
112
      * @return string  The body of the email.
113
      *
114
      */
115 View Code Duplication
     private static function getEmailVerificationBody($userData, $data){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
117
         $body  = "";
118
         $body .= "Dear " . $userData["name"] . ", \n\nPlease verify your email from the following link: ";
119
         $body .= Config::get('EMAIL_EMAIL_VERIFICATION_URL') . "?id=" . urlencode(Encryption::encryptId($userData["id"])) . "&token=" . urlencode($data["email_token"]);
120
         $body .= "\n\nIf you didn't edit/add your email, Please contact the admin directly.";
121
         $body .= "\n\nRegards\nmini PHP Team";
122
123
         return $body;
124
     }
125
126
     /**
127
      * Construct the body of Revoke Email Changes email
128
      *
129
      * @access private
130
      * @static static method
131
      * @param  array   $userData
132
      * @param  array   $data
133
      * @return string  The body of the email.
134
      *
135
      */
136 View Code Duplication
     private static function getRevokeEmailBody($userData, $data){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
138
         $body  = "";
139
         $body .= "Dear " . $userData["name"] . ", \n\nYour email has been changed, You can revoke your changes from the following link: ";
140
         $body .= Config::get('EMAIL_REVOKE_EMAIL_URL') . "?id=" . urlencode(Encryption::encryptId($userData["id"])) . "&token=" . urlencode($data["email_token"]);
141
         $body .= "\n\nIf you didn't update your email, Please contact the admin directly.";
142
         $body .= "\n\nRegards\nmini PHP Team";
143
144
         return $body;
145
     }
146
147
     /**
148
      * Construct the body of Update Email email
149
      *
150
      * @access private
151
      * @static static method
152
      * @param  array   $userData
153
      * @param  array   $data
154
      * @return string  The body of the email.
155
      *
156
      */
157 View Code Duplication
     private static function getUpdateEmailBody($userData, $data){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
159
         $body  = "";
160
         $body .= "Dear " . $userData["name"] . ", \n\nPlease confirm your new email from the following link: ";
161
         $body .= Config::get('EMAIL_UPDATE_EMAIL_URL') . "?id=" . urlencode(Encryption::encryptId($userData["id"])) . "&token=" . urlencode($data["pending_email_token"]);
162
         $body .= "\n\nIf you have no idea what is this email for, you can ignore it.";
163
         $body .= "\n\nRegards\nmini PHP Team";
164
165
         return $body;
166
     }
167
168
     /**
169
      * Construct the body of Report Bug, Feature or Enhancement email
170
      *
171
      * @access private
172
      * @static static method
173
      * @param  array   $userData
174
      * @param  array   $data
175
      * @return string  The body of the email.
176
      *
177
      */
178
     private static function getReportBugBody($userData, $data){
179
180
         $body  = "";
181
         $body .= "User: " . $userData["name"] . ", \n\n" . $data["message"];
182
         $body .= "\n\n\nFrom: " . $userData["id"] . " | " . $userData["name"];
183
         $body .= "\n\nRegards\nmini PHP Team";
184
185
         return $body;
186
     }
187
188
 }
189
	
190