From 77f9657305e89fb8bb58ccdf86ac7694e3eaaf09 Mon Sep 17 00:00:00 2001 From: Phillip Davis Date: Sat, 20 Jun 2026 21:59:46 +0930 Subject: [PATCH] chore: refactor to declare more types --- lib/Component/VAlarm.php | 2 +- lib/ITip/Broker.php | 2 +- lib/Node.php | 33 +++++++---------------------- lib/Parameter.php | 32 ++++++++++------------------ lib/Property.php | 7 +++--- tests/VObject/ITip/BrokerTester.php | 6 +++++- tests/VObject/ParameterTest.php | 10 +++++---- tests/VObject/Parser/JsonTest.php | 2 ++ 8 files changed, 37 insertions(+), 57 deletions(-) diff --git a/lib/Component/VAlarm.php b/lib/Component/VAlarm.php index 47474dfbf..0133d4bdf 100644 --- a/lib/Component/VAlarm.php +++ b/lib/Component/VAlarm.php @@ -32,7 +32,7 @@ class VAlarm extends VObject\Component public function getEffectiveTriggerTime(): \DateTimeImmutable { $trigger = $this->TRIGGER; - if (!isset($trigger['VALUE']) || ($trigger['VALUE'] && 'DURATION' === strtoupper((string) $trigger['VALUE']))) { + if (!isset($trigger['VALUE']) || ('DURATION' === strtoupper((string) $trigger['VALUE']))) { $triggerDuration = VObject\DateTimeParser::parseDuration($this->TRIGGER); $related = (isset($trigger['RELATED']) && 'END' === strtoupper($trigger['RELATED'])) ? 'END' : 'START'; diff --git a/lib/ITip/Broker.php b/lib/ITip/Broker.php index 42862cd8b..b5cc5f723 100644 --- a/lib/ITip/Broker.php +++ b/lib/ITip/Broker.php @@ -396,7 +396,7 @@ protected function processMessageReply(Message $itipMessage, ?VCalendar $existin } } - if (!$masterObject) { + if (null === $masterObject) { // No master object, we can't add new instances. return null; } diff --git a/lib/Node.php b/lib/Node.php index 47889a2ab..fb36ee81c 100644 --- a/lib/Node.php +++ b/lib/Node.php @@ -1,5 +1,7 @@ iterator)) { return $this->iterator; @@ -143,7 +139,6 @@ public function validate(int $options = 0): array /** * Returns the number of elements. */ - #[\ReturnTypeWillChange] public function count(): int { $it = $this->getIterator(); @@ -159,11 +154,8 @@ public function count(): int * Checks if an item exists through ArrayAccess. * * This method just forwards the request to the inner iterator - * - * @param int $offset */ - #[\ReturnTypeWillChange] - public function offsetExists($offset): bool + public function offsetExists(mixed $offset): bool { $iterator = $this->getIterator(); @@ -174,11 +166,8 @@ public function offsetExists($offset): bool * Gets an item through ArrayAccess. * * This method just forwards the request to the inner iterator - * - * @param int $offset */ - #[\ReturnTypeWillChange] - public function offsetGet($offset) + public function offsetGet(mixed $offset): mixed { $iterator = $this->getIterator(); @@ -189,11 +178,8 @@ public function offsetGet($offset) * Sets an item through ArrayAccess. * * This method just forwards the request to the inner iterator - * - * @param int $offset */ - #[\ReturnTypeWillChange] - public function offsetSet($offset, $value): void + public function offsetSet(mixed $offset, $value): void { $iterator = $this->getIterator(); $iterator->offsetSet($offset, $value); @@ -210,11 +196,8 @@ public function offsetSet($offset, $value): void * Sets an item through ArrayAccess. * * This method just forwards the request to the inner iterator - * - * @param int $offset */ - #[\ReturnTypeWillChange] - public function offsetUnset($offset): void + public function offsetUnset(mixed $offset): void { $iterator = $this->getIterator(); $iterator->offsetUnset($offset); diff --git a/lib/Parameter.php b/lib/Parameter.php index 64f77c187..1f3a60c17 100644 --- a/lib/Parameter.php +++ b/lib/Parameter.php @@ -1,5 +1,7 @@ root = $root; if (is_null($name)) { @@ -61,7 +59,9 @@ public function __construct(Document $root, ?string $name, $value = null) $this->noName = false; $this->name = strtoupper($value); } else { - $this->setValue($value); + if (null !== $value) { + $this->setValue($value); + } } } @@ -88,10 +88,8 @@ public static function guessParameterNameByValue(string $value): string * Updates the current value. * * This may be either a single, or multiple strings in an array. - * - * @param string|array $value */ - public function setValue($value): void + public function setValue(array|string $value): void { $this->value = $value; } @@ -140,10 +138,8 @@ public function getParts(): array * * If the argument is specified as an array, all items will be added to the * parameter value list. - * - * @param string|array $part */ - public function addValue($part): void + public function addValue(array|string $part): void { if (is_null($this->value)) { $this->value = $part; @@ -206,7 +202,7 @@ function ($out, $item) { // But we've found that iCal (7.0, shipped with OSX 10.9) // severely trips on + characters not being quoted, so we // added + as well. - if (!preg_match('#(?: [\n":;\^,\+] )#x', $item)) { + if (!preg_match('#(?: [\n":;\^,\+] )#x', (string) $item)) { return $out.$item; } // Enclosing in double-quotes, and using RFC6868 for encoding any @@ -228,11 +224,8 @@ function ($out, $item) { /** * This method returns an array, with the representation as it should be * encoded in JSON. This is used to create jCard or jCal documents. - * - * @return array|string|null */ - #[\ReturnTypeWillChange] - public function jsonSerialize() + public function jsonSerialize(): array|string|null { return $this->value; } @@ -240,8 +233,6 @@ public function jsonSerialize() /** * This method serializes the data into XML. This is used to create xCard or * xCal documents. - * - * @param Xml\Writer $writer XML writer */ public function xmlSerialize(Xml\Writer $writer): void { @@ -261,7 +252,6 @@ public function __toString(): string /** * Returns the iterator for this object. */ - #[\ReturnTypeWillChange] public function getIterator(): ElementList { if (!is_null($this->iterator)) { diff --git a/lib/Property.php b/lib/Property.php index 7432e5064..d992c40ce 100644 --- a/lib/Property.php +++ b/lib/Property.php @@ -362,17 +362,16 @@ public function __toString(): string /** * Checks if an array element exists. */ - #[\ReturnTypeWillChange] - public function offsetExists($offset): bool + public function offsetExists(mixed $offset): bool { if (is_int($offset)) { return parent::offsetExists($offset); } - $offset = strtoupper($offset); + $offset = strtoupper((string) $offset); foreach ($this->parameters as $parameter) { - if ($parameter->name == $offset) { + if ($parameter->name === $offset) { return true; } } diff --git a/tests/VObject/ITip/BrokerTester.php b/tests/VObject/ITip/BrokerTester.php index 43eb91814..392b0862c 100644 --- a/tests/VObject/ITip/BrokerTester.php +++ b/tests/VObject/ITip/BrokerTester.php @@ -53,11 +53,12 @@ public function parse($oldMessage, $newMessage, array $expected = [], string $cu * @throws NoInstancesException * @throws InvalidDataException */ - public function process($input, $existingObject = null, $expected = false): void + public function process(string $input, ?string $existingObject = null, bool|string|null $expected = false): void { $version = Version::VERSION; $vcal = Reader::read($input); + self::assertNotNull($vcal); $mainComponent = new VEvent($vcal, 'VEVENT'); foreach ($vcal->getComponents() as $nextComponent) { @@ -90,6 +91,7 @@ public function process($input, $existingObject = null, $expected = false): void $existingObject ); $existingObject = Reader::read($existingObject); + self::assertNotNull($existingObject, 'existingObject could not be read'); } $result = $broker->processMessage($message, $existingObject); @@ -100,6 +102,8 @@ public function process($input, $existingObject = null, $expected = false): void return; } + self::assertNotNull($result, 'processMessage returned null'); + self::assertVObjectEqualsVObject( $expected, $result diff --git a/tests/VObject/ParameterTest.php b/tests/VObject/ParameterTest.php index 4c9f2c774..93ba09db4 100644 --- a/tests/VObject/ParameterTest.php +++ b/tests/VObject/ParameterTest.php @@ -1,5 +1,7 @@ addValue(1); + $param->addValue('1'); self::assertEquals([1], $param->getParts()); $param->setParts([1, 2]); self::assertEquals([1, 2], $param->getParts()); - $param->addValue(3); + $param->addValue('3'); self::assertEquals([1, 2, 3], $param->getParts()); - $param->setValue(4); - $param->addValue(5); + $param->setValue('4'); + $param->addValue('5'); self::assertEquals([4, 5], $param->getParts()); } diff --git a/tests/VObject/Parser/JsonTest.php b/tests/VObject/Parser/JsonTest.php index 392cc7c11..caa631174 100644 --- a/tests/VObject/Parser/JsonTest.php +++ b/tests/VObject/Parser/JsonTest.php @@ -1,5 +1,7 @@