Skip to content

Commit 6ae79b7

Browse files
anzinxmav
authored andcommitted
Adjusted code with new deprecations in PHP 8.1
1 parent 7b3ae93 commit 6ae79b7

File tree

5 files changed

+75
-12
lines changed

5 files changed

+75
-12
lines changed

app/code/Magento/Config/Model/Config/Source/Date/Short.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,49 @@
55
*/
66
namespace Magento\Config\Model\Config\Source\Date;
77

8+
use IntlDateFormatter;
9+
810
/**
911
* @api
1012
* @since 100.0.2
1113
*/
1214
class Short implements \Magento\Framework\Option\ArrayInterface
1315
{
16+
/**
17+
* @var IntlDateFormatter
18+
*/
19+
private $dateFormatter;
20+
1421
/**
1522
* @return array
1623
*/
1724
public function toOptionArray()
1825
{
1926
$arr = [];
2027
$arr[] = ['label' => '', 'value' => ''];
21-
$arr[] = ['label' => strftime('MM/DD/YY (%m/%d/%y)'), 'value' => '%m/%d/%y'];
22-
$arr[] = ['label' => strftime('MM/DD/YYYY (%m/%d/%Y)'), 'value' => '%m/%d/%Y'];
23-
$arr[] = ['label' => strftime('DD/MM/YY (%d/%m/%y)'), 'value' => '%d/%m/%y'];
24-
$arr[] = ['label' => strftime('DD/MM/YYYY (%d/%m/%Y)'), 'value' => '%d/%m/%Y'];
28+
$arr[] = ['label' => 'MM/DD/YY ' . $this->getTimeFormat(time(), '(M/d/y)'), 'value' => 'M/d/y'];
29+
$arr[] = ['label' => 'MM/DD/YYYY '. $this->getTimeFormat(time(), '(M/d/Y)'), 'value' => 'M/d/Y'];
30+
$arr[] = ['label' => 'DD/MM/YY ' . $this->getTimeFormat(time(), '(d/m/y)'), 'value' => 'd/M/y'];
31+
$arr[] = ['label' => 'DD/MM/YYYY ' . $this->getTimeFormat(time(), '(d/m/Y)'), 'value' => 'd/M/Y'];
2532
return $arr;
2633
}
34+
35+
/**
36+
* This method format timestamp value.
37+
*
38+
* @param int $datetime
39+
* @param string $format
40+
*
41+
* @return string
42+
*/
43+
private function getTimeFormat(int $datetime, string $format = 'Y/M/d'): string
44+
{
45+
if (!$this->dateFormatter) {
46+
$locale = \Locale::getDefault();
47+
$this->dateFormatter = new \IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::SHORT);
48+
}
49+
$this->dateFormatter->setPattern($format);
50+
51+
return $this->dateFormatter->format($datetime);
52+
}
2753
}

dev/tests/integration/testsuite/Magento/Cron/Model/ScheduleTest.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Cron\Model;
77

8+
use IntlDateFormatter;
89
use Magento\Framework\Stdlib\DateTime\DateTime;
910
use \Magento\TestFramework\Helper\Bootstrap;
1011

@@ -25,6 +26,14 @@ class ScheduleTest extends \PHPUnit\Framework\TestCase
2526
*/
2627
protected $dateTime;
2728

