Skip to content

Commit 2f1f68c

Browse files
authored
Merge pull request #33919 from karyna-tsymbal-atwix/improvement/m2-33563-phpcs-fixes
phpcs fixes for php8
2 parents 68bf52f + 55890ff commit 2f1f68c

File tree

9 files changed

+50
-17
lines changed

9 files changed

+50
-17
lines changed

app/code/Magento/AwsS3/Driver/AwsS3.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ public function fileClose($resource): bool
851851
//phpcs:enable
852852

853853
foreach ($this->streams as $path => $stream) {
854-
//phpcs:disable
854+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
855855
if (stream_get_meta_data($stream)['uri'] === $resourcePath) {
856856
$this->adapter->writeStream($path, $resource, new Config(self::CONFIG));
857857

app/code/Magento/Catalog/Model/Product/ProductFrontendAction/Synchronizer.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,9 @@ private function filterNewestActions(array $productsData, $typeId)
133133
$productsData,
134134
function (array $firstProduct, array $secondProduct) {
135135
if (isset($firstProduct['added_at'], $secondProduct['added_at'])) {
136-
return $firstProduct['added_at'] > $secondProduct['added_at'];
136+
return ($firstProduct['added_at'] <=> $secondProduct['added_at']);
137137
}
138-
139-
return false;
138+
return 0;
140139
}
141140
);
142141

app/code/Magento/CatalogGraphQl/Model/Resolver/Products/DataProvider/ExtractDataFromCategoryTree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private function sortTree(array $tree): array
154154
foreach ($tree as &$node) {
155155
if ($node['children']) {
156156
uasort($node['children'], function ($element1, $element2) {
157-
return $element1['position'] > $element2['position'];
157+
return ($element1['position'] <=> $element2['position']);
158158
});
159159
$node['children'] = $this->sortTree($node['children']);
160160
}

app/code/Magento/Config/App/Config/Source/DumpConfigSourceAggregated.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private function filterPath($path)
209209
private function sortSources()
210210
{
211211
uasort($this->sources, function ($firstItem, $secondItem) {
212-
return $firstItem['sortOrder'] > $secondItem['sortOrder'];
212+
return ($firstItem['sortOrder'] <=> $secondItem['sortOrder']);
213213
});
214214
}
215215

dev/tests/integration/testsuite/Magento/Framework/Jwt/JwtManagerTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ private function createRsaKeys(): array
997997
if (!openssl_pkey_export($rsaPrivateResource, $rsaPrivate, 'pass')) {
998998
throw new \RuntimeException('Failed to read RSA private key');
999999
}
1000-
openssl_free_key($rsaPrivateResource);
1000+
$this->freeResource($rsaPrivateResource);
10011001

10021002
return [$rsaPrivate, $rsaPublic];
10031003
}
@@ -1024,11 +1024,23 @@ private function createEcKeys(): array
10241024
if (!openssl_pkey_export($privateResource, $esPrivate, 'pass')) {
10251025
throw new \RuntimeException('Failed to read EC private key');
10261026
}
1027-
openssl_free_key($privateResource);
1027+
$this->freeResource($privateResource);
10281028
$ecKeys[$bits] = [$esPrivate, $esPublic];
10291029
unset($privateResource, $esPublic, $esPrivate);
10301030
}
10311031

10321032
return $ecKeys;
10331033
}
1034+
1035+
/**
1036+
* @param mixed $resource
1037+
*
1038+
* @return void
1039+
*/
1040+
private function freeResource($resource): void
1041+
{
1042+
if (\is_resource($resource) && (version_compare(PHP_VERSION, '8.0') < 0)) {
1043+
openssl_free_key($resource);
1044+
}
1045+
}
10341046
}

