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

Check date sanity #40

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/TodoByDateRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ public function processNode(Node $node, Scope $scope): array
$date = $match['date'][0];
$todoText = trim($match['comment'][0]);

sscanf($date, '%4s-%2s-%2s', $year, $month, $day);

if (!checkdate((int) $month, (int) $day, (int) $year)) {
$errors[] = $this->errorBuilder->buildError(
$comment,
'Invalid date "'. $date .'". Expected format "YYYY-MM-DD".',
null,
$match[0][1]
);

continue;
}

/**
* strtotime() will parse date-only values with time set to 00:00:00.
* This is fine, because this will count any expiration matching
Expand Down
24 changes: 24 additions & 0 deletions tests/TodoByDateRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,28 @@ public function testReferenceTime2(): void
],
]);
}

public function testInvalidDate(): void
{
$this->referenceTime = '18th january 2023';

$this->analyse([__DIR__ . '/data/invalidDate.php'], [
[
'Invalid date "2099-13-01". Expected format "YYYY-MM-DD".',
6,
],
[
'Invalid date "2099-11-31". Expected format "YYYY-MM-DD".',
7,
],
[
'Invalid date "2096-02-30". Expected format "YYYY-MM-DD".',
12,
],
[
'Invalid date "2095-02-29". Expected format "YYYY-MM-DD".',
16,
],
]);
}
}
16 changes: 16 additions & 0 deletions tests/data/invalidDate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace InvalidDate;

// TODO: 2099-12-01 valid date
// TODO: 2099-13-01 invalid date
// TODO: 2099-11-31 invalid date


// test leap year
// TODO: 2096-02-29 valid
// TODO: 2096-02-30 invalid

// test non-leap year
// TODO: 2095-02-28 valid
// TODO: 2095-02-29 invalid