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
Added better stubs for DateTimeImmutable
, highlighting how the constructor is **NOT** immutable
#8350
Added better stubs for DateTimeImmutable
, highlighting how the constructor is **NOT** immutable
#8350
Commits on Jul 31, 2022
-
Added better stubs for
DateTimeImmutable
, highlighting how the cons……tructor is **NOT** immutable `DateTimeImmutable` is **almost** immutable: `DateTimeImmutable::__construct()` is in fact not a pure method, since `new DateTimeImmutable('now')` produces a different value at each instantiation (by design). This change makes sure that `DateTimeImmutable` loses its `@psalm-immutable` class-level marker, preventing downstream misuse of the constructor inside otherwise referentially transparent code. Note: only pure methods are stubbed here: all other methods declared by `DateTimeImmutable` (parent interface) are NOT present here, and are either inferred from runtime reflection, or `CallMap*.php` definitions. Methods are sorted in the order defined by reflection on PHP 8.1.8, at the time of writing this ( https://3v4l.org/3TGg8 ). Following simplistic snippet was used to infer the current signature: ```php <?php $c = new \ReflectionClass(\DateTimeImmutable::class); $methods = array_map(function ($m) { return $m->getName() . '(' . implode(',', array_map(function ($p) { return $p->getType() . ' $' . $p->getName() . ($p->isOptional() ? ' = ' . var_export($p->getDefaultValue(), true) : ''); }, $m->getParameters())) . ')' . ($m->getReturnType() ? (': ' . $m->getReturnType()) : ''); }, $c->getMethods()); $properties = array_map(function ($m) { return $m->getName(); }, $c->getProperties()); var_dump($methods, $properties); ```
Configuration menu - View commit details
-
Copy full SHA for b4b2bc6 - Browse repository at this point
Copy the full SHA b4b2bc6View commit details -
Removed
@psalm-immutable
marked fromMyDate
extending `DateTimeIm……mutable` `DateTimeImmutable` is not really immutable, therefore this marker was wrong upfront
Configuration menu - View commit details
-
Copy full SHA for dcaf610 - Browse repository at this point
Copy the full SHA dcaf610View commit details -
Configuration menu - View commit details
-
Copy full SHA for c205d65 - Browse repository at this point
Copy the full SHA c205d65View commit details
Commits on Aug 1, 2022
-
s/psalm-pure/psalm-mutation-free, since psalm-mutation-free is safer …
…to use Ref: https://github.com/vimeo/psalm/pull/8350/files/c205d652d1e9afd9510db59e72c3fd0a4a093b3d#r934032422 The idea is that `@psalm-pure` disallows `$this` usage in child classes, which is not wanted, while `@psalm-mutation-free` allows it. By using `@psalm-mutation-free`, we don't completely destroy inheritance use-cases based on internal (immutable) state.
Configuration menu - View commit details
-
Copy full SHA for 68978b9 - Browse repository at this point
Copy the full SHA 68978b9View commit details
Commits on Aug 5, 2022
-
Configuration menu - View commit details
-
Copy full SHA for dc7d26a - Browse repository at this point
Copy the full SHA dc7d26aView commit details -
Configuration menu - View commit details
-
Copy full SHA for 267d760 - Browse repository at this point
Copy the full SHA 267d760View commit details -
Removed
DateTimeImmutable::createFromFormat()
from the CallMap: ful……ly covered by stub
Configuration menu - View commit details
-
Copy full SHA for 58ca4e0 - Browse repository at this point
Copy the full SHA 58ca4e0View commit details -
Removed
DateTimeImmutable::format()
from the CallMap: fully covered…… by stub Note: some conditional return type magic was required here. See: vimeo#8350 (comment)
Configuration menu - View commit details
-
Copy full SHA for 7ee12c7 - Browse repository at this point
Copy the full SHA 7ee12c7View commit details -
Removed
DateTimeImmutable::getTimezone()
from the CallMap: fully co……vered by stub Note: also verified that a `DateTimeImmutable#getTimezone()` always returns a default timezone (initialized internally), and therefore restricted the type a bit.
Configuration menu - View commit details
-
Copy full SHA for 2b6fddf - Browse repository at this point
Copy the full SHA 2b6fddfView commit details -
Configuration menu - View commit details
-
Copy full SHA for 1be04e0 - Browse repository at this point
Copy the full SHA 1be04e0View commit details -
Removed
DateTimeImmutable::getTimestamp()
from the CallMap: fully c……overed by stub This also simplifies the return type from `int|false` to always `int`, since a timestamp can always be produced. Ref: https://github.com/php/php-src/blob/eff9aed1592f59cddb12d36a55dec0ccc3bbbfd6/ext/date/php_date.stub.php#L496-L500
Configuration menu - View commit details
-
Copy full SHA for 002585b - Browse repository at this point
Copy the full SHA 002585bView commit details -
Configuration menu - View commit details
-
Copy full SHA for 18557b8 - Browse repository at this point
Copy the full SHA 18557b8View commit details -
Removed
DateTimeImmutable::modify()
from the CallMap: fully covered…… by stub Also expanded the return type from `static` to `static|false`, since the operation can fail (with a warning too), such as in following example: https://3v4l.org/Xrjlc ```php <?php var_dump( (new DateTimeImmutable()) ->modify('potato') ); ``` Produces ``` Warning: DateTimeImmutable::modify(): Failed to parse time string (potato) at position 0 (p): The timezone could not be found in the database in /in/Xrjlc on line 6 bool(false) ``` Ref: https://github.com/php/php-src/blob/534127d3b22b193ffb9511c4447584f0d2bd4e24/ext/date/php_date.stub.php#L508-L509
Configuration menu - View commit details
-
Copy full SHA for 7cd3d49 - Browse repository at this point
Copy the full SHA 7cd3d49View commit details -
Removed
DateTimeImmutable::add()
from the CallMap: fully covered by…Configuration menu - View commit details
-
Copy full SHA for cb9939c - Browse repository at this point
Copy the full SHA cb9939cView commit details -
Removed
DateTimeImmutable::setTimezone()
from the CallMap: fully co……vered by stub Also simplified the return type from `static|false` to `static`, since the method throws at all times, on failure. On PHP 7.x, it could only fail if an invalid type was passed in, which is not really valid anyway, from a type perspective. Ref (PHP 8.2.x): https://github.com/php/php-src/blob/534127d3b22b193ffb9511c4447584f0d2bd4e24/ext/date/php_date.c#L3291-L3307 Ref (PHP 8.2.x): https://github.com/php/php-src/blob/534127d3b22b193ffb9511c4447584f0d2bd4e24/ext/date/php_date.stub.php#L517-L518 Ref (PHP 7.0.33): https://github.com/php/php-src/blob/bf574c2b67a1f786e36cf679f41b758b973a82c4/ext/date/php_date.c#L3363-L3379
Configuration menu - View commit details
-
Copy full SHA for 4fe554d - Browse repository at this point
Copy the full SHA 4fe554dView commit details -
Removed
DateTimeImmutable::setTime()
from the CallMap: fully covere……d by stub Also simplified the return type from `static|false` to `static`, since the method throws at all times, on failure. On PHP 7.x, it could only fail if an invalid type was passed in, which is not really valid anyway, from a type perspective. Ref (PHP 8.1.x): https://github.com/php/php-src/blob/32d55f74229e7913db0d59ef874a401744479b6a/ext/date/php_date.c#L3212-L3228 Ref (PHP 7.0.33): https://github.com/php/php-src/blob/bf574c2b67a1f786e36cf679f41b758b973a82c4/ext/date/php_date.c#L3447-L3463
Configuration menu - View commit details
-
Copy full SHA for e61c593 - Browse repository at this point
Copy the full SHA e61c593View commit details -
Removed
DateTimeImmutable::setDate()
from the CallMap: fully covere……d by stub Also simplified the return type from `static|false` to `static`, since the method throws at all times, on failure. On PHP 7.x, it could only fail if an invalid type was passed in, which is not really valid anyway, from a type perspective. Ref (PHP 8.1.x): https://github.com/php/php-src/blob/32d55f74229e7913db0d59ef874a401744479b6a/ext/date/php_date.c#L3258-L3274 Ref (PHP 7.0.33): https://github.com/php/php-src/blob/bf574c2b67a1f786e36cf679f41b758b973a82c4/ext/date/php_date.c#L3496-L3512
Configuration menu - View commit details
-
Copy full SHA for 0a6c9d0 - Browse repository at this point
Copy the full SHA 0a6c9d0View commit details -
Removed
DateTimeImmutable::setISODate()
from the CallMap: fully cov……ered by stub Also simplified the return type from `static|false` to `static`, since the method throws at all times, on failure. On PHP 7.x, it could only fail if an invalid type was passed in, which is not really valid anyway, from a type perspective. Ref (PHP 8.1.x): https://github.com/php/php-src/blob/32d55f74229e7913db0d59ef874a401744479b6a/ext/date/php_date.c#L3308-L3324 Ref (PHP 7.0.33): https://github.com/php/php-src/blob/bf574c2b67a1f786e36cf679f41b758b973a82c4/ext/date/php_date.c#L3549-L3565
Configuration menu - View commit details
-
Copy full SHA for 964f64a - Browse repository at this point
Copy the full SHA 964f64aView commit details -
Removed
DateTimeImmutable::setTimestamp()
from the CallMap: fully c……overed by stub Also simplified the return type from `static|false` to `static`, since the method throws at all times, on failure. On PHP 7.x, it could only fail if an invalid type was passed in, which is not really valid anyway, from a type perspective. Ref (PHP 8.1.x): https://github.com/php/php-src/blob/32d55f74229e7913db0d59ef874a401744479b6a/ext/date/php_date.c#L3353-L3369 Ref (PHP 7.0.33): https://github.com/php/php-src/blob/bf574c2b67a1f786e36cf679f41b758b973a82c4/ext/date/php_date.c#L3596-L3612
Configuration menu - View commit details
-
Copy full SHA for aaac9cc - Browse repository at this point
Copy the full SHA aaac9ccView commit details -
Removed
DateTimeImmutable::createFromMutable()
from the CallMap: fu……lly covered by stub
Configuration menu - View commit details
-
Copy full SHA for a1ed84f - Browse repository at this point
Copy the full SHA a1ed84fView commit details -
Simplified
DateTimeImmutable::format()
: always returns astring
Also: * a non-empty format string will always lead to `non-empty-string` * it seems that you can throw **everything** at `DateTimeInterface#format()`, even null bytes, and it will always produce a `string`
Configuration menu - View commit details
-
Copy full SHA for 68ffae0 - Browse repository at this point
Copy the full SHA 68ffae0View commit details -
Removed
DateTimeImmutable::createFromInterface()
from stubsWhile there is value in declaring `DateTimeImmutable::createFromInterface()` as mutation-free in a stub, this method was introduced in PHP 8.0, so we cannot really declare it in a stub. For now, we drop it, as the value of its stub declaration is much lower than the problems it introduces through its conditional existence.
Configuration menu - View commit details
-
Copy full SHA for 1382877 - Browse repository at this point
Copy the full SHA 1382877View commit details