callWarningLimitationAssignOnlyToLeaf()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
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\CreateMaterial;
9
10
use Domain\Category\Entity\CategoryInterface;
11
use Domain\Common\UseCase\ResponderAwareInterface;
12
use Domain\Common\UseCase\ResponderAwareTrait;
13
use Domain\Material\MaterialFactoryInterface;
14
use Domain\Material\Repository\MaterialRepositoryInterface;
15
16
/**
17
 * Class CreateMaterialUseCase
18
 *
19
 * @package Domain\Material\UseCase\CreateMaterial
20
 */
21
class CreateMaterialUseCase implements ResponderAwareInterface
22
{
23
    use ResponderAwareTrait;
24
25
    /** @var  MaterialRepositoryInterface */
26
    private $materialRepository;
27
28
    /** @var  MaterialFactoryInterface */
29
    private $materialFactory;
30
31
    /**
32
     * CreateMaterialUseCase constructor.
33
     *
34
     * @param MaterialRepositoryInterface $materialRepository
35
     * @param MaterialFactoryInterface $materialFactory
36
     */
37
    public function __construct(
38
        MaterialRepositoryInterface $materialRepository,
39
        MaterialFactoryInterface $materialFactory
40
    ) {
41
        $this->materialRepository = $materialRepository;
42
        $this->materialFactory = $materialFactory;
43
    }
44
45
    /**
46
     * @param CreateMaterialRequest $request
47
     */
48
    public function execute(CreateMaterialRequest $request)
49
    {
50
        $category = $request->getCategory();
51
        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...
52
            $this->callWarningLimitationAssignOnlyToLeaf($category);
53
54
            return;
55
        }
56
57
        $material = $this->materialFactory->create(
58
            $request->getName(),
59
            $request->getCode(),
60
            $request->getCategory(),
61
            $request->getUnit()
62
        );
63
        $this->materialRepository->add($material);
64
    }
65
66
    /**
67
     * @param CategoryInterface $category
68
     */
69
    private function callWarningLimitationAssignOnlyToLeaf(CategoryInterface $category)
70
    {
71
        /** @var CreateMaterialResponderInterface $responder */
72
        foreach ($this->responders as $responder) {
73
            $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...
74
        }
75
    }
76
}