Skip to content
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

Type hint for \DateTimeInterface instead of \DateTime #7174

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function getHtml()
public function getEscapedValue($index = null)
{
$value = $this->getValue($index);
if ($value instanceof \DateTime) {
if ($value instanceof \DateTimeInterface) {
return $this->dateTimeFormatter->formatObject(
$value,
$this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function getEscapedValue($index = null)
{
if ($this->getColumn()->getFilterTime()) {
$value = $this->getValue($index);
if ($value instanceof \DateTime) {
if ($value instanceof \DateTimeInterface) {
return $this->_localeDate->formatDateTime($value);
}
return $value;
Expand Down
6 changes: 3 additions & 3 deletions app/code/Magento/CatalogRule/Model/ResourceModel/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ protected function _afterDelete(\Magento\Framework\Model\AbstractModel $rule)
* Get catalog rules product price for specific date, website and
* customer group
*
* @param \DateTime $date
* @param \DateTimeInterface $date
* @param int $wId
* @param int $gId
* @param int $pId
Expand All @@ -173,13 +173,13 @@ public function getRulePrice($date, $wId, $gId, $pId)
* Retrieve product prices by catalog rule for specific date, website and customer group
* Collect data with product Id => price pairs
*
* @param \DateTime $date
* @param \DateTimeInterface $date
* @param int $websiteId
* @param int $customerGroupId
* @param array $productIds
* @return array
*/
public function getRulePrices(\DateTime $date, $websiteId, $customerGroupId, $productIds)
public function getRulePrices(\DateTimeInterface $date, $websiteId, $customerGroupId, $productIds)
{
$connection = $this->getConnection();
$select = $connection->select()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function formatDate($date)
// unix timestamp given - simply instantiate date object
if (is_scalar($date) && preg_match('/^[0-9]+$/', $date)) {
$date = (new \DateTime())->setTimestamp($date);
} elseif (!($date instanceof \DateTime)) {
} elseif (!($date instanceof \DateTimeInterface)) {
// normalized format expecting Y-m-d[ H:i:s] - time is optional
$date = new \DateTime($date);
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Indexer/Model/Mview/View/State.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function getUpdated()
/**
* Set state updated time
*
* @param string|int|\DateTime $updated
* @param string|int|\DateTimeInterface $updated
* @return $this
*/
public function setUpdated($updated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ protected function _clearTableByDateRange(
* @param string $table
* @param string $column
* @param string $whereColumn
* @param null|string|\DateTime $from
* @param null|string|\DateTime $to
* @param null|string|\DateTimeInterface $from
* @param null|string|\DateTimeInterface $to
* @param [][] $additionalWhere
* @param string $alias
* @return \Magento\Framework\DB\Select
Expand Down Expand Up @@ -440,12 +440,12 @@ protected function _getTZOffsetTransitions($timezone, $from = null, $to = null)
$tzTransitions = [];
try {
if (!empty($from)) {
$from = $from instanceof \DateTime
$from = $from instanceof \DateTimeInterface
? $from->getTimestamp()
: (new \DateTime($from))->getTimestamp();
}

$to = $to instanceof \DateTime
$to = $to instanceof \DateTimeInterface
? $to
: new \DateTime($to);
$nextPeriod = $this->getConnection()->formatDate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class Collection extends \Magento\Framework\Data\Collection
/**
* From value
*
* @var string
* @var \DateTime
*/
protected $_from;

/**
* To value
*
* @var string
* @var \DateTime
*/
protected $_to;

Expand Down Expand Up @@ -114,14 +114,14 @@ public function setPeriod($period)
* Set interval
* @codeCoverageIgnore
*
* @param \DateTime $fromDate
* @param \DateTime $toDate
* @param \DateTimeInterface $fromDate
* @param \DateTimeInterface $toDate
* @return $this
*/
public function setInterval($fromDate, $toDate)
public function setInterval(\DateTimeInterface $fromDate, \DateTimeInterface $toDate)
{
$this->_from = $fromDate;
$this->_to = $toDate;
$this->_from = new \DateTime($fromDate->format('Y-m-d'), $fromDate->getTimezone());
$this->_to = new \DateTime($toDate->format('Y-m-d'), $toDate->getTimezone());

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public function testGetStoreIds()

/**
* @param string $period
* @param \DateTime $fromDate
* @param \DateTime $toDate
* @param \DateTimeInterface $fromDate
* @param \DateTimeInterface $toDate
* @param int $size
* @dataProvider intervalsDataProvider
* @return void
Expand All @@ -105,8 +105,8 @@ public function testGetPageSize()

/**
* @param string $period
* @param \DateTime $fromDate
* @param \DateTime $toDate
* @param \DateTimeInterface $fromDate
* @param \DateTimeInterface $toDate
* @param int $size
* @dataProvider intervalsDataProvider
* @return void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
private function resolveDate(\Magento\Framework\Model\AbstractModel $object, $dateIdentifier)
{
$date = $object->getData($dateIdentifier);
if ($date instanceof \DateTime) {
if ($date instanceof \DateTimeInterface) {
$object->setData($dateIdentifier, $date->format('Y-m-d H:i:s'));
} elseif (!is_string($date) || empty($date)) {
$object->setData($dateIdentifier, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function getItemComment(array $item)
*
* @param string $label
* @param bool $notified
* @param \DateTime $created
* @param \DateTimeInterface $created
* @param string $comment
* @return array
*/
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/SalesRule/Model/Coupon/Massgenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function generatePool()
} while ($this->getResource()->exists($code));

$expirationDate = $this->getToDate();
if ($expirationDate instanceof \DateTime) {
if ($expirationDate instanceof \DateTimeInterface) {
$expirationDate = $expirationDate->format('Y-m-d H:i:s');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function joinStore()
/**
* Add date filter to collection
*
* @param null|int|string|\DateTime $date
* @param null|int|string|\DateTimeInterface $date
* @return $this
*/
public function addDateFilter($date = null)
Expand Down
1 change: 1 addition & 0 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="DateTimeInterface" type="DateTime" />
<preference for="Psr\Log\LoggerInterface" type="Magento\Framework\Logger\Monolog" />
<preference for="Magento\Framework\EntityManager\EntityMetadataInterface" type="Magento\Framework\EntityManager\EntityMetadata" />
<preference for="Magento\Framework\EntityManager\HydratorInterface" type="Magento\Framework\EntityManager\Hydrator" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ public function quoteTableAs($ident, $alias = null, $auto = false);
/**
* Format Date to internal database date format
*
* @param int|string|\DateTime $date
* @param int|string|\DateTimeInterface $date
* @param boolean $includeTime
* @return \Zend_Db_Expr
*/
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public function getTransactionLevel()
/**
* Convert date to DB format
*
* @param int|string|\DateTime $date
* @param int|string|\DateTimeInterface $date
* @return \Zend_Db_Expr
*/
public function convertDate($date)
Expand All @@ -343,7 +343,7 @@ public function convertDate($date)
/**
* Convert date and time to DB format
*
* @param int|string|\DateTime $datetime
* @param int|string|\DateTimeInterface $datetime
* @return \Zend_Db_Expr
*/
public function convertDateTime($datetime)
Expand Down Expand Up @@ -2746,7 +2746,7 @@ public function addForeignKey(
/**
* Format Date to internal database date format
*
* @param int|string|\DateTime $date
* @param int|string|\DateTimeInterface $date
* @param bool $includeTime
* @return \Zend_Db_Expr
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function getUpdated();
/**
* Set state updated time
*
* @param string|int|\DateTime $updated
* @param string|int|\DateTimeInterface $updated
* @return \Magento\Framework\Mview\View\StateInterface
*/
public function setUpdated($updated);
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/Magento/Framework/Stdlib/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ class DateTime
/**
* Format date to internal format
*
* @param string|\DateTime|bool|null $date
* @param string|\DateTimeInterface|bool|null $date
* @param boolean $includeTime
* @return string|null
* @api
*/
public function formatDate($date, $includeTime = true)
{
if ($date instanceof \DateTime) {
if ($date instanceof \DateTimeInterface) {
$format = $includeTime ? self::DATETIME_PHP_FORMAT : self::DATE_PHP_FORMAT;
return $date->format($format);
} elseif (empty($date)) {
Expand Down
28 changes: 18 additions & 10 deletions lib/internal/Magento/Framework/Stdlib/DateTime/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function date($format = null, $input = null)
/**
* Forms GMT timestamp
*
* @param int|string $input date in current timezone
* @param int|string|\DateTimeInterface $input date in current timezone
* @return int
*/
public function gmtTimestamp($input = null)
Expand All @@ -106,6 +106,8 @@ public function gmtTimestamp($input = null)
return (int)gmdate('U');
} elseif (is_numeric($input)) {
$result = $input;
} elseif ($input instanceof \DateTimeInterface) {
$result = $input->getTimestamp();
} else {
$result = strtotime($input);
}
Expand All @@ -128,17 +130,23 @@ public function gmtTimestamp($input = null)
*/
public function timestamp($input = null)
{
if ($input === null) {
$result = $this->gmtTimestamp();
} elseif (is_numeric($input)) {
$result = $input;
} else {
$result = strtotime($input);
switch (true) {
case ($input === null):
$result = $this->gmtTimestamp();
break;
case (is_numeric($input)):
$result = $input;
break;
case ($input instanceof \DateTimeInterface):
$result = $input->getTimestamp();
break;
default:
$result = strtotime($input);
}

$date = $this->_localeDate->date($result);
$timestamp = $date->getTimestamp();
unset($date);
return $timestamp;

return $date->getTimestamp();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function formatObject($object, $format = null, $locale = null)
/**
* Implements what IntlDateFormatter::formatObject() is in PHP 5.5+
*
* @param \IntlCalendar|\DateTime $object
* @param \IntlCalendar|\DateTimeInterface $object
* @param string|int|array|null $format
* @param string|null $locale
* @return string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface DateTimeFormatterInterface
/**
* Returns a translated and localized date string
*
* @param \IntlCalendar|\DateTime $object
* @param \IntlCalendar|\DateTimeInterface $object
* @param string|int|array|null $format
* @param string|null $locale
* @return string
Expand Down
43 changes: 26 additions & 17 deletions lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,25 @@ public function date($date = null, $locale = null, $useTimezone = true, $include
? $this->getConfigTimezone()
: date_default_timezone_get();

if (empty($date)) {
return new \DateTime('now', new \DateTimeZone($timezone));
} elseif ($date instanceof \DateTime) {
return $date->setTimezone(new \DateTimeZone($timezone));
} elseif (!is_numeric($date)) {
$timeType = $includeTime ? \IntlDateFormatter::SHORT : \IntlDateFormatter::NONE;
$formatter = new \IntlDateFormatter(
$locale,
\IntlDateFormatter::SHORT,
$timeType,
new \DateTimeZone($timezone)
);
$date = $formatter->parse($date) ?: (new \DateTime($date))->getTimestamp();
switch (true) {
case (empty($date)):
return new \DateTime('now', new \DateTimeZone($timezone));
case ($date instanceof \DateTime):
return $date->setTimezone(new \DateTimeZone($timezone));
case ($date instanceof \DateTimeImmutable):
return new \DateTime($date->format('Y-m-d H:i:s'), $date->getTimezone());
case (!is_numeric($date)):
$timeType = $includeTime ? \IntlDateFormatter::SHORT : \IntlDateFormatter::NONE;
$formatter = new \IntlDateFormatter(
$locale,
\IntlDateFormatter::SHORT,
$timeType,
new \DateTimeZone($timezone)
);
$date = $formatter->parse($date) ?: (new \DateTime($date))->getTimestamp();
break;
}

return (new \DateTime(null, new \DateTimeZone($timezone)))->setTimestamp($date);
}

Expand All @@ -197,7 +202,7 @@ public function formatDate($date = null, $format = \IntlDateFormatter::SHORT, $s
{
$formatTime = $showTime ? $format : \IntlDateFormatter::NONE;

if (!($date instanceof \DateTime)) {
if (!($date instanceof \DateTimeInterface)) {
$date = new \DateTime($date);
}

Expand Down Expand Up @@ -260,7 +265,7 @@ public function formatDateTime(
$timezone = null,
$pattern = null
) {
if (!($date instanceof \DateTime)) {
if (!($date instanceof \DateTimeInterface)) {
$date = new \DateTime($date);
}

Expand Down Expand Up @@ -296,8 +301,12 @@ public function formatDateTime(
*/
public function convertConfigTimeToUtc($date, $format = 'Y-m-d H:i:s')
{
if (!($date instanceof \DateTime)) {
$date = new \DateTime($date, new \DateTimeZone($this->getConfigTimezone()));
if (!($date instanceof \DateTimeInterface)) {
if ($date instanceof \DateTimeImmutable) {
$date = new \DateTime($date->format('Y-m-d H:i:s'), new \DateTimeZone($this->getConfigTimezone()));
} else {
$date = new \DateTime($date, new \DateTimeZone($this->getConfigTimezone()));
}
} else {
if ($date->getTimezone()->getName() !== $this->getConfigTimezone()) {
throw new LocalizedException(
Expand Down
Loading