Phpstan: Call to function [timecop function] with incorrect case: [standard time function]

Created on 18 Jan 2018  路  3Comments  路  Source: phpstan/phpstan

We use php-timecop in our application for test cases with time ("time travel"). And phpstan find errors for all standard time php functions like time(), microtime(), etc.

Example file timecop.php:

<?php

time(); // phpstan error
microtime(); // phpstan error
new DateTime(); // no error
timecop_travel(new DateTime('2018-01-01')); // no error

phpstan result for this file:

------ --------------------------------------------------------------------- 
  Line   timecop.php                                                   
 ------ --------------------------------------------------------------------- 
  3      Call to function timecop_time() with incorrect case: time            
  4      Call to function timecop_microtime() with incorrect case: microtime  
 ------ --------------------------------------------------------------------- 

Perhaps you have a solution to this problem?

bug

Most helpful comment

All 3 comments

The problem is that function reflection behaves incorrectly when this extension is loaded:

$reflection = new \ReflectionFunction('time');
$reflection->getName() // returns timecop_time, confusing PHPStan

I could definitely improve this piece of code in CallToNonExistentFunctionRule so that it checks that strtolower() of both $reflection->getName() and resolved function name as called in the code matches before I report the incorrect case:

if ($function->getName() !== $this->broker->resolveFunctionName($node->name, $scope)) {
    return [sprintf('Call to function %s() with incorrect case: %s', $function->getName(), $name)];
}

@ondrejmirtes thank you! Now it's working good!

Was this page helpful?
0 / 5 - 0 ratings