Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
minor #926 Migrate to php7 (Rotzbua)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 6.0-dev branch (closes #926).

Discussion
----------

Migrate to php7

Thanks for release 6 and php7 👍

Some improvements
- better random fct
- use Null Coalesce Operator
- replace `random` with now php7 `random_int`

Commits
-------

5a057a5 Migrate to php7
  • Loading branch information
fabpot committed May 20, 2017
2 parents e5ecb02 + 5a057a5 commit fb4800d
Show file tree
Hide file tree
Showing 26 changed files with 56 additions and 39 deletions.
Expand Up @@ -103,7 +103,7 @@ public function init()
*/
public function getReaderFor($charset)
{
$charset = trim(strtolower($charset));
$charset = strtolower(trim($charset));
foreach (self::$map as $pattern => $spec) {
$re = '/^'.$pattern.'$/D';
if (preg_match($re, $charset)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/Swift/CharacterStream/ArrayCharacterStream.php
Expand Up @@ -172,7 +172,7 @@ public function readBytes($length)
}
$this->offset += ($i - $this->offset); // Limit function calls

return call_user_func_array('array_merge', $arrays);
return array_merge(...$arrays);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions lib/classes/Swift/FileSpool.php
Expand Up @@ -95,7 +95,7 @@ public function queueMessage(Swift_Mime_SimpleMessage $message)
$fileName = $this->path.'/'.$this->getRandomString(10);
for ($i = 0; $i < $this->retryLimit; ++$i) {
/* We try an exclusive creation of the file. This is an atomic operation, it avoid locking mechanism */
$fp = @fopen($fileName.'.message', 'x');
$fp = @fopen($fileName.'.message', 'xb');
if (false !== $fp) {
if (false === fwrite($fp, $ser)) {
return false;
Expand Down Expand Up @@ -164,6 +164,7 @@ public function flushQueue(Swift_Transport $transport, &$failedRecipients = null

/* We try a rename, it's an atomic operation, and avoid locking the file */
if (rename($file, $file.'.sending')) {
// TODO SECURITY
$message = unserialize(file_get_contents($file.'.sending'));

$count += $transport->send($message, $failedRecipients);
Expand Down Expand Up @@ -200,7 +201,7 @@ protected function getRandomString($count)
$ret = '';
$strlen = strlen($base);
for ($i = 0; $i < $count; ++$i) {
$ret .= $base[((int) rand(0, $strlen - 1))];
$ret .= $base[random_int(0, $strlen - 1)];
}

return $ret;
Expand Down
4 changes: 2 additions & 2 deletions lib/classes/Swift/Mime/ContentEncoder/PlainContentEncoder.php
Expand Up @@ -56,7 +56,7 @@ public function encodeString($string, $firstLineOffset = 0, $maxLineLength = 0)
$string = $this->canonicalize($string);
}

return $this->safeWordWrap($string, $maxLineLength, "\r\n");
return $this->safeWordwrap($string, $maxLineLength, "\r\n");
}

/**
Expand All @@ -75,7 +75,7 @@ public function encodeByteStream(Swift_OutputByteStream $os, Swift_InputByteStre
if ($this->canonical) {
$toencode = $this->canonicalize($toencode);
}
$wrapped = $this->safeWordWrap($toencode, $maxLineLength, "\r\n");
$wrapped = $this->safeWordwrap($toencode, $maxLineLength, "\r\n");
$lastLinePos = strrpos($wrapped, "\r\n");
$leftOver = substr($wrapped, $lastLinePos);
$wrapped = substr($wrapped, 0, $lastLinePos);
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/Swift/Mime/Headers/ParameterizedHeader.php
Expand Up @@ -97,7 +97,7 @@ public function getParameter($parameter)
{
$params = $this->getParameters();

return array_key_exists($parameter, $params) ? $params[$parameter] : null;
return $params[$parameter] ?? null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/Swift/Mime/MimePart.php
Expand Up @@ -128,7 +128,7 @@ public function setFormat($format)
*/
public function getDelSp()
{
return 'yes' == $this->getHeaderParameter('Content-Type', 'delsp') ? true : false;
return 'yes' === $this->getHeaderParameter('Content-Type', 'delsp');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/Swift/Mime/SimpleMessage.php
Expand Up @@ -493,7 +493,7 @@ public function getPriority()
'%[1-5]'
);

return isset($priority) ? $priority : 3;
return $priority ?? 3;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/Swift/Mime/SimpleMimeEntity.php
Expand Up @@ -293,7 +293,7 @@ public function getChildren()
public function setChildren(array $children, $compoundLevel = null)
{
// TODO: Try to refactor this logic
$compoundLevel = isset($compoundLevel) ? $compoundLevel : $this->getCompoundLevel($children);
$compoundLevel = $compoundLevel ?? $this->getCompoundLevel($children);
$immediateChildren = array();
$grandchildren = array();
$newContentType = $this->userContentType;
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/Swift/Plugins/DecoratorPlugin.php
Expand Up @@ -159,7 +159,7 @@ public function getReplacementsFor($address)
return $this->replacements->getReplacementsFor($address);
}

return isset($this->replacements[$address]) ? $this->replacements[$address] : null;
return $this->replacements[$address] ?? null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/Swift/Plugins/ThrottlerPlugin.php
Expand Up @@ -179,7 +179,7 @@ private function throttleBytesPerMinute($timePassed)
*/
private function throttleMessagesPerSecond($timePassed)
{
$expectedDuration = $this->messages / ($this->rate);
$expectedDuration = $this->messages / $this->rate;

return (int) ceil($expectedDuration - $timePassed);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/Swift/Signers/SMimeSigner.php
Expand Up @@ -278,7 +278,7 @@ protected function messageStreamToSignedByteStream(Swift_FileStream $outputStrea
$args[] = $this->extraCerts;
}

if (!call_user_func_array('openssl_pkcs7_sign', $args)) {
if (!openssl_pkcs7_sign(...$args)) {
throw new Swift_IoException(sprintf('Failed to sign S/Mime message. Error: "%s".', openssl_error_string()));
}

Expand Down
Expand Up @@ -553,7 +553,7 @@ protected function createByte($input, $bytes = 4, $isHex = true)
*
* @return string
*/
protected function getRandomBytes($length)
protected function getRandomBytes($length) : string
{
$bytes = openssl_random_pseudo_bytes($length, $strong);

Expand Down
2 changes: 1 addition & 1 deletion lib/classes/Swift/Transport/EsmtpTransport.php
Expand Up @@ -208,7 +208,7 @@ public function setSourceIp($source)
*/
public function getSourceIp()
{
return isset($this->params['sourceIp']) ? $this->params['sourceIp'] : null;
return $this->params['sourceIp'] ?? null;
}

/**
Expand Down
Expand Up @@ -17,7 +17,7 @@ protected function setUp()

$serverStarted = false;
for ($i = 0; $i < 5; ++$i) {
$this->randomHighPort = rand(50000, 65000);
$this->randomHighPort = random_int(50000, 65000);
$this->server = stream_socket_server('tcp://127.0.0.1:'.$this->randomHighPort);
if ($this->server) {
$serverStarted = true;
Expand Down
2 changes: 1 addition & 1 deletion tests/bug/Swift/Bug51Test.php
Expand Up @@ -87,7 +87,7 @@ private function fillFileWithRandomBytes($byteCount, $file)
file_put_contents($file, '');
$fp = fopen($file, 'wb');
for ($i = 0; $i < $byteCount; ++$i) {
$byteVal = rand(0, 255);
$byteVal = random_int(0, 255);
fwrite($fp, pack('i', $byteVal));
}
fclose($fp);
Expand Down
2 changes: 1 addition & 1 deletion tests/bug/Swift/Bug76Test.php
Expand Up @@ -53,7 +53,7 @@ private function fillFileWithRandomBytes($byteCount, $file)
file_put_contents($file, '');
$fp = fopen($file, 'wb');
for ($i = 0; $i < $byteCount; ++$i) {
$byteVal = rand(0, 255);
$byteVal = random_int(0, 255);
fwrite($fp, pack('i', $byteVal));
}
fclose($fp);
Expand Down
2 changes: 1 addition & 1 deletion tests/smoke/Swift/Smoke/AttachmentSmokeTest.php
Expand Up @@ -9,7 +9,7 @@ class Swift_Smoke_AttachmentSmokeTest extends SwiftMailerSmokeTestCase

protected function setUp()
{
parent::setup(); // For skip
parent::setUp(); // For skip
$this->attFile = __DIR__.'/../../../_samples/files/textfile.zip';
}

Expand Down
2 changes: 1 addition & 1 deletion tests/smoke/Swift/Smoke/InternationalSmokeTest.php
Expand Up @@ -9,7 +9,7 @@ class Swift_Smoke_InternationalSmokeTest extends SwiftMailerSmokeTestCase

protected function setUp()
{
parent::setup(); // For skip
parent::setUp(); // For skip
$this->attFile = __DIR__.'/../../../_samples/files/textfile.zip';
}

Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Swift/Encoder/Base64EncoderTest.php
Expand Up @@ -64,23 +64,23 @@ public function testPadLength()
*/

for ($i = 0; $i < 30; ++$i) {
$input = pack('C', rand(0, 255));
$input = pack('C', random_int(0, 255));
$this->assertRegExp(
'~^[a-zA-Z0-9/\+]{2}==$~', $this->encoder->encodeString($input),
'%s: A single byte should have 2 bytes of padding'
);
}

for ($i = 0; $i < 30; ++$i) {
$input = pack('C*', rand(0, 255), rand(0, 255));
$input = pack('C*', random_int(0, 255), random_int(0, 255));
$this->assertRegExp(
'~^[a-zA-Z0-9/\+]{3}=$~', $this->encoder->encodeString($input),
'%s: Two bytes should have 1 byte of padding'
);
}

for ($i = 0; $i < 30; ++$i) {
$input = pack('C*', rand(0, 255), rand(0, 255), rand(0, 255));
$input = pack('C*', random_int(0, 255), random_int(0, 255), random_int(0, 255));
$this->assertRegExp(
'~^[a-zA-Z0-9/\+]{4}$~', $this->encoder->encodeString($input),
'%s: Three bytes should have no padding'
Expand Down
Expand Up @@ -82,7 +82,7 @@ public function testPadLength()
->andReturnUsing($collection);
$os->shouldReceive('read')
->once()
->andReturn(pack('C', rand(0, 255)));
->andReturn(pack('C', random_int(0, 255)));
$os->shouldReceive('read')
->zeroOrMoreTimes()
->andReturn(false);
Expand All @@ -103,7 +103,7 @@ public function testPadLength()
->andReturnUsing($collection);
$os->shouldReceive('read')
->once()
->andReturn(pack('C*', rand(0, 255), rand(0, 255)));
->andReturn(pack('C*', random_int(0, 255), random_int(0, 255)));
$os->shouldReceive('read')
->zeroOrMoreTimes()
->andReturn(false);
Expand All @@ -124,7 +124,7 @@ public function testPadLength()
->andReturnUsing($collection);
$os->shouldReceive('read')
->once()
->andReturn(pack('C*', rand(0, 255), rand(0, 255), rand(0, 255)));
->andReturn(pack('C*', random_int(0, 255), random_int(0, 255), random_int(0, 255)));
$os->shouldReceive('read')
->zeroOrMoreTimes()
->andReturn(false);
Expand Down
Expand Up @@ -125,6 +125,9 @@ public function crlfProvider()

/**
* @dataProvider crlfProvider
*
* @param $test
* @param $expected
*/
public function testCanonicEncodeByteStreamGeneratesCorrectCrlf($test, $expected)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/Swift/Plugins/LoggerPluginTest.php
Expand Up @@ -40,7 +40,7 @@ public function testCommandIsSentToLogger()
$logger = $this->createLogger();
$logger->expects($this->once())
->method('add')
->with($this->regExp('~foo\r\n~'));
->with(static::regExp('~foo\r\n~'));

$plugin = $this->createPlugin($logger);
$plugin->commandSent($evt);
Expand All @@ -52,7 +52,7 @@ public function testResponseIsSentToLogger()
$logger = $this->createLogger();
$logger->expects($this->once())
->method('add')
->with($this->regExp('~354 Go ahead\r\n~'));
->with(static::regExp('~354 Go ahead\r\n~'));

$plugin = $this->createPlugin($logger);
$plugin->responseReceived($evt);
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/Swift/Signers/SMimeSignerTest.php
Expand Up @@ -490,8 +490,9 @@ protected static function getBodyOfMessage($message)
/**
* Strips of the sender headers and Mime-Version.
*
* @param Swift_ByteStream_TemporaryFileByteStream $messageStream
* @param Swift_ByteStream_TemporaryFileByteStream $inputStream
* @param $content
*
* @return string
*/
protected function cleanMessage($content)
{
Expand Down
20 changes: 14 additions & 6 deletions tests/unit/Swift/Transport/AbstractSmtpTest.php
Expand Up @@ -2,6 +2,11 @@

abstract class Swift_Transport_AbstractSmtpTest extends \SwiftMailerTestCase
{
/** Abstract test method
* @param $buf
*
* @return
*/
abstract protected function getTransport($buf);

public function testStartAccepts220ServiceGreeting()
Expand Down Expand Up @@ -1251,6 +1256,9 @@ protected function createMessage()
return $this->getMockery('Swift_Mime_SimpleMessage')->shouldIgnoreMissing();
}

/**
* @param $buf
*/
protected function finishBuffer($buf)
{
$buf->shouldReceive('readLine')
Expand All @@ -1260,47 +1268,47 @@ protected function finishBuffer($buf)
$buf->shouldReceive('write')
->zeroOrMoreTimes()
->with('~^(EH|HE)LO .*?\r\n$~D')
->andReturn($x = uniqid());
->andReturn($x = uniqid('', true));
$buf->shouldReceive('readLine')
->zeroOrMoreTimes()
->with($x)
->andReturn('250 ServerName'."\r\n");
$buf->shouldReceive('write')
->zeroOrMoreTimes()
->with('~^MAIL FROM:<.*?>\r\n$~D')
->andReturn($x = uniqid());
->andReturn($x = uniqid('', true));
$buf->shouldReceive('readLine')
->zeroOrMoreTimes()
->with($x)
->andReturn("250 OK\r\n");
$buf->shouldReceive('write')
->zeroOrMoreTimes()
->with('~^RCPT TO:<.*?>\r\n$~D')
->andReturn($x = uniqid());
->andReturn($x = uniqid('', true));
$buf->shouldReceive('readLine')
->zeroOrMoreTimes()
->with($x)
->andReturn("250 OK\r\n");
$buf->shouldReceive('write')
->zeroOrMoreTimes()
->with("DATA\r\n")
->andReturn($x = uniqid());
->andReturn($x = uniqid('', true));
$buf->shouldReceive('readLine')
->zeroOrMoreTimes()
->with($x)
->andReturn("354 OK\r\n");
$buf->shouldReceive('write')
->zeroOrMoreTimes()
->with("\r\n.\r\n")
->andReturn($x = uniqid());
->andReturn($x = uniqid('', true));
$buf->shouldReceive('readLine')
->zeroOrMoreTimes()
->with($x)
->andReturn("250 OK\r\n");
$buf->shouldReceive('write')
->zeroOrMoreTimes()
->with("RSET\r\n")
->andReturn($x = uniqid());
->andReturn($x = uniqid('', true));
$buf->shouldReceive('readLine')
->zeroOrMoreTimes()
->with($x)
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/Swift/Transport/LoadBalancedTransportTest.php
Expand Up @@ -804,6 +804,11 @@ public function testTransportShowsAsNotStartedIfAllPingFails()

/**
* Adapted from Yay_Matchers_ReferenceMatcher.
*
* @param $ref1
* @param $ref2
*
* @return bool
*/
public function varsAreReferences(&$ref1, &$ref2)
{
Expand All @@ -815,7 +820,7 @@ public function varsAreReferences(&$ref1, &$ref2)
}

$copy = $ref2;
$randomString = uniqid('yay');
$randomString = uniqid('yay', true);
$ref2 = $randomString;
$isRef = ($ref1 === $ref2);
$ref2 = $copy;
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/Swift/Transport/SendmailTransportTest.php
Expand Up @@ -18,9 +18,8 @@ protected function getSendmail($buf, $dispatcher = null)
if (!$dispatcher) {
$dispatcher = $this->createEventDispatcher();
}
$sendmail = new Swift_Transport_SendmailTransport($buf, $dispatcher, 'example.org');

return $sendmail;
return new Swift_Transport_SendmailTransport($buf, $dispatcher);
}

public function testCommandCanBeSetAndFetched()
Expand Down

0 comments on commit fb4800d

Please sign in to comment.