Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

IndexablePagePartsService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Kunstmaan\NodeSearchBundle\Helper;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\PagePartBundle\Entity\PagePartRef;
7
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
8
use Kunstmaan\SearchBundle\Helper\IndexableInterface;
9
10
/**
11
 * Class IndexablePagePartsService
12
 *
13
 * Quick & dirty for now, needs to be in a generic PagePartService without static call &
14
 * without passing EntityManager as param...
15
 */
16
class IndexablePagePartsService
17
{
18
    /** @var EntityManagerInterface */
19
    private $em;
20
21
    /** @var [] */
22
    private $contexts;
23
24
    /**
25
     * IndexablePagePartsService constructor.
26
     *
27
     * @param EntityManagerInterface $em
28
     * @param []                     $contexts
0 ignored issues
show
Documentation introduced by
The doc-type [] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
29
     */
30
    public function __construct(EntityManagerInterface $em, $contexts = [])
31
    {
32
        $this->em = $em;
33
        $this->contexts = $contexts;
34
    }
35
36
    /**
37
     * Returns all indexable pageparts for the specified page and context
38
     *
39
     * @param HasPagePartsInterface $page
40
     * @param string                $context
41
     *
42
     * @return array
43
     */
44
    public function getIndexablePageParts(HasPagePartsInterface $page, $context = 'main')
45
    {
46
        $contexts = array_unique(array_merge($this->contexts, [$context]));
47
48
        $indexablePageParts = [];
49
        foreach ($contexts as $context) {
50
            $pageParts = $this->em
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getPageParts() does only exist in the following implementations of said interface: Kunstmaan\PagePartBundle...y\PagePartRefRepository.

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...
51
                ->getRepository(PagePartRef::class)
52
                ->getPageParts($page, $context);
53
54
            foreach ($pageParts as $pagePart) {
55
                if ($pagePart instanceof IndexableInterface && !$pagePart->isIndexable()) {
56
                    continue;
57
                }
58
                $indexablePageParts[] = $pagePart;
59
            }
60
        }
61
62
        return $indexablePageParts;
63
    }
64
}
65