Skip to content

Commit

Permalink
TimeframeForm: Fix DateTime handling
Browse files Browse the repository at this point in the history
- `DateTime::createFromFormat()` returns false, if seconds are `00`, which leads to a false positive result at the end.
- Check the checkboxes only if start/end is actually relative
  • Loading branch information
sukhwinder33445 committed Jul 23, 2024
1 parent e4d3866 commit 2d2277e
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions library/Reporting/Web/Forms/TimeframeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,10 @@ protected function assemble()
'description' => $this->translate('A unique name of this timeframe')
]);

$default = new DateTime('00:00:00');
$start = $this->getPopulatedValue('start', $default);
if (! $start instanceof DateTime) {
$datetime = DateTime::createFromFormat(LocalDateTimeElement::FORMAT, $start);
if ($datetime) {
$start = $datetime;
}
}

$relativeStart = $this->getPopulatedValue('relative-start', $start instanceof DateTime ? 'n' : 'y');
$start = $this->getPopulatedValue('start', new DateTime('00:00:00'));
$canBeConverted = $start instanceof DateTime
|| DateTime::createFromFormat(LocalDateTimeElement::FORMAT, $start) !== false;
$relativeStart = $this->getPopulatedValue('relative-start', $canBeConverted ? 'n' : 'y');
$this->addElement('checkbox', 'relative-start', [
'required' => false,
'class' => 'autosubmit',
Expand All @@ -65,7 +59,7 @@ protected function assemble()

if ($relativeStart === 'n') {
if (! $start instanceof DateTime) {
$start = $default;
$start = (new DateTime($start))->format(LocalDateTimeElement::FORMAT);

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 62 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.
$this->clearPopulatedValue('start');
}

Expand Down Expand Up @@ -102,16 +96,10 @@ protected function assemble()
]);
}

$default = new DateTime('23:59:59');
$end = $this->getPopulatedValue('end', $default);
if (! $end instanceof DateTime) {
$datetime = DateTime::createFromFormat(LocalDateTimeElement::FORMAT, $end);
if ($datetime) {
$end = $datetime;
}
}

$relativeEnd = $this->getPopulatedValue('relative-end', $end instanceof DateTime ? 'n' : 'y');
$end = $this->getPopulatedValue('end', new DateTime('23:59:59'));
$canBeConverted = $end instanceof DateTime
|| DateTime::createFromFormat(LocalDateTimeElement::FORMAT, $end) !== false;
$relativeEnd = $this->getPopulatedValue('relative-end', $canBeConverted ? 'n' : 'y');
if ($relativeStart === 'y') {
$this->addElement('checkbox', 'relative-end', [
'required' => false,
Expand Down Expand Up @@ -153,7 +141,7 @@ protected function assemble()

if ($relativeEnd === 'n' || $relativeStart === 'n') {
if (! $end instanceof DateTime) {
$end = $default;
$end = (new DateTime($end))->format(LocalDateTimeElement::FORMAT);

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.

Check failure on line 144 in library/Reporting/Web/Forms/TimeframeForm.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #1 $datetime of class DateTime constructor expects string, mixed given.
$this->clearPopulatedValue('end');
}

Expand Down

0 comments on commit 2d2277e

Please sign in to comment.