From 621d34487988ee886deb3353f7adaedbb2c64f1e Mon Sep 17 00:00:00 2001 From: Jeroen van Oort Date: Tue, 7 Jan 2020 14:09:21 +0100 Subject: [PATCH 1/3] Fixed phpstan level 1 errors --- lib/Property/ICalendar/DateTime.php | 16 ++++++++-------- lib/Recur/RRuleIterator.php | 7 +++++++ phpstan.neon | 4 +++- tests/VObject/CliTest.php | 10 ++++++++-- tests/VObject/ComponentTest.php | 1 + tests/VObject/ElementListTest.php | 1 + tests/VObject/ITip/BrokerTester.php | 1 + .../Recur/EventIterator/ByMonthInDailyTest.php | 1 + .../Recur/EventIterator/BySetPosHangTest.php | 1 + tests/bootstrap.php | 8 -------- 10 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/Property/ICalendar/DateTime.php b/lib/Property/ICalendar/DateTime.php index a0d1b38d4..66d7a122f 100644 --- a/lib/Property/ICalendar/DateTime.php +++ b/lib/Property/ICalendar/DateTime.php @@ -338,8 +338,8 @@ public function validate($options = 0) $messages = parent::validate($options); $valueType = $this->getValueType(); $values = $this->getParts(); - try { - foreach ($values as $value) { + foreach ($values as $value) { + try { switch ($valueType) { case 'DATE': DateTimeParser::parseDate($value); @@ -348,13 +348,13 @@ public function validate($options = 0) DateTimeParser::parseDateTime($value); break; } + } catch (InvalidDataException $e) { + $messages[] = [ + 'level' => 3, + 'message' => 'The supplied value ('.$value.') is not a correct '.$valueType, + 'node' => $this, + ]; } - } catch (InvalidDataException $e) { - $messages[] = [ - 'level' => 3, - 'message' => 'The supplied value ('.$value.') is not a correct '.$valueType, - 'node' => $this, - ]; } return $messages; diff --git a/lib/Recur/RRuleIterator.php b/lib/Recur/RRuleIterator.php index dd07310e5..55581e9ac 100644 --- a/lib/Recur/RRuleIterator.php +++ b/lib/Recur/RRuleIterator.php @@ -322,14 +322,17 @@ protected function nextDaily() return; } + $recurrenceHours = []; if (!empty($this->byHour)) { $recurrenceHours = $this->getHours(); } + $recurrenceDays = []; if (!empty($this->byDay)) { $recurrenceDays = $this->getDays(); } + $recurrenceMonths = []; if (!empty($this->byMonth)) { $recurrenceMonths = $this->getMonths(); } @@ -372,10 +375,12 @@ protected function nextWeekly() return; } + $recurrenceHours = []; if ($this->byHour) { $recurrenceHours = $this->getHours(); } + $recurrenceDays = []; if ($this->byDay) { $recurrenceDays = $this->getDays(); } @@ -436,6 +441,7 @@ protected function nextMonthly() return; } + $occurrence = -1; while (true) { $occurrences = $this->getMonthlyOccurrences(); @@ -605,6 +611,7 @@ protected function nextYearly() // If we got a byDay or getMonthDay filter, we must first expand // further. if ($this->byDay || $this->byMonthDay) { + $occurrence = -1; while (true) { $occurrences = $this->getMonthlyOccurrences(); diff --git a/phpstan.neon b/phpstan.neon index 5335bc651..c705178c9 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,2 +1,4 @@ parameters: - level: 0 + level: 1 + universalObjectCratesClasses: + - \Sabre\VObject\Component diff --git a/tests/VObject/CliTest.php b/tests/VObject/CliTest.php index 2fa0162e4..5736f9a01 100644 --- a/tests/VObject/CliTest.php +++ b/tests/VObject/CliTest.php @@ -14,8 +14,14 @@ class CliTest extends TestCase /** @var CliMock */ private $cli; + private $sabreTempDir = __DIR__.'/../temp/'; + public function setUp(): void { + if (!file_exists($this->sabreTempDir)) { + mkdir($this->sabreTempDir); + } + $this->cli = new CliMock(); $this->cli->stderr = fopen('php://memory', 'r+'); $this->cli->stdout = fopen('php://memory', 'r+'); @@ -269,7 +275,7 @@ public function testConvertMimeDir() public function testConvertDefaultFormats() { - $outputFile = SABRE_TEMPDIR.'bar.json'; + $outputFile = $this->sabreTempDir.'bar.json'; $this->assertEquals( 2, @@ -282,7 +288,7 @@ public function testConvertDefaultFormats() public function testConvertDefaultFormats2() { - $outputFile = SABRE_TEMPDIR.'bar.ics'; + $outputFile = $this->sabreTempDir.'bar.ics'; $this->assertEquals( 2, diff --git a/tests/VObject/ComponentTest.php b/tests/VObject/ComponentTest.php index b1e73c634..6026aaa54 100644 --- a/tests/VObject/ComponentTest.php +++ b/tests/VObject/ComponentTest.php @@ -19,6 +19,7 @@ public function testIterate() $comp->add($sub); $count = 0; + $key = null; foreach ($comp->children() as $key => $subcomponent) { ++$count; $this->assertInstanceOf('Sabre\\VObject\\Component', $subcomponent); diff --git a/tests/VObject/ElementListTest.php b/tests/VObject/ElementListTest.php index 1842ca963..2046ae4ca 100644 --- a/tests/VObject/ElementListTest.php +++ b/tests/VObject/ElementListTest.php @@ -20,6 +20,7 @@ public function testIterate() $elemList = new ElementList($elems); $count = 0; + $key = null; foreach ($elemList as $key => $subcomponent) { ++$count; $this->assertInstanceOf('Sabre\\VObject\\Component', $subcomponent); diff --git a/tests/VObject/ITip/BrokerTester.php b/tests/VObject/ITip/BrokerTester.php index 350e37e23..9e9956e03 100644 --- a/tests/VObject/ITip/BrokerTester.php +++ b/tests/VObject/ITip/BrokerTester.php @@ -45,6 +45,7 @@ public function process($input, $existingObject = null, $expected = false) $vcal = Reader::read($input); + $mainComponent = new \Sabre\VObject\Component\VEvent($vcal, 'VEVENT'); foreach ($vcal->getComponents() as $mainComponent) { if ('VEVENT' === $mainComponent->name) { break; diff --git a/tests/VObject/Recur/EventIterator/ByMonthInDailyTest.php b/tests/VObject/Recur/EventIterator/ByMonthInDailyTest.php index 71858c36f..3448032e8 100644 --- a/tests/VObject/Recur/EventIterator/ByMonthInDailyTest.php +++ b/tests/VObject/Recur/EventIterator/ByMonthInDailyTest.php @@ -40,6 +40,7 @@ public function testExpand() $vcal = $vcal->expand(new DateTime('2013-09-28'), new DateTime('2014-09-11')); + $dates = []; foreach ($vcal->VEVENT as $event) { $dates[] = $event->DTSTART->getValue(); } diff --git a/tests/VObject/Recur/EventIterator/BySetPosHangTest.php b/tests/VObject/Recur/EventIterator/BySetPosHangTest.php index bd00cb52a..caf6b0dc3 100644 --- a/tests/VObject/Recur/EventIterator/BySetPosHangTest.php +++ b/tests/VObject/Recur/EventIterator/BySetPosHangTest.php @@ -35,6 +35,7 @@ public function testExpand() $vcal = $vcal->expand(new DateTime('2015-01-01'), new DateTime('2016-01-01')); + $dates = []; foreach ($vcal->VEVENT as $event) { $dates[] = $event->DTSTART->getValue(); } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 590b25080..2496aa4ff 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -13,11 +13,3 @@ break; } } - -if (!defined('SABRE_TEMPDIR')) { - define('SABRE_TEMPDIR', __DIR__.'/temp/'); -} - -if (!file_exists(SABRE_TEMPDIR)) { - mkdir(SABRE_TEMPDIR); -} From c476d933e1ee16b1b859f1b6fe8aa2523880b3c1 Mon Sep 17 00:00:00 2001 From: Jeroen van Oort Date: Wed, 8 Jan 2020 09:06:05 +0100 Subject: [PATCH 2/3] Make sure there is no logic change --- lib/Property/ICalendar/DateTime.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Property/ICalendar/DateTime.php b/lib/Property/ICalendar/DateTime.php index 66d7a122f..f2dbdeba3 100644 --- a/lib/Property/ICalendar/DateTime.php +++ b/lib/Property/ICalendar/DateTime.php @@ -354,6 +354,7 @@ public function validate($options = 0) 'message' => 'The supplied value ('.$value.') is not a correct '.$valueType, 'node' => $this, ]; + break; } } From 36d00ee5642ecf97a8cbe2a890c183aeaf3dfbe6 Mon Sep 17 00:00:00 2001 From: Jeroen van Oort Date: Wed, 8 Jan 2020 09:19:35 +0100 Subject: [PATCH 3/3] Prevent setting foreach key beforehand --- tests/VObject/ComponentTest.php | 6 ++++-- tests/VObject/ElementListTest.php | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/VObject/ComponentTest.php b/tests/VObject/ComponentTest.php index 6026aaa54..4979592a0 100644 --- a/tests/VObject/ComponentTest.php +++ b/tests/VObject/ComponentTest.php @@ -19,13 +19,15 @@ public function testIterate() $comp->add($sub); $count = 0; - $key = null; foreach ($comp->children() as $key => $subcomponent) { ++$count; $this->assertInstanceOf('Sabre\\VObject\\Component', $subcomponent); + + if (2 === $count) { + $this->assertEquals(1, $key); + } } $this->assertEquals(2, $count); - $this->assertEquals(1, $key); } public function testMagicGet() diff --git a/tests/VObject/ElementListTest.php b/tests/VObject/ElementListTest.php index 2046ae4ca..4b1ce999f 100644 --- a/tests/VObject/ElementListTest.php +++ b/tests/VObject/ElementListTest.php @@ -20,12 +20,14 @@ public function testIterate() $elemList = new ElementList($elems); $count = 0; - $key = null; foreach ($elemList as $key => $subcomponent) { ++$count; $this->assertInstanceOf('Sabre\\VObject\\Component', $subcomponent); + + if (3 === $count) { + $this->assertEquals(2, $key); + } } $this->assertEquals(3, $count); - $this->assertEquals(2, $key); } }