Verified Commit 37ae91a1 authored by Elias Häußler's avatar Elias Häußler 🐛
Browse files

[TASK] Apply Symfony rules in PHP-CS-Fixer and fix linting issues

parent 15a4cc21
<?php <?php
declare(strict_types=1); declare(strict_types=1);
/* /*
...@@ -21,10 +22,12 @@ declare(strict_types=1); ...@@ -21,10 +22,12 @@ declare(strict_types=1);
*/ */
$finder = \PhpCsFixer\Finder::create() $finder = \PhpCsFixer\Finder::create()
->in([__DIR__ . '/src', __DIR__ . '/tests']); ->in([__DIR__.'/src', __DIR__.'/tests']);
$config = new \PhpCsFixer\Config(); $config = new \PhpCsFixer\Config();
return $config->setRules([ return $config->setRules([
'@PSR2' => true, '@PSR2' => true,
'@Symfony' => true,
]) ])
->setFinder($finder); ->setFinder($finder);
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace EliasHaeussler\ComposerUpdateReporter; namespace EliasHaeussler\ComposerUpdateReporter;
/* /*
...@@ -28,7 +30,7 @@ use Composer\Plugin\PluginInterface; ...@@ -28,7 +30,7 @@ use Composer\Plugin\PluginInterface;
use EliasHaeussler\ComposerUpdateCheck\Event\PostUpdateCheckEvent; use EliasHaeussler\ComposerUpdateCheck\Event\PostUpdateCheckEvent;
/** /**
* Plugin * Plugin.
* *
* @author Elias Häußler <elias@haeussler.dev> * @author Elias Häußler <elias@haeussler.dev>
* @license GPL-3.0-or-later * @license GPL-3.0-or-later
...@@ -60,7 +62,7 @@ class Plugin implements PluginInterface, EventSubscriberInterface ...@@ -60,7 +62,7 @@ class Plugin implements PluginInterface, EventSubscriberInterface
{ {
return [ return [
PostUpdateCheckEvent::NAME => [ PostUpdateCheckEvent::NAME => [
['onPostUpdateCheck'] ['onPostUpdateCheck'],
], ],
]; ];
} }
......
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace EliasHaeussler\ComposerUpdateReporter; namespace EliasHaeussler\ComposerUpdateReporter;
/* /*
...@@ -36,7 +38,7 @@ use EliasHaeussler\ComposerUpdateReporter\Service\Slack; ...@@ -36,7 +38,7 @@ use EliasHaeussler\ComposerUpdateReporter\Service\Slack;
use EliasHaeussler\ComposerUpdateReporter\Service\Teams; use EliasHaeussler\ComposerUpdateReporter\Service\Teams;
/** /**
* Reporter * Reporter.
* *
* @author Elias Häußler <elias@haeussler.dev> * @author Elias Häußler <elias@haeussler.dev>
* @license GPL-3.0-or-later * @license GPL-3.0-or-later
...@@ -94,10 +96,7 @@ class Reporter ...@@ -94,10 +96,7 @@ class Reporter
/** @var ServiceInterface $registeredService */ /** @var ServiceInterface $registeredService */
foreach ($this->registeredServices as $registeredService) { foreach ($this->registeredServices as $registeredService) {
if (!in_array(ServiceInterface::class, class_implements($registeredService), true)) { if (!in_array(ServiceInterface::class, class_implements($registeredService), true)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(sprintf('Service "%s" must implement "%s".', $registeredService, ServiceInterface::class), 1600814017);
sprintf('Service "%s" must implement "%s".', $registeredService, ServiceInterface::class),
1600814017
);
} }
if ($registeredService::isEnabled($this->configuration)) { if ($registeredService::isEnabled($this->configuration)) {
$service = $registeredService::fromConfiguration($this->configuration); $service = $registeredService::fromConfiguration($this->configuration);
...@@ -106,6 +105,7 @@ class Reporter ...@@ -106,6 +105,7 @@ class Reporter
$services[] = $service; $services[] = $service;
} }
} }
return $services; return $services;
} }
...@@ -122,6 +122,7 @@ class Reporter ...@@ -122,6 +122,7 @@ class Reporter
public function setRegisteredServices(array $registeredServices): self public function setRegisteredServices(array $registeredServices): self
{ {
$this->registeredServices = $registeredServices; $this->registeredServices = $registeredServices;
return $this; return $this;
} }
......
...@@ -33,7 +33,7 @@ use Spatie\Emoji\Emoji; ...@@ -33,7 +33,7 @@ use Spatie\Emoji\Emoji;
use Spatie\Emoji\Exceptions\UnknownCharacter; use Spatie\Emoji\Exceptions\UnknownCharacter;
/** /**
* AbstractService * AbstractService.
* *
* @author Elias Häußler <elias@haeussler.dev> * @author Elias Häußler <elias@haeussler.dev>
* @license GPL-3.0-or-later * @license GPL-3.0-or-later
...@@ -53,14 +53,14 @@ abstract class AbstractService implements ServiceInterface ...@@ -53,14 +53,14 @@ abstract class AbstractService implements ServiceInterface
public static function isEnabled(array $configuration): bool public static function isEnabled(array $configuration): bool
{ {
$identifier = static::getIdentifier(); $identifier = static::getIdentifier();
$envVariable = strtoupper($identifier . '_enable'); $envVariable = strtoupper($identifier.'_enable');
$extra = $configuration[strtolower($identifier)] ?? null; $extra = $configuration[strtolower($identifier)] ?? null;
if (getenv($envVariable) !== false) { if (false !== getenv($envVariable)) {
return (bool)getenv($envVariable); return (bool) getenv($envVariable);
} }
return is_array($extra) && (bool)($extra['enable'] ?? false); return is_array($extra) && (bool) ($extra['enable'] ?? false);
} }
abstract protected static function getIdentifier(): string; abstract protected static function getIdentifier(): string;
...@@ -70,19 +70,20 @@ abstract class AbstractService implements ServiceInterface ...@@ -70,19 +70,20 @@ abstract class AbstractService implements ServiceInterface
public function report(UpdateCheckResult $result): bool public function report(UpdateCheckResult $result): bool
{ {
// Fall back to default output behavior if no custom behavior is defined // Fall back to default output behavior if no custom behavior is defined
if ($this->behavior === null) { if (null === $this->behavior) {
$this->behavior = $this->getDefaultBehavior(); $this->behavior = $this->getDefaultBehavior();
} }
$outdatedPackages = $result->getOutdatedPackages(); $outdatedPackages = $result->getOutdatedPackages();
// Do not send report if packages are up to date // Do not send report if packages are up to date
if ($outdatedPackages === []) { if ([] === $outdatedPackages) {
if (!$this->behavior->style->isJson()) { if (!$this->behavior->style->isJson()) {
$this->behavior->io->write( $this->behavior->io->write(
sprintf('%s Skipped %s report', Emoji::prohibited(), static::getName()) sprintf('%s Skipped %s report', Emoji::prohibited(), static::getName())
); );
} }
return true; return true;
} }
...@@ -93,6 +94,7 @@ abstract class AbstractService implements ServiceInterface ...@@ -93,6 +94,7 @@ abstract class AbstractService implements ServiceInterface
$this->behavior->io->writeError( $this->behavior->io->writeError(
sprintf('%s <error>Error during %s report</error>', Emoji::crossMark(), static::getName()) sprintf('%s <error>Error during %s report</error>', Emoji::crossMark(), static::getName())
); );
return false; return false;
} }
...@@ -111,21 +113,19 @@ abstract class AbstractService implements ServiceInterface ...@@ -111,21 +113,19 @@ abstract class AbstractService implements ServiceInterface
return true; return true;
} }
/**
* @param UpdateCheckResult $result
* @return bool
*/
abstract protected function sendReport(UpdateCheckResult $result): bool; abstract protected function sendReport(UpdateCheckResult $result): bool;
public function setBehavior(OutputBehavior $behavior): ServiceInterface public function setBehavior(OutputBehavior $behavior): ServiceInterface
{ {
$this->behavior = $behavior; $this->behavior = $behavior;
return $this; return $this;
} }
public function setOptions(Options $options): ServiceInterface public function setOptions(Options $options): ServiceInterface
{ {
$this->options = $options; $this->options = $options;
return $this; return $this;
} }
......
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace EliasHaeussler\ComposerUpdateReporter\Service; namespace EliasHaeussler\ComposerUpdateReporter\Service;
/* /*
...@@ -30,7 +32,7 @@ use Symfony\Component\Mailer\Transport\TransportInterface; ...@@ -30,7 +32,7 @@ use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Mime\Email as SymfonyEmail; use Symfony\Component\Mime\Email as SymfonyEmail;
/** /**
* Email * Email.
* *
* @author Elias Häußler <elias@haeussler.dev> * @author Elias Häußler <elias@haeussler.dev>
* @license GPL-3.0-or-later * @license GPL-3.0-or-later
...@@ -68,38 +70,29 @@ class Email extends AbstractService ...@@ -68,38 +70,29 @@ class Email extends AbstractService
// Parse Email DSN // Parse Email DSN
if (is_array($extra) && array_key_exists('dsn', $extra)) { if (is_array($extra) && array_key_exists('dsn', $extra)) {
$dsn = (string)$extra['dsn']; $dsn = (string) $extra['dsn'];
} elseif (getenv('EMAIL_DSN') !== false) { } elseif (false !== getenv('EMAIL_DSN')) {
$dsn = getenv('EMAIL_DSN'); $dsn = getenv('EMAIL_DSN');
} else { } else {
throw new \RuntimeException( throw new \RuntimeException('Email DSN is not defined. Define it either in composer.json or as $EMAIL_DSN.', 1601391909);
'Email DSN is not defined. Define it either in composer.json or as $EMAIL_DSN.',
1601391909
);
} }
// Parse Email receivers // Parse Email receivers
if (is_array($extra) && array_key_exists('receivers', $extra)) { if (is_array($extra) && array_key_exists('receivers', $extra)) {
$receivers = explode(',', (string)$extra['receivers']); $receivers = explode(',', (string) $extra['receivers']);
} elseif (getenv('EMAIL_RECEIVERS') !== false) { } elseif (false !== getenv('EMAIL_RECEIVERS')) {
$receivers = explode(',', getenv('EMAIL_RECEIVERS')); $receivers = explode(',', getenv('EMAIL_RECEIVERS'));
} else { } else {
throw new \RuntimeException( throw new \RuntimeException('Email receivers are not defined. Define it either in composer.json or as $EMAIL_RECEIVERS.', 1601391943);
'Email receivers are not defined. Define it either in composer.json or as $EMAIL_RECEIVERS.',
1601391943
);
} }
// Parse Email sender // Parse Email sender
if (is_array($extra) && array_key_exists('sender', $extra)) { if (is_array($extra) && array_key_exists('sender', $extra)) {
$sender = (string)$extra['sender']; $sender = (string) $extra['sender'];
} elseif (getenv('EMAIL_SENDER') !== false) { } elseif (false !== getenv('EMAIL_SENDER')) {
$sender = getenv('EMAIL_SENDER'); $sender = getenv('EMAIL_SENDER');
} else { } else {
throw new \RuntimeException( throw new \RuntimeException('Email sender is not defined. Define it either in composer.json or as $EMAIL_SENDER.', 1601391961);
'Email sender is not defined. Define it either in composer.json or as $EMAIL_SENDER.',
1601391961
);
} }
return new self($dsn, array_map('trim', array_filter($receivers)), $sender); return new self($dsn, array_map('trim', array_filter($receivers)), $sender);
...@@ -116,7 +109,8 @@ class Email extends AbstractService ...@@ -116,7 +109,8 @@ class Email extends AbstractService
} }
/** /**
* @inheritDoc * {@inheritDoc}
*
* @throws TransportExceptionInterface * @throws TransportExceptionInterface
*/ */
protected function sendReport(UpdateCheckResult $result): bool protected function sendReport(UpdateCheckResult $result): bool
...@@ -125,7 +119,7 @@ class Email extends AbstractService ...@@ -125,7 +119,7 @@ class Email extends AbstractService
// Set subject // Set subject
$count = count($outdatedPackages); $count = count($outdatedPackages);
$subject = sprintf('%d outdated package%s', $count, $count !== 1 ? 's' : ''); $subject = sprintf('%d outdated package%s', $count, 1 !== $count ? 's' : '');
// Set plain text body and html content // Set plain text body and html content
$body = $this->parsePlainBody($outdatedPackages); $body = $this->parsePlainBody($outdatedPackages);
...@@ -133,7 +127,7 @@ class Email extends AbstractService ...@@ -133,7 +127,7 @@ class Email extends AbstractService
// Send email // Send email
if (!$this->behavior->style->isJson()) { if (!$this->behavior->style->isJson()) {
$this->behavior->io->write(Emoji::rocket() . ' Sending report via Email...'); $this->behavior->io->write(Emoji::rocket().' Sending report via Email...');
} }
$email = (new SymfonyEmail()) $email = (new SymfonyEmail())
->from($this->sender) ->from($this->sender)
...@@ -143,12 +137,11 @@ class Email extends AbstractService ...@@ -143,12 +137,11 @@ class Email extends AbstractService
->html($html); ->html($html);
$sentMessage = $this->transport->send($email); $sentMessage = $this->transport->send($email);
return $sentMessage !== null; return null !== $sentMessage;
} }
/** /**
* @param OutdatedPackage[] $outdatedPackages * @param OutdatedPackage[] $outdatedPackages
* @return string
*/ */
private function parsePlainBody(array $outdatedPackages): string private function parsePlainBody(array $outdatedPackages): string
{ {
...@@ -166,39 +159,40 @@ class Email extends AbstractService ...@@ -166,39 +159,40 @@ class Email extends AbstractService
$outdatedPackage->getNewVersion() $outdatedPackage->getNewVersion()
); );
} }
return implode(PHP_EOL, $textParts); return implode(PHP_EOL, $textParts);
} }
/** /**
* @param OutdatedPackage[] $outdatedPackages * @param OutdatedPackage[] $outdatedPackages
* @return string
*/ */
private function parseHtmlBody(array $outdatedPackages): string private function parseHtmlBody(array $outdatedPackages): string
{ {
$html = []; $html = [];
$html[] = '<table>'; $html[] = '<table>';
$html[] = '<tr>'; $html[] = '<tr>';
$html[] = '<th>Package name</th>'; $html[] = '<th>Package name</th>';
$html[] = '<th>Outdated version</th>'; $html[] = '<th>Outdated version</th>';
$html[] = '<th>New version</th>'; $html[] = '<th>New version</th>';
$html[] = '</tr>'; $html[] = '</tr>';
foreach ($outdatedPackages as $outdatedPackage) { foreach ($outdatedPackages as $outdatedPackage) {
$insecure = ''; $insecure = '';
if ($outdatedPackage->isInsecure()) { if ($outdatedPackage->isInsecure()) {
$insecure = ' <strong style="color: red;">(insecure)</strong>'; $insecure = ' <strong style="color: red;">(insecure)</strong>';
} }
$html[] = '<tr>'; $html[] = '<tr>';
/** @noinspection HtmlUnknownTarget */ /* @noinspection HtmlUnknownTarget */
$html[] = sprintf( $html[] = sprintf(
'<td><a href="%s">%s</a></td>', '<td><a href="%s">%s</a></td>',
$outdatedPackage->getProviderLink(), $outdatedPackage->getProviderLink(),
$outdatedPackage->getName() $outdatedPackage->getName()
); );
$html[] = '<td>' . $outdatedPackage->getOutdatedVersion() . $insecure . '</td>'; $html[] = '<td>'.$outdatedPackage->getOutdatedVersion().$insecure.'</td>';
$html[] = '<td><strong>' . $outdatedPackage->getNewVersion() . '</strong></td>'; $html[] = '<td><strong>'.$outdatedPackage->getNewVersion().'</strong></td>';
$html[] = '</tr>'; $html[] = '</tr>';
} }
$html[] = '</table>'; $html[] = '</table>';
return implode(PHP_EOL, $html); return implode(PHP_EOL, $html);
} }
...@@ -219,25 +213,22 @@ class Email extends AbstractService ...@@ -219,25 +213,22 @@ class Email extends AbstractService
private function validateReceivers(): void private function validateReceivers(): void
{ {
if ($this->receivers === []) { if ([] === $this->receivers) {
throw new \InvalidArgumentException('Email receivers must not be empty.', 1601395103); throw new \InvalidArgumentException('Email receivers must not be empty.', 1601395103);
} }
foreach ($this->receivers as $receiver) { foreach ($this->receivers as $receiver) {
if (filter_var($receiver, FILTER_VALIDATE_EMAIL) === false) { if (false === filter_var($receiver, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException( throw new \InvalidArgumentException(sprintf('Email receiver "%s" is no valid email address.', $receiver), 1601395301);
sprintf('Email receiver "%s" is no valid email address.', $receiver),
1601395301
);
} }
} }
} }
private function validateSender(): void private function validateSender(): void
{ {
if (trim($this->sender) === '') { if ('' === trim($this->sender)) {
throw new \InvalidArgumentException('Email sender must not be empty.', 1601395109); throw new \InvalidArgumentException('Email sender must not be empty.', 1601395109);
} }
if (filter_var($this->sender, FILTER_VALIDATE_EMAIL) === false) { if (false === filter_var($this->sender, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('Email sender is no valid email address.', 1601395313); throw new \InvalidArgumentException('Email sender is no valid email address.', 1601395313);
} }
} }
......
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace EliasHaeussler\ComposerUpdateReporter\Service; namespace EliasHaeussler\ComposerUpdateReporter\Service;
/* /*
...@@ -32,7 +34,7 @@ use Spatie\Emoji\Emoji; ...@@ -32,7 +34,7 @@ use Spatie\Emoji\Emoji;
use Symfony\Component\HttpClient\Psr18Client; use Symfony\Component\HttpClient\Psr18Client;
/** /**
* GitLab * GitLab.
* *
* @author Elias Häußler <elias@haeussler.dev> * @author Elias Häußler <elias@haeussler.dev>
* @license GPL-3.0-or-later * @license GPL-3.0-or-later
...@@ -68,26 +70,20 @@ class GitLab extends AbstractService ...@@ -68,26 +70,20 @@ class GitLab extends AbstractService
// Parse GitLab URL // Parse GitLab URL
if (is_array($extra) && array_key_exists('url', $extra)) { if (is_array($extra) && array_key_exists('url', $extra)) {
$uri = new Uri((string)$extra['url']); $uri = new Uri((string) $extra['url']);
} elseif (getenv('GITLAB_URL') !== false) { } elseif (false !== getenv('GITLAB_URL')) {
$uri = new Uri(getenv('GITLAB_URL')); $uri = new Uri(getenv('GITLAB_URL'));
} else { } else {
throw new \RuntimeException( throw new \RuntimeException('GitLab URL is not defined. Define it either in composer.json or as $GITLAB_URL.', 1600852917);
'GitLab URL is not defined. Define it either in composer.json or as $GITLAB_URL.',
1600852917
);
} }
// Parse GitLab authorization key // Parse GitLab authorization key
if (is_array($extra) && array_key_exists('authKey', $extra)) { if (is_array($extra) && array_key_exists('authKey', $extra)) {
$authKey = (string)$extra['authKey']; $authKey = (string) $extra['authKey'];
} elseif (getenv('GITLAB_AUTH_KEY') !== false) { } elseif (false !== getenv('GITLAB_AUTH_KEY')) {
$authKey = getenv('GITLAB_AUTH_KEY'); $authKey = getenv('GITLAB_AUTH_KEY');
} else { } else {
throw new \RuntimeException( throw new \RuntimeException('GitLab authorization key is not defined. Define it either in composer.json or as $GITLAB_AUTH_KEY.', 1600852990);
'GitLab authorization key is not defined. Define it either in composer.json or as $GITLAB_AUTH_KEY.',
1600852990
);
} }
return new self($uri, $authKey); return new self($uri, $authKey);
...@@ -104,7 +100,8 @@ class GitLab extends AbstractService ...@@ -104,7 +100,8 @@ class GitLab extends AbstractService
} }
/** /**
* @inheritDoc * {@inheritDoc}
*
* @throws ClientExceptionInterface * @throws ClientExceptionInterface
*/ */
protected function sendReport(UpdateCheckResult $result): bool protected function sendReport(UpdateCheckResult $result): bool
...@@ -114,21 +111,20 @@ class GitLab extends AbstractService ...@@ -114,21 +111,20 @@ class GitLab extends AbstractService
// Build JSON payload // Build JSON payload
$count = count($outdatedPackages); $count = count($outdatedPackages);
$payload = array_merge([ $payload = array_merge([
'title' => sprintf('%d outdated package%s', $count, $count !== 1 ? 's' : ''), 'title' => sprintf('%d outdated package%s', $count, 1 !== $count ? 's' : ''),