Skip to content

Commit 65d188e

Browse files
committed
PT-12993 - Improve code quality
1 parent e9d97ee commit 65d188e

20 files changed

+70
-174
lines changed

.gitlab-ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,6 @@ SW 5.7 PHP 7.4:
122122
only:
123123
- master
124124

125-
SW 5.7 PHP 8.0:
125+
SW 5.7 PHP 8.1:
126126
extends: .SW 5.7 default
127-
image: gitlab.shopware.com:5005/shopware/5/product/image/continuous:8.0
127+
image: gitlab.shopware.com:5005/shopware/5/product/image/continuous:8.1

.php-cs-fixer.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*
@@ -39,6 +40,7 @@
3940
'blank_line_after_opening_tag' => false,
4041
'class_attributes_separation' => ['elements' => ['method' => 'one', 'property' => 'one']],
4142
'concat_space' => ['spacing' => 'one'],
43+
'declare_strict_types' => true,
4244
'doctrine_annotation_indentation' => true,
4345
'doctrine_annotation_spaces' => true,
4446
'general_phpdoc_annotation_remove' => ['annotations' => ['copyright', 'category']],

Bootstrap/Setup.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*
@@ -80,6 +81,8 @@ public function uninstall(): void
8081

8182
/**
8283
* The update method handles the migration of the old data.
84+
*
85+
* @param string $oldVersion
8386
*/
8487
public function update($oldVersion): bool
8588
{
@@ -271,13 +274,9 @@ private function importOldData(): void
271274
*/
272275
private function removeMenuEntry(): void
273276
{
274-
$menuItem = $this->getEntityManager()->getRepository(Menu::class)->findOneBy(
275-
[
276-
'label' => 'Kundenspezifische Preise',
277-
]
278-
);
277+
$menuItem = $this->modelManager->getRepository(Menu::class)->findOneBy(['controller' => 'UserPrice']);
279278

280-
if (!$menuItem) {
279+
if (!$menuItem instanceof Menu) {
281280
return;
282281
}
283282

Bundle/SearchBundleDBAL/PriceHelper.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*
@@ -70,6 +71,8 @@ public function getSelection(ProductContextInterface $context)
7071
* joins the user-prices from the plugin now.
7172
*
7273
* {@inheritdoc}
74+
*
75+
* @return void
7376
*/
7477
public function joinPrices(QueryBuilder $query, ShopContextInterface $context)
7578
{
@@ -104,6 +107,8 @@ public function joinPrices(QueryBuilder $query, ShopContextInterface $context)
104107
* Additionally the user-prices from this plugin are joined.
105108
*
106109
* {@inheritdoc}
110+
*
111+
* @return void
107112
*/
108113
public function joinDefaultPrices(QueryBuilder $query, ShopContextInterface $context)
109114
{
@@ -124,16 +129,20 @@ public function joinDefaultPrices(QueryBuilder $query, ShopContextInterface $con
124129

125130
/**
126131
* {@inheritdoc}
132+
*
133+
* @return void
127134
*/
128135
public function joinAvailableVariant(QueryBuilder $query)
129136
{
130-
return $this->coreHelper->joinAvailableVariant($query);
137+
$this->coreHelper->joinAvailableVariant($query);
131138
}
132139

133140
/**
134141
* Builds the query to join all the needed prices.
135142
* Default-price for the default customer-group, customer-price for the current customer-group and
136143
* the own user-prices from this plugin.
144+
*
145+
* @param array{0: string, 1: string} $group
137146
*/
138147
public function buildQuery(QueryBuilder $query, string $name, array $group): QueryBuilder
139148
{

Bundle/StoreFrontBundle/Service/Core/CheapestUserPriceService.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*

Bundle/StoreFrontBundle/Service/Core/GraduatedUserPricesService.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*
@@ -94,7 +95,7 @@ private function getCustomRules(PriceRule $coreRule, string $number): array
9495

9596
// This must not be translated!
9697
// Do not translate, this is not shown to the user and only used for the logic!
97-
$addEntry = $lastEntry['to'] != 'beliebig';
98+
$addEntry = $lastEntry['to'] !== 'beliebig';
9899

99100
if (!$addEntry) {
100101
return $customRules;

Bundle/StoreFrontBundle/Service/DependencyProvider.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*

Bundle/StoreFrontBundle/Service/DependencyProviderInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*

Components/AccessValidator.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*
@@ -9,8 +10,6 @@
910

1011
namespace SwagUserPrice\Components;
1112

12-
use Doctrine\DBAL\Query\QueryBuilder;
13-
use Enlight_Components_Session_Namespace as Session;
1413
use Shopware\Components\Model\ModelManager;
1514
use SwagUserPrice\Bundle\StoreFrontBundle\Service\DependencyProvider;
1615

@@ -23,7 +22,7 @@
2322
class AccessValidator
2423
{
2524
/**
26-
* @var Session
25+
* @var DependencyProvider
2726
*/
2827
private $dependencyProvider;
2928

@@ -43,6 +42,8 @@ public function __construct(DependencyProvider $dependencyProvider, ModelManager
4342
* If a product owns custom user-prices, this will return true.
4443
* In case there is no logged in user or the current article has no custom user-prices, it returns false.
4544
*
45+
* @param string $number
46+
*
4647
* @throws \Exception
4748
*/
4849
public function validateProduct($number): bool
@@ -68,10 +69,8 @@ public function validateProduct($number): bool
6869
->setParameter('number', $number)
6970
->execute()->fetchColumn();
7071

71-
/** @var QueryBuilder $builder */
72-
$builder = $this->modelManager->getDBALQueryBuilder();
73-
74-
$stmt = $builder->select('COUNT(prices.id)')
72+
$stmt = $this->modelManager->getDBALQueryBuilder()
73+
->select('COUNT(prices.id)')
7574
->from('s_plugin_pricegroups_prices', 'prices')
7675
->innerJoin(
7776
'prices',

Components/ServiceHelper.php

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*
@@ -14,7 +15,6 @@
1415
use Shopware\Components\Model\ModelManager;
1516
use Shopware_Components_Config as Config;
1617
use SwagUserPrice\Bundle\StoreFrontBundle\Service\DependencyProvider;
17-
use Symfony\Component\HttpFoundation\Session\Session;
1818

1919
/**
2020
* Plugin ServiceHelper class.
@@ -34,7 +34,7 @@ class ServiceHelper
3434
private $config;
3535

3636
/**
37-
* @var Session
37+
* @var DependencyProvider
3838
*/
3939
private $dependencyProvider;
4040

@@ -51,15 +51,15 @@ public function __construct(
5151
/**
5252
* Get the prices for a product.
5353
*/
54-
public function getPrices(string $number): ?array
54+
public function getPrices(string $number): array
5555
{
5656
$result = $this->getPricesQueryBuilder($number)
5757
->orderBy('prices.from', 'ASC')
5858
->execute()
5959
->fetchAll();
6060

61-
if ($result === false) {
62-
return null;
61+
if (empty($result)) {
62+
return [];
6363
}
6464

6565
return $result;
@@ -68,7 +68,7 @@ public function getPrices(string $number): ?array
6868
/**
6969
* Get a single price for a product.
7070
*/
71-
public function getPrice(string $number): ?array
71+
public function getPrice(string $number): array
7272
{
7373
$builder = $this->getPricesQueryBuilder($number);
7474
if ($this->config->get('useLastGraduationForCheapestPrice')) {
@@ -79,8 +79,8 @@ public function getPrice(string $number): ?array
7979
->execute()
8080
->fetch();
8181

82-
if ($result === false) {
83-
return null;
82+
if (!\is_array($result)) {
83+
return [];
8484
}
8585

8686
return $result;
@@ -101,7 +101,7 @@ public function getPriceForQuantity(string $number, int $quantity): ?array
101101
->execute()
102102
->fetch();
103103

104-
if ($result === false) {
104+
if (!\is_array($result)) {
105105
return null;
106106
}
107107

@@ -114,7 +114,7 @@ public function buildRule(array $price): PriceRule
114114
$priceRuleStruct->setPrice((float) $price['price']);
115115
$priceRuleStruct->setFrom((int) $price['from']);
116116
$priceRuleStruct->setTo((int) $price['to'] > 0 ? (int) $price['to'] : null);
117-
$priceRuleStruct->setPseudoPrice((float) 0);
117+
$priceRuleStruct->setPseudoPrice(0.0);
118118

119119
return $priceRuleStruct;
120120
}
@@ -158,17 +158,15 @@ private function getPricesQueryBuilder(string $number): QueryBuilder
158158

159159
private function getDetailIdByNumber(string $number): ?int
160160
{
161-
$result = $this->modelManager->getDBALQueryBuilder()
161+
$result = (int) $this->modelManager->getDBALQueryBuilder()
162162
->select('detail.id')
163-
->from(
164-
's_articles_details',
165-
'detail'
166-
)->where('detail.ordernumber = :number')
163+
->from('s_articles_details', 'detail')
164+
->where('detail.ordernumber = :number')
167165
->setParameter('number', $number)
168166
->execute()
169167
->fetchColumn();
170168

171-
if ($result === false) {
169+
if ($result === 0) {
172170
return null;
173171
}
174172

Components/UserPrice.php

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*
@@ -9,7 +10,6 @@
910

1011
namespace SwagUserPrice\Components;
1112

12-
use Doctrine\ORM\EntityRepository;
1313
use Shopware\Components\Model\ModelManager;
1414
use SwagUserPrice\Models\UserPrice\Group;
1515

@@ -27,26 +27,24 @@ public function __construct(ModelManager $modelManager)
2727

2828
/**
2929
* Formats the prices for an article in the backend.
30+
*
31+
* @param array<array<string, mixed>> $articles
32+
*
33+
* @return array<array<string, mixed>>
3034
*/
31-
public function formatArticlePrices(?array $articles, int $groupId): ?array
35+
public function formatArticlePrices(array $articles, int $groupId): array
3236
{
33-
/** @var Group $model */
34-
$model = $this->getRepository()->find($groupId);
37+
$priceGroup = $this->modelManager->getRepository(Group::class)->find($groupId);
3538

36-
if (!$model->getGross()) {
39+
if (!$priceGroup instanceof Group || !$priceGroup->getGross()) {
3740
return $articles;
3841
}
3942

40-
foreach ($articles as &$article) {
41-
$article['defaultPrice'] = round($article['defaultPrice'] / 100 * (100 + $article['tax']), 3);
42-
$article['current'] = round($article['current'] / 100 * (100 + $article['tax']), 3);
43+
foreach ($articles as &$product) {
44+
$product['defaultPrice'] = round($product['defaultPrice'] / 100 * (100 + $product['tax']), 3);
45+
$product['current'] = round($product['current'] / 100 * (100 + $product['tax']), 3);
4346
}
4447

4548
return $articles;
4649
}
47-
48-
private function getRepository(): EntityRepository
49-
{
50-
return $this->modelManager->getRepository(Group::class);
51-
}
5250
}

Models/UserPrice/Group.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*

Models/UserPrice/Price.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*

Subscriber/CacheKeyExtender.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*
@@ -57,7 +58,7 @@ public function onCreateCacheHash(\Enlight_Event_EventArgs $args): string
5758
return $originalHash;
5859
}
5960

60-
return json_encode(['original_hash' => $originalHash, 'swag_user_price_group' => $priceGroup]);
61+
return (string) json_encode(['original_hash' => $originalHash, 'swag_user_price_group' => $priceGroup]);
6162
}
6263

6364
private function getCustomerPriceGroupId(int $userId): int

Subscriber/TemplateRegistration.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*

SwagUserPrice.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* (c) shopware AG <[email protected]>
45
*
@@ -48,7 +49,7 @@ public function deactivate(DeactivateContext $deactivateContext)
4849
$deactivateContext->scheduleClearCache(DeactivateContext::CACHE_LIST_ALL);
4950
}
5051

51-
private function getSetup()
52+
private function getSetup(): Setup
5253
{
5354
return new Setup(
5455
$this->container->get('models'),

Tests/Functional/Components/AccessValidatorTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public function testValidateProductShouldReturnTrue(): void
4343
$validator = $this->getValidator();
4444

4545
$sql = file_get_contents(__DIR__ . '/_fixtures/prices.sql');
46+
static::assertIsString($sql);
4647
$this->getContainer()->get('dbal_connection')->exec($sql);
4748

4849
$this->getContainer()->get('session')->offsetSet('sUserId', 1);

0 commit comments

Comments
 (0)