Skip to content

Commit d47e8f3

Browse files
committed
[TASK] Add some phpstan extensions and raise level
Relates: #87
1 parent bd515ac commit d47e8f3

33 files changed

Lines changed: 194 additions & 275 deletions

Build/phpstan-baseline.neon

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertInstanceOf\(\) with ''Extcode\\\\CartEvents\\\\Domain\\\\Model\\\\AbstractEventDate'' and Extcode\\CartEvents\\Domain\\Model\\CalendarEntry will always evaluate to true\.$#'
5+
identifier: staticMethod.alreadyNarrowedType
6+
count: 1
7+
path: ../Tests/Unit/Domain/Model/CalenderEntryTest.php
8+
9+
-
10+
message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertInstanceOf\(\) with ''TYPO3\\\\CMS\\\\Extbase\\\\Domain\\\\Model\\\\Category'' and Extcode\\CartEvents\\Domain\\Model\\Category will always evaluate to true\.$#'
11+
identifier: staticMethod.alreadyNarrowedType
12+
count: 1
13+
path: ../Tests/Unit/Domain/Model/CategoryTest.php
14+
15+
-
16+
message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertInstanceOf\(\) with ''Extcode\\\\CartEvents\\\\Domain\\\\Model\\\\AbstractEventDate'' and Extcode\\CartEvents\\Domain\\Model\\EventDate will always evaluate to true\.$#'
17+
identifier: staticMethod.alreadyNarrowedType
18+
count: 1
19+
path: ../Tests/Unit/Domain/Model/EventDateTest.php

Build/phpstan.neon

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,38 @@
1+
includes:
2+
- 'phpstan-baseline.neon'
3+
14
parameters:
2-
level: 0
5+
level: 5
6+
37
paths:
48
- ../Classes
59
- ../Configuration
610
- ../Tests
711
- ../ext_emconf.php
812
- ../ext_localconf.php
13+
excludePaths:
14+
- '../Tests/Acceptance/Support/_generated/TesterActions.php'
15+
16+
disallowedFunctionCalls:
17+
-
18+
function:
19+
- 'var_dump()'
20+
- 'xdebug_break()'
21+
message: 'Do not add debugging'
22+
-
23+
function: 'header()'
24+
message: 'Use API instead'
25+
26+
disallowedStaticCalls:
27+
-
28+
method: 'TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump()'
29+
message: 'Do not add debugging'
30+
31+
disallowedSuperglobals:
32+
-
33+
superglobal:
34+
- '$_GET'
35+
- '$_POST'
36+
- '$_FILES'
37+
- '$_SERVER'
38+
message: 'Use API instead'

Classes/Controller/EventController.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
* LICENSE file that was distributed with this source code.
1212
*/
1313

14+
use Exception;
1415
use Extcode\Cart\Domain\Model\Cart\Cart;
1516
use Extcode\Cart\Service\SessionHandler;
1617
use Extcode\Cart\Utility\CartUtility;
18+
use Extcode\CartEvents\Domain\Model\Category;
1719
use Extcode\CartEvents\Domain\Model\Dto\EventDemand;
1820
use Extcode\CartEvents\Domain\Model\Event;
1921
use Extcode\CartEvents\Domain\Model\EventDate;
@@ -39,6 +41,7 @@ public function __construct(
3941
private readonly SessionHandler $sessionHandler,
4042
private readonly CartUtility $cartUtility,
4143
private readonly EventRepository $eventRepository,
44+
private readonly EventDateRepository $eventDateRepository,
4245
private readonly CategoryRepository $categoryRepository,
4346
) {}
4447

@@ -129,12 +132,15 @@ public function formAction(?EventDate $eventDate = null, ?PriceCategory $priceCa
129132
$priceCategoryId = (int)$argumentValue['priceCategoryId'];
130133

131134
if ($eventDateId) {
132-
$eventDateRepository = GeneralUtility::makeInstance(
133-
EventDateRepository::class
134-
);
135-
$eventDate = $eventDateRepository->findByUid($eventDateId);
136-
137-
$formDefinition = $eventDate->getEvent()->getFormDefinition();
135+
$eventDate = $this->eventDateRepository->findByUid($eventDateId);
136+
if (($eventDate instanceof EventDate) === false) {
137+
throw new Exception('Can not find EventDate with uid ' . $eventDateId . '.', 1769617660);
138+
}
139+
$event = $eventDate->getEvent();
140+
if (($event instanceof Event) === false) {
141+
throw new Exception('EventDate with uid ' . $eventDateId . ' has no event!', 1769617873);
142+
}
143+
$formDefinition = $event->getFormDefinition();
138144
$formPersistenceManager = GeneralUtility::makeInstance(
139145
\TYPO3\CMS\Form\Mvc\Persistence\FormPersistenceManagerInterface::class
140146
);
@@ -149,14 +155,17 @@ public function formAction(?EventDate $eventDate = null, ?PriceCategory $priceCa
149155
PriceCategoryRepository::class
150156
);
151157
$priceCategory = $priceCategoryRepository->findByUid($priceCategoryId);
158+
if (($priceCategory instanceof PriceCategory) === false) {
159+
throw new Exception('Can not find PriceCategory with uid ' . $priceCategoryId . '.', 1769642011);
160+
}
152161
}
153162
}
154163
}
155164
}
156165
}
157166

