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

Helper/IndexablePagePartsService.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\NodeSearchBundle\Helper;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
7
use Kunstmaan\SearchBundle\Helper\IndexableInterface;
8
9
/**
10
 * Class IndexablePagePartsService
11
 *
12
 * Quick & dirty for now, needs to be in a generic PagePartService without static call &
13
 * without passing EntityManager as param...
14
 */
15
class IndexablePagePartsService
16
{
17
    /** @var EntityManagerInterface */
18
    private $em;
19
20
    /** @var [] */
21
    private $contexts;
22
23
    /**
24
     * IndexablePagePartsService constructor.
25
     *
26
     * @param EntityManagerInterface $em
27
     * @param []                     $contexts
28
     */
29
    public function __construct(EntityManagerInterface $em, $contexts = [])
30
    {
31
        $this->em = $em;
32
        $this->contexts = $contexts;
33
    }
34
35
    /**
36
     * Returns all indexable pageparts for the specified page and context
37
     *
38
     * @param HasPagePartsInterface $page
39
     * @param string                $context
40
     *
41
     * @return array
42
     */
43
    public function getIndexablePageParts(HasPagePartsInterface $page, $context = 'main')
44
    {
45
        $contexts = array_unique(array_merge($this->contexts, [$context]));
46
47
        $indexablePageParts = [];
48
        foreach ($contexts as $context) {
49
            $pageParts = $this->em
0 ignored issues
show
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...
50
                ->getRepository('KunstmaanPagePartBundle:PagePartRef')
51
                ->getPageParts($page, $context);
52
53
            foreach ($pageParts as $pagePart) {
54
                if ($pagePart instanceof IndexableInterface && !$pagePart->isIndexable()) {
55
                    continue;
56
                }
57
                $indexablePageParts[] = $pagePart;
58
            }
59
        }
60
61
        return $indexablePageParts;
62
    }
63
}
64