EditMaterialUseCase   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 50
wmc 5
lcom 1
cbo 4
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 18 2
A callWarningLimitationAssignOnlyToLeaf() 0 7 2
1
<?php
2
/**
3
 * (c) Tomasz Kunicki <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace Domain\Material\UseCase\EditMaterial;
9
10
use Domain\Category\Entity\CategoryInterface;
11
use Domain\Common\UseCase\ResponderAwareTrait;
12
use Domain\Common\UseCase\ResponderInterface;
13
use Domain\Material\Repository\MaterialRepositoryInterface;
14
15
/**
16
 * Class EditMaterialUseCase
17
 *
18
 * @package Domain\Material\UseCase\EditMaterial
19
 */
20
class EditMaterialUseCase implements ResponderInterface
21
{
22
    use ResponderAwareTrait;
23
24
    /** @var  MaterialRepositoryInterface */
25
    private $materialRepository;
26
27
    /**
28
     * EditMaterialUseCase constructor.
29
     *
30
     * @param MaterialRepositoryInterface $materialRepository
31
     */
32
    public function __construct(MaterialRepositoryInterface $materialRepository)
33
    {
34
        $this->materialRepository = $materialRepository;
35
    }
36
37
    /**
38
     * @param EditMaterialRequest $request
39
     */
40
    public function execute(EditMaterialRequest $request)
41
    {
42
        $material = $this->materialRepository->findById($request->getMaterialId());
43
44
        $category = $request->getCategory();
45
        if (count($category->getChildren())) {
0 ignored issues
show
Bug introduced by
The method getChildren() does not seem to exist on object<Domain\Category\Entity\CategoryInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
            $this->callWarningLimitationAssignOnlyToLeaf($category);
47
48
            return;
49
        }
50
51
52
        $material->compose($request->getNewName(), $request->getNewCode());
53
        $material->setCategory($category);
54
        $material->setUnit($request->getUnit());
55
56
        $this->materialRepository->update($material);
57
    }
58
59
    /**
60
     * @param CategoryInterface $category
61
     */
62
    private function callWarningLimitationAssignOnlyToLeaf(CategoryInterface $category)
63
    {
64
        /** @var EditMaterialResponderInterface $responder */
65
        foreach ($this->responders as $responder) {
66
            $responder->callWarningLimitationAssignOnlyToLeaf($category);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Domain\Common\UseCase\ResponderInterface as the method callWarningLimitationAssignOnlyToLeaf() does only exist in the following implementations of said interface: Domain\Material\UseCase\...ial\EditMaterialUseCase.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
67
        }
68
    }
69
}