cakephp / chronos

A standalone DateTime library originally based off of Carbon
http://book.cakephp.org/chronos
MIT License
1.34k stars 61 forks source link

PHP Warning: Undefined array key "class" in /srv/www/vendor/cakephp/chronos/src/Traits/FrozenTimeTrait.php on line 166 #414

Closed gcumbaya closed 10 months ago

gcumbaya commented 10 months ago

Description:

The following warning occurs when calling setTimezone() on ChronosDate is done from within a function:

PHP Warning: Undefined array key "class" in /srv/www/vendor/cakephp/chronos/src/Traits/FrozenTimeTrait.php on line 166

How i reproduced it locally?

$date = new ChronosDate();

// example when called from within a function like twig does when configured in symfony
function foo(ChronosDate $date) {
    $date->setTimezone("Europe/Amsterdam");
}

foo($date);

The output:

PHP Warning: Undefined array key "class" in /srv/www/vendor/cakephp/chronos/src/Traits/FrozenTimeTrait.php on line 166

PHP Deprecated: 2.5 setTimezone() will be removed in 3.x. in /srv/www/vendor/cakephp/chronos/src/Traits/FrozenTimeTrait.php on line 175

How did i run into this?

When upgrading from v2.2 to 2.4 and running a project on symfony 6 which uses twig configuration to handle date filters.

Additional information related to debugging the issue for our symfony / twig situation

When added the dd($trace) in that trait:


    public function setTimezone($value)
    {
        if (static::class === ChronosDate::class) {
            $trace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 5);
            $found = false;
            dd($trace);
            foreach ($trace as $frame) {

                $found = in_array(
                    $frame['class'],
                    ['PHPUnit\Framework\Assert', 'PHPUnit\Framework\Constraint\IsEqual'],
                    true
                );
                if ($found) {
                    break;
                }
            }
            if (!$found) {
                trigger_error('2.5 setTimezone() will be removed in 3.x.', E_USER_DEPRECATED);
            }
        }

        return $this;
    }

The following (partly) result is dumped:


array:5 [
  0 => array:5 [
    "file" => "/srv/www/vendor/twig/twig/src/Extension/CoreExtension.php"
    "line" => 488
    "function" => "setTimezone"
    "class" => "Cake\Chronos\ChronosDate"
    "type" => "->"
  ]
  1 => array:3 [
    "file" => "/srv/www/vendor/twig/twig/src/Extension/CoreExtension.php"
    "line" => 430
    "function" => "twig_date_converter"
  ]
  2 => array:3 [
    "file" => "/srv/www/var/cache/test/twig/fd/fd5c0ce3282c91527013da236d4f6279.php"
    "line" => 505
    "function" => "twig_date_format_filter"
  ]
  3 => array:5 [
    "file" => "/srv/www/vendor/twig/twig/src/Template.php"
    "line" => 394
    "function" => "doDisplay"
    "class" => "__TwigTemplate_39d0b2e4b26bf524d89bec2f4f3f0635"
    "type" => "->"
  ]
  4 => array:5 [
    "file" => "/srv/www/vendor/twig/twig/src/Template.php"
    "line" => 367
    "function" => "displayWithErrorHandling"
    "class" => "Twig\Template"
    "type" => "->"
  ]
]

For us this comment already applied to not go to v3. Unfortunately going to v2.4 is currently also not an option.