Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Component/VAlarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion lib/ITip/Broker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
33 changes: 8 additions & 25 deletions lib/Node.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Sabre\VObject;

use Sabre\Xml;
Expand Down Expand Up @@ -62,17 +64,12 @@ abstract public function serialize(): string;
/**
* 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
*/
#[\ReturnTypeWillChange]
abstract public function jsonSerialize();
abstract public function jsonSerialize(): array|string|null;

/**
* This method serializes the data into XML. This is used to create xCard or
* xCal documents.
*
* @param Xml\Writer $writer XML writer
*/
abstract public function xmlSerialize(Xml\Writer $writer): void;

Expand All @@ -93,8 +90,7 @@ public function destroy(): void
/**
* Returns the iterator for this object.
*/
#[\ReturnTypeWillChange]
public function getIterator(): ?ElementList
public function getIterator(): ElementList
{
if (!is_null($this->iterator)) {
return $this->iterator;
Expand Down Expand Up @@ -143,7 +139,6 @@ public function validate(int $options = 0): array
/**
* Returns the number of elements.
*/
#[\ReturnTypeWillChange]
public function count(): int
{
$it = $this->getIterator();
Expand All @@ -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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't look so good. The PHPdoc used to say that $offset is an int
But the parent ArrayAccess::offsetExists(mixed $offset): boolallows anything as parameter 1. So we can't writepublic function offsetExists(int $offset): bool`
So we seem to lose something here, the PHPdoc type hint goes, but we can't preserve it in the actual parameter type declaration.

{
$iterator = $this->getIterator();

Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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);
Expand Down
32 changes: 11 additions & 21 deletions lib/Parameter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Sabre\VObject;

use Sabre\Xml;
Expand Down Expand Up @@ -32,19 +34,15 @@ class Parameter extends Node implements \Stringable

/**
* Parameter value.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Parameter value.
* Parameter value.
*
* @var string|list<string>|null

*
* @var string|array|null
*/
protected $value;
protected array|string|null $value = null;

/**
* Sets up the object.
*
* It's recommended to use the create:: factory method instead.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* It's recommended to use the create:: factory method instead.
* It's recommended to use the create:: factory method instead.
*
* @param string|list<string>|null $value

*
* @param string|array|null $value
*/
public function __construct(Document $root, ?string $name, $value = null)
public function __construct(Document $root, ?string $name, array|string|null $value = null)
{
$this->root = $root;
if (is_null($name)) {
Expand All @@ -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);
}
}
}

Expand All @@ -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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* This may be either a single, or multiple strings in an array.
* This may be either a single, or multiple strings in an array.
* @param string|list<string> $value

I would keep the type just make it more correct

*
* @param string|array $value
*/
public function setValue($value): void
public function setValue(array|string $value): void
{
$this->value = $value;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -228,20 +224,15 @@ 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;
}

/**
* 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
{
Expand All @@ -261,7 +252,6 @@ public function __toString(): string
/**
* Returns the iterator for this object.
*/
#[\ReturnTypeWillChange]
public function getIterator(): ElementList
{
if (!is_null($this->iterator)) {
Expand Down
7 changes: 3 additions & 4 deletions lib/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
6 changes: 5 additions & 1 deletion tests/VObject/ITip/BrokerTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -100,6 +102,8 @@ public function process($input, $existingObject = null, $expected = false): void
return;
}

self::assertNotNull($result, 'processMessage returned null');

self::assertVObjectEqualsVObject(
$expected,
$result
Expand Down
10 changes: 6 additions & 4 deletions tests/VObject/ParameterTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Sabre\VObject;

use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -30,17 +32,17 @@ public function testModify(): void
$cal = new Component\VCalendar();

$param = new Parameter($cal, 'name', null);
$param->addValue(1);
$param->addValue('1');
Comment on lines -33 to +35

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO addValue was supposed to always expect to be passed a string.
In this PR I enforced that. But this test was passing int literals to addValue

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());
}

Expand Down
2 changes: 2 additions & 0 deletions tests/VObject/Parser/JsonTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Sabre\VObject\Parser;

use PHPUnit\Framework\TestCase;
Expand Down