Skip to content

rector: Replace php date functions, Varien_Date with Carbon date library #4463

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

Draft
wants to merge 32 commits into
base: main
Choose a base branch
from
Draft
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
54 changes: 0 additions & 54 deletions .phpstan.dist.baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -618,18 +618,6 @@ parameters:
count: 1
path: app/code/core/Mage/Adminhtml/controllers/Customer/System/Config/ValidatevatController.php

-
message: '#^Parameter \#2 \$timestamp of function date expects int\|null, array\|bool given\.$#'
identifier: argument.type
count: 1
path: app/code/core/Mage/Adminhtml/controllers/CustomerController.php

-
message: '#^Parameter \#2 \$value of method Zend_Controller_Response_Abstract\:\:setHeader\(\) expects string, array\|bool given\.$#'
identifier: argument.type
count: 1
path: app/code/core/Mage/Adminhtml/controllers/CustomerController.php

-
message: '#^Variable \$id in isset\(\) always exists and is not nullable\.$#'
identifier: isset.variable
Expand Down Expand Up @@ -708,18 +696,6 @@ parameters:
count: 1
path: app/code/core/Mage/Adminhtml/controllers/Sales/Order/ShipmentController.php

-
message: '#^Parameter \#1 \$order of method Mage_Sales_Model_Resource_Order_Collection_Abstract\:\:setOrderFilter\(\) expects int\|Mage_Sales_Model_Order, array\<string, mixed\> given\.$#'
identifier: argument.type
count: 1
path: app/code/core/Mage/Adminhtml/controllers/Sales/Order/ShipmentController.php

-
message: '#^Parameter \#1 \$prefix of function uniqid expects string, int\<0, max\> given\.$#'
identifier: argument.type
count: 1
path: app/code/core/Mage/Adminhtml/controllers/Sales/Order/ShipmentController.php

-
message: '#^Parameter \#2 \$callback of function array_filter expects \(callable\(mixed\)\: bool\)\|null, ''\\\\intval'' given\.$#'
identifier: argument.type
Expand Down Expand Up @@ -2574,24 +2550,6 @@ parameters:
count: 1
path: app/code/core/Mage/Core/Model/Config.php

-
message: '#^Binary operation "\+" between string and string results in an error\.$#'
identifier: binaryOp.invalid
count: 1
path: app/code/core/Mage/Core/Model/Date.php

-
message: '#^Binary operation "\-" between string and string results in an error\.$#'
identifier: binaryOp.invalid
count: 1
path: app/code/core/Mage/Core/Model/Date.php

-
message: '#^Property Mage_Core_Model_Date\:\:\$_systemOffset is never read, only written\.$#'
identifier: property.onlyWritten
count: 1
path: app/code/core/Mage/Core/Model/Date.php

-
message: '#^Call to an undefined method Mage_Core_Model_Resource_Design\:\:validate\(\)\.$#'
identifier: method.notFound
Expand Down Expand Up @@ -4296,12 +4254,6 @@ parameters:
count: 1
path: app/code/core/Mage/Rss/Helper/Data.php

-
message: '#^Property Mage_Rule_Model_Abstract\:\:\$_conditions \(Mage_Rule_Model_Condition_Combine\) in empty\(\) is not falsy\.$#'
identifier: empty.property
count: 1
path: app/code/core/Mage/Rule/Model/Abstract.php

-
message: '#^Call to an undefined method Mage_Rule_Model_Action_Interface\:\:getId\(\)\.$#'
identifier: method.notFound
Expand Down Expand Up @@ -4380,12 +4332,6 @@ parameters:
count: 1
path: app/code/core/Mage/Sales/Block/Recurring/Profiles.php

-
message: '#^Parameter \#1 \$order of method Mage_Sales_Model_Resource_Order_Collection_Abstract\:\:setOrderFilter\(\) expects int\|Mage_Sales_Model_Order, array given\.$#'
identifier: argument.type
count: 1
path: app/code/core/Mage/Sales/Model/Api2/Order.php

-
message: '#^Parameter \#2 \$code of method Mage_Api2_Model_Resource\:\:_critical\(\) expects int\|null, string given\.$#'
identifier: argument.type
Expand Down
2 changes: 1 addition & 1 deletion .rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
instanceOf: false,
earlyReturn: false,
strictBooleans: false,
carbon: false,
carbon: true,
rectorPreset: false,
phpunitCodeQuality: true,
doctrineCodeQuality: false,
Expand Down
4 changes: 3 additions & 1 deletion app/Mage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @package Mage
*/

use Carbon\Carbon;

defined('DS') || define('DS', DIRECTORY_SEPARATOR);
defined('PS') || define('PS', PATH_SEPARATOR);

Expand Down Expand Up @@ -830,7 +832,7 @@ public static function isInstalled($options = [])
if (is_readable($localConfigFile)) {
$localConfig = simplexml_load_file($localConfigFile);
date_default_timezone_set('UTC');
if (($date = $localConfig->global->install->date) && strtotime((string) $date)) {
if (($date = $localConfig->global->install->date) && Carbon::parse((string) $date)->getTimestamp()) {
self::$_isInstalled = true;
}
}
Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Admin/Model/Acl/Assert/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @package Mage_Admin
*/

use Carbon\Carbon;

/**
* Assert time for admin acl
*
Expand All @@ -26,7 +28,7 @@ public function assert(
?Mage_Admin_Model_Acl_Resource $resource = null,
$privilege = null
) {
return $this->_isCleanTime(time());
return $this->_isCleanTime(Carbon::now()->getTimestamp());
}

/**
Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Admin/Model/Resource/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @package Mage_Admin
*/

use Carbon\Carbon;

/**
* ACL user resource
*
Expand Down Expand Up @@ -47,7 +49,7 @@ public function recordLogin(Mage_Admin_Model_User $user)
$adapter = $this->_getWriteAdapter();

$data = [
'logdate' => Varien_Date::now(),
'logdate' => Carbon::now()->format(Carbon::DEFAULT_TO_STRING_FORMAT),
'lognum' => $user->getLognum() + 1,
];

Expand Down
18 changes: 9 additions & 9 deletions app/code/core/Mage/Admin/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @package Mage_Admin
*/

use Carbon\Carbon;

/**
* Admin user model
*
Expand Down Expand Up @@ -670,7 +672,7 @@ public function changeResetPasswordLinkToken($newResetPasswordLinkToken)
throw Mage::exception('Mage_Core', Mage::helper('adminhtml')->__('Invalid password reset token.'));
}
$this->setRpToken($newResetPasswordLinkToken);
$currentDate = Varien_Date::now();
$currentDate = Carbon::now()->format(Carbon::DEFAULT_TO_STRING_FORMAT);
$this->setRpTokenCreatedAt($currentDate);

return $this;
Expand All @@ -690,17 +692,14 @@ public function isResetPasswordLinkTokenExpired()
return true;
}

$tokenExpirationPeriod = Mage::helper('admin')->getResetPasswordLinkExpirationPeriod();

$currentDate = Varien_Date::now();
$currentTimestamp = Varien_Date::toTimestamp($currentDate);
$tokenTimestamp = Varien_Date::toTimestamp($resetPasswordLinkTokenCreatedAt);
$currentTimestamp = Carbon::now();
$tokenTimestamp = Carbon::now()->setTimeFromTimeString($resetPasswordLinkTokenCreatedAt);
if ($tokenTimestamp > $currentTimestamp) {
return true;
}

$hoursDifference = floor(($currentTimestamp - $tokenTimestamp) / (60 * 60));
if ($hoursDifference >= $tokenExpirationPeriod) {
$tokenExpirationPeriod = Mage::helper('admin')->getResetPasswordLinkExpirationPeriod();
if ($currentTimestamp->diffInDays($tokenTimestamp) >= $tokenExpirationPeriod) {
return true;
}

Expand Down Expand Up @@ -729,7 +728,8 @@ public function cleanPasswordsValidationData()
*/
protected function _getDateNow($dayOnly = false)
{
return Varien_Date::now($dayOnly);
$format = $dayOnly ? Mage_Core_Helper_Date::DATE_PHP_FORMAT : Mage_Core_Helper_Date::DATETIME_PHP_FORMAT;
return Carbon::now()->format($format);
}

/**
Expand Down
8 changes: 5 additions & 3 deletions app/code/core/Mage/AdminNotification/Model/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @package Mage_AdminNotification
*/

use Carbon\Carbon;

/**
* AdminNotification Feed model
*
Expand Down Expand Up @@ -52,7 +54,7 @@ public function getFeedUrl()
*/
public function checkUpdate()
{
if (($this->getFrequency() + $this->getLastUpdate()) > time()) {
if (($this->getFrequency() + $this->getLastUpdate()) > Carbon::now()->getTimestamp()) {
return $this;
}

Expand Down Expand Up @@ -88,7 +90,7 @@ public function checkUpdate()
*/
public function getDate($rssDate)
{
return gmdate(Varien_Db_Adapter_Pdo_Mysql::TIMESTAMP_FORMAT, strtotime($rssDate));
return gmdate(Varien_Db_Adapter_Pdo_Mysql::TIMESTAMP_FORMAT, Carbon::parse($rssDate)->getTimestamp());
}

/**
Expand Down Expand Up @@ -118,7 +120,7 @@ public function getLastUpdate()
*/
public function setLastUpdate()
{
Mage::app()->saveCache(time(), 'admin_notifications_lastcheck');
Mage::app()->saveCache(Carbon::now()->getTimestamp(), 'admin_notifications_lastcheck');
return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @package Mage_Adminhtml
*/

use Carbon\Carbon;

/**
* Customer account form block
*
Expand Down Expand Up @@ -136,7 +138,7 @@ public function getCurrentStatus()
$log = $this->getCustomerLog();
if ($log->getLogoutAt()
|| !$log->getLastVisitAt()
|| strtotime(Varien_Date::now()) - strtotime($log->getLastVisitAt()) > Mage_Log_Model_Visitor::getOnlineMinutesInterval() * 60
|| Carbon::parse(Carbon::now()->format(Carbon::DEFAULT_TO_STRING_FORMAT))->getTimestamp() - Carbon::parse($log->getLastVisitAt())->getTimestamp() > Mage_Log_Model_Visitor::getOnlineMinutesInterval() * 60
) {
return Mage::helper('customer')->__('Offline');
}
Expand Down
4 changes: 3 additions & 1 deletion app/code/core/Mage/Adminhtml/Block/Dashboard/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @package Mage_Adminhtml
*/

use Carbon\Carbon;

/**
* Adminhtml dashboard google chart block
*
Expand Down Expand Up @@ -211,7 +213,7 @@ public function generateChart(): array
$formats = Mage::app()->getLocale()->getTranslationList('datetime');
$format = $formats['yyMM'] ?? 'MM/yyyy';
$format = str_replace(['yyyy', 'yy', 'MM'], ['Y', 'y', 'm'], $format);
$this->_axisLabels[$idx][$_index] = date($format, strtotime($_label));
$this->_axisLabels[$idx][$_index] = Carbon::parse($_label)->format($format);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function _prepareForm()

if ($queue->getQueueStartAt()) {
$form->getElement('date')->setValue(
Mage::app()->getLocale()->date($queue->getQueueStartAt(), Varien_Date::DATETIME_INTERNAL_FORMAT),
Mage::app()->getLocale()->date($queue->getQueueStartAt(), Mage_Core_Helper_Date::DATETIME_INTERNAL_FORMAT),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ protected function _prepareForm()
'label' => Mage::helper('catalogrule')->__('From Date'),
'title' => Mage::helper('catalogrule')->__('From Date'),
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'input_format' => Varien_Date::DATE_INTERNAL_FORMAT,
'input_format' => Mage_Core_Helper_Date::DATE_INTERNAL_FORMAT,
'format' => $dateFormatIso,
]);
$fieldset->addField('to_date', 'date', [
'name' => 'to_date',
'label' => Mage::helper('catalogrule')->__('To Date'),
'title' => Mage::helper('catalogrule')->__('To Date'),
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'input_format' => Varien_Date::DATE_INTERNAL_FORMAT,
'input_format' => Mage_Core_Helper_Date::DATE_INTERNAL_FORMAT,
'format' => $dateFormatIso,
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,15 @@ protected function _prepareForm()
'label' => Mage::helper('salesrule')->__('From Date'),
'title' => Mage::helper('salesrule')->__('From Date'),
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'input_format' => Varien_Date::DATE_INTERNAL_FORMAT,
'input_format' => Mage_Core_Helper_Date::DATE_INTERNAL_FORMAT,
'format' => $dateFormatIso,
]);
$fieldset->addField('to_date', 'date', [
'name' => 'to_date',
'label' => Mage::helper('salesrule')->__('To Date'),
'title' => Mage::helper('salesrule')->__('To Date'),
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'input_format' => Varien_Date::DATE_INTERNAL_FORMAT,
'input_format' => Mage_Core_Helper_Date::DATE_INTERNAL_FORMAT,
'format' => $dateFormatIso,
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected function _toHtml()
) {
$id = "{$key}_{$idSuffix}";
$element = new Varien_Data_Form_Element_Date([
'format' => Varien_Date::DATE_INTERNAL_FORMAT, // hardcode because hardcoded values delimiter
'format' => Mage_Core_Helper_Date::DATE_INTERNAL_FORMAT, // hardcode because hardcoded values delimiter
'label' => $label,
'image' => $this->getSkinUrl('images/grid-cal.gif'),
'onchange' => "dateTimeChoose_{$idSuffix}()", // won't work through Event.observe()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @package Mage_Adminhtml
*/

use Carbon\Carbon;

/**
* Dashboard Year-To-Date Month and Day starts Field Renderer
*
Expand All @@ -19,7 +21,7 @@ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
$_months = [];
for ($i = 1; $i <= 12; $i++) {
$_months[$i] = Mage::app()->getLocale()
->date(mktime(0, 0, 0, $i))
->date(Carbon::now()->setHour(0)->setMinute(0)->setSecond(0)->setMonth($i))
->get(Zend_Date::MONTH_NAME);
}

Expand Down
13 changes: 11 additions & 2 deletions app/code/core/Mage/Adminhtml/Block/Report/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @package Mage_Adminhtml
*/

use Carbon\Carbon;

/**
* Adminhtml report grid block
*
Expand Down Expand Up @@ -104,10 +106,11 @@ protected function _prepareColumns()

/**
* @return $this
* @throws Zend_Date_Exception
*/
protected function _prepareCollection()
{
$filter = $this->getParam($this->getVarNameFilter(), null);
$filter = $this->getParam($this->getVarNameFilter());

if (is_null($filter)) {
$filter = $this->_defaultFilter;
Expand All @@ -120,7 +123,13 @@ protected function _prepareCollection()

if (!isset($data['report_from'])) {
// getting all reports from 2001 year
$date = new Zend_Date(mktime(0, 0, 0, 1, 1, 2001));
$date = new Zend_Date(Carbon::now()
->setHour(0)
->setMinute(0)
->setSecond(0)
->setMonth(1)
->setYear(2001));

$data['report_from'] = $date->toString($this->getLocale()->getDateFormat('short'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function _getUpdatedAt($reportCode)
return ($flag->hasData())
? Mage::app()->getLocale()->storeDate(
0,
new Zend_Date($flag->getLastUpdate(), Varien_Date::DATETIME_INTERNAL_FORMAT),
new Zend_Date($flag->getLastUpdate(), Mage_Core_Helper_Date::DATETIME_INTERNAL_FORMAT),
true,
)
: '';
Expand Down
Loading
Loading