Skip to content
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

Rule/date time diff #1463

Closed
wants to merge 9 commits into from

Conversation

gvieiragoulart
Copy link
Contributor

Resolves #1462


What?

This PR introduces a new validation rule, DateTimeDiff wich validates the difference of date/time against a specific rule.

The $type argument on our construct should follow PHP's DateInterval properties. So we can use types such as year, months,days,full days,hours,minutes,seconds,microseconds.


Note: This is my first PR in a relatively large open-source project, so if I've done anything incorrectly, please let me know. 👍

@gvieiragoulart gvieiragoulart mentioned this pull request Jul 14, 2024
Comment on lines 139 to 152
private function getTranslatedType(string $type): string
{
return match ($type) {
'y' => 'years',
'm' => 'months',
'd' => 'days',
'days' => 'full days',
'h' => 'hours',
'i' => 'minutes',
's' => 'seconds',
'f' => 'microseconds',
};
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we are going to use this, I believe that we can improve to a Enum file. I didn't create because the project don't have any

Comment on lines +70 to +73
private function isDateIntervalType(string $age): bool
{
return in_array($age, array_keys(get_object_vars((new DateInterval('P1Y')))));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know this seems weird, but i wanted to use those DateInterval vars. what do you guys think about it? How do we can improve this validation?

@alganet
Copy link
Member

alganet commented Jul 15, 2024

@gvieiragoulart I approved the CI run. There are some static analysis checks you'll need to fix before we can merge this!

I'll keep an eye here to approve the CI run again as soon as you fix them, and once it passes, I'll do a full review!

Copy link

codecov bot commented Jul 15, 2024

Codecov Report

Attention: Patch coverage is 94.11765% with 5 lines in your changes missing coverage. Please review.

Project coverage is 97.28%. Comparing base (d7dc0f2) to head (906f722).
Report is 3 commits behind head on main.

Files Patch % Lines
library/Rules/DateTimeDiff.php 93.22% 4 Missing ⚠️
library/Helpers/CanExtractRules.php 93.75% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1463      +/-   ##
============================================
+ Coverage     96.20%   97.28%   +1.07%     
- Complexity      917      950      +33     
============================================
  Files           199      200       +1     
  Lines          2109     2206      +97     
============================================
+ Hits           2029     2146     +117     
+ Misses           80       60      -20     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -35,7 +35,7 @@ final class DateTimeDiff extends Standard

private readonly Validatable $rule;

/** @param "years"|"months"|"days"|"hours"|"minutes"|"seconds"|"microseconds" $type */
/** @param string $type "years"|"months"|"days"|"hours"|"minutes"|"seconds"|"microseconds" */
Copy link
Member

Choose a reason for hiding this comment

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

This was like that on purpose, it's a literal constant. See https://phpstan.org/writing-php-code/phpdoc-types#literals-and-constants

/**
* @param string $type "years"|"months"|"days"|"hours"|"minutes"|"seconds"|"microseconds"
* @param string|null $format Example: "Y-m-d H:i:s.u"
* @param DateTimeImmutable|null $now The value that will be compared to the input
Copy link
Member

Choose a reason for hiding this comment

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

I'm a against adding documentation to the code like that, I think the name of the parameter should be pretty obvious for what it does, otherwise we should do that for everything. I understand that it can be useful to add some documentation here and there, but for this specific method, I would rather not have it.

@@ -36,22 +35,20 @@ final class DateTimeDiff extends Standard
private readonly Validatable $rule;

/**
* @param string $type "years"|"months"|"days"|"hours"|"minutes"|"seconds"|"microseconds"
* @param string|null $format Example: "Y-m-d H:i:s.u"
* @param string $type DateInterval format examples: (y, m, d, days, h, i, s, f)
Copy link
Member

@henriquemoody henriquemoody Nov 26, 2024

Choose a reason for hiding this comment

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

I do understand the appeal of using DateInterval formats directly. However, I think having more descriptive values is better as it's more understandable. With the literal constant, there's no reason to translate or document anything.

'y' => 'years',
'm' => 'months',
'd' => 'days',
'days' => 'full days',
Copy link
Member

Choose a reason for hiding this comment

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

Very nice catch!

@@ -95,7 +95,7 @@ public function evaluate(mixed $input): Result
return (new Result($nextSibling->isValid, $input, $this, $parameters))->withNextSibling($nextSibling);
}

private function comparisonValue(DateTimeInterface $now, DateTimeInterface $compareTo)
private function comparisonValue(DateTimeInterface $now, DateTimeInterface $compareTo): int|float
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if that's correct, because days can return false

@henriquemoody
Copy link
Member

I mixed some of my changes and yours @gvieiragoulart! Here's the merge request: #1475

I'm not sure whether we should add full days or not yet, it can be useful, but it doesn't seem necessary. What are your thoughts?

@henriquemoody
Copy link
Member

I've closing this since it was merged already. I haven't implemented the full days, though, is that something you would consider implementin, @gvieiragoulart?

Anyways, thank you so much for contributing and helping this rule get done! I really appreciate it! 🐼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DateDiff rule
3 participants