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\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 |
||
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
|
|||
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 |
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: