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

CRM-21148 - Refactor two near-identical functions into one #10941

Closed
wants to merge 4 commits into from

Conversation

MegaphoneJon
Copy link
Contributor

@MegaphoneJon MegaphoneJon commented Sep 5, 2017

Overview

At some point, a method was copy-pasted, then modified. This reconciles the differences and reduces code duplication.

Before

CRM_Utils_Date::getFromTo() and CRM_Report_Form::getFromTo() contain almost identical code.

After

CRM_Report_Form::getFromTo() wraps CRM_Utils_Date::getFromTo().

Comments

My unit tests bypass the normal setUp() and tearDown() methods, which cuts the time to run down considerably. These are unit tests and not functional tests; there's no need to load the database. Is this a kosher technique? My thinking is that if someone adds functional tests later, it'll be obvious that this needs changing - but that a Utils subclass probably never needs functional tests.


@@ -800,17 +800,20 @@ public static function getRange($startDate, $endDate) {
* @return array
* start date, end date
*/
public static function getFromTo($relative, $from, $to) {
public static function getFromTo($relative, $from, $to, $fromTime = NULL, $toTime = NULL) {
if (empty($toTime)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this be cleaner in the function signature - ie
public static function getFromTo($relative, $from, $to, $fromTime = 0, $toTime = '235959') {

$dateRange = self::relativeToAbsolute($term, $unit);
$from = $dateRange['from'];
$from = substr($dateRange['from'], 0, 8);
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if there could be any existing cases where it is working 'because' the string is passed through uncut here - looks like it would work :-(

Copy link
Contributor

Choose a reason for hiding this comment

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

I did some checks & it seems existing usages of this function are mostly in the advanced search & they don't appear to offer time granularity so it's probably fine / safe

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 did the same check - I actually considered removing the $fromTime and $toTime from the function signature since this function never returns time-level granularity. However, since my goal is to insert a hook here, I kept them in, in case someone wants to write an extension that provides it.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK - well I guess we both checked & didn't find something it's probably ok

Copy link
Contributor

Choose a reason for hiding this comment

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

I like the fact this leaves a path to adding 'last one hour' as a filter too

@eileenmcnaughton
Copy link
Contributor

@MegaphoneJon can you squash (rebase -i) & check the fails

@MegaphoneJon
Copy link
Contributor Author

Well, I failed my own test in the same PR; I don't know whether to be proud or ashamed. Anyway, @eileenmcnaughton - turns out I couldn't drop this:

if (empty($toTime)) {
  $toTime = '235959';
}

There's the edge case of someone passing NULL into the function itself, which is what my test did. It doesn't happen in the core codebase, but someone COULD, so I changed the code rather than the test.

@eileenmcnaughton
Copy link
Contributor

Hmm - I personally disagree with handling invalid developer-written bugs. People should actually have to write valid code for it to work.

Could you make passing in NULL more obviously invalid by going with

($relative, $from, $to, $fromTime = NULL, (int) $toTime = 23599)

More unit tests

properly handle times

CRM-21148 - incorporate feedback

style fix

style fix

Handle  passed in as NULL

CRM-21148 - Refactor two functions into one
@eileenmcnaughton
Copy link
Contributor

jenkins is not your friend

@MegaphoneJon
Copy link
Contributor Author

@eileenmcnaughton I know it's been a minute since you looked at this, but since this my changes are in response to your review, could you please take a look?

@@ -1996,14 +1996,14 @@ public function whereClause(&$field, $op, $value, $min, $max) {
* @param string $from
* @param string $to
* @param string $type
* @param string $fromTime
* @param string $toTime
* @param int $fromTime
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm concerned of the impact of changing fromTime/toTime to an int. What if the desired fromTime is 000100 (12:01 am) or any other value in the first hour of the day? I suspect that value will be interpreted as 100 and impact the datetime construction downstream.

@MegaphoneJon
Copy link
Contributor Author

I suspect this is fine: http://php.net/manual/en/datetime.formats.compound.php#119223

However, it's worth checking. I'm adding it to my to-do list. That said, this is a pet project. @lcdservices - if you have a paid client interested in this, you have incentive to do it first :)

@eileenmcnaughton
Copy link
Contributor

@MegaphoneJon I've opened #11887 as a cut down version of this - can you check it with a view to merging?

@MegaphoneJon
Copy link
Contributor Author

Last week was tough but I should be able to do this later this week.

@eileenmcnaughton
Copy link
Contributor

@MegaphoneJon any chance to look at the proposed replacement?

@eileenmcnaughton
Copy link
Contributor

Closing this since we remerged the other - you can decide if there is more in here you want to open a new PR for

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

Successfully merging this pull request may close these issues.

4 participants