Skip to content

[VarDumper] Add interval caster #23357

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

Merged
merged 1 commit into from
Jul 19, 2017

Conversation

maidmaid
Copy link
Contributor

@maidmaid maidmaid commented Jul 3, 2017

Q A
Branch? 3.4
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets /
License MIT
Doc PR /

cf #22431 (comment)

@maidmaid
Copy link
Contributor Author

maidmaid commented Jul 3, 2017

First proposal:

  • the main public properties (y, m, d, h, i, s, and f on PHP 7.1) are merged in oneline interval virtual property;
  • the others public properties (invert and days) are unmodified;
  • and the public properties not officially documentated (weekday, weekday_behavior, first_last_day_of, special_type, special_amount, have_weekday_relative and have_special_relative) are removed (see the Class synopsis).
Before After


public static function castInterval(\DateInterval $i, array $a, Stub $stub, $isNested, $filter)
{
$prefix = Caster::PREFIX_VIRTUAL;
Copy link
Member

Choose a reason for hiding this comment

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

no need to define a variable, as it is used only once

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

.($i->d ? '%dd ' : '')
;

if (PHP_VERSION_ID >= 70100) {
Copy link
Member

Choose a reason for hiding this comment

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

please use \PHP_VERSION_ID, to let the version comparison be applied at compile time (not qualifying the constant requires doing it at runtime to check the constant first in the current namespace)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

\ added


public function provideIntervals()
{
$ms = function () { return PHP_VERSION_ID >= 70100 ? '.000000' : ''; };
Copy link
Member

Choose a reason for hiding this comment

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

no need to use a function. Use a simple variable, as this is purely static.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated

@maidmaid maidmaid force-pushed the datecaster-interval-2 branch from 00552e1 to fcafa3a Compare July 3, 2017 12:17
@ogizanagi
Copy link
Contributor

ogizanagi commented Jul 4, 2017

I agree with @nicolas-grekas's original comment (#22431 (comment)), so I'm not convinced by the new suggested output.
Perhaps we could just exclude non-officially documented public properties and add your interval virtual property. Then, potentially exclude public properties merged in the interval virtual properties when used with the EXCLUDE_VERBOSE filter, so it won't be shown within the form and validator data collectors for instance, but still shown when dumping around elsewhere.

Also, why showing the invert field when it's already part of the interval? According to the above suggestions, I'll hide it too when using EXCLUDE_VERBOSE.

Would it be interesting to also output it as the time_interval spec?
https://github.com/jaroslavtyc/granam-date-interval/blob/master/Granam/DateInterval/DateInterval.php#L175-L200

WDYT?

@maidmaid
Copy link
Contributor Author

maidmaid commented Jul 4, 2017

Thank you for your thoughts @ogizanagi. I would say this:

  • Why do not you like removing time properties? This deletion is in favor of interval that is a full representation of this props.
  • About invert prop, you are right.
  • About time_interval spec, I think that interval virtual prop is a good replacement and more intuitive to read (P1Y2M3DT4H5M6S vs + 1y 2m 3d 04:05:06). Moreover, it does not handle milliseconds and the negative intervals.

@ogizanagi
Copy link
Contributor

ogizanagi commented Jul 4, 2017

Why do not you like removing time properties? This deletion is in favor of interval that is a full representation of this props.

Just quoting @nicolas-grekas:

Dump should be informational about how things actually works or can be used. On a DateInterval, the listed values are actual public properties. VarDumper is about showing actual facts about objects, not alternate ones

I'm using psysh quite often, to quickly test or demonstrate things, and despite built-in introspection/doc commands like ls -al, show, doc, ... I like the usage of the var-dumper component to output everything and the fact it shows real internals, not representations. I wish it'd stay this way, but I'm not against adding some helpful representations in addition.

@nicolas-grekas
Copy link
Member

Same opinion as @ogizanagi.
Maybe display the short output when the EXCLUDE_VERBOSE flag is set (the default on psysh)?

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Jul 11, 2017
@maidmaid maidmaid force-pushed the datecaster-interval-2 branch from fcafa3a to 79e7909 Compare July 11, 2017 10:18
@maidmaid
Copy link
Contributor Author

Ok, here the new result according to your comments:

normal EXCLUDE_VERBOSE

@nicolas-grekas
Copy link
Member

"normal" should display all existing properties, even undocumented ones. Also, why keep "days" in compact mode?

@maidmaid maidmaid force-pushed the datecaster-interval-2 branch from 79e7909 to 1971d78 Compare July 11, 2017 17:06
@maidmaid
Copy link
Contributor Author

maidmaid commented Jul 11, 2017

It's updated: "normal" displays all, and only interval prop is kept in compact mode.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

with some suggestions

/**
* @dataProvider provideIntervals
*/
public function testCastInterval($interval_spec, $invert, $expected)
Copy link
Member

Choose a reason for hiding this comment

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

should be $intervalSpec

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed (in official doc, this argument is named $interval_spec).


public static function castInterval(\DateInterval $interval, array $a, Stub $stub, $isNested, $filter)
{
$i = array(Caster::PREFIX_VIRTUAL.'interval' => self::formatInterval($interval));
Copy link
Member

Choose a reason for hiding this comment

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

what about using a ConstStub here that gives the number of seconds on hover (html mode):
new ConstStub(self::formatInterval($interval), $numberOfSeconds);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Number of seconds added.

@maidmaid maidmaid force-pushed the datecaster-interval-2 branch 2 times, most recently from a123ea0 to 2a64de6 Compare July 12, 2017 10:07
@maidmaid
Copy link
Contributor Author

How to fix kind of HHVM failure?

1) Symfony\Component\VarDumper\Tests\Caster\DateCasterTest::testDumpInterval with data set #0 ('PT0S', 0, '0s', '0s')
Failed asserting that format description matches text.
--- Expected
+++ Actual
@@ @@
 DateInterval {
   interval: 0s
 }
-}

@nicolas-grekas
Copy link
Member

diff --git a/src/Symfony/Component/VarDumper/Caster/DateCaster.php b/src/Symfony/Component/VarDumper/Caster/DateCaster.php
index c9edc10..1ecd962 100644
--- a/src/Symfony/Component/VarDumper/Caster/DateCaster.php
+++ b/src/Symfony/Component/VarDumper/Caster/DateCaster.php
@@ -58,7 +58,7 @@ class DateCaster
             .($i->d ? '%dd ' : '')
         ;
 
-        if (\PHP_VERSION_ID >= 70100) {
+        if (isset($i->f)) {
             $format .= $i->h || $i->i || $i->s || $i->f ? '%H:%I:%S.%F' : '';
         } else {
             $format .= $i->h || $i->i || $i->s ? '%H:%I:%S' : '';
diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
index 9db333e..c4c1263 100644
--- a/src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
+++ b/src/Symfony/Component/VarDumper/Tests/Caster/DateCasterTest.php
@@ -97,8 +97,7 @@ EODUMP;
         $xDump = <<<EODUMP
 DateInterval {
   interval: $expected
-%a
-}
+%A}
 EODUMP;
 
         $this->assertDumpMatchesFormat($xDump, $interval);
@@ -162,7 +161,8 @@ EODUMP;
 
     public function provideIntervals()
     {
-        $ms = \PHP_VERSION_ID >= 70100 ? '.000000' : '';
+        $interval = new \DateInterval('PT0S');
+        $ms = isset($interval->f);
 
         return array(
             array('PT0S', 0, '0s', '0s'),

@maidmaid maidmaid force-pushed the datecaster-interval-2 branch from 2a64de6 to 95a51e7 Compare July 12, 2017 12:11
@maidmaid
Copy link
Contributor Author

Mmh, isset($interval->f) triggers a notice in 7.0, see https://3v4l.org/hJB5A. I use PHP_VERSION_ID?

@nicolas-grekas
Copy link
Member

but my HHVM fails when PHP_VERSION_ID is used...

@nicolas-grekas
Copy link
Member

you should report the notice as a bug I guess

@nicolas-grekas
Copy link
Member

using both PHP_VERSION_ID and the isset check should do it

@maidmaid maidmaid force-pushed the datecaster-interval-2 branch 2 times, most recently from 264e264 to ee7bcf5 Compare July 12, 2017 13:47
@maidmaid
Copy link
Contributor Author

maidmaid commented Jul 12, 2017

There was no error related with PHP_VERSION_ID previously. Tests seem fine now (failure seems unrelated).

@maidmaid
Copy link
Contributor Author

you should report the notice as a bug

https://bugs.php.net/bug.php?id=74911 ;)

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jul 12, 2017

was no error related with PHP_VERSION_ID previously

that's because the behavior of hhvm changes from version to version and Travis doesn't have the latest. But trust me, this check is not enough, you need the isset one also :)

@nicolas-grekas
Copy link
Member

(or install hhvm to see by yourself ;) )

@maidmaid maidmaid force-pushed the datecaster-interval-2 branch from ee7bcf5 to a73522c Compare July 12, 2017 18:01
@maidmaid
Copy link
Contributor Author

maidmaid commented Jul 12, 2017

Updated: used PHP_VERSION_ID + the isset check.

@maidmaid
Copy link
Contributor Author

Time to merge? :)

@nicolas-grekas
Copy link
Member

Thank you @maidmaid.

@nicolas-grekas nicolas-grekas merged commit a73522c into symfony:3.4 Jul 19, 2017
nicolas-grekas added a commit that referenced this pull request Jul 19, 2017
This PR was merged into the 3.4 branch.

Discussion
----------

[VarDumper] Add interval caster

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | /
| License       | MIT
| Doc PR        | /

 cf #22431 (comment)

Commits
-------

a73522c Add interval caster
nicolas-grekas added a commit that referenced this pull request Jul 21, 2017
…interval caster (maidmaid)

This PR was merged into the 4.0-dev branch.

Discussion
----------

[VarDumper] Remove low PHP version and hhvm compat in interval caster

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #23357 (comment)
| License       | MIT
| Doc PR        | /

Commits
-------

ccca65b Remove hhvm compat
This was referenced Oct 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants