Failed Conditions
Pull Request — experimental/sf (#3247)
by Kiyotaka
114:07 queued 103:28
created

StockMultipleValidator::formatProductName()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 0
cts 7
cp 0
crap 12
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Service\PurchaseFlow\Processor;
15
16
use Eccube\Entity\ItemHolderInterface;
17
use Eccube\Entity\ProductClass;
18
use Eccube\Repository\ProductClassRepository;
19
use Eccube\Service\PurchaseFlow\ItemHolderValidator;
20
use Eccube\Service\PurchaseFlow\PurchaseContext;
21
22
class StockMultipleValidator extends ItemHolderValidator
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
23
{
24
    /**
25
     * @var ProductClassRepository
26
     */
27
    protected $productClassRepository;
28
29
    /**
30
     * StockProcessor constructor.
31
     *
32
     * @param ProductClassRepository $productClassRepository
33
     */
34 64
    public function __construct(ProductClassRepository $productClassRepository)
35
    {
36 64
        $this->productClassRepository = $productClassRepository;
37
    }
38
39
    /**
40
     * @param ItemHolderInterface $itemHolder
41
     * @param PurchaseContext $context
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
42
     *
43
     * @throws \Eccube\Service\PurchaseFlow\InvalidItemException
44
     */
45 55
    public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context)
46
    {
47 55
        $OrderItemsByProductClass = [];
48 55 View Code Duplication
        foreach ($itemHolder->getItems() as $Item) {
49 55
            if ($Item->isProduct()) {
50 55
                $id = $Item->getProductClass()->getId();
51 55
                $OrderItemsByProductClass[$id][] = $Item;
52
            }
53
        }
54
55 55
        foreach ($OrderItemsByProductClass as $id => $Items) {
56 55
            $ProductClass = $this->productClassRepository->find($id);
57 55
            if ($ProductClass->isStockUnlimited()) {
58 24
                continue;
59
            }
60 34
            $stock = $ProductClass->getStock();
61 34
            if ($stock == 0) {
62 3
                $this->throwInvalidItemException('cart.zero.stock', $ProductClass);
0 ignored issues
show
Bug introduced by
It seems like $ProductClass defined by $this->productClassRepository->find($id) on line 56 can also be of type object; however, Eccube\Service\PurchaseF...wInvalidItemException() does only seem to accept null|object<Eccube\Entity\ProductClass>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
63
            }
64 32
            $total = 0;
65 32
            foreach ($Items as $Item) {
66 32
                $total += $Item->getQuantity();
67 32
                if ($stock < $total) {
68 32
                    $this->throwInvalidItemException('cart.over.stock', $ProductClass);
0 ignored issues
show
Bug introduced by
It seems like $ProductClass defined by $this->productClassRepository->find($id) on line 56 can also be of type object; however, Eccube\Service\PurchaseF...wInvalidItemException() does only seem to accept null|object<Eccube\Entity\ProductClass>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
69
                }
70
            }
71
        }
72
    }
73
74 View Code Duplication
    protected function formatProductName(ProductClass $ProductClass)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $productName = $ProductClass->getProduct()->getName();
77
        if ($ProductClass->hasClassCategory1()) {
78
            $productName .= ' - '.$ProductClass->getClassCategory1()->getName();
79
        }
80
        if ($ProductClass->hasClassCategory2()) {
81
            $productName .= ' - '.$ProductClass->getClassCategory2()->getName();
82
        }
83
84
        return $productName;
85
    }
86
}
87