Skip to content

Added missing obj methods to DateTimeInterface #13491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
31 changes: 31 additions & 0 deletions ext/date/php_date.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,43 @@ interface DateTimeInterface
/** @tentative-return-type */
public function format(string $format): string;

/** @tentative-return-type */
public function modify(string $modifier): DateTimeInterface|false;
Copy link
Member

@kocsismate kocsismate Feb 29, 2024

Choose a reason for hiding this comment

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

IMO adding any mutator methods to the interface is not applicable now, because the two implementations have different behavior regarding mutability. In practice, this means that users of the DateTimeInterface should deal with the possibility that the interface implementation is either mutable or immutable.

function setTime(DateTimeInterface $d) {
    $d1 = clone $d;                     // cloning is needed as a defensive measure
    $d1->modify("+1 days");

    return $d1;
}

Copy link
Contributor Author

@marc-mabe marc-mabe Mar 6, 2024

Choose a reason for hiding this comment

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

Yes, the behavior is different, but this kind of difference is not possible to be defined in method signatures.

Actually your example should be

function setTime(DateTimeInterface $d) {
    $new = clone $d;
    $new = $new->modify("+1 days");

    return $new;
}

Copy link

@ghost ghost Mar 8, 2024

Choose a reason for hiding this comment

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

I agree mutator methods should not be added. When one works with an interface instance it's impossible to know whether it's a mutable or immutable instance. If PHPStan, IDE etc. need some more information then we can write stub files for them. I would leave it as is (hoping this interface will be removed together with mutable class).



/** @tentative-return-type */
public function add(DateInterval $interval): DateTimeInterface;

/** @tentative-return-type */
public function sub(DateInterval $interval): DateTimeInterface;

/** @tentative-return-type */
public function getTimezone(): DateTimeZone|false;

/** @tentative-return-type */
public function setTimezone(DateTimeZone $timezone): DateTimeInterface;

/** @tentative-return-type */
public function getOffset(): int;

/** @tentative-return-type */
public function getMicroseconds(): int; // TODO: Rename https://github.com/php/php-src/pull/13486
Copy link
Member

@kocsismate kocsismate Feb 29, 2024

Choose a reason for hiding this comment

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

This one method is definitely missing from the interface right now, so maybe this one could be committed separately without any further discussion.


/** @tentative-return-type */
public function setMicroseconds(int $microseconds): static; // TODO: Rename https://github.com/php/php-src/pull/13486

/** @tentative-return-type */
public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): DateTimeInterface;

/** @tentative-return-type */
public function setDate(int $year, int $month, int $day): DateTimeInterface;

/** @tentative-return-type */
public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTimeInterface;

/** @tentative-return-type */
public function setTimestamp(int $timestamp): DateTimeInterface;

/** @tentative-return-type */
public function getTimestamp(): int;

Expand Down
61 changes: 56 additions & 5 deletions ext/date/php_date_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.