dev/tests/static/testsuite/Magento/Test/Legacy/ObsoleteCodeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ protected function _testObsoleteClasses($content)
210210
foreach (self::$_classes as $row) {
211211
list($class, , $replacement) = $row;
212212
$this->_assertNotRegExp(
213-
'/[^a-z\d_]' . preg_quote($class, '/') . '[^a-z\d_\\\\]/iS',
213+
'/[^a-z\d_\$]' . preg_quote($class, '/') . '[^a-z\d_\\\\]/iS',
214214
$content,
215215
$this->_suggestReplacement(sprintf("Class '%s' is obsolete.", $class), $replacement)
216216
);

lib/internal/Magento/Framework/Jwt/JwkFactory.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ private function createPrivateRsa(string $key, ?string $pass, string $use, strin
785785
{
786786
$resource = openssl_get_privatekey($key, (string)$pass);
787787
$keyData = openssl_pkey_get_details($resource)['rsa'];
788-
openssl_free_key($resource);
788+
$this->freeResource($resource);
789789
$keysMap = [
790790
'n' => 'n',
791791
'e' => 'e',
@@ -821,7 +821,7 @@ private function createPublicRsa(string $key, string $use, string $algorithm, ?s
821821
{
822822
$resource = openssl_get_publickey($key);
823823
$keyData = openssl_pkey_get_details($resource)['rsa'];
824-
openssl_free_key($resource);
824+
$this->freeResource($resource);
825825
$keysMap = [
826826
'n' => 'n',
827827
'e' => 'e'
@@ -857,7 +857,7 @@ private function createPrivateEc(
857857
): Jwk {
858858
$resource = openssl_get_privatekey($key, (string)$pass);
859859
$keyData = openssl_pkey_get_details($resource)['ec'];
860-
openssl_free_key($resource);
860+
$this->freeResource($resource);
861861
if (!array_key_exists($keyData['curve_oid'], self::EC_CURVE_MAP)) {
862862
throw new \RuntimeException('Unsupported EC curve');
863863
}
@@ -895,7 +895,7 @@ private function createPublicEc(
895895
): Jwk {
896896
$resource = openssl_get_publickey($key);
897897
$keyData = openssl_pkey_get_details($resource)['ec'];
898-
openssl_free_key($resource);
898+
$this->freeResource($resource);
899899
if (!array_key_exists($keyData['curve_oid'], self::EC_CURVE_MAP)) {
900900
throw new \RuntimeException('Unsupported EC curve');
901901
}
@@ -949,4 +949,16 @@ private static function base64Decode(string $encoded): string
949949

950950
return $value;
951951
}
952+
953+
/**
954+
* @param mixed $resource
955+
*
956+
* @return void
957+
*/
958+
private function freeResource($resource): void
959+
{
960+
if (\is_resource($resource) && (version_compare(PHP_VERSION, '8.0') < 0)) {
961+
openssl_free_key($resource);
962+
}
963+
}
952964
}

lib/internal/Magento/Framework/Webapi/Rest/Request/Deserializer/Xml.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,18 @@ public function deserialize($xmlRequestBody)
5757
);
5858
}
5959
/** Disable external entity loading to prevent possible vulnerability */
60-
$previousLoaderState = libxml_disable_entity_loader(true);
60+
if (version_compare(PHP_VERSION, '8.0') < 0) {
61+
// this function no longer has an effect in PHP 8.0, but it's required in earlier versions
62+
$previousLoaderState = libxml_disable_entity_loader(true);
63+
}
6164
set_error_handler([$this, 'handleErrors']);
6265

6366
$this->_xmlParser->loadXML($xmlRequestBody);
6467

6568
restore_error_handler();
66-
libxml_disable_entity_loader($previousLoaderState);
69+
if (isset($previousLoaderState)) {
70+
libxml_disable_entity_loader($previousLoaderState);
71+
}
6772

6873
/** Process errors during XML parsing. */
6974
if ($this->_errorMessage !== null) {

lib/internal/Magento/Framework/Xml/Security.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public function scan($xmlContent)
5454

5555
$document = new DOMDocument();
5656

57-
$loadEntities = libxml_disable_entity_loader(true);
57+
if (version_compare(PHP_VERSION, '8.0') < 0) {
58+
// this function no longer has an effect in PHP 8.0, but it's required in earlier versions
59+
$loadEntities = libxml_disable_entity_loader(true);
60+
}
5861
$useInternalXmlErrors = libxml_use_internal_errors(true);
5962

6063
/**
@@ -74,7 +77,9 @@ function ($errno, $errstr) {
7477
$result = (bool)$document->loadXML($xmlContent, LIBXML_NONET);
7578
restore_error_handler();
7679
// Entity load to previous setting
77-
libxml_disable_entity_loader($loadEntities);
80+
if (isset($loadEntities)) {
81+
libxml_disable_entity_loader($loadEntities);
82+
}
7883
libxml_use_internal_errors($useInternalXmlErrors);
7984

8085
if (!$result) {

0 commit comments

Comments
 (0)