Skip to content

Commit

Permalink
native_function_invocation and php_unit_test_case_static_method_calls…
Browse files Browse the repository at this point in the history
… cs rules
  • Loading branch information
massimilianobraglia committed Apr 16, 2019
1 parent dbba118 commit c365785
Show file tree
Hide file tree
Showing 29 changed files with 94 additions and 101 deletions.
2 changes: 2 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ return PhpCsFixer\Config::create()
'not_operator_with_successor_space' => true,
'ordered_imports' => true,
'declare_strict_types' => true,
'native_function_invocation' => true,
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
])
;
8 changes: 4 additions & 4 deletions lib/Converter/SwiftMailerConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class SwiftMailerConverter
public function convert(Email $email): \Swift_Message
{
$message = $this->createInstance()->setSubject($email->getSubject());
$message->setBoundary($boundary = md5(uniqid()));
$message->setBoundary($boundary = \md5(\uniqid()));

$this->addAddresses($email, $message);
$this->addParts($email, $message, $boundary);
Expand Down Expand Up @@ -87,8 +87,8 @@ protected function addAddress(\Swift_Message $email, string $type, string $addre
protected function addParts(Email $notification, \Swift_Message $email, string $boundary)
{
$parts = $notification->getParts();
if (1 === count($parts)) {
$part = reset($parts);
if (1 === \count($parts)) {
$part = \reset($parts);
$email->setBody($part->getContent(), $part->getContentType());

if ($encoder = $this->getEncoder($part)) {
Expand Down Expand Up @@ -159,7 +159,7 @@ protected function createInstance(): \Swift_Message
/**
* @param Email\Part $part
*
* @return null|\Swift_Encoder
* @return \Swift_Encoder|null
*/
private function getEncoder(Email\Part $part)
{
Expand Down
4 changes: 2 additions & 2 deletions lib/EventListener/TwigProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function processPart(Email\Part $part)
return;
}

if (method_exists($this->twig, 'load')) {
if (\method_exists($this->twig, 'load')) {
$template = $this->twig->load($part->getTemplateName());
} else {
$template = $this->twig->loadTemplate($part->getTemplateName());
Expand All @@ -56,7 +56,7 @@ protected function processPart(Email\Part $part)
$email = $part->getEmail();

if (empty($email->getSubject()) && $template->hasBlock('subject')) {
$email->setSubject(trim($template->renderBlock('subject', $part->getVars())));
$email->setSubject(\trim($template->renderBlock('subject', $part->getVars())));
}

$part->setContent($template->render($part->getVars()));
Expand Down
10 changes: 5 additions & 5 deletions lib/Handler/Email/MailgunHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(Mailgun $mailgun, string $domain, string $mailerName
*/
public function notify(NotificationInterface $notification)
{
if (! class_exists('Swift_Message')) {
if (! \class_exists('Swift_Message')) {
throw new \RuntimeException('You need to install swift mailer to use mailgun transport');
}

Expand Down Expand Up @@ -74,15 +74,15 @@ public function notify(NotificationInterface $notification)
}

if ($recipientVariables = $notification->getRecipientVariables()) {
$postData['recipient-variables'] = json_encode($recipientVariables);
$postData['recipient-variables'] = \json_encode($recipientVariables);
}

$failed = [];
$success = [];

$to = array_merge(array_values($notification->getTo()), array_values($notification->getCc()), array_values($notification->getBcc()));
$to = \array_merge(\array_values($notification->getTo()), \array_values($notification->getCc()), \array_values($notification->getBcc()));
if (! empty($to)) {
foreach (array_chunk($to, 1000) as $to_chunk) {
foreach (\array_chunk($to, 1000) as $to_chunk) {
$result = new Result('mailgun', $this->getName());

$data = $postData;
Expand All @@ -100,7 +100,7 @@ public function notify(NotificationInterface $notification)
$notification->addResult($result);
}

if (0 === count($success)) {
if (0 === \count($success)) {
throw new NotificationFailedException("Sending failed for message {$notification->getSubject()}", $failed);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Handler/Email/SendGridHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public function notify(NotificationInterface $notification)
/** @var Email $notification */
$fromEmails = $notification->getFrom();

if (count($fromEmails) > 1) {
if (\count($fromEmails) > 1) {
throw new \Exception('With sendgrid you can use one from email address');
}
$fromEmail = array_pop($fromEmails);
$fromEmail = \array_pop($fromEmails);

$from = new SendGrid\Email(null, $fromEmail);
$subject = $notification->getSubject();
Expand Down
2 changes: 1 addition & 1 deletion lib/Handler/Sms/SkebbyHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function notify(NotificationInterface $notification)
$notification->addResult($result);
}

if (count($tos) === count($failedSms)) {
if (\count($tos) === \count($failedSms)) {
throw new NotificationFailedException('All the sms failed to be send', ['failed_sms' => $failedSms]);
}
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Handler/Sms/TwilioHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct($twilio, string $name = 'default')
$this->twilio = $twilio;

if (! $twilio instanceof \Services_Twilio && ! $twilio instanceof Client) {
throw new \TypeError('Expected '.\Services_Twilio::class.' or '.Client::class.'. Got '.get_class($twilio));
throw new \TypeError('Expected '.\Services_Twilio::class.' or '.Client::class.'. Got '.\get_class($twilio));
}

parent::__construct($name);
Expand Down Expand Up @@ -68,7 +68,7 @@ public function notify(NotificationInterface $notification)
if ($this->twilio instanceof Client) {
unset($params['To']);

$params = array_combine(array_map('lcfirst', array_keys($params)), $params);
$params = \array_combine(\array_map('lcfirst', \array_keys($params)), $params);

$response = $this->twilio->messages->create($to, $params);
} else {
Expand All @@ -88,7 +88,7 @@ public function notify(NotificationInterface $notification)
'error_message' => $e->getMessage(),
];

$this->logger->debug(get_class($e).' from Twilio: '.$e->getMessage(), [
$this->logger->debug(\get_class($e).' from Twilio: '.$e->getMessage(), [
'exception' => $e,
'response_status' => $e->getStatusCode(),
]);
Expand All @@ -103,7 +103,7 @@ public function notify(NotificationInterface $notification)
'error_message' => $e->getMessage(),
];

$this->logger->debug(get_class($e).' from Twilio: '.$e->getMessage(), [
$this->logger->debug(\get_class($e).' from Twilio: '.$e->getMessage(), [
'exception' => $e,
'response_status' => $e->getStatus(),
'error_info' => $e->getInfo(),
Expand All @@ -123,7 +123,7 @@ public function notify(NotificationInterface $notification)
$notification->addResult($result);
}

if (count($tos) === count($failedSms)) {
if (\count($tos) === \count($failedSms)) {
throw new NotificationFailedException('All the sms failed to be send', ['failed_sms' => $failedSms]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
interface HandlerSelectorStrategyInterface
{
/**
* This method retrieve an handler choosen between handlers passed.
* This method retrieve an handler chosen between handlers passed.
*
* @param NotificationHandlerInterface[]
*
* @return null|NotificationHandlerInterface
* @return NotificationHandlerInterface|null
*/
public function select(array $handlers);
}
2 changes: 1 addition & 1 deletion lib/HandlerSelectorStrategy/RandStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public function select(array $handlers)
return null;
}

return $handlers[mt_rand(0, count($handlers) - 1)];
return $handlers[\mt_rand(0, \count($handlers) - 1)];
}
}
6 changes: 3 additions & 3 deletions lib/Manager/NotificationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public function notify(NotificationInterface $notification)
}

if ($this->throwIfNotNotified && ! $notified) {
$message = 'No handler has been defined for '.get_class($notification);
if (method_exists($notification, 'getConfig')) {
$message .= ' ('.json_encode($notification->getConfig()).')';
$message = 'No handler has been defined for '.\get_class($notification);
if (\method_exists($notification, 'getConfig')) {
$message .= ' ('.\json_encode($notification->getConfig()).')';
}

throw new NotificationFailedException($message);
Expand Down
8 changes: 4 additions & 4 deletions lib/Notification/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public function setText(string $text): self
* Get the html body of the mail if set
* Returns NULL if no html part is present.
*
* @return null|string
* @return string|null
*/
public function getHtml()
{
Expand All @@ -362,7 +362,7 @@ public function getHtml()
* Get the plain text body of the mail if set
* Returns NULL if no text part is present.
*
* @return null|string
* @return string|null
*/
public function getText()
{
Expand Down Expand Up @@ -521,7 +521,7 @@ public function setContentType(string $contentType): self
*/
public function getTags(): array
{
return array_values($this->tags);
return \array_values($this->tags);
}

/**
Expand All @@ -533,7 +533,7 @@ public function getTags(): array
*/
public function setTags(array $tags): self
{
$this->tags = array_combine($tags, $tags);
$this->tags = \array_combine($tags, $tags);

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Notification/Email/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public static function create(): self
public static function createFromFile(string $filename, string $contentType = null): self
{
$instance = new static();
$instance->content = file_get_contents($filename);
$instance->name = basename($filename);
$instance->content = \file_get_contents($filename);
$instance->name = \basename($filename);

if (null !== $contentType) {
$instance->contentType = $contentType;
Expand Down
4 changes: 2 additions & 2 deletions lib/Notification/Sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function setContent(string $content): self
}

/**
* @return null|string
* @return string|null
*/
public function getFrom()
{
Expand Down Expand Up @@ -171,7 +171,7 @@ public function addTo(string $to): self
*/
public function removeTo(string $to): self
{
$itemPosition = array_search($to, $this->to);
$itemPosition = \array_search($to, $this->to);

if (false !== $itemPosition) {
unset($this->to[$itemPosition]);
Expand Down
19 changes: 11 additions & 8 deletions lib/Notifire.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function addNotification(string $notificationName, string $notific
}

$notificationInterfaceName = NotificationInterface::class;
if (! is_subclass_of($notificationClass, $notificationInterfaceName)) {
if (! \is_subclass_of($notificationClass, $notificationInterfaceName)) {
$message = "Expected instance of $notificationInterfaceName, got $notificationClass";

throw new UnsupportedClassException($message);
Expand All @@ -86,8 +86,11 @@ public static function addNotification(string $notificationName, string $notific
*
* @throws UnregisteredNotificationException
*/
public static function factory(string $notificationName, string $handler = 'default', array $options = [])
{
public static function factory(
string $notificationName,
string $handler = 'default',
array $options = []
): NotificationInterface {
if (! isset(static::$notifications[$notificationName])) {
throw new UnregisteredNotificationException();
}
Expand All @@ -108,7 +111,7 @@ public static function factory(string $notificationName, string $handler = 'defa
*
* @throws UnregisteredNotificationException
*/
public static function __callStatic(string $name, array $arguments)
public static function __callStatic(string $name, array $arguments): NotificationInterface
{
if (! isset($arguments[0])) {
$arguments[0] = 'default';
Expand All @@ -118,11 +121,11 @@ public static function __callStatic(string $name, array $arguments)
$arguments[1] = [];
}

if (! is_string($arguments[0])) {
if (! \is_string($arguments[0])) {
throw new \InvalidArgumentException('Argument 1 should be a string or null');
}

if (! is_array($arguments[1])) {
if (! \is_array($arguments[1])) {
throw new \InvalidArgumentException('Argument 2 should be an array or null');
}

Expand All @@ -136,12 +139,12 @@ public static function create()
{
$builder = NotifireBuilder::create();

if (class_exists('Swift_Mailer')) {
if (\class_exists('Swift_Mailer')) {
$transport = new \Swift_SmtpTransport('localhost', 25);
$mailer = new \Swift_Mailer($transport);

$handler = new SwiftMailerHandler($mailer, 'default');
if (class_exists('Twig_Environment')) {
if (\class_exists('Twig_Environment')) {
$env = new \Twig_Environment(new \Twig_Loader_Filesystem());
$handler->setTwig($env);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Result/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function addResult(Result $result): self
public function all(): array
{
$results = [];
array_walk_recursive($this->results, function ($v) use (&$results) {
\array_walk_recursive($this->results, function ($v) use (&$results) {
$results[] = $v;
});

Expand Down
4 changes: 2 additions & 2 deletions lib/Util/Email/AddressParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public static function parse(string $address)
$regex = '/(?<address>'.$addrSpec.')|(?:(?<name>'.$phrase.')(?:'.$cfws.'?)<(?<addressalt>'.$addrSpec.')>(?:'.$cfws.'?))/';
}

if (! preg_match($regex, $address, $match)) {
if (! \preg_match($regex, $address, $match)) {
return null;
}

$addr = isset($match['addressalt']) ? $match['addressalt'] : $match['address'];

$name = $match['name'] ?? '';
$name = trim($name, " \t\n\r\0\x0B\"");
$name = \trim($name, " \t\n\r\0\x0B\"");

return [
'address' => $addr,
Expand Down
8 changes: 4 additions & 4 deletions lib/Util/Email/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static function getInstance(): self
*/
public function getDefinition(string $name): string
{
if (array_key_exists($name, self::$_grammar)) {
if (\array_key_exists($name, self::$_grammar)) {
return self::$_grammar[$name];
}

Expand Down Expand Up @@ -86,16 +86,16 @@ public function getSpecials(): array
*/
public function escapeSpecials(string $token, array $include = [], array $exclude = []): string
{
foreach (array_merge(['\\'], array_diff(self::$_specials, $exclude), $include) as $char) {
$token = str_replace($char, '\\'.$char, $token);
foreach (\array_merge(['\\'], \array_diff(self::$_specials, $exclude), $include) as $char) {
$token = \str_replace($char, '\\'.$char, $token);
}

return $token;
}

protected function init()
{
if (count(self::$_specials) > 0) {
if (\count(self::$_specials) > 0) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions tests/EventListener/TwigProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testShouldRenderTwigParts()

$this->processor->onPreNotify(new PreNotifyEvent($email));

$this->assertEquals('This is the body', $email->getPart('text/html')->getContent());
self::assertEquals('This is the body', $email->getPart('text/html')->getContent());
}

public function testShouldAlsoSetSubjectIfTemplateHasBlockSubject()
Expand All @@ -56,7 +56,7 @@ public function testShouldAlsoSetSubjectIfTemplateHasBlockSubject()

$this->processor->onPreNotify(new PreNotifyEvent($email));

$this->assertEquals('This is the body', $email->getPart('text/html')->getContent());
$this->assertEquals('This is the subject', $email->getSubject());
self::assertEquals('This is the body', $email->getPart('text/html')->getContent());
self::assertEquals('This is the subject', $email->getSubject());
}
}
Loading

0 comments on commit c365785

Please sign in to comment.