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
|
|||
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 |
Let’s take a look at an example:
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
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: