Skip to content

Commit 4409572

Browse files
authored
refactor: use xxh hash (#85)
1 parent 7f5da1d commit 4409572

17 files changed

+53
-51
lines changed

phpstan.neon.dist

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,6 @@ parameters:
2525
paths:
2626
- tests/**/*Test.php
2727

28-
- # Ignore hash functions rule for the following files because we use it in another context
29-
message: '#Do not use .* function, use class Shopware\\Core\\Framework\\Util\\Hasher instead#'
30-
paths:
31-
- src/Migration/Media/Processor/HttpDownloadServiceBase.php
32-
- src/Migration/Service/MigrationDataConverter.php
33-
- src/Profile/Shopware/Converter/OrderConverter.php
34-
- src/Profile/Shopware/Converter/ProductConverter.php
35-
- src/Profile/Shopware/Converter/ProductOptionRelationConverter.php
36-
- src/Profile/Shopware/Converter/ProductPropertyRelationConverter.php
37-
- src/Profile/Shopware/Converter/PropertyGroupOptionConverter.php
38-
- src/Profile/Shopware/Converter/ShippingMethodConverter.php
39-
- src/Profile/Shopware/Media/Strategy/Md5StrategyResolver.php
40-
- src/Migration/Converter/Converter.php
41-
4228
- # To fix those issues, the AbstractProvider needs a generic type, which could then be used further down in the methods as typehints for other generic typed parameters
4329
message: '#(readTotalFromRepo|readTableFromRepo|cleanupSearchResult)\(\) has parameter .* with generic class#'
4430
path: src/DataProvider/Provider/Data/AbstractProvider.php

src/Migration/Converter/Converter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Shopware\Core\Framework\Context;
1111
use Shopware\Core\Framework\Log\Package;
12+
use Shopware\Core\Framework\Util\Hasher;
1213
use SwagMigrationAssistant\Migration\Logging\LoggingServiceInterface;
1314
use SwagMigrationAssistant\Migration\Mapping\MappingServiceInterface;
1415
use SwagMigrationAssistant\Migration\MigrationContextInterface;
@@ -58,7 +59,7 @@ public function getMediaUuids(array $converted): ?array
5859
*/
5960
protected function generateChecksum(array $data): void
6061
{
61-
$this->checksum = \md5(\serialize($data));
62+
$this->checksum = Hasher::hash(\serialize($data));
6263
}
6364

6465
/**

src/Migration/Media/Processor/HttpDownloadServiceBase.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
2121
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
2222
use Shopware\Core\Framework\Log\Package;
23+
use Shopware\Core\Framework\Util\Hasher;
2324
use Shopware\Core\Framework\Uuid\Uuid;
2425
use SwagMigrationAssistant\Exception\MigrationException;
2526
use SwagMigrationAssistant\Migration\Gateway\HttpClientInterface;
@@ -321,13 +322,13 @@ private function persistFileToMedia(string $filePath, string $uuid, string $name
321322
return;
322323
}
323324

324-
$fileHash = \hash_file('md5', $filePath);
325+
$fileHash = Hasher::hashFile($filePath);
325326
$mediaFile = new MediaFile(
326327
$filePath,
327328
$mimeType,
328329
$fileExtension,
329330
$fileSize,
330-
$fileHash === false ? null : $fileHash
331+
$fileHash
331332
);
332333
$name = \preg_replace('/[^a-zA-Z0-9_-]+/', '-', \mb_strtolower($name)) ?? $uuid;
333334

src/Migration/Service/MigrationDataConverter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Shopware\Core\Framework\DataAbstractionLayer\Write\EntityWriterInterface;
1313
use Shopware\Core\Framework\DataAbstractionLayer\Write\WriteContext;
1414
use Shopware\Core\Framework\Log\Package;
15+
use Shopware\Core\Framework\Util\Hasher;
1516
use SwagMigrationAssistant\Migration\Converter\ConverterInterface;
1617
use SwagMigrationAssistant\Migration\Converter\ConverterRegistryInterface;
1718
use SwagMigrationAssistant\Migration\DataSelection\DataSet\DataSet;
@@ -153,7 +154,7 @@ private function filterDeltas(array $data, ConverterInterface $converter, Migrat
153154

154155
foreach ($data as $dataSet) {
155156
$mappedData[$converter->getSourceIdentifier($dataSet)] = $dataSet;
156-
$checksums[$converter->getSourceIdentifier($dataSet)] = \md5(\serialize($dataSet));
157+
$checksums[$converter->getSourceIdentifier($dataSet)] = Hasher::hash(\serialize($dataSet));
157158
}
158159

159160
$dataSet = $migrationContext->getDataSet();

src/Profile/Shopware/Converter/OrderConverter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
2929
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
3030
use Shopware\Core\Framework\Log\Package;
31+
use Shopware\Core\Framework\Util\Hasher;
3132
use Shopware\Core\System\SalesChannel\SalesChannelCollection;
3233
use SwagMigrationAssistant\Exception\AssociationEntityRequiredMissingException;
3334
use SwagMigrationAssistant\Exception\MigrationException;
@@ -386,7 +387,7 @@ public function convert(
386387
}
387388
unset($data['locale']);
388389

389-
$converted['deepLinkCode'] = \md5($converted['id']);
390+
$converted['deepLinkCode'] = Hasher::hash($converted['id']);
390391

391392
// Legacy data which don't need a mapping or there is no equivalent field
392393
unset(

src/Profile/Shopware/Converter/ProductConverter.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Shopware\Core\Framework\Log\Package;
1919
use Shopware\Core\Framework\Rule\Container\AndRule;
2020
use Shopware\Core\Framework\Rule\Container\OrRule;
21+
use Shopware\Core\Framework\Util\Hasher;
2122
use Shopware\Core\Framework\Uuid\Uuid;
2223
use SwagMigrationAssistant\Exception\MigrationException;
2324
use SwagMigrationAssistant\Migration\Converter\ConvertStruct;
@@ -672,14 +673,14 @@ private function applyOptions(array &$converted, array &$data): void
672673
$optionMapping = $this->mappingService->getOrCreateMapping(
673674
$this->connectionId,
674675
DefaultEntities::PROPERTY_GROUP_OPTION,
675-
\hash('md5', \mb_strtolower($option['name'] . '_' . $option['group']['name'])),
676+
Hasher::hash(\mb_strtolower($option['name'] . '_' . $option['group']['name']), 'md5'),
676677
$this->context
677678
);
678679
$this->mappingIds[] = $optionMapping['id'];
679680
$optionGroupMapping = $this->mappingService->getOrCreateMapping(
680681
$this->connectionId,
681682
DefaultEntities::PROPERTY_GROUP,
682-
\hash('md5', \mb_strtolower($option['group']['name'])),
683+
Hasher::hash(\mb_strtolower($option['group']['name']), 'md5'),
683684
$this->context
684685
);
685686
$this->mappingIds[] = $optionGroupMapping['id'];
@@ -1228,7 +1229,7 @@ private function applyOptionTranslation(array &$option, array $data): void
12281229
$mapping = $this->mappingService->getOrCreateMapping(
12291230
$this->connectionId,
12301231
DefaultEntities::PROPERTY_GROUP_OPTION_TRANSLATION,
1231-
\hash('md5', \mb_strtolower($data['name'] . '_' . $data['group']['name'])) . ':' . $this->locale,
1232+
Hasher::hash(\mb_strtolower($data['name'] . '_' . $data['group']['name']), 'md5') . ':' . $this->locale,
12321233
$this->context
12331234
);
12341235
$localeOptionTranslation['id'] = $mapping['entityId'];
@@ -1240,7 +1241,7 @@ private function applyOptionTranslation(array &$option, array $data): void
12401241
$mapping = $this->mappingService->getOrCreateMapping(
12411242
$this->connectionId,
12421243
DefaultEntities::PROPERTY_GROUP_TRANSLATION,
1243-
\hash('md5', \mb_strtolower($data['group']['name'])) . ':' . $this->locale,
1244+
Hasher::hash(\mb_strtolower($data['group']['name']), 'md5') . ':' . $this->locale,
12441245
$this->context
12451246
);
12461247
$localeGroupTranslation['id'] = $mapping['entityId'];

src/Profile/Shopware/Converter/ProductOptionRelationConverter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Shopware\Core\Framework\Context;
1111
use Shopware\Core\Framework\Log\Package;
12+
use Shopware\Core\Framework\Util\Hasher;
1213
use SwagMigrationAssistant\Migration\Converter\ConvertStruct;
1314
use SwagMigrationAssistant\Migration\DataSelection\DefaultEntities;
1415
use SwagMigrationAssistant\Migration\MigrationContextInterface;
@@ -51,7 +52,7 @@ public function convert(array $data, Context $context, MigrationContextInterface
5152
$optionMapping = $this->mappingService->getMapping(
5253
$this->connectionId,
5354
DefaultEntities::PROPERTY_GROUP_OPTION,
54-
\hash('md5', \mb_strtolower($data['name'] . '_' . $data['group']['name'])),
55+
Hasher::hash(\mb_strtolower($data['name'] . '_' . $data['group']['name']), 'md5'),
5556
$context
5657
);
5758

src/Profile/Shopware/Converter/ProductPropertyRelationConverter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Shopware\Core\Framework\Context;
1111
use Shopware\Core\Framework\Log\Package;
12+
use Shopware\Core\Framework\Util\Hasher;
1213
use SwagMigrationAssistant\Migration\Converter\ConvertStruct;
1314
use SwagMigrationAssistant\Migration\DataSelection\DefaultEntities;
1415
use SwagMigrationAssistant\Migration\MigrationContextInterface;
@@ -62,7 +63,7 @@ public function convert(array $data, Context $context, MigrationContextInterface
6263
$optionMapping = $this->mappingService->getMapping(
6364
$this->connectionId,
6465
DefaultEntities::PROPERTY_GROUP_OPTION,
65-
\hash('md5', \mb_strtolower($data['name'] . '_' . $data['group']['name'])),
66+
Hasher::hash(\mb_strtolower($data['name'] . '_' . $data['group']['name']), 'md5'),
6667
$context
6768
);
6869

src/Profile/Shopware/Converter/PropertyGroupOptionConverter.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Shopware\Core\Content\Property\PropertyGroupDefinition;
1111
use Shopware\Core\Framework\Context;
1212
use Shopware\Core\Framework\Log\Package;
13+
use Shopware\Core\Framework\Util\Hasher;
1314
use SwagMigrationAssistant\Migration\Converter\ConvertStruct;
1415
use SwagMigrationAssistant\Migration\DataSelection\DefaultEntities;
1516
use SwagMigrationAssistant\Migration\Logging\Log\Builder\SwagMigrationLogBuilder;
@@ -49,7 +50,7 @@ public function getSourceIdentifier(array $data): string
4950
$group = $data['group']['name'];
5051
}
5152

52-
return \hash('md5', \mb_strtolower($data['name'] . '_' . $group . '_' . $data['type']));
53+
return Hasher::hash(\mb_strtolower($data['name'] . '_' . $group . '_' . $data['type']), 'md5');
5354
}
5455

5556
public function getMediaUuids(array $converted): ?array
@@ -92,15 +93,15 @@ public function convert(array $data, Context $context, MigrationContextInterface
9293
$mapping = $this->mappingService->getOrCreateMapping(
9394
$this->connectionId,
9495
DefaultEntities::PROPERTY_GROUP_OPTION,
95-
\hash('md5', \mb_strtolower($data['name'] . '_' . $data['group']['name'])),
96+
Hasher::hash(\mb_strtolower($data['name'] . '_' . $data['group']['name']), 'md5'),
9697
$context
9798
);
9899
$this->mappingIds[] = $mapping['id'];
99100

100101
$propertyGroupMapping = $this->mappingService->getOrCreateMapping(
101102
$this->connectionId,
102103
DefaultEntities::PROPERTY_GROUP,
103-
\hash('md5', \mb_strtolower($data['group']['name'])),
104+
Hasher::hash(\mb_strtolower($data['group']['name']), 'md5'),
104105
$context
105106
);
106107
$this->mappingIds[] = $propertyGroupMapping['id'];
@@ -257,15 +258,15 @@ protected function createAndDeleteNecessaryMappings(array $data, array $converte
257258
$this->mainMapping = $this->mappingService->getOrCreateMapping(
258259
$this->connectionId,
259260
DefaultEntities::PROPERTY_GROUP_OPTION,
260-
\hash('md5', \mb_strtolower($data['name'] . '_' . $data['group']['name'] . '_' . $data['type'])),
261+
Hasher::hash(\mb_strtolower($data['name'] . '_' . $data['group']['name'] . '_' . $data['type']), 'md5'),
261262
$this->context,
262263
$this->checksum
263264
);
264265

265266
$mapping = $this->mappingService->getOrCreateMapping(
266267
$this->connectionId,
267268
DefaultEntities::PROPERTY_GROUP_OPTION,
268-
\hash('md5', \mb_strtolower($data['group']['name'] . '_' . $data['type'])),
269+
Hasher::hash(\mb_strtolower($data['group']['name'] . '_' . $data['type']), 'md5'),
269270
$this->context
270271
);
271272
$this->mappingIds[] = $mapping['id'];

src/Profile/Shopware/Converter/ShippingMethodConverter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Shopware\Core\Framework\Log\Package;
1414
use Shopware\Core\Framework\Rule\Container\AndRule;
1515
use Shopware\Core\Framework\Rule\Container\OrRule;
16+
use Shopware\Core\Framework\Util\Hasher;
1617
use SwagMigrationAssistant\Migration\Converter\ConvertStruct;
1718
use SwagMigrationAssistant\Migration\DataSelection\DefaultEntities;
1819
use SwagMigrationAssistant\Migration\Logging\Log\Builder\SwagMigrationLogBuilder;
@@ -748,7 +749,7 @@ private function setCustomAvailabilityRule(array $data, array &$converted): void
748749
$ruleData = $this->getRelevantDataForAvailabilityRule($data);
749750

750751
$jsonRuleData = \json_encode($ruleData, \JSON_THROW_ON_ERROR);
751-
$hash = \md5($jsonRuleData);
752+
$hash = Hasher::hash($jsonRuleData, 'md5');
752753

753754
$mainRuleMapping = $this->mappingService->getOrCreateMapping(
754755
$this->connectionId,

0 commit comments

Comments
 (0)