29+
/**
30+
* @var IntlDateFormatter
31+
*/
32+
private $dateFormatter;
33+
34+
/**
35+
* @ingeritdoc
36+
*/
2837
protected function setUp(): void
2938
{
3039
$this->dateTime = Bootstrap::getObjectManager()->create(DateTime::class);
@@ -60,14 +69,15 @@ public function testTryLockJobAlreadyLockedFails()
6069
public function testTryLockJobAlreadyLockedSucceeds()
6170
{
6271
$offsetInThePast = 2*24*60*60;
72+
$gmtTimestamp = $this->dateTime->gmtTimestamp();
6373

6474
$oldSchedule = $this->scheduleFactory->create()
6575
->setCronExpr("* * * * *")
6676
->setJobCode("test_job")
6777
->setStatus(Schedule::STATUS_RUNNING)
68-
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp() - $offsetInThePast))
69-
->setScheduledAt(strftime('%Y-%m-%d %H:%M', $this->dateTime->gmtTimestamp() - $offsetInThePast + 60))
70-
->setExecutedAt(strftime('%Y-%m-%d %H:%M', $this->dateTime->gmtTimestamp() - $offsetInThePast + 61));
78+
->setCreatedAt($this->getTimeFormat($gmtTimestamp - $offsetInThePast))
79+
->setScheduledAt($this->getTimeFormat($gmtTimestamp - $offsetInThePast + 60, 'Y-M-d H:m'))
80+
->setExecutedAt($this->getTimeFormat($gmtTimestamp - $offsetInThePast + 61, 'Y-M-d H:m'));
7181
$oldSchedule->save();
7282

7383
$schedule = $this->createSchedule("test_job", Schedule::STATUS_PENDING);
@@ -107,14 +117,35 @@ public function testTryLockJobDifferentJobLocked()
107117
*/
108118
private function createSchedule($jobCode, $status, $timeOffset = 0)
109119
{
120+
$gmtTimestamp = $this->dateTime->gmtTimestamp();
121+
110122
$schedule = $this->scheduleFactory->create()
111123
->setCronExpr("* * * * *")
112124
->setJobCode($jobCode)
113125
->setStatus($status)
114-
->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->dateTime->gmtTimestamp()))
115-
->setScheduledAt(strftime('%Y-%m-%d %H:%M', $this->dateTime->gmtTimestamp() + $timeOffset));
126+
->setCreatedAt($this->getTimeFormat($gmtTimestamp))
127+
->setScheduledAt($this->getTimeFormat($gmtTimestamp + $timeOffset, 'Y-M-d H:m'));
116128
$schedule->save();
117129

118130
return $schedule;
119131
}
132+
133+
/**
134+
* This method format timestamp value.
135+
*
136+
* @param int $datetime
137+
* @param string $format
138+
*
139+
* @return string
140+
*/
141+
private function getTimeFormat(int $datetime, string $format = 'Y-M-d H:m:s'): string
142+
{
143+
if (!$this->dateFormatter) {
144+
$locale = \Locale::getDefault();
145+
$this->dateFormatter = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::SHORT);
146+
}
147+
$this->dateFormatter->setPattern($format);
148+
149+
return $this->dateFormatter->format($datetime);
150+
}
120151
}

dev/tests/utils/phpunitGroupConfig.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
$generateConfig = false;
9090
} else {
9191
assertUsage(
92-
(empty($options['get-group']) || !ctype_digit($options['get-group']))
92+
(empty($options['get-group']) || !(is_string($options['get-group']) && ctype_digit($options['get-group'])))
9393
&& strtolower($options['get-group']) != 'all',
9494
"Option --get-group: must be a positive integer or 'all'\n"
9595
);

lib/internal/Magento/Framework/Escaper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public function escapeJs($string)
309309
$string = (string) $string;
310310
}
311311

312-
if ($string === '' || ctype_digit($string)) {
312+
if ($string === '' || (is_string($string) && ctype_digit($string))) {
313313
return $string;
314314
}
315315

lib/internal/Magento/Framework/View/Element/Html/Date.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Framework\View\Element\Html;
77

8+
use IntlDateFormatter;
9+
810
/**
911
* Date element block
1012
*/
@@ -76,7 +78,11 @@ protected function _toHtml()
7678
public function getEscapedValue()
7779
{
7880
if ($this->getFormat() && $this->getValue()) {
79-
return strftime($this->getFormat(), strtotime($this->getValue()));
81+
$locale = \Locale::getDefault();
82+
$dateFormatter = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::SHORT);
83+
$dateFormatter->setPattern($this->getFormat());
84+
85+
return $dateFormatter->format(strtotime($this->getValue()));
8086
}
8187
return $this->escapeHtml($this->getValue());
8288
}

0 commit comments

Comments
 (0)