Skip to content

PHPStan reports that there is no constructor for DateTimeImmutable #36

Description

@rquadling

phpstan/phpstan#842

We have just started using Timecop for controlling time in our unit tests. Works REALLY well.

We also run PHPStan against all our code, to make sure things are sane and that they obey our standards.

One of the things the PHPStan is picking up on is a lack of a constructor in Timecop's DateTimeImmutable.

I've linked the PHPStan issue above, but the here is a snippet of the code and the error that PHPStan is generating.

<?php

namespace DT\ValueObjects;

use DateTimeImmutable;

class DateTimeMicro extends DateTimeImmutable
{
    public function __construct($time = 'now', $timezone = null)
    {
        if ($time === 'now') {
            list($dateTime, $micro) = explode('.', microtime(true));
            $time = date('Y-m-d H:i:s.', $dateTime).$micro;
        }
        parent::__construct($time, $timezone);
    }
}

With the Timecop extension enabled, we'll get an error:

DT\ValueObjects\DateTimeMicro::__construct() calls parent constructor but parent does not have one.

We have now configured things to only enable Timecop when running unit tests and use a polyfill for the Timecop functions we are using for PHPStan. That is all working fine.

But it is all a bit of a kerfuffle which could probably be fixed within Timecop.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions