From 3d85ca7e6d73fd15567a1f8d152134342ac43574 Mon Sep 17 00:00:00 2001 From: tlallement Date: Wed, 20 Sep 2023 13:51:31 +0200 Subject: [PATCH] Modernization of code (update dependencies & types) --- composer.json | 13 ++- src/Component/Calendar.php | 36 ++---- src/DependencyInjection/Configuration.php | 12 +- src/DependencyInjection/WelpIcalExtension.php | 9 +- src/Factory/Factory.php | 110 +++++------------- src/Mailer/CalendarAttachment.php | 12 +- src/Response/CalendarResponse.php | 31 +---- src/Tests/CalendarTestCase.php | 3 +- src/Tests/Factory/FactoryTest.php | 2 +- src/Tests/Mailer/CalendarAttachmentTest.php | 2 +- src/Tests/Response/CalendarResponseTest.php | 4 +- src/WelpIcalBundle.php | 2 +- 12 files changed, 61 insertions(+), 175 deletions(-) diff --git a/composer.json b/composer.json index bfe0025..c336e4a 100644 --- a/composer.json +++ b/composer.json @@ -25,13 +25,16 @@ } }, "require": { - "php": ">=5.6", - "jsvrcek/ics": "^0.4" + "php": ">=7.4", + "symfony/config": "~5.4|~6.0", + "symfony/dependency-injection": "~5.4|~6.0", + "symfony/http-foundation": "~5.4|~6.0", + "symfony/http-kernel": "~5.4|~6.0", + "symfony/mime": "~5.4|~6.0", + "jsvrcek/ics": ">=0.4" }, "require-dev": { - "phpunit/phpunit": "~5", - "swiftmailer/swiftmailer": "@stable", - "symfony/symfony": "~2.7 | ~3.0" + "phpunit/phpunit": "~9" }, "config": { "bin-dir": "bin" diff --git a/src/Component/Calendar.php b/src/Component/Calendar.php index 3d37db1..943bf47 100644 --- a/src/Component/Calendar.php +++ b/src/Component/Calendar.php @@ -16,25 +16,15 @@ */ class Calendar extends vCalendar { - /** - * String $filename - */ - private $filename = 'calendar.ics'; + private string $filename = 'calendar.ics'; - /** - * Calendar contentType - * @return String calendar contentType - */ - public function getContentType(){ + public function getContentType(): string + { return 'text/calendar'; } - /** - * Export - * @param Boolean $doImmediateOutput = false - * @return String .ics formatted text - */ - public function export($doImmediateOutput = false){ + public function export(bool $doImmediateOutput = false): string + { //setup exporter $calendarExport = new CalendarExport(new CalendarStream, new Formatter()); $calendarExport->addCalendar($this); @@ -46,27 +36,15 @@ public function export($doImmediateOutput = false){ return $calendarExport->getStream(); } - /** - * Set filename - * - * @param String $filename - * @return Calendar - */ - public function setFilename($filename) + public function setFilename(string $filename): Calendar { $this->filename = $filename; return $this; } - /** - * Get filename - * - * @return String - */ - public function getFilename() + public function getFilename(): string { return $this->filename; } - } diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 41c09cb..4c897a5 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -12,18 +12,10 @@ */ class Configuration implements ConfigurationInterface { - /** - * {@inheritdoc} - */ - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('welp_ical'); - if (\method_exists($treeBuilder, 'getRootNode')) { - $rootNode = $treeBuilder->getRootNode(); - } else { - // BC layer for symfony/config 4.1 and older - $rootNode = $treeBuilder->root('stof_doctrine_extensions'); - } + $rootNode = $treeBuilder->getRootNode(); // Here you should define the parameters that are allowed to // configure your bundle. See the documentation linked above for diff --git a/src/DependencyInjection/WelpIcalExtension.php b/src/DependencyInjection/WelpIcalExtension.php index fd73d2c..583e9a8 100644 --- a/src/DependencyInjection/WelpIcalExtension.php +++ b/src/DependencyInjection/WelpIcalExtension.php @@ -14,10 +14,7 @@ */ class WelpIcalExtension extends Extension { - /** - * {@inheritdoc} - */ - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); @@ -32,10 +29,8 @@ public function load(array $configs, ContainerBuilder $container) /** * Get alias - * - * @return string */ - public function getAlias() + public function getAlias(): string { return 'welp_ical'; } diff --git a/src/Factory/Factory.php b/src/Factory/Factory.php index 8e89dcf..12e2365 100644 --- a/src/Factory/Factory.php +++ b/src/Factory/Factory.php @@ -3,23 +3,17 @@ namespace Welp\IcalBundle\Factory; use Welp\IcalBundle\Component\Calendar; - use Jsvrcek\ICS\Model\CalendarEvent; use Jsvrcek\ICS\Model\CalendarAlarm; use Jsvrcek\ICS\Model\CalendarFreeBusy; use Jsvrcek\ICS\Model\CalendarTodo; - use Jsvrcek\ICS\Model\Relationship\Attendee; use Jsvrcek\ICS\Model\Relationship\Organizer; - use Jsvrcek\ICS\Model\Description\Geo; use Jsvrcek\ICS\Model\Description\Location; - use Jsvrcek\ICS\Model\Recurrence\RecurrenceRule; - use Jsvrcek\ICS\Utility\Formatter; -use Jsvrcek\ICS\CalendarStream; -use Jsvrcek\ICS\CalendarExport; + /** * Calendar Factory * @@ -28,30 +22,22 @@ */ class Factory { - /** - * @var string - */ - protected $timezone; + protected ?\DateTimeZone $timezone = null; - /** - * @var string - */ - protected $prodid; + protected ?string $prodid = null; /** * Create new calendar - * - * @return Calendar */ - public function createCalendar() + public function createCalendar(): Calendar { $calendar = new Calendar(); - if (!is_null($this->timezone)) { + if ($this->timezone !== null) { $calendar->setTimezone($this->timezone); } - if (!is_null($this->prodid)) { + if ($this->prodid !== null) { $calendar->setProdId($this->prodid); } @@ -60,128 +46,90 @@ public function createCalendar() /** * Create new CalendarEvent - * - * @return CalendarEvent */ - public function createCalendarEvent() + public function createCalendarEvent(): CalendarEvent { - $calendarEvent = new CalendarEvent(); - - return $calendarEvent; + return new CalendarEvent(); } /** * Create new CalendarAlarm - * - * @return CalendarAlarm */ - public function createCalendarAlarm() + public function createCalendarAlarm(): CalendarAlarm { - $calendarAlarm = new CalendarAlarm(); - - return $calendarAlarm; + return new CalendarAlarm(); } /** * Create new CalendarFreeBusy - * - * @return CalendarFreeBusy */ - public function createCalendarFreeBusy() + public function createCalendarFreeBusy(): CalendarFreeBusy { - $calendarFreeBusy = new CalendarFreeBusy(); - - return $calendarFreeBusy; + return new CalendarFreeBusy(); } /** * Create new CalendarTodo - * - * @return CalendarTodo */ - public function createCalendarTodo() + public function createCalendarTodo(): CalendarTodo { - $calendarTodo = new CalendarTodo(); - - return $calendarTodo; + return new CalendarTodo(); } /** * Create new Attendee - * - * @return Attendee */ - public function createAttendee() + public function createAttendee(): Attendee { - $attendee = new Attendee(new Formatter()); - - return $attendee; + return new Attendee(new Formatter()); } /** * Create new Organizer - * - * @return Organizer */ - public function createOrganizer() + public function createOrganizer(): Organizer { - $organizer = new Organizer(new Formatter()); - - return $organizer; + return new Organizer(new Formatter()); } /** * Create new Geo - * - * @return Geo */ - public function createGeo() + public function createGeo(): Geo { - $geo = new Geo(); - - return $geo; + return new Geo(); } /** * Create new Location - * - * @return Location */ - public function createLocation() + public function createLocation(): Location { - $location = new Location(); - - return $location; + return new Location(); } /** * Create new RecurrenceRule - * - * @return RecurrenceRule */ - public function createRecurrenceRule() + public function createRecurrenceRule(): RecurrenceRule { - $recurrenceRule = new RecurrenceRule(new Formatter()); - - return $recurrenceRule; + return new RecurrenceRule(new Formatter()); } /** * Set default timezone for calendars - * - * @param string $timezone */ - public function setTimezone($timezone) + public function setTimezone(?string $timezone) { - $this->timezone = new \DateTimeZone($timezone); + if ($timezone !== null) { + $this->timezone = new \DateTimeZone($timezone); + } } /** * Set default prodid for calendars - * - * @param string $prodid */ - public function setProdid($prodid) + public function setProdid(?string $prodid) { $this->prodid = $prodid; } diff --git a/src/Mailer/CalendarAttachment.php b/src/Mailer/CalendarAttachment.php index cfac51d..35eb45b 100644 --- a/src/Mailer/CalendarAttachment.php +++ b/src/Mailer/CalendarAttachment.php @@ -2,25 +2,17 @@ namespace Welp\IcalBundle\Mailer; +use Symfony\Component\Mime\Part\DataPart; use Welp\IcalBundle\Component\Calendar; -use Jsvrcek\ICS\Utility\Formatter; -use Jsvrcek\ICS\CalendarStream; -use Jsvrcek\ICS\CalendarExport; - /** * Calendar attachment for Swift mailer messages * * @package Welp\IcalBundle\Mailer * @author Titouan BENOIT */ -class CalendarAttachment extends \Swift_Attachment +class CalendarAttachment extends DataPart { - /** - * Calendar attachment constructor - * - * @param Calendar $calendar - */ public function __construct(Calendar $calendar) { $data = $calendar->export(); diff --git a/src/Response/CalendarResponse.php b/src/Response/CalendarResponse.php index fdec274..ada3ae0 100644 --- a/src/Response/CalendarResponse.php +++ b/src/Response/CalendarResponse.php @@ -5,10 +5,6 @@ use Welp\IcalBundle\Component\Calendar; use Symfony\Component\HttpFoundation\Response; -use Jsvrcek\ICS\Utility\Formatter; -use Jsvrcek\ICS\CalendarStream; -use Jsvrcek\ICS\CalendarExport; - /** * Represents a HTTP response for a calendar file download * @@ -17,22 +13,9 @@ */ class CalendarResponse extends Response { - /** - * Calendar - * - * @var Calendar - */ - protected $calendar; - - - /** - * Construct calendar response - * - * @param Calendar $calendar Calendar - * @param int $status Response status - * @param array $headers Response headers - */ - public function __construct(Calendar $calendar, $status = 200, $headers = array()) + protected Calendar $calendar; + + public function __construct(Calendar $calendar, int $status = 200, array $headers = array()) { $this->calendar = $calendar; @@ -42,13 +25,7 @@ public function __construct(Calendar $calendar, $status = 200, $headers = array( parent::__construct($content, $status, $headers); } - - /** - * Get default response headers for a calendar - * - * @return array - */ - protected function getDefaultHeaders() + protected function getDefaultHeaders(): array { $headers = array(); diff --git a/src/Tests/CalendarTestCase.php b/src/Tests/CalendarTestCase.php index da181cb..14c8d51 100644 --- a/src/Tests/CalendarTestCase.php +++ b/src/Tests/CalendarTestCase.php @@ -2,6 +2,7 @@ namespace Welp\IcalBundle\Tests; +use PHPUnit\Framework\TestCase; use Welp\IcalBundle\Component\Calendar; /** @@ -10,7 +11,7 @@ * @package Welp\IcalBundle\Tests * @author Titouan BENOIT */ -abstract class CalendarTestCase extends \PHPUnit_Framework_TestCase +abstract class CalendarTestCase extends TestCase { /** diff --git a/src/Tests/Factory/FactoryTest.php b/src/Tests/Factory/FactoryTest.php index ecc3d04..53df202 100644 --- a/src/Tests/Factory/FactoryTest.php +++ b/src/Tests/Factory/FactoryTest.php @@ -24,7 +24,7 @@ class FactoryTest extends CalendarTestCase /** * Set up tests */ - public function setUp() + public function setUp(): void { $this->factory = new Factory(); } diff --git a/src/Tests/Mailer/CalendarAttachmentTest.php b/src/Tests/Mailer/CalendarAttachmentTest.php index 739962c..713b205 100644 --- a/src/Tests/Mailer/CalendarAttachmentTest.php +++ b/src/Tests/Mailer/CalendarAttachmentTest.php @@ -23,7 +23,7 @@ public function testCalendarAttachment() $calendar = new Calendar(); $attachment = new CalendarAttachment($calendar); - $this->assertInstanceOf('Swift_Attachment', $attachment); + $this->assertInstanceOf('Symfony\Component\Mime\Part\DataPart', $attachment); $this->assertInstanceOf('Welp\IcalBundle\Mailer\CalendarAttachment', $attachment); $this->assertEquals($calendar->export(), $attachment->getBody()); diff --git a/src/Tests/Response/CalendarResponseTest.php b/src/Tests/Response/CalendarResponseTest.php index dd5a45e..df5825f 100644 --- a/src/Tests/Response/CalendarResponseTest.php +++ b/src/Tests/Response/CalendarResponseTest.php @@ -29,7 +29,7 @@ public function testCalendarResponse() $this->assertEquals($calendar->export(), $response->getContent()); - $this->assertContains($calendar->getContentType()."; charset=utf-8", $response->headers->get('Content-Type')); - $this->assertContains($calendar->getFilename(), $response->headers->get('Content-Disposition')); + $this->assertStringContainsString($calendar->getContentType()."; charset=utf-8", $response->headers->get('Content-Type')); + $this->assertStringContainsString($calendar->getFilename(), $response->headers->get('Content-Disposition')); } } diff --git a/src/WelpIcalBundle.php b/src/WelpIcalBundle.php index f932c2e..d7ff4c7 100644 --- a/src/WelpIcalBundle.php +++ b/src/WelpIcalBundle.php @@ -13,7 +13,7 @@ class WelpIcalBundle extends Bundle * * @return ExtensionInterface */ - public function getContainerExtension() + public function getContainerExtension(): ?ExtensionInterface { if (!$this->extension instanceof ExtensionInterface) { $this->extension = new WelpIcalExtension();