158-
if (!$eventDate) {
159-
throw new \InvalidArgumentException();
167+
if (($eventDate instanceof EventDate) === false) {
168+
throw new Exception('Can not find EventDate.', 1769641914);
160169
}
161170

162171
$this->view->assign('eventDate', $eventDate);
@@ -172,19 +181,19 @@ public function formAction(?EventDate $eventDate = null, ?PriceCategory $priceCa
172181
'type' => 'Hidden',
173182
'identifier' => 'productType',
174183
'label' => 'productType',
175-
'defaultValue' => ($eventDate ? 'CartEvents' : ''),
184+
'defaultValue' => 'CartEvents',
176185
],
177186
9998 => [
178187
'type' => 'Hidden',
179188
'identifier' => 'eventDateId',
180189
'label' => 'eventDateId',
181-
'defaultValue' => ($eventDate ? $eventDate->getUid() : ''),
190+
'defaultValue' => $eventDate->getUid(),
182191
],
183192
9999 => [
184193
'type' => 'Hidden',
185194
'identifier' => 'priceCategoryId',
186195
'label' => 'priceCategoryId',
187-
'defaultValue' => ($priceCategory ? $priceCategory->getUid() : ''),
196+
'defaultValue' => (($priceCategory instanceof PriceCategory) ? $priceCategory->getUid() : ''),
188197
],
189198
],
190199
],
@@ -250,6 +259,9 @@ private function addCategoriesToDemandObjectFromSettings(EventDemand &$demand):
250259
if ($this->settings['listSubcategories']) {
251260
foreach ($selectedCategories as $selectedCategory) {
252261
$category = $this->categoryRepository->findByUid($selectedCategory);
262+
if (($category instanceof Category) === false) {
263+
continue;
264+
}
253265
$categories = array_merge(
254266
$categories,
255267
$this->categoryRepository->findSubcategoriesRecursiveAsArray($category)

Classes/Controller/EventDateController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function listAction(): ResponseInterface
4949
ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK
5050
)['persistence']['storagePid'];
5151

52-
$eventDates = $this->eventDateRepository->findNext($limit, $pidList)->fetchAll();
52+
$eventDates = $this->eventDateRepository->findNext($limit, $pidList);
5353

5454
$this->view->assign('eventDates', $eventDates);
5555

Classes/Domain/Finisher/Form/AddToCartFinisher.php

Lines changed: 17 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,18 @@
1212
*/
1313

1414
use Extcode\Cart\Domain\Finisher\Form\AddToCartFinisherInterface;
15-
use Extcode\Cart\Domain\Model\Cart\BeVariant;
1615
use Extcode\Cart\Domain\Model\Cart\Cart;
1716
use Extcode\Cart\Domain\Model\Cart\FeVariant;
18-
use Extcode\Cart\Domain\Model\Cart\Product;
19-
use Extcode\CartEvents\Domain\Model\EventDate;
20-
use Extcode\CartEvents\Domain\Model\PriceCategory;
21-
use Extcode\CartEvents\Domain\Repository\EventDateRepository;
22-
use Extcode\CartEvents\Domain\Repository\PriceCategoryRepository;
17+
use Extcode\CartEvents\Domain\Model\Cart\ProductFactoryInterface;
2318
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
2419
use TYPO3\CMS\Core\Utility\GeneralUtility;
2520

2621
class AddToCartFinisher implements AddToCartFinisherInterface
2722
{
2823
protected Cart $cart;
2924

30-
protected EventDate $eventDate;
31-
32-
protected PriceCategory $priceCategory;
33-
3425
public function __construct(
35-
private readonly EventDateRepository $eventDateRepository,
36-
private readonly PriceCategoryRepository $priceCategoryRepository,
26+
private readonly ProductFactoryInterface $productFactory,
3727
) {}
3828

3929
public function getProductFromForm(
@@ -45,107 +35,37 @@ public function getProductFromForm(
4535
if ($formValues['productType'] !== 'CartEvents') {
4636
return [$errors, []];
4737
}
48-
49-
$eventDateId = $formValues['eventDateId'];
50-
$priceCategoryId = (int)$formValues['priceCategoryId'];
51-
5238
unset($formValues['productType']);
39+
40+
$requestArguments = [
41+
'eventDate' => $formValues['eventDateId'],
42+
'priceCategory' => $formValues['priceCategoryId'],
43+
'quantity' => $formValues['quantity'] ?? 1,
44+
];
5345
unset($formValues['eventDateId']);
5446
unset($formValues['priceCategoryId']);
47+
unset($formValues['quantity']);
5548

56-
$this->eventDate = $this->eventDateRepository->findByUid((int)$eventDateId);
57-
$quantity = 1;
58-
59-
if ($priceCategoryId) {
60-
$this->priceCategory = $this->priceCategoryRepository->findByUid((int)$priceCategoryId);
49+
if (!empty($formValues)) {
50+
$requestArguments['feVariant'] = $this->getFeVariant($formValues);
6151
}
62-
63-
$newProduct = $this->getProductFromEventDate($quantity, $cart->getTaxClasses(), $formValues);
52+
$newProduct = $this->productFactory->createProductFromRequestArguments(
53+
$requestArguments,
54+
$cart->getTaxClasses(),
55+
(bool)GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('cart_events', 'inputIsNetPrice')
56+
);
6457

6558
$newProduct->setMaxNumberInCart(1);
6659
$newProduct->setMinNumberInCart(1);
6760

6861
return [$errors, [$newProduct]];
6962
}
7063

71-
protected function getProductFromEventDate(
72-
int $quantity,
73-
array $taxClasses,
74-
array $feVariants = []
75-
): Product {
76-
$event = $this->eventDate->getEvent();
77-
$title = implode(' - ', [$event->getTitle(), $this->eventDate->getTitle()]);
78-
$sku = implode(' - ', [$event->getSku(), $this->eventDate->getSku()]);
79-
80-
$price = $this->eventDate->getBestPrice();
81-
if ($this->priceCategory) {
82-
$price = $this->priceCategory->getBestPrice();
83-
}
84-
85-
$inputIsNetPrice = (bool)GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('cart_events', 'inputIsNetPrice');
86-
87-
$product = new Product(
88-
'CartEvents',
89-
$this->eventDate->getUid(),
90-
$sku,
91-
$title,
92-
$price,
93-
$taxClasses[$event->getTaxClassId()],
94-
$quantity,
95-
$inputIsNetPrice,
96-
$this->getFeVariant($feVariants)
97-
);
98-
$product->setIsVirtualProduct($event->isVirtualProduct());
99-
100-
if ($this->priceCategory) {
101-
$product->addBeVariant($this->getProductBackendVariant($product, $quantity));
102-
}
103-
104-
if ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart_events']['getProductFromEventDate']) {
105-
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['cart_events']['getProductFromEventDate'] ?? [] as $className) {
106-
$params = [
107-
'cart' => $this->cart,
108-
'eventDate' => $this->eventDate,
109-
];
110-
111-
$_procObj = GeneralUtility::makeInstance($className);
112-
$_procObj->changeProductFromEventDate($product, $params);
113-
}
114-
}
115-
116-
return $product;
117-
}
118-
119-
protected function getProductBackendVariant(
120-
Product $product,
121-
int $quantity
122-
): BeVariant {
123-
$cartBackendVariant = GeneralUtility::makeInstance(
124-
BeVariant::class,
125-
PriceCategory::class . '-' . $this->priceCategory->getUid(),
126-
$product,
127-
$this->priceCategory->getTitle(),
128-
$this->priceCategory->getSku(),
129-
1,
130-
$this->priceCategory->getBestPrice(),
131-
$quantity
132-
);
133-
134-
/*
135-
TODO
136-
if ($bestSpecialPrice) {
137-
$cartBackendVariant->setSpecialPrice($bestSpecialPrice->getPrice());
138-
}
139-
*/
140-
141-
return $cartBackendVariant;
142-
}
143-
14464
protected function getFeVariant(array $data): ?FeVariant
14565
{
14666
$feVariant = null;
14767

148-
if (!empty($data) && is_array($data)) {
68+
if (!empty($data)) {
14969
$feVariants = [];
15070
foreach ($data as $dataKey => $dataValue) {
15171
if (!empty($dataKey) && !empty($dataValue)) {

Classes/Domain/Model/Cart/ProductFactory.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
use Extcode\Cart\Domain\Model\Cart\BeVariant;
13+
use Extcode\Cart\Domain\Model\Cart\FeVariant;
1314
use Extcode\Cart\Domain\Model\Cart\Product;
1415
use Extcode\CartEvents\Domain\Model\EventDate;
1516
use Extcode\CartEvents\Domain\Model\PriceCategory;
@@ -52,12 +53,18 @@ public function createProductFromRequestArguments(
5253
$priceCategory = $this->getPriceCategoryFromRequestArgument($requestArguments['priceCategory']);
5354
}
5455

56+
$feVariant = null;
57+
if (isset($requestArguments['feVariant']) && ($requestArguments['feVariant'] instanceof FeVariant)) {
58+
$feVariant = $requestArguments['feVariant'];
59+
}
60+
5561
return $this->getProductFromEventDate(
5662
$quantity,
5763
$taxClasses,
5864
$isNetPrice,
5965
$eventDate,
6066
$priceCategory,
67+
$feVariant
6168
);
6269
}
6370

@@ -84,10 +91,6 @@ private function getEventDateFromRequestArgument(
8491
private function getPriceCategoryFromRequestArgument(
8592
int $identifier,
8693
): PriceCategory {
87-
if (is_numeric($identifier) === false) {
88-
throw new InvalidArgumentException('Price category argument is invalid', 1741692831);
89-
}
90-
9194
$priceCategory = $this->priceCategoryRepository->findByUid($identifier);
9295

9396
if (($priceCategory instanceof PriceCategory) === false) {
@@ -107,6 +110,7 @@ private function getProductFromEventDate(
107110
bool $isNetPrice,
108111
EventDate $eventDate,
109112
?PriceCategory $priceCategory = null,
113+
?FeVariant $feVariant = null,
110114
): Product {
111115
$event = $eventDate->getEvent();
112116
$title = implode(' - ', [$event->getTitle(), $eventDate->getTitle()]);
@@ -128,7 +132,7 @@ private function getProductFromEventDate(
128132
$taxClasses[$event->getTaxClassId()],
129133
$quantity,
130134
$isNetPrice,
131-
null
135+
$feVariant
132136
);
133137
$product->setIsVirtualProduct($event->isVirtualProduct());
134138

Classes/Domain/Model/EventDate.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,12 @@ public function getBestSpecialPrice(?array $frontendUserGroupIds = null): ?Speci
181181

182182
$bestSpecialPrice = null;
183183

184-
if ($this->specialPrices) {
185-
foreach ($this->specialPrices as $specialPrice) {
186-
if (!isset($bestSpecialPrice) || $specialPrice->getPrice() < $bestSpecialPrice->getPrice()) {
187-
if (!$specialPrice->getFrontendUserGroup()
188-
|| in_array($specialPrice->getFrontendUserGroup()->getUid(), $frontendUserGroupIds)
189-
) {
190-
$bestSpecialPrice = $specialPrice;
191-
}
184+
foreach ($this->specialPrices as $specialPrice) {
185+
if (!isset($bestSpecialPrice) || $specialPrice->getPrice() < $bestSpecialPrice->getPrice()) {
186+
if (!$specialPrice->getFrontendUserGroup()
187+
|| in_array($specialPrice->getFrontendUserGroup()->getUid(), $frontendUserGroupIds)
188+
) {
189+
$bestSpecialPrice = $specialPrice;
192190
}
193191
}
194192
}

0 commit comments

Comments
 (0)