ListCategoryUseCase::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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\Category\UseCase\ListCategory;
9
10
use Domain\Category\Entity\CategoryInterface;
11
use Domain\Category\Repository\CategoryRepositoryInterface;
12
use Domain\Common\UseCase\ResponderAwareInterface;
13
use Domain\Common\UseCase\ResponderAwareTrait;
14
15
/**
16
 * Class ListCategoryUseCase
17
 *
18
 * @package Domain\Category\UseCase\ListCategory
19
 */
20
class ListCategoryUseCase implements ResponderAwareInterface
21
{
22
    use ResponderAwareTrait;
23
24
    /** @var  CategoryRepositoryInterface */
25
    private $categoryRepository;
26
27
    /**
28
     * ListCategoryUseCase constructor.
29
     *
30
     * @param CategoryRepositoryInterface $categoryRepository
31
     */
32
    public function __construct(CategoryRepositoryInterface $categoryRepository)
33
    {
34
        $this->categoryRepository = $categoryRepository;
35
    }
36
37
    /**
38
     *
39
     */
40
    public function execute()
41
    {
42
        $categories = $this->categoryRepository->findAll();
43
        $items = $this->fetchCategoriesItems($categories);
44
        $this->listFetched($items);
45
    }
46
47
    /**
48
     * @param $categories
49
     * @return CategoryListItem[]
50
     */
51
    private function fetchCategoriesItems($categories)
52
    {
53
        $list = [];
54
        /** @var CategoryInterface $category */
55
        foreach ($categories as $category) {
56
            $list[] = new CategoryListItem($category->getId(), $category->getName());
57
        }
58
59
        return $list;
60
    }
61
62
    /**
63
     * @param $items
64
     */
65
    private function listFetched($items)
66
    {
67
        /** @var ListCategoryResponderInterface $responder */
68
        foreach ($this->responders as $responder) {
69
            $responder->listFetched(new ListCategoryResponse($items));
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 listFetched() does only exist in the following implementations of said interface: Domain\Material\UseCase\...ial\ListMaterialUseCase.

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...
70
        }
71
    }
72
}