Skip to content

DateTimeSetTimeUsage ​

error on by default PHP ≤ 7.0

Group: Probable bugs · PhpStorm name: DateTimeSetTimeUsageInspection

The microseconds argument of DateTime::setTime() / date_time_set() only exists since PHP 7.1. On older versions passing it makes the call fail and return false, so the extra argument is a bug when targeting PHP < 7.1.

Example ​

Target PHP 7.0:

php
<?php
class Clock extends DateTime {}

function rewind_clock(DateTime $d, Clock $c, DateTimeImmutable $i, $usec)
{
    $d->setTime(8, 30, 0, $usec);
    $c->setTime(8, 30, 0, 250);
    $d->setTime(8, 30, 0);
    $i->setTime(8, 30, 0, 250);
    date_time_set($d, 8, 30, 0, 0);
    \date_time_set($d, 8, 30, 0, null);
    date_time_set($d, 8, 30, 0);
}

Reported:

  • line 6: Microseconds argument requires PHP 7.1+; on this version the call returns false.
  • line 7: Microseconds argument requires PHP 7.1+; on this version the call returns false.
  • line 10: Microseconds argument requires PHP 7.1+; on this version the call returns false.
  • line 11: Microseconds argument requires PHP 7.1+; on this version the call returns false.

Configure ​

In custos.json:

json
{
  "rules": {
    "DateTimeSetTimeUsage": {
      "enabled": false
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file), either of:

php
// @custos-ignore DateTimeSetTimeUsage

/**
 * @noinspection DateTimeSetTimeUsageInspection
 */

Released under the MIT License. Rule catalogue modelled on Php Inspections (EA Extended); independent clean-room implementation.