silverstripe /
silverstripe-environmentcheck
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\EnvironmentCheck\Checks; |
||
| 4 | |||
| 5 | use SilverStripe\EnvironmentCheck\EnvironmentCheck; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Checks if the SMTP connection configured through PHP.ini works as expected. |
||
| 9 | * |
||
| 10 | * Only checks socket connection with HELO command, not actually sending the email. |
||
| 11 | * |
||
| 12 | * @package environmentcheck |
||
| 13 | */ |
||
| 14 | class SMTPConnectCheck implements EnvironmentCheck |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $host; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | protected $port; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Timeout (in seconds). |
||
| 28 | * |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | protected $timeout; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @param null|string $host |
||
| 35 | * @param null|int $port |
||
| 36 | * @param int $timeout |
||
| 37 | */ |
||
| 38 | public function __construct($host = null, $port = null, $timeout = 15) |
||
| 39 | { |
||
| 40 | $this->host = ($host) ? $host : ini_get('SMTP'); |
||
| 41 | if (!$this->host) { |
||
| 42 | $this->host = 'localhost'; |
||
| 43 | } |
||
| 44 | |||
| 45 | $this->port = ($port) ? $port : ini_get('smtp_port'); |
||
|
0 ignored issues
–
show
|
|||
| 46 | if (!$this->port) { |
||
| 47 | $this->port = 25; |
||
| 48 | } |
||
| 49 | |||
| 50 | $this->timeout = $timeout; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * {@inheritDoc} |
||
| 55 | * |
||
| 56 | * @return array |
||
| 57 | */ |
||
| 58 | public function check() |
||
| 59 | { |
||
| 60 | $f = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); |
||
| 61 | if (!$f) { |
||
|
0 ignored issues
–
show
|
|||
| 62 | return [ |
||
| 63 | EnvironmentCheck::ERROR, |
||
| 64 | sprintf("Couldn't connect to SMTP on %s:%s (Error: %s %s)", $this->host, $this->port, $errno, $errstr) |
||
| 65 | ]; |
||
| 66 | } |
||
| 67 | |||
| 68 | fwrite($f, "HELO its_me\r\n"); |
||
| 69 | $response = fread($f, 26); |
||
| 70 | if (substr($response, 0, 3) != '220') { |
||
| 71 | return [ |
||
| 72 | EnvironmentCheck::ERROR, |
||
| 73 | sprintf('Invalid mail server response: %s', $response) |
||
| 74 | ]; |
||
| 75 | } |
||
| 76 | |||
| 77 | return [EnvironmentCheck::OK, '']; |
||
| 78 | } |
||
| 79 | } |
||
| 80 |